From ce49256a4da2648282d704ef1c28898d5f643f96 Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 11 Feb 2021 20:16:43 +0100 Subject: [PATCH] AccountS - Balance ordering based on dynamic weight, CGREvent.AsDatProvider() --- accounts/accounts.go | 14 +++++++++++- accounts/libaccounts.go | 8 +------ accounts/libaccounts_test.go | 6 ++--- utils/accountprofile.go | 44 ++++++++++++++++++++++++++++++++---- utils/accountprofile_test.go | 13 ----------- utils/cgrevent.go | 8 +++++++ 6 files changed, 64 insertions(+), 29 deletions(-) diff --git a/accounts/accounts.go b/accounts/accounts.go index 5fa0c2d2e..43280a9e7 100644 --- a/accounts/accounts.go +++ b/accounts/accounts.go @@ -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 } diff --git a/accounts/libaccounts.go b/accounts/libaccounts.go index eafb41c48..aca694aad 100644 --- a/accounts/libaccounts.go +++ b/accounts/libaccounts.go @@ -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 diff --git a/accounts/libaccounts_test.go b/accounts/libaccounts_test.go index 85acb82db..84d516500 100644 --- a/accounts/libaccounts_test.go +++ b/accounts/libaccounts_test.go @@ -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: " - 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) } diff --git a/utils/accountprofile.go b/utils/accountprofile.go index 32e6b7ce2..222f61cfa 100644 --- a/utils/accountprofile.go +++ b/utils/accountprofile.go @@ -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 diff --git a/utils/accountprofile_test.go b/utils/accountprofile_test.go index 4ffbb7cc6..2fc0f35a3 100644 --- a/utils/accountprofile_test.go +++ b/utils/accountprofile_test.go @@ -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]) - } } diff --git a/utils/cgrevent.go b/utils/cgrevent.go index 2b09270c4..4a6d3be15 100644 --- a/utils/cgrevent.go +++ b/utils/cgrevent.go @@ -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 {