From b8e4cfdaea0567494c169931f4c6edfa6db09ed2 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Tue, 25 Jan 2022 14:05:54 +0200 Subject: [PATCH] Improved accounts code --- accounts/accounts.go | 12 ++++++++++-- accounts/libaccounts.go | 22 +++++----------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/accounts/accounts.go b/accounts/accounts.go index 2bbd951a0..5a28237a8 100644 --- a/accounts/accounts.go +++ b/accounts/accounts.go @@ -268,18 +268,26 @@ func (aS *AccountS) refundCharges(ctx *context.Context, tnt string, ecs *utils.E } for _, chrg := range ecs.Charges { acntChrg := ecs.Accounting[chrg.ChargingID] + var uf *utils.UnitFactor + if _, has := ecs.UnitFactors[acntChrg.UnitFactorID]; has { + uf = ecs.UnitFactors[acntChrg.UnitFactorID] + } if acntChrg.BalanceID != utils.MetaTransAbstract { // *transAbstracts is not a real balance, hence the exception refundUnitsOnAccount( acntsIdxed[acntChrg.AccountID], - uncompressUnits(acntChrg.Units, chrg.CompressFactor, acntChrg, ecs.UnitFactors), + uncompressUnits(acntChrg.Units, chrg.CompressFactor, acntChrg, uf), ecs.Accounts[acntChrg.AccountID].Balances[acntChrg.BalanceID]) alteredAcnts.Add(acntChrg.AccountID) } for _, chrgID := range acntChrg.JoinedChargeIDs { // refund extra charges extraChrg := ecs.Accounting[chrgID] + var joinedChargeUf *utils.UnitFactor + if _, has := ecs.UnitFactors[extraChrg.UnitFactorID]; has { + joinedChargeUf = ecs.UnitFactors[extraChrg.UnitFactorID] + } refundUnitsOnAccount( acntsIdxed[extraChrg.AccountID], - uncompressUnits(extraChrg.Units, chrg.CompressFactor, extraChrg, ecs.UnitFactors), + uncompressUnits(extraChrg.Units, chrg.CompressFactor, extraChrg, joinedChargeUf), ecs.Accounts[acntChrg.AccountID].Balances[extraChrg.BalanceID]) alteredAcnts.Add(extraChrg.AccountID) } diff --git a/accounts/libaccounts.go b/accounts/libaccounts.go index ce7c0a1b3..8b0b92174 100644 --- a/accounts/libaccounts.go +++ b/accounts/libaccounts.go @@ -229,12 +229,8 @@ func maxDebitAbstractsFromConcretes(ctx *context.Context, aUnits *decimal.Big, origConcrtUnts := cloneUnitsFromConcretes(cncrtBlncs) // so we can revert on errors paidConcrtUnts := origConcrtUnts // so we can revert when higher abstracts are not possible var aPaid, aDenied *decimal.Big - maxItr := config.CgrConfig().AccountSCfg().MaxIterations - ecBkp := utils.NewEventCharges() // so we can keep the record of the last sucessful debit - if ec != nil { - ecBkp.Merge(ec) // copy the original EC inside - } + ec = utils.NewEventCharges() // for i := 0; i <= maxItr; i++ { if i != 0 { restoreUnitsFromClones(cncrtBlncs, origConcrtUnts) @@ -294,18 +290,10 @@ func maxDebitAbstractsFromConcretes(ctx *context.Context, aUnits *decimal.Big, } continue } - ec = ecBkp - if ecDbt != nil { - ec.Merge(ecDbt) // write the partial units debited - } } else { // debit for the usage succeeded aPaid = utils.CloneDecimalBig(aUnits) paidConcrtUnts = cloneUnitsFromConcretes(cncrtBlncs) - if ec == nil { - ec = utils.NewEventCharges() - } - ec = utils.NewEventCharges() // so we can restore the backup inside - ec.Merge(ecBkp) // restore the backup to avoid recording all loops + ec = utils.NewEventCharges() // so we can keep the record of the last sucessful debit ec.Merge(ecDbt) // merge the last debit if i == 0 { // no estimation done, covering full break @@ -359,15 +347,15 @@ func unlockAccounts(acnts utils.AccountsWithWeight) { } // uncompressUnits returns the uncompressed value of the units if compressFactor is provided -func uncompressUnits(units *utils.Decimal, cmprsFctr int, acntChrg *utils.AccountCharge, uf map[string]*utils.UnitFactor) (tU *utils.Decimal) { +func uncompressUnits(units *utils.Decimal, cmprsFctr int, acntChrg *utils.AccountCharge, uf *utils.UnitFactor) (tU *utils.Decimal) { tU = units if cmprsFctr > 1 { tU = &utils.Decimal{utils.MultiplyBig(tU.Big, decimal.New(int64(cmprsFctr), 0))} } // check it this has unit factor - if newUf, has := uf[acntChrg.UnitFactorID]; has { - tU = utils.MultiplyDecimal(tU, newUf.Factor) + if uf != nil { + tU = utils.MultiplyDecimal(tU, uf.Factor) } return }