*usage and *startTime in ApiOpts for rates and accounts

This commit is contained in:
porosnicuadrian
2021-07-22 17:56:44 +03:00
committed by Dan Christian Bogos
parent d7bd43b0bc
commit 96b7c979bf
4 changed files with 20 additions and 13 deletions

View File

@@ -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))

View File

@@ -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

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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) {

View File

@@ -850,6 +850,7 @@ const (
MetaExporterIDs = "*exporterIDs"
MetaAsync = "*async"
MetaUsage = "*usage"
MetaStartTime = "*startTime"
Weights = "Weights"
UnitFactors = "UnitFactors"
CostIncrements = "CostIncrements"