mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-13 11:06:25 +05:00
AccountS UsageFactor - UnitFactor
This commit is contained in:
@@ -27,7 +27,7 @@ import (
|
||||
)
|
||||
|
||||
// newAbstractBalance constructs an abstractBalanceOperator
|
||||
func newAbstractBalanceOperator(blnCfg *utils.Balance, cncrtBlncs []balanceOperator,
|
||||
func newAbstractBalanceOperator(blnCfg *utils.Balance, cncrtBlncs []*concreteBalance,
|
||||
fltrS *engine.FilterS, ralsConns []string) balanceOperator {
|
||||
return &abstractBalance{blnCfg, cncrtBlncs, fltrS, ralsConns}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ func newAbstractBalanceOperator(blnCfg *utils.Balance, cncrtBlncs []balanceOpera
|
||||
// abstractBalance is the operator for *abstract balance type
|
||||
type abstractBalance struct {
|
||||
blnCfg *utils.Balance
|
||||
cncrtBlncs []balanceOperator // paying balances
|
||||
cncrtBlncs []*concreteBalance // paying balances
|
||||
fltrS *engine.FilterS
|
||||
ralsConns []string
|
||||
}
|
||||
@@ -44,7 +44,7 @@ type abstractBalance struct {
|
||||
func (ab *abstractBalance) debit(cgrEv *utils.CGREventWithOpts,
|
||||
startTime time.Time, usage *decimal.Big) (ec *utils.EventCharges, err error) {
|
||||
//var uF *utils.UsageFactor
|
||||
if _, err = usageWithFactor(usage, ab.blnCfg, ab.fltrS, cgrEv); err != nil {
|
||||
if _, _, err = usageWithFactor(ab.blnCfg, ab.fltrS, cgrEv, usage); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
|
||||
@@ -44,7 +44,7 @@ type concreteBalance struct {
|
||||
func (cb *concreteBalance) debit(cgrEv *utils.CGREventWithOpts,
|
||||
startTime time.Time, usage *decimal.Big) (ec *utils.EventCharges, err error) {
|
||||
//var uF *utils.UsageFactor
|
||||
if _, err = usageWithFactor(usage, cb.blnCfg, cb.fltrS, cgrEv); err != nil {
|
||||
if _, _, err = usageWithFactor(cb.blnCfg, cb.fltrS, cgrEv, usage); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
|
||||
@@ -38,17 +38,17 @@ func newAccountBalances(acnt *utils.AccountProfile,
|
||||
acntBlncs.typIdx[blnCfg.Type] = append(acntBlncs.typIdx[blnCfg.Type], i)
|
||||
}
|
||||
// populate cncrtBlncs
|
||||
acntBlncs.cncrtBlncs = make([]balanceOperator, len(acntBlncs.typIdx[utils.MetaConcrete]))
|
||||
acntBlncs.cncrtBlncs = make([]*concreteBalance, len(acntBlncs.typIdx[utils.MetaConcrete]))
|
||||
for i, blncIdx := range acntBlncs.typIdx[utils.MetaConcrete] {
|
||||
acntBlncs.cncrtBlncs[i] = newConcreteBalanceOperator(acntBlncs.blnCfgs[blncIdx], fltrS, ralsConns)
|
||||
acntBlncs.procs[acntBlncs.blnCfgs[blncIdx].ID] = acntBlncs.cncrtBlncs[i]
|
||||
acntBlncs.cncrtBlncs[i] = newConcreteBalanceOperator(acntBlncs.blnCfgs[blncIdx], fltrS, ralsConns).(*concreteBalance)
|
||||
acntBlncs.opers[acntBlncs.blnCfgs[blncIdx].ID] = acntBlncs.cncrtBlncs[i]
|
||||
}
|
||||
// populate procs
|
||||
// populate opers
|
||||
for _, blnCfg := range acntBlncs.blnCfgs {
|
||||
if blnCfg.Type == utils.MetaConcrete { // already computed above
|
||||
continue
|
||||
}
|
||||
if acntBlncs.procs[blnCfg.ID], err = newBalanceOperator(blnCfg,
|
||||
if acntBlncs.opers[blnCfg.ID], err = newBalanceOperator(blnCfg,
|
||||
acntBlncs.cncrtBlncs, fltrS, ralsConns); err != nil {
|
||||
return
|
||||
}
|
||||
@@ -60,8 +60,8 @@ func newAccountBalances(acnt *utils.AccountProfile,
|
||||
type accountBalances struct {
|
||||
blnCfgs []*utils.Balance // ordered list of balance configurations
|
||||
typIdx map[string][]int // index based on type
|
||||
cncrtBlncs []balanceOperator // concrete balances so we can pass them to the newBalanceOperator
|
||||
procs map[string]balanceOperator // map[blncID]balanceOperator
|
||||
cncrtBlncs []*concreteBalance // concrete balances so we can pass them to the newBalanceOperator
|
||||
opers map[string]balanceOperator // map[blncID]balanceOperator
|
||||
|
||||
fltrS *engine.FilterS
|
||||
ralsConns []string
|
||||
@@ -69,7 +69,7 @@ type accountBalances struct {
|
||||
|
||||
// newBalanceOperator instantiates balanceOperator interface
|
||||
// cncrtBlncs are needed for abstract balance debits
|
||||
func newBalanceOperator(blncCfg *utils.Balance, cncrtBlncs []balanceOperator,
|
||||
func newBalanceOperator(blncCfg *utils.Balance, cncrtBlncs []*concreteBalance,
|
||||
fltrS *engine.FilterS, ralsConns []string) (bP balanceOperator, err error) {
|
||||
switch blncCfg.Type {
|
||||
default:
|
||||
@@ -89,11 +89,12 @@ type balanceOperator interface {
|
||||
|
||||
// usagewithFactor returns the usage considering also factor for the debit
|
||||
// includes event filtering to avoid code duplication
|
||||
func usageWithFactor(usage *decimal.Big, blnCfg *utils.Balance, fltrS *engine.FilterS,
|
||||
cgrEv *utils.CGREventWithOpts) (mtchUF *utils.UsageFactor, err error) {
|
||||
func usageWithFactor(blnCfg *utils.Balance, fltrS *engine.FilterS,
|
||||
cgrEv *utils.CGREventWithOpts, usage *decimal.Big) (resUsage *decimal.Big, mtchedUF *utils.UnitFactor, err error) {
|
||||
resUsage = usage
|
||||
fctr := decimal.New(1, 0)
|
||||
if len(blnCfg.FilterIDs) == 0 &&
|
||||
len(blnCfg.UsageFactors) == 0 {
|
||||
len(blnCfg.UnitFactors) == 0 {
|
||||
return
|
||||
}
|
||||
evNm := utils.MapStorage{
|
||||
@@ -103,26 +104,26 @@ func usageWithFactor(usage *decimal.Big, blnCfg *utils.Balance, fltrS *engine.Fi
|
||||
// match the general balance filters
|
||||
var pass bool
|
||||
if pass, err = fltrS.Pass(cgrEv.CGREvent.Tenant, blnCfg.FilterIDs, evNm); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
} else if !pass {
|
||||
return nil, utils.ErrFilterNotPassingNoCaps
|
||||
return nil, nil, utils.ErrFilterNotPassingNoCaps
|
||||
}
|
||||
// find out the factor
|
||||
for _, uF := range blnCfg.UsageFactors {
|
||||
for _, uF := range blnCfg.UnitFactors {
|
||||
if pass, err = fltrS.Pass(cgrEv.CGREvent.Tenant, uF.FilterIDs, evNm); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
} else if !pass {
|
||||
continue
|
||||
}
|
||||
fctr = uF.DecimalFactor()
|
||||
mtchUF = uF
|
||||
mtchedUF = uF
|
||||
break
|
||||
}
|
||||
if mtchUF == nil {
|
||||
if mtchedUF == nil {
|
||||
return
|
||||
}
|
||||
if fctr.Cmp(decimal.New(1, 0)) == 0 {
|
||||
*usage = *utils.MultiplyBig(usage, fctr)
|
||||
if fctr.Cmp(decimal.New(1, 0)) != 0 {
|
||||
resUsage = utils.MultiplyBig(usage, fctr)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -39,20 +39,20 @@ type AccountProfile struct {
|
||||
|
||||
// Balance represents one Balance inside an Account
|
||||
type Balance struct {
|
||||
ID string // Balance identificator, unique within an Account
|
||||
FilterIDs []string
|
||||
Weight float64
|
||||
Blocker bool
|
||||
Type string
|
||||
Opts map[string]interface{}
|
||||
UsageFactors []*UsageFactor
|
||||
Value float64
|
||||
ID string // Balance identificator, unique within an Account
|
||||
FilterIDs []string
|
||||
Weight float64
|
||||
Blocker bool
|
||||
Type string
|
||||
Opts map[string]interface{}
|
||||
UnitFactors []*UnitFactor
|
||||
Value float64
|
||||
|
||||
val *decimal.Big
|
||||
}
|
||||
|
||||
// UsageFactor is a multiplicator for the usage received
|
||||
type UsageFactor struct {
|
||||
// UnitFactor is a multiplicator for the usage received
|
||||
type UnitFactor struct {
|
||||
FilterIDs []string
|
||||
Factor float64
|
||||
|
||||
@@ -60,7 +60,7 @@ type UsageFactor struct {
|
||||
}
|
||||
|
||||
// DecimalFactor exports the decimal value of the factor
|
||||
func (uf *UsageFactor) DecimalFactor() *decimal.Big {
|
||||
func (uf *UnitFactor) DecimalFactor() *decimal.Big {
|
||||
return uf.fct
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ func (aP *AccountProfile) Compile() (err error) {
|
||||
// Compile populates the internal data
|
||||
func (b *Balance) Compile() (err error) {
|
||||
b.val = new(decimal.Big).SetFloat64(b.Value)
|
||||
for _, uf := range b.UsageFactors {
|
||||
for _, uf := range b.UnitFactors {
|
||||
uf.fct = new(decimal.Big).SetFloat64(uf.Factor)
|
||||
}
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user