From 96b7c979bf2ce7bf4f01e6db9b669d07a3fc9155 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Thu, 22 Jul 2021 17:56:44 +0300 Subject: [PATCH] *usage and *startTime in ApiOpts for rates and accounts --- accounts/accounts.go | 16 +++++++++------- utils/apitpdata.go | 9 ++++++--- utils/cgrevent.go | 7 ++++--- utils/consts.go | 1 + 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/accounts/accounts.go b/accounts/accounts.go index eb0b91586..031240c7f 100644 --- a/accounts/accounts.go +++ b/accounts/accounts.go @@ -133,22 +133,24 @@ func (aS *AccountS) matchingAccountsForEvent(ctx *context.Context, tnt string, c func (aS *AccountS) accountsDebit(ctx *context.Context, acnts []*utils.AccountWithWeight, cgrEv *utils.CGREvent, concretes, store bool) (ec *utils.EventCharges, err error) { usage := decimal.New(int64(72*time.Hour), 0) - var usgEv time.Duration - if usgEv, err = cgrEv.FieldAsDuration(utils.Usage); err != nil { + + var usgEve *decimal.Big + // first we try from opts to search in *usage + if usgEve, err = cgrEv.OptsAsDecimal(utils.MetaUsage); err != nil { if err != utils.ErrNotFound { return } - // not found, try at opts level - if usgEv, err = cgrEv.OptAsDuration(utils.MetaUsage); err != nil { + // not found/ will try in *ratesUsage + if usgEve, err = cgrEv.OptsAsDecimal(utils.OptsRatesUsage); err != nil { if err != utils.ErrNotFound { return } err = nil } else { // found, overwrite usage - usage = decimal.New(int64(usgEv), 0) + usage = usgEve } - } else { - usage = decimal.New(int64(usgEv), 0) + } else { // not found either in *usage or *ratesUsage, use default + usage = usgEve } dbted := decimal.New(0, 0) acntBkps := make([]utils.AccountBalancesBackup, len(acnts)) diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 5fbc5b4e9..e44788548 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -896,17 +896,20 @@ func (args *ArgsCostForEvent) StartTime(tmz string) (sTime time.Time, err error) if tIface, has := args.APIOpts[OptsRatesStartTime]; has { return IfaceAsTime(tIface, tmz) } + if tIface, has := args.APIOpts[MetaStartTime]; has { + return IfaceAsTime(tIface, tmz) + } return time.Now(), nil } // usage returns the event time used to check active rate profiles func (args *ArgsCostForEvent) Usage() (usage *decimal.Big, err error) { - // first search for the usage in opts + // first search for the rateUsage in opts if uIface, has := args.APIOpts[OptsRatesUsage]; has { return IfaceAsBig(uIface) } - // if the usage is not present in opts search in event - if uIface, has := args.Event[Usage]; has { + // second search for the usage in opts + if uIface, has := args.APIOpts[MetaUsage]; has { return IfaceAsBig(uIface) } // if the usage is not found in the event populate with default value and overwrite the NOT_FOUND error with nil diff --git a/utils/cgrevent.go b/utils/cgrevent.go index 9c9b91dc9..201aac562 100644 --- a/utils/cgrevent.go +++ b/utils/cgrevent.go @@ -19,6 +19,7 @@ along with this program. If not, see package utils import ( + "github.com/ericlagergren/decimal" "strings" "time" ) @@ -83,14 +84,14 @@ func (ev *CGREvent) FieldAsDuration(fldName string) (d time.Duration, err error) return IfaceAsDuration(iface) } -// OptAsDuration returns an option as Duration instance -func (ev *CGREvent) OptAsDuration(optName string) (d time.Duration, err error) { +// OptsAsDecimal OptAsDecimal returns an option as decimal.Big instance +func (ev *CGREvent) OptsAsDecimal(optName string) (d *decimal.Big, err error) { iface, has := ev.APIOpts[optName] if !has { err = ErrNotFound return } - return IfaceAsDuration(iface) + return IfaceAsBig(iface) } func (ev *CGREvent) Clone() (clned *CGREvent) { diff --git a/utils/consts.go b/utils/consts.go index c6456b516..0a8706a99 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -850,6 +850,7 @@ const ( MetaExporterIDs = "*exporterIDs" MetaAsync = "*async" MetaUsage = "*usage" + MetaStartTime = "*startTime" Weights = "Weights" UnitFactors = "UnitFactors" CostIncrements = "CostIncrements"