New field in accounts Max_iterations + tests

This commit is contained in:
porosnicuadrian
2021-02-01 15:43:47 +02:00
committed by Dan Christian Bogos
parent 0f5856ee8b
commit 88eeea97b5
7 changed files with 25 additions and 6 deletions

View File

@@ -22,6 +22,8 @@ import (
"errors"
"fmt"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
"github.com/ericlagergren/decimal"
@@ -270,12 +272,12 @@ func maxDebitUsageFromConcretes(cncrtBlncs []*concreteBalance, usage *utils.Deci
origConcrtUnts := cloneUnitsFromConcretes(cncrtBlncs) // so we can revert on errors
paidConcrtUnts := origConcrtUnts // so we can revert when higher usages are not possible
var usagePaid, usageDenied *decimal.Big
maxIter := 100
for i := 0; i < maxIter; i++ {
maxItr := config.CgrConfig().AccountSCfg().MaxIterations
for i := 0; i < maxItr; i++ {
if i != 0 {
restoreUnitsFromClones(cncrtBlncs, origConcrtUnts)
}
if i == maxIter {
if i == maxItr {
return nil, nil, utils.ErrMaxIncrementsExceeded
}
qriedUsage := usage.Big // so we can detect loops

View File

@@ -31,6 +31,7 @@ type AccountSCfg struct {
PrefixIndexedFields *[]string
SuffixIndexedFields *[]string
NestedFields bool
MaxIterations int
}
func (acS *AccountSCfg) loadFromJSONCfg(jsnCfg *AccountSJsonCfg) (err error) {
@@ -97,6 +98,9 @@ func (acS *AccountSCfg) loadFromJSONCfg(jsnCfg *AccountSJsonCfg) (err error) {
if jsnCfg.Nested_fields != nil {
acS.NestedFields = *jsnCfg.Nested_fields
}
if jsnCfg.Max_iterations != nil {
acS.MaxIterations = *jsnCfg.Max_iterations
}
return
}
@@ -106,6 +110,7 @@ func (acS *AccountSCfg) AsMapInterface() (initialMP map[string]interface{}) {
utils.EnabledCfg: acS.Enabled,
utils.IndexedSelectsCfg: acS.IndexedSelects,
utils.NestedFieldsCfg: acS.NestedFields,
utils.MaxIterations: acS.MaxIterations,
}
if acS.AttributeSConns != nil {
attributeSConns := make([]string, len(acS.AttributeSConns))
@@ -167,6 +172,7 @@ func (acS AccountSCfg) Clone() (cln *AccountSCfg) {
Enabled: acS.Enabled,
IndexedSelects: acS.IndexedSelects,
NestedFields: acS.NestedFields,
MaxIterations: acS.MaxIterations,
}
if acS.AttributeSConns != nil {
cln.AttributeSConns = make([]string, len(acS.AttributeSConns))

View File

@@ -36,6 +36,7 @@ func TestAccountSCfgLoadFromJSONCfg(t *testing.T) {
Prefix_indexed_fields: &[]string{"*req.index1"},
Suffix_indexed_fields: &[]string{"*req.index1"},
Nested_fields: utils.BoolPointer(true),
Max_iterations: utils.IntPointer(1000),
}
expected := &AccountSCfg{
Enabled: true,
@@ -47,6 +48,7 @@ func TestAccountSCfgLoadFromJSONCfg(t *testing.T) {
PrefixIndexedFields: &[]string{"*req.index1"},
SuffixIndexedFields: &[]string{"*req.index1"},
NestedFields: true,
MaxIterations: 1000,
}
jsnCfg := NewDefaultCGRConfig()
if err = jsnCfg.accountSCfg.loadFromJSONCfg(jsonCfg); err != nil {
@@ -67,7 +69,8 @@ func TestAccountSCfgAsMapInterface(t *testing.T) {
"string_indexed_fields": ["*req.index1"],
"prefix_indexed_fields": ["*req.index1"],
"suffix_indexed_fields": ["*req.index1"],
"nested_fields": true,
"nested_fields": true,
"max_iterations": 100,
},
}`
@@ -81,6 +84,7 @@ func TestAccountSCfgAsMapInterface(t *testing.T) {
utils.PrefixIndexedFieldsCfg: []string{"*req.index1"},
utils.SuffixIndexedFieldsCfg: []string{"*req.index1"},
utils.NestedFieldsCfg: true,
utils.MaxIterations: 100,
}
if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgJSONStr); err != nil {
t.Error(err)
@@ -100,6 +104,7 @@ func TestAccountSCfgClone(t *testing.T) {
PrefixIndexedFields: &[]string{"*req.index1", "*req.index2"},
SuffixIndexedFields: &[]string{"*req.index1"},
NestedFields: true,
MaxIterations: 1000,
}
rcv := ban.Clone()
if !reflect.DeepEqual(ban, rcv) {

View File

@@ -1139,6 +1139,7 @@ const CGRATES_CFG_JSON = `
"prefix_indexed_fields": [], // query indexes based on these fields for faster processing
"suffix_indexed_fields": [], // query indexes based on these fields for faster processing
"nested_fields": false, // determines which field is checked when matching indexed filters(true: all; false: only the one on the first level)
"max_iterations": 1000, // maximum number of iterations
},

File diff suppressed because one or more lines are too long

View File

@@ -695,4 +695,5 @@ type AccountSJsonCfg struct {
Prefix_indexed_fields *[]string
Suffix_indexed_fields *[]string
Nested_fields *bool // applies when indexed fields is not defined
Max_iterations *int
}

View File

@@ -2337,6 +2337,9 @@ const (
CapsStrategyCfg = "caps_strategy"
CapsStatsIntervalCfg = "caps_stats_interval"
ShutdownTimeoutCfg = "shutdown_timeout"
// AccountSCfg
MaxIterations = "max_iterations"
)
// FC Template