From d182d6f601e5b5e6663e357aaed4963896909b65 Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Mon, 5 Feb 2024 10:21:12 -0500 Subject: [PATCH] Revise behaviour of balance factor Now it multiplies instead of dividing. --- engine/account_test.go | 4 ++-- engine/actions_test.go | 2 +- engine/balance_filter_test.go | 4 ++-- engine/balances.go | 4 ++-- general_tests/balance_it_test.go | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/engine/account_test.go b/engine/account_test.go index b5c47984a..58d6d7778 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -1735,7 +1735,7 @@ func TestDebitGenericBalance(t *testing.T) { utils.MetaGeneric: { &Balance{Uuid: "testm", Value: 100, Weight: 5, DestinationIDs: utils.StringMap{"NAT": true}, - Factor: ValueFactor{"call": 60 * float64(time.Second)}}}, + Factor: ValueFactor{"call": 1 / (60 * float64(time.Second))}}}, utils.MetaMonetary: {&Balance{Value: 21}}, }} var err error @@ -1789,7 +1789,7 @@ func TestDebitGenericBalanceWithRatingSubject(t *testing.T) { utils.MetaGeneric: { &Balance{Uuid: "testm", Value: 100, Weight: 5, DestinationIDs: utils.StringMap{"NAT": true}, - Factor: ValueFactor{"call": 60 * float64(time.Second)}, + Factor: ValueFactor{"call": 1 / (60 * float64(time.Second))}, RatingSubject: "free"}}, utils.MetaMonetary: {&Balance{Value: 21}}, }} diff --git a/engine/actions_test.go b/engine/actions_test.go index 2f330a00f..7e7775087 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -1086,7 +1086,7 @@ func TestActionTopupValueFactor(t *testing.T) { Type: utils.StringPointer(utils.MetaMonetary), Value: &utils.ValueFormula{Static: 10}, }, - ExtraParameters: `{"*monetary":2.0}`, + ExtraParameters: `{"*monetary":2}`, } topupResetAction(ub, a, nil, nil, nil) if len(ub.BalanceMap) != 1 || diff --git a/engine/balance_filter_test.go b/engine/balance_filter_test.go index 1d13a98a6..086265707 100644 --- a/engine/balance_filter_test.go +++ b/engine/balance_filter_test.go @@ -232,7 +232,7 @@ func TestBalanceLoadFromBalance(t *testing.T) { "timing": true, }, Factor: ValueFactor{ - "valfac": 22, + "valfac": 1 / 22, }, Disabled: true, Blocker: true, @@ -274,7 +274,7 @@ func TestBalanceLoadFromBalance(t *testing.T) { "timing": true, }, Factor: &ValueFactor{ - "valfac": 22, + "valfac": 1 / 22, }, Disabled: utils.BoolPointer(true), Blocker: utils.BoolPointer(true), diff --git a/engine/balances.go b/engine/balances.go index 21689dc4d..0ee06d8f4 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -878,7 +878,7 @@ func (b *Balance) debit(cd *CallDescriptor, ub *Account, moneyBalances Balances, //log.Printf("INCREMENET: %+v", inc) amount := float64(inc.Duration) if b.Factor != nil { - amount = utils.Round(amount/b.Factor.GetValue(cd.Category), + amount = utils.Round(amount*b.Factor.GetValue(cd.Category), globalRoundingDecimals, utils.MetaRoundingUp) } if b.GetValue() >= amount { @@ -1022,7 +1022,7 @@ func (b *Balance) debit(cd *CallDescriptor, ub *Account, moneyBalances Balances, var moneyBal *Balance if isUnitBal { if b.Factor != nil { - amount = utils.Round(amount/b.Factor.GetValue(cd.Category), globalRoundingDecimals, utils.MetaRoundingUp) + amount = utils.Round(amount*b.Factor.GetValue(cd.Category), globalRoundingDecimals, utils.MetaRoundingUp) } for _, mb := range moneyBalances { if mb.GetValue() >= cost { diff --git a/general_tests/balance_it_test.go b/general_tests/balance_it_test.go index f251c59f1..3394769c6 100644 --- a/general_tests/balance_it_test.go +++ b/general_tests/balance_it_test.go @@ -254,11 +254,11 @@ cgrates.org,sms,1001,2014-01-14T00:00:00Z,RP_ANY,`, // the session being processed. This has now been updated to depend on Category instead of ToR. // // The test steps are as follows: -// 1. Create an account with an *sms balance of 10 units with a factor of 0.25 (essentially, this means that for every 1 sms, 4 will +// 1. Create an account with an *sms balance of 10 units with a factor of 4 (essentially, this means that for every 1 sms, 4 will // be exhausted), and a *monetary balance of 5 units. The RatingPlan used when debiting the *monetary balance will charge 1 unit // per second. // 2. Process an 3 usage (representing 12 sms, when taking into consideration the balance factor) event. -// 3. Ensure that the *sms balance has 2 units left (10 - (2 sms / 0.25 factor)) and that 1 unit was subtracted from the *monetary balance. +// 3. Ensure that the *sms balance has 2 units left (10 - (2 sms * 4 factor)) and that 1 unit was subtracted from the *monetary balance. func TestBalanceFactor(t *testing.T) { switch *dbType { case utils.MetaInternal: @@ -308,7 +308,7 @@ cgrates.org,1001,PACKAGE_1001,,,`, utils.ActionPlansCsv: `#Id,ActionsId,TimingId,Weight PACKAGE_1001,ACT_TOPUP,*asap,10`, utils.ActionsCsv: `#ActionsId[0],Action[1],ExtraParameters[2],Filter[3],BalanceId[4],BalanceType[5],Categories[6],DestinationIds[7],RatingSubject[8],SharedGroup[9],ExpiryTime[10],TimingIds[11],Units[12],BalanceWeight[13],BalanceBlocker[14],BalanceDisabled[15],Weight[16] -ACT_TOPUP,*topup_reset,"{""sms"":0.25}",,balance_sms,*sms,,,,,*unlimited,,10,20,false,false,20 +ACT_TOPUP,*topup_reset,"{""sms"":4}",,balance_sms,*sms,,,,,*unlimited,,10,20,false,false,20 ACT_TOPUP,*topup_reset,,,balance_monetary,*monetary,,*any,,,*unlimited,,5,10,false,false,20`, utils.DestinationRatesCsv: `#Id,DestinationId,RatesTag,RoundingMethod,RoundingDecimals,MaxCost,MaxCostStrategy DR_ANY,*any,RT_ANY,*up,20,0,`,