mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Make usageTTL opt a pointer to be able to determine whether or not it has been set in config
This commit is contained in:
committed by
Dan Christian Bogos
parent
38cad48094
commit
9b406acf71
@@ -720,7 +720,7 @@ func TestCgrCfgJSONDefaultsResLimCfg(t *testing.T) {
|
||||
SuffixIndexedFields: &[]string{},
|
||||
Opts: &ResourcesOpts{
|
||||
UsageID: utils.EmptyString,
|
||||
UsageTTL: 72 * time.Hour,
|
||||
UsageTTL: utils.DurationPointer(72 * time.Hour),
|
||||
Units: 1,
|
||||
},
|
||||
}
|
||||
@@ -2003,7 +2003,7 @@ func TestResourceSConfig(t *testing.T) {
|
||||
NestedFields: false,
|
||||
Opts: &ResourcesOpts{
|
||||
UsageID: "",
|
||||
UsageTTL: 72 * time.Hour,
|
||||
UsageTTL: utils.DurationPointer(72 * time.Hour),
|
||||
Units: 1,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
|
||||
type ResourcesOpts struct {
|
||||
UsageID string
|
||||
UsageTTL time.Duration
|
||||
UsageTTL *time.Duration
|
||||
Units float64
|
||||
}
|
||||
|
||||
@@ -51,9 +51,11 @@ func (resOpts *ResourcesOpts) loadFromJSONCfg(jsnCfg *ResourcesOptsJson) (err er
|
||||
resOpts.UsageID = *jsnCfg.UsageID
|
||||
}
|
||||
if jsnCfg.UsageTTL != nil {
|
||||
if resOpts.UsageTTL, err = utils.ParseDurationWithNanosecs(*jsnCfg.UsageTTL); err != nil {
|
||||
var ttl time.Duration
|
||||
if ttl, err = utils.ParseDurationWithNanosecs(*jsnCfg.UsageTTL); err != nil {
|
||||
return err
|
||||
}
|
||||
resOpts.UsageTTL = utils.DurationPointer(ttl)
|
||||
}
|
||||
if jsnCfg.Units != nil {
|
||||
resOpts.Units = *jsnCfg.Units
|
||||
@@ -119,9 +121,11 @@ func (rlcfg *ResourceSConfig) loadFromJSONCfg(jsnCfg *ResourceSJsonCfg) (err err
|
||||
// AsMapInterface returns the config as a map[string]interface{}
|
||||
func (rlcfg *ResourceSConfig) AsMapInterface() (initialMP map[string]interface{}) {
|
||||
opts := map[string]interface{}{
|
||||
utils.MetaUsageIDCfg: rlcfg.Opts.UsageID,
|
||||
utils.MetaUsageTTLCfg: rlcfg.Opts.UsageTTL,
|
||||
utils.MetaUnitsCfg: rlcfg.Opts.Units,
|
||||
utils.MetaUsageIDCfg: rlcfg.Opts.UsageID,
|
||||
utils.MetaUnitsCfg: rlcfg.Opts.Units,
|
||||
}
|
||||
if rlcfg.Opts.UsageTTL != nil {
|
||||
opts[utils.MetaUsageTTLCfg] = *rlcfg.Opts.UsageTTL
|
||||
}
|
||||
initialMP = map[string]interface{}{
|
||||
utils.EnabledCfg: rlcfg.Enabled,
|
||||
@@ -167,12 +171,15 @@ func (rlcfg *ResourceSConfig) AsMapInterface() (initialMP map[string]interface{}
|
||||
return
|
||||
}
|
||||
|
||||
func (resOpts *ResourcesOpts) Clone() *ResourcesOpts {
|
||||
return &ResourcesOpts{
|
||||
UsageID: resOpts.UsageID,
|
||||
UsageTTL: resOpts.UsageTTL,
|
||||
Units: resOpts.Units,
|
||||
func (resOpts *ResourcesOpts) Clone() (cln *ResourcesOpts) {
|
||||
cln = &ResourcesOpts{
|
||||
UsageID: resOpts.UsageID,
|
||||
Units: resOpts.Units,
|
||||
}
|
||||
if resOpts.UsageTTL != nil {
|
||||
cln.UsageTTL = utils.DurationPointer(*resOpts.UsageTTL)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Clone returns a deep copy of ResourceSConfig
|
||||
|
||||
@@ -46,7 +46,7 @@ func TestResourceSConfigloadFromJsonCfgCase1(t *testing.T) {
|
||||
SuffixIndexedFields: &[]string{"*req.index1"},
|
||||
NestedFields: true,
|
||||
Opts: &ResourcesOpts{
|
||||
UsageTTL: 72 * time.Hour,
|
||||
UsageTTL: utils.DurationPointer(72 * time.Hour),
|
||||
Units: 1,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -706,13 +706,11 @@ func (rS *ResourceService) V1ResourcesForEvent(args *utils.CGREvent, reply *Reso
|
||||
}
|
||||
// end of RPC caching
|
||||
|
||||
ttl := rS.cgrcfg.ResourceSCfg().Opts.UsageTTL
|
||||
if opt, has := args.APIOpts[utils.OptsResourcesUsageTTL]; has {
|
||||
if ttl, err = utils.IfaceAsDuration(opt); err != nil {
|
||||
return
|
||||
}
|
||||
var usageTTL *time.Duration
|
||||
if usageTTL, err = utils.GetDurationPointerOpts(args, rS.cgrcfg.ResourceSCfg().Opts.UsageTTL,
|
||||
utils.OptsResourcesUsageTTL); err != nil {
|
||||
return
|
||||
}
|
||||
usageTTL := utils.DurationPointer(ttl)
|
||||
var mtcRLs Resources
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(tnt, args, usageID, usageTTL); err != nil {
|
||||
return err
|
||||
@@ -758,13 +756,11 @@ func (rS *ResourceService) V1AuthorizeResources(args *utils.CGREvent, reply *str
|
||||
}
|
||||
// end of RPC caching
|
||||
|
||||
ttl := rS.cgrcfg.ResourceSCfg().Opts.UsageTTL
|
||||
if opt, has := args.APIOpts[utils.OptsResourcesUsageTTL]; has {
|
||||
if ttl, err = utils.IfaceAsDuration(opt); err != nil {
|
||||
return
|
||||
}
|
||||
var usageTTL *time.Duration
|
||||
if usageTTL, err = utils.GetDurationPointerOpts(args, rS.cgrcfg.ResourceSCfg().Opts.UsageTTL,
|
||||
utils.OptsResourcesUsageTTL); err != nil {
|
||||
return
|
||||
}
|
||||
usageTTL := utils.DurationPointer(ttl)
|
||||
var mtcRLs Resources
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(tnt, args, usageID, usageTTL); err != nil {
|
||||
return err
|
||||
@@ -827,13 +823,11 @@ func (rS *ResourceService) V1AllocateResources(args *utils.CGREvent, reply *stri
|
||||
}
|
||||
// end of RPC caching
|
||||
|
||||
ttl := rS.cgrcfg.ResourceSCfg().Opts.UsageTTL
|
||||
if opt, has := args.APIOpts[utils.OptsResourcesUsageTTL]; has {
|
||||
if ttl, err = utils.IfaceAsDuration(opt); err != nil {
|
||||
return
|
||||
}
|
||||
var usageTTL *time.Duration
|
||||
if usageTTL, err = utils.GetDurationPointerOpts(args, rS.cgrcfg.ResourceSCfg().Opts.UsageTTL,
|
||||
utils.OptsResourcesUsageTTL); err != nil {
|
||||
return
|
||||
}
|
||||
usageTTL := utils.DurationPointer(ttl)
|
||||
var mtcRLs Resources
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(tnt, args, usageID,
|
||||
usageTTL); err != nil {
|
||||
@@ -900,13 +894,11 @@ func (rS *ResourceService) V1ReleaseResources(args *utils.CGREvent, reply *strin
|
||||
}
|
||||
// end of RPC caching
|
||||
|
||||
ttl := rS.cgrcfg.ResourceSCfg().Opts.UsageTTL
|
||||
if opt, has := args.APIOpts[utils.OptsResourcesUsageTTL]; has {
|
||||
if ttl, err = utils.IfaceAsDuration(opt); err != nil {
|
||||
return
|
||||
}
|
||||
var usageTTL *time.Duration
|
||||
if usageTTL, err = utils.GetDurationPointerOpts(args, rS.cgrcfg.ResourceSCfg().Opts.UsageTTL,
|
||||
utils.OptsResourcesUsageTTL); err != nil {
|
||||
return
|
||||
}
|
||||
usageTTL := utils.DurationPointer(ttl)
|
||||
var mtcRLs Resources
|
||||
if mtcRLs, err = rS.matchingResourcesForEvent(tnt, args, usageID,
|
||||
usageTTL); err != nil {
|
||||
|
||||
@@ -130,3 +130,18 @@ func GetIntPointerOpts(ev *CGREvent, dftOpt *int, optNames ...string) (cfgOpt *i
|
||||
}
|
||||
return dftOpt, nil
|
||||
}
|
||||
|
||||
// GetDurationPointerOpts checks the specified option names in order among the keys in APIOpts returning the first value it finds as *time.Duration, otherwise it
|
||||
// returns the default option (usually the value specified in config)
|
||||
func GetDurationPointerOpts(ev *CGREvent, dftOpt *time.Duration, optNames ...string) (cfgOpt *time.Duration, err error) {
|
||||
for _, optName := range optNames {
|
||||
if opt, has := ev.APIOpts[optName]; has {
|
||||
var value time.Duration
|
||||
if value, err = IfaceAsDuration(opt); err != nil {
|
||||
return
|
||||
}
|
||||
return DurationPointer(value), nil
|
||||
}
|
||||
}
|
||||
return dftOpt, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user