Send balance update event from rals to threshold ( in case of negative ) only once

This commit is contained in:
TeoV
2020-10-20 13:04:59 +03:00
committed by Dan Christian Bogos
parent 562357fbb5
commit 21e20fe57f
7 changed files with 40 additions and 48 deletions

View File

@@ -16,6 +16,7 @@
"rals": {
"enabled": true,
"thresholds_conns": ["*localhost"],
"dynaprepaid_actionplans": ["PACKAGE_1001"],
},

View File

@@ -16,6 +16,7 @@
"rals": {
"enabled": true,
"thresholds_conns": ["*localhost"],
"dynaprepaid_actionplans": ["PACKAGE_1001"],
},

View File

@@ -25,6 +25,7 @@
"rals": {
"enabled": true,
"thresholds_conns": ["conn1"],
"dynaprepaid_actionplans": ["PACKAGE_1001"],
},

View File

@@ -16,6 +16,7 @@
"rals": {
"enabled": true,
"thresholds_conns": ["*localhost"],
"dynaprepaid_actionplans": ["PACKAGE_1001"],
},

View File

@@ -24,6 +24,7 @@
"rals": {
"enabled": true,
"thresholds_conns": ["conn1"],
"dynaprepaid_actionplans": ["PACKAGE_1001"],
},

View File

@@ -16,6 +16,7 @@
"rals": {
"enabled": true,
"thresholds_conns": ["*localhost"],
"dynaprepaid_actionplans": ["PACKAGE_1001"],
},

View File

@@ -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("<Rater> 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("<AccountS> 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("<AccountS> error: <%s> processing balance event <%+v> with ThresholdS.",
err.Error(), utils.ToJSON(thEv)))
}
}
}
COMMIT: