Add Factor map and Weight to BalanceSummaries

Added additional tests + updated old ones.
This commit is contained in:
ionutboangiu
2024-03-22 14:38:40 -04:00
committed by Dan Christian Bogos
parent 699989a336
commit 6d61fd479d
5 changed files with 88 additions and 4 deletions

View File

@@ -2184,8 +2184,8 @@ func TestAccountAsAccountDigest(t *testing.T) {
{ID: "data1", Type: utils.MetaData, Value: 1204, Disabled: false},
{ID: "sms1", Type: utils.MetaSMS, Value: 14, Disabled: false},
{ID: "mms1", Type: utils.MetaMMS, Value: 140, Disabled: false},
{ID: "voice1", Type: utils.MetaVoice, Value: 3600, Disabled: false},
{ID: "voice2", Type: utils.MetaVoice, Value: 1200, Disabled: false},
{ID: "voice1", Type: utils.MetaVoice, Weight: 20, Value: 3600, Disabled: false},
{ID: "voice2", Type: utils.MetaVoice, Weight: 10, Value: 1200, Disabled: false},
},
AllowNegative: true,
Disabled: false,

View File

@@ -1261,7 +1261,9 @@ func resetAccountCDR(ub *Account, action *Action, acts Actions, fltrS *FilterS,
ID: &bsum.ID,
Type: &bsum.Type,
Value: &utils.ValueFormula{Static: bsum.Value},
Weight: &bsum.Weight,
Disabled: &bsum.Disabled,
Factor: &bsum.Factor,
},
}, fltrS); err != nil {
utils.Logger.Warning(fmt.Sprintf("<%s> Error %s setting balance %s for account: %s", utils.Actions, err, bsum.UUID, account))

View File

@@ -681,7 +681,15 @@ func (b *Balance) debitMoney(cd *CallDescriptor, ub *Account, moneyBalances Bala
// AsBalanceSummary converts the balance towards compressed information to be displayed
func (b *Balance) AsBalanceSummary(typ string) *BalanceSummary {
bd := &BalanceSummary{UUID: b.Uuid, ID: b.ID, Type: typ, Value: b.Value, Disabled: b.Disabled}
bd := &BalanceSummary{
UUID: b.Uuid,
ID: b.ID,
Type: typ,
Value: b.Value,
Weight: b.Weight,
Disabled: b.Disabled,
Factor: b.Factor,
}
if bd.ID == "" {
bd.ID = b.Uuid
}
@@ -791,7 +799,9 @@ type BalanceSummary struct {
Type string // *voice, *data, etc
Initial float64 // initial value before the debit operation
Value float64
Weight float64 `json:",omitempty"`
Disabled bool
Factor ValueFactor `json:",omitempty"`
}
// BalanceSummaries is a list of BalanceSummaries
@@ -810,7 +820,7 @@ func (bs BalanceSummaries) BalanceSummaryWithUUD(bsUUID string) (b *BalanceSumma
// FieldAsInterface func to help EventCost FieldAsInterface
func (bl *BalanceSummary) FieldAsInterface(fldPath []string) (val any, err error) {
if bl == nil || len(fldPath) != 1 {
if bl == nil || len(fldPath) < 1 {
return nil, utils.ErrNotFound
}
switch fldPath[0] {
@@ -824,10 +834,17 @@ func (bl *BalanceSummary) FieldAsInterface(fldPath []string) (val any, err error
return bl.Type, nil
case utils.Value:
return bl.Value, nil
case utils.Weight:
return bl.Weight, nil
case utils.Disabled:
return bl.Disabled, nil
case utils.Initial:
return bl.Initial, nil
case utils.Factor:
if len(fldPath) == 1 {
return bl.Factor, nil
}
return bl.Factor.FieldAsInterface(fldPath[1:])
}
}

View File

@@ -110,23 +110,37 @@ var testEC = &EventCost{
Type: utils.MetaMonetary,
Value: 50,
Initial: 60,
Weight: 20,
Disabled: false,
Factor: ValueFactor{
"factor2": 2,
"factor3": 3,
},
},
{
UUID: "7a54a9e9-d610-4c82-bcb5-a315b9a65010",
ID: "BALANCE_2",
Type: utils.MetaMonetary,
Value: 25,
Weight: 0,
Initial: 60,
Disabled: false,
Factor: ValueFactor{
"factor2": 2,
},
},
{
UUID: "4b8b53d7-c1a1-4159-b845-4623a00a0165",
ID: "BALANCE_3",
Type: utils.MetaVoice,
Value: 200,
Weight: 10,
Initial: 250,
Disabled: true,
Factor: ValueFactor{
"factor4": 4,
"factor5": 5,
},
},
},
AllowNegative: false,
@@ -2526,7 +2540,12 @@ func TestECSyncKeys(t *testing.T) {
Type: utils.MetaMonetary,
Value: 50,
Initial: 60,
Weight: 20,
Disabled: false,
Factor: ValueFactor{
"factor2": 2,
"factor3": 3,
},
},
{
UUID: "7a54a9e9-d610-4c82-bcb5-a315b9a65010",
@@ -2534,7 +2553,11 @@ func TestECSyncKeys(t *testing.T) {
Type: utils.MetaMonetary,
Value: 25,
Initial: 60,
Weight: 0,
Disabled: false,
Factor: ValueFactor{
"factor2": 2,
},
},
{
UUID: "4b8b53d7-c1a1-4159-b845-4623a00a0165",
@@ -2542,7 +2565,12 @@ func TestECSyncKeys(t *testing.T) {
Type: utils.MetaVoice,
Value: 200,
Initial: 250,
Weight: 10,
Disabled: true,
Factor: ValueFactor{
"factor4": 4,
"factor5": 5,
},
},
},
AllowNegative: false,
@@ -3866,6 +3894,21 @@ func TestECAsDataProvider3(t *testing.T) {
fields: []string{"Charges[0]", "Increments[3]", "Accounting", "Balance", "Disabled"},
exp: "true",
},
{
name: "Weight",
fields: []string{"Charges[0]", "Increments[3]", "Accounting", "Balance", "Weight"},
exp: "10",
},
{
name: "Factor map",
fields: []string{"Charges[0]", "Increments[3]", "Accounting", "Balance", "Factor"},
exp: `{"factor4":4,"factor5":5}`,
},
{
name: "Factor value",
fields: []string{"Charges[0]", "Increments[3]", "Accounting", "Balance", "Factor", "factor4"},
exp: "4",
},
{
name: "IncrementCost",
fields: []string{"Charges[0]", "Increments[2]", "Cost"},
@@ -4031,6 +4074,21 @@ func TestECAsDataProvider3(t *testing.T) {
fields: []string{"Charges[0]", "Increments[3]", "Accounting", "ExtraCharge", "Balance", "Disabled"},
exp: "false",
},
{
name: "Weight through ExtraCharge",
fields: []string{"Charges[0]", "Increments[3]", "Accounting", "ExtraCharge", "Balance", "Weight"},
exp: "20",
},
{
name: "Factor map through ExtraCharge",
fields: []string{"Charges[0]", "Increments[3]", "Accounting", "ExtraCharge", "Balance", "Factor"},
exp: `{"factor2":2,"factor3":3}`,
},
{
name: "Factor value through ExtraCharge",
fields: []string{"Charges[0]", "Increments[3]", "Accounting", "ExtraCharge", "Balance", "Factor", "factor3"},
exp: "3",
},
{
name: "AccountID through ExtraCharge",
fields: []string{"Charges[0]", "Increments[3]", "Accounting", "ExtraCharge", "AccountID"},

View File

@@ -431,6 +431,13 @@ cgrates.org,call,1001,2014-01-14T00:00:00Z,RP_ANY,`,
if monetaryBalanceValue != 4. {
t.Errorf("unexpected balance value: expected %v, received %v", 4., monetaryBalanceValue)
}
smsBalanceFactor, err := cdrs[0].CostDetails.FieldAsInterface([]string{"AccountSummary", "BalanceSummaries[0]", "Factor", "smsFactor"})
if err != nil {
t.Fatalf("could not retrieve *sms balance factor: %v", err)
}
if smsBalanceFactor != 4. {
t.Errorf("unexpected balance factor: expected %v, received %v", 4., smsBalanceValue)
}
})
t.Run("SessionSv1ProcessCDR", func(t *testing.T) {