From a2144f686722326c2da782a468fdf295e7af3eb1 Mon Sep 17 00:00:00 2001 From: TeoV Date: Tue, 20 Oct 2020 13:27:48 +0300 Subject: [PATCH] Send balance update event from rals to threshold ( in case of negative ) only once --- engine/account.go | 72 ++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/engine/account.go b/engine/account.go index 5bb634e16..1613fe624 100644 --- a/engine/account.go +++ b/engine/account.go @@ -381,7 +381,7 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo // debit minutes unitBalanceChecker := true for unitBalanceChecker { - // try every balance multiple times in case one becomes active or ratig changes + // try every balance multiple times in case one becomes active or rating changes unitBalanceChecker = false for _, balance := range usefulUnitBalances { partCC, debitErr := balance.debitUnits(cd, balance.account, @@ -397,7 +397,7 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo cc.Timespans = append(cc.Timespans, partCC.Timespans...) cc.negativeConnectFee = partCC.negativeConnectFee cd.TimeStart = cc.GetEndTime() - // check if the calldescriptor is covered + // check if the call descriptor is covered if cd.GetDuration() <= 0 { goto COMMIT } @@ -411,7 +411,6 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo } // check for blocker if dryRun && balance.Blocker { - //log.Print("BLOCKER!") return // don't go to next balances } } @@ -419,28 +418,20 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo // debit money moneyBalanceChecker := true for moneyBalanceChecker { - // try every balance multiple times in case one becomes active or ratig changes + // try every balance multiple times in case one becomes active or rating changes moneyBalanceChecker = false for _, balance := range usefulMoneyBalances { - //utils.Logger.Info(fmt.Sprintf("Money balance: %+v", balance)) - //utils.Logger.Info(fmt.Sprintf("CD BEFORE MONEY: %+v", cd)) partCC, debitErr := balance.debitMoney(cd, balance.account, usefulMoneyBalances, count, dryRun, len(cc.Timespans) == 0) if debitErr != nil { return nil, debitErr } - //utils.Logger.Info(fmt.Sprintf("CD AFTER MONEY: %+v", cd)) if partCC != nil { cc.Timespans = append(cc.Timespans, partCC.Timespans...) cc.negativeConnectFee = partCC.negativeConnectFee - /*for i, ts := range cc.Timespans { - log.Printf("cc.times[an[%d]: %+v\n", i, ts) - }*/ cd.TimeStart = cc.GetEndTime() - //log.Printf("CD: %+v", cd) - //log.Printf("CD: %+v - %+v", cd.TimeStart, cd.TimeEnd) - // check if the calldescriptor is covered + // check if the call descriptor is covered if cd.GetDuration() <= 0 { goto COMMIT } @@ -453,13 +444,10 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo } // check for blocker if dryRun && balance.Blocker { - //log.Print("BLOCKER!") return // don't go to next balances } } } - //log.Printf("END CD: %+v", cd) - //log.Print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") } //log.Printf("After balances CD: %+v", cd) if hadBalanceSubj { @@ -493,8 +481,7 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo // this is the first add, debit the connect fee ok, debitedConnectFeeBalance = acc.DebitConnectionFee(cc, usefulMoneyBalances, count, true) } - //log.Printf("Left CC: %+v ", leftCC) - // get the default money balanance + // get the default money balance // and go negative on it with the amount still unpaid if len(leftCC.Timespans) > 0 && leftCC.Cost > 0 && !acc.AllowNegative && !dryRun { utils.Logger.Warning(fmt.Sprintf(" Going negative on account %s with AllowNegative: false", cd.GetAccountKey())) @@ -525,37 +512,14 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo } for incIndex, increment := range ts.Increments { - + // connect fee was processed and skip it if tsIndex == 0 && incIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && cc.deductConnectFee && ok { - // go to nextincrement continue } cost := increment.Cost defaultBalance := acc.GetDefaultMoneyBalance() defaultBalance.SubstractValue(cost) - //send default balance to thresholdS to be processed - if len(config.CgrConfig().RalsCfg().ThresholdSConns) != 0 { - acntTnt := utils.NewTenantID(acc.ID) - thEv := &ArgsProcessEvent{ - CGREvent: &utils.CGREvent{ - Tenant: acntTnt.Tenant, - ID: utils.GenUUID(), - Event: map[string]interface{}{ - utils.EventType: utils.BalanceUpdate, - utils.EventSource: utils.AccountService, - utils.Account: acntTnt.ID, - utils.BalanceID: defaultBalance.ID, - utils.Units: defaultBalance.Value}}} - var tIDs []string - if err := connMgr.Call(config.CgrConfig().RalsCfg().ThresholdSConns, nil, - utils.ThresholdSv1ProcessEvent, thEv, &tIDs); err != nil && - err.Error() != utils.ErrNotFound.Error() { - utils.Logger.Warning( - fmt.Sprintf(" error: <%s> processing balance event <%+v> with ThresholdS.", - err.Error(), utils.ToJSON(thEv))) - } - } increment.BalanceInfo.Monetary = &MonetaryInfo{ UUID: defaultBalance.Uuid, @@ -576,6 +540,30 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo } } } + + // in case of going to negative we send the default balance to thresholdS to be processed + if len(config.CgrConfig().RalsCfg().ThresholdSConns) != 0 { + defaultBalance := acc.GetDefaultMoneyBalance() + acntTnt := utils.NewTenantID(acc.ID) + thEv := &ArgsProcessEvent{ + CGREvent: &utils.CGREvent{ + Tenant: acntTnt.Tenant, + ID: utils.GenUUID(), + Event: map[string]interface{}{ + utils.EventType: utils.BalanceUpdate, + utils.EventSource: utils.AccountService, + utils.Account: acntTnt.ID, + utils.BalanceID: defaultBalance.ID, + utils.Units: defaultBalance.Value}}} + var tIDs []string + if err := connMgr.Call(config.CgrConfig().RalsCfg().ThresholdSConns, nil, + utils.ThresholdSv1ProcessEvent, thEv, &tIDs); err != nil && + err.Error() != utils.ErrNotFound.Error() { + utils.Logger.Warning( + fmt.Sprintf(" error: <%s> processing balance event <%+v> with ThresholdS.", + err.Error(), utils.ToJSON(thEv))) + } + } } COMMIT: