AccountDigest and BalanceDigest structures

This commit is contained in:
DanB
2016-08-27 11:45:01 +02:00
parent f9b068fba6
commit 1b51fb3050
4 changed files with 82 additions and 0 deletions

View File

@@ -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
}

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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) {

View File

@@ -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
}

View File

@@ -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