From 21e20fe57fe835ea69f6a0518dcb29059e64224b Mon Sep 17 00:00:00 2001 From: TeoV Date: Tue, 20 Oct 2020 13:04:59 +0300 Subject: [PATCH] Send balance update event from rals to threshold ( in case of negative ) only once --- data/conf/samples/cdrsv2internal/cgrates.json | 1 + data/conf/samples/cdrsv2mongo/cgrates.json | 1 + .../conf/samples/cdrsv2mongo_gob/cgrates.json | 1 + .../conf/samples/cdrsv2mysql/cdrsv2mysql.json | 1 + .../samples/cdrsv2mysql_gob/cdrsv2mysql.json | 1 + data/conf/samples/cdrsv2psql/cdrsv2psql.json | 1 + engine/account.go | 82 ++++++++----------- 7 files changed, 40 insertions(+), 48 deletions(-) diff --git a/data/conf/samples/cdrsv2internal/cgrates.json b/data/conf/samples/cdrsv2internal/cgrates.json index 30f56ffa2..e30280fdf 100644 --- a/data/conf/samples/cdrsv2internal/cgrates.json +++ b/data/conf/samples/cdrsv2internal/cgrates.json @@ -16,6 +16,7 @@ "rals": { "enabled": true, + "thresholds_conns": ["*localhost"], "dynaprepaid_actionplans": ["PACKAGE_1001"], }, diff --git a/data/conf/samples/cdrsv2mongo/cgrates.json b/data/conf/samples/cdrsv2mongo/cgrates.json index dfc79a466..77c3c081b 100644 --- a/data/conf/samples/cdrsv2mongo/cgrates.json +++ b/data/conf/samples/cdrsv2mongo/cgrates.json @@ -16,6 +16,7 @@ "rals": { "enabled": true, + "thresholds_conns": ["*localhost"], "dynaprepaid_actionplans": ["PACKAGE_1001"], }, diff --git a/data/conf/samples/cdrsv2mongo_gob/cgrates.json b/data/conf/samples/cdrsv2mongo_gob/cgrates.json index 671d4c6a9..db7858367 100644 --- a/data/conf/samples/cdrsv2mongo_gob/cgrates.json +++ b/data/conf/samples/cdrsv2mongo_gob/cgrates.json @@ -25,6 +25,7 @@ "rals": { "enabled": true, + "thresholds_conns": ["conn1"], "dynaprepaid_actionplans": ["PACKAGE_1001"], }, diff --git a/data/conf/samples/cdrsv2mysql/cdrsv2mysql.json b/data/conf/samples/cdrsv2mysql/cdrsv2mysql.json index e698054d5..99cc5e872 100644 --- a/data/conf/samples/cdrsv2mysql/cdrsv2mysql.json +++ b/data/conf/samples/cdrsv2mysql/cdrsv2mysql.json @@ -16,6 +16,7 @@ "rals": { "enabled": true, + "thresholds_conns": ["*localhost"], "dynaprepaid_actionplans": ["PACKAGE_1001"], }, diff --git a/data/conf/samples/cdrsv2mysql_gob/cdrsv2mysql.json b/data/conf/samples/cdrsv2mysql_gob/cdrsv2mysql.json index 0fd049816..e75472e84 100644 --- a/data/conf/samples/cdrsv2mysql_gob/cdrsv2mysql.json +++ b/data/conf/samples/cdrsv2mysql_gob/cdrsv2mysql.json @@ -24,6 +24,7 @@ "rals": { "enabled": true, + "thresholds_conns": ["conn1"], "dynaprepaid_actionplans": ["PACKAGE_1001"], }, diff --git a/data/conf/samples/cdrsv2psql/cdrsv2psql.json b/data/conf/samples/cdrsv2psql/cdrsv2psql.json index 8c67d43f2..fcca99d62 100644 --- a/data/conf/samples/cdrsv2psql/cdrsv2psql.json +++ b/data/conf/samples/cdrsv2psql/cdrsv2psql.json @@ -16,6 +16,7 @@ "rals": { "enabled": true, + "thresholds_conns": ["*localhost"], "dynaprepaid_actionplans": ["PACKAGE_1001"], }, diff --git a/engine/account.go b/engine/account.go index 20f8dd5f3..be642a5d6 100644 --- a/engine/account.go +++ b/engine/account.go @@ -416,7 +416,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 } } @@ -427,24 +426,16 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo // try every balance multiple times in case one becomes active or ratig 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 if cd.GetDuration() <= 0 { goto COMMIT @@ -458,15 +449,11 @@ 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 { cd.RatingInfos = nil } @@ -498,8 +485,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())) @@ -530,45 +516,13 @@ 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 := &ThresholdsArgsProcessEvent{ - CGREventWithOpts: &utils.CGREventWithOpts{ - 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, - }, - }, - Opts: map[string]interface{}{ - utils.MetaEventType: utils.BalanceUpdate, - }, - }, - } - 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, @@ -589,6 +543,38 @@ 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 := &ThresholdsArgsProcessEvent{ + CGREventWithOpts: &utils.CGREventWithOpts{ + 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, + }, + }, + Opts: map[string]interface{}{ + utils.MetaEventType: utils.BalanceUpdate, + }, + }, + } + 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: