AccountS - Balance ordering based on dynamic weight, CGREvent.AsDatProvider()

This commit is contained in:
DanB
2021-02-11 20:16:43 +01:00
parent fd331429c1
commit ce49256a4d
6 changed files with 64 additions and 29 deletions

View File

@@ -151,8 +151,20 @@ func (aS *AccountS) matchingAccountForEvent(tnt string, cgrEv *utils.CGREvent,
// accountProcessEvent implements event processing by an Account
func (aS *AccountS) accountDebitUsage(acnt *utils.AccountProfile,
cgrEv *utils.CGREvent) (ec *utils.EventCharges, err error) {
//ev := cgrEv.AsDataProvider()
blcsWithWeight := make(utils.BalancesWithWeight, 0, len(acnt.Balances))
for _, blnCfg := range acnt.Balances {
var weight float64
/*
if weight, err = engine.WeightFromDynamics(blnCfg.DynamicWeights,
aS.fltrS, cgrEv.Tenant, ev); err != nil {
return
}
*/
blcsWithWeight = append(blcsWithWeight, &utils.BalanceWithWeight{blnCfg, weight})
}
var blncOpers []balanceOperator
if blncOpers, err = newAccountBalanceOperators(acnt, aS.fltrS, aS.connMgr,
if blncOpers, err = newBalanceOperators(blcsWithWeight.Balances(), aS.fltrS, aS.connMgr,
aS.cfg.AccountSCfg().AttributeSConns, aS.cfg.AccountSCfg().RateSConns); err != nil {
return
}

View File

@@ -30,16 +30,10 @@ import (
)
// newAccountBalances constructs accountBalances
func newAccountBalanceOperators(acnt *utils.AccountProfile,
func newBalanceOperators(blnCfgs []*utils.Balance,
fltrS *engine.FilterS, connMgr *engine.ConnManager,
attrSConns, rateSConns []string) (blncOpers []balanceOperator, err error) {
blnCfgs := make(utils.Balances, 0, len(acnt.Balances))
for _, blnCfg := range acnt.Balances {
blnCfgs = append(blnCfgs, blnCfg)
}
blnCfgs.Sort()
blncOpers = make([]balanceOperator, len(blnCfgs))
var cncrtBlncs []*concreteBalance
for i, blnCfg := range blnCfgs { // build the concrete balances

View File

@@ -67,8 +67,8 @@ func TestNewAccountBalanceOperators(t *testing.T) {
fltrS: filters,
cncrtBlncs: cncrtBlncs,
}
if blcOp, err := newAccountBalanceOperators(acntPrf, filters, nil,
blnCfgs := []*utils.Balance{acntPrf.Balances["BL0"], acntPrf.Balances["BL1"]}
if blcOp, err := newBalanceOperators(blnCfgs, filters, nil,
nil, nil); err != nil {
t.Error(err)
} else {
@@ -80,7 +80,7 @@ func TestNewAccountBalanceOperators(t *testing.T) {
acntPrf.Balances["BL1"].Type = "INVALID_TYPE"
expectedErr := "unsupported balance type: <INVALID_TYPE>"
if _, err := newAccountBalanceOperators(acntPrf, filters, nil,
if _, err := newBalanceOperators(blnCfgs, filters, nil,
nil, nil); err == nil || err.Error() != expectedErr {
t.Errorf("Expected %+v, received %+v", expectedErr, err)
}

View File

@@ -200,22 +200,56 @@ func (bL *Balance) Clone() (blnc *Balance) {
return
}
// ActionProfiles is a sortable list of ActionProfiles
type AccountProfiles []*AccountProfile
// AccountProfileWithWeight attaches static weight to AccountProfile
type AccountProfileWithWeight struct {
*AccountProfile
Weight float64
}
// AccountProfilesWithWeight is a sortable list of AccountProfileWithWeight
type AccountProfilesWithWeight []*AccountProfileWithWeight
// Sort is part of sort interface, sort based on Weight
func (aps AccountProfiles) Sort() {
func (aps AccountProfilesWithWeight) Sort() {
sort.Slice(aps, func(i, j int) bool { return aps[i].Weight > aps[j].Weight })
}
// AccountProfiles returns the list of AccountProfiles
func (apWws AccountProfilesWithWeight) AccountProfiles() (aps []*AccountProfile) {
if apWws != nil {
aps = make([]*AccountProfile, len(apWws))
for i, apWw := range apWws {
aps[i] = apWw.AccountProfile
}
}
return
}
// BalanceWithWeight attaches static Weight to Balance
type BalanceWithWeight struct {
*Balance
Weight float64
}
// Balances is a sortable list of Balances
type Balances []*Balance
type BalancesWithWeight []*BalanceWithWeight
// Sort is part of sort interface, sort based on Weight
func (blcs Balances) Sort() {
func (blcs BalancesWithWeight) Sort() {
sort.Slice(blcs, func(i, j int) bool { return blcs[i].Weight > blcs[j].Weight })
}
// Balances returns the list of Balances
func (bWws BalancesWithWeight) Balances() (blncs []*Balance) {
if bWws != nil {
blncs = make([]*Balance, len(bWws))
for i, bWw := range bWws {
blncs[i] = bWw.Balance
}
}
return
}
// APIAccountProfileWithOpts is used in API calls
type APIAccountProfileWithOpts struct {
*APIAccountProfile

View File

@@ -171,13 +171,6 @@ func TestAccountProfileAsAccountProfile(t *testing.T) {
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expected %+v, received %+v", ToJSON(expected), ToJSON(rcv))
}
accPrfList := AccountProfiles{}
accPrfList = append(accPrfList, expected)
accPrfList.Sort()
if !reflect.DeepEqual(accPrfList[0], expected) {
t.Errorf("Expected %+v \n, received %+v", expected, accPrfList[0])
}
}
func TestAPIBalanceAsBalance(t *testing.T) {
@@ -222,10 +215,4 @@ func TestAPIBalanceAsBalance(t *testing.T) {
t.Errorf("Expected %+v \n, received %+v", ToJSON(expected), ToJSON(rcv))
}
blcList := Balances{}
blcList = append(blcList, expected)
blcList.Sort()
if !reflect.DeepEqual(blcList[0], expected) {
t.Errorf("Expected %+v \n, received %+v", expected, blcList[0])
}
}

View File

@@ -166,6 +166,14 @@ func (ev *CGREvent) Clone() (clned *CGREvent) {
return
}
// AsDataProvider returns the CGREvent as MapStorage with *opts and *req paths set
func (cgrEv *CGREvent) AsDataProvider() (ev DataProvider) {
return MapStorage{
MetaOpts: cgrEv.Opts,
MetaReq: cgrEv.Event,
}
}
// CGREvents is a group of generic events processed by CGR services
// ie: derived CDRs
type CGREvents struct {