From 1b51fb305059e052eabb8b900f0506473824653f Mon Sep 17 00:00:00 2001 From: DanB Date: Sat, 27 Aug 2016 11:45:01 +0200 Subject: [PATCH] AccountDigest and BalanceDigest structures --- engine/account.go | 26 ++++++++++++++++++++++++++ engine/account_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ engine/balances.go | 13 +++++++++++++ engine/callcost.go | 1 + 4 files changed, 82 insertions(+) diff --git a/engine/account.go b/engine/account.go index 7ebbd97f4..928859871 100644 --- a/engine/account.go +++ b/engine/account.go @@ -1018,3 +1018,29 @@ func (acc *Account) AsOldStructure() interface{} { } return result } + +func (acc *Account) AsAccountDigest() *AccountDigest { + idSplt := strings.Split(acc.ID, utils.CONCATENATED_KEY_SEP) + ad := &AccountDigest{AllowNegative: acc.AllowNegative, Disabled: acc.Disabled} + if len(idSplt) == 1 { + ad.ID = idSplt[0] + } else if len(idSplt) == 2 { + ad.Tenant = idSplt[0] + ad.ID = idSplt[1] + } + for balanceType, balances := range acc.BalanceMap { + for _, balance := range balances { + ad.BalanceDigests = append(ad.BalanceDigests, balance.AsBalanceDigest(balanceType)) + } + } + return ad +} + +// AccountDigest contains compressed information about an Account +type AccountDigest struct { + Tenant string + ID string + BalanceDigests []*BalanceDigest + AllowNegative bool + Disabled bool +} diff --git a/engine/account_test.go b/engine/account_test.go index bfa598112..3952f2240 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -19,6 +19,7 @@ along with this program. If not, see package engine import ( + "reflect" "testing" "time" @@ -1872,6 +1873,47 @@ func TestAccountGetBalancesForPrefixMixedBad(t *testing.T) { } } +func TestAccountAsAccountDigest(t *testing.T) { + acnt1 := &Account{ + ID: "cgrates.org:account1", + AllowNegative: true, + BalanceMap: map[string]Balances{ + utils.SMS: Balances{&Balance{ID: "sms1", Value: 14}}, + utils.DATA: Balances{&Balance{ID: "data1", Value: 1204}}, + utils.VOICE: Balances{ + &Balance{ID: "voice1", Weight: 20, DestinationIDs: utils.StringMap{"NAT": true}, Value: 3600}, + &Balance{ID: "voice2", Weight: 10, DestinationIDs: utils.StringMap{"RET": true}, Value: 1200}, + }, + }, + } + expectAcntDigest := &AccountDigest{ + Tenant: "cgrates.org", + ID: "account1", + BalanceDigests: []*BalanceDigest{ + &BalanceDigest{ID: "sms1", Type: utils.SMS, Value: 14, Disabled: false}, + &BalanceDigest{ID: "data1", Type: utils.DATA, Value: 1204, Disabled: false}, + &BalanceDigest{ID: "voice1", Type: utils.VOICE, Value: 1204, Disabled: false}, + &BalanceDigest{ID: "voice2", Type: utils.VOICE, Value: 1200, Disabled: false}, + }, + AllowNegative: true, + Disabled: false, + } + acntDigest := acnt1.AsAccountDigest() + if expectAcntDigest.Tenant != acntDigest.Tenant || + expectAcntDigest.ID != acntDigest.ID || + expectAcntDigest.AllowNegative != acntDigest.AllowNegative || + expectAcntDigest.Disabled != acntDigest.Disabled || + len(expectAcntDigest.BalanceDigests) != len(acntDigest.BalanceDigests) { + t.Errorf("Expecting: %+v, received: %+v", expectAcntDigest, acntDigest) + } + // Since maps are unordered, slices will be too so we need to find element to compare before doing it + for _, bd := range acntDigest.BalanceDigests { + if b; d.ID == "sms1" && !reflect.DeepEqual(expectAcntDigest.BalanceDigests[0], bd) { + t.Errorf("Expecting: %+v, received: %+v", expectAcntDigest, acntDigest) + } + } +} + /*********************************** Benchmarks *******************************/ func BenchmarkGetSecondForPrefix(b *testing.B) { diff --git a/engine/balances.go b/engine/balances.go index 4078a0d1b..3eda020cf 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -620,6 +620,11 @@ func (b *Balance) debitMoney(cd *CallDescriptor, ub *Account, moneyBalances Bala return cc, nil } +// Converts the balance towards compressed information to be displayed +func (b *Balance) AsBalanceDigest(typ string) *BalanceDigest { + return &BalanceDigest{ID: b.ID, Type: typ, Value: b.Value, Disabled: b.Disabled} +} + /* Structure to store minute buckets according to weight, precision or price. */ @@ -741,3 +746,11 @@ func (f ValueFactor) GetValue(tor string) float64 { } return 1.0 } + +// BalanceDigest represents compressed information about a balance +type BalanceDigest struct { + ID string // ID or UUID if not defined + Type string // *voice, *data, etc + Value float64 + Disabled bool +} diff --git a/engine/callcost.go b/engine/callcost.go index 97bf4e780..be26634cd 100644 --- a/engine/callcost.go +++ b/engine/callcost.go @@ -30,6 +30,7 @@ type CallCost struct { Cost float64 Timespans TimeSpans RatedUsage float64 + AccountDigest *AccountDigest deductConnectFee bool negativeConnectFee bool // the connect fee went negative on default balance maxCostDisconect bool