diff --git a/engine/libfilters.go b/engine/libfilters.go index a232af9c2..293c61c77 100644 --- a/engine/libfilters.go +++ b/engine/libfilters.go @@ -26,27 +26,6 @@ import ( "github.com/ericlagergren/decimal" ) -// FilterFloat64CfgOpts returns the option as float64 if the filters match -func FilterFloat64CfgOpts(ctx *context.Context, tnt string, ev utils.DataProvider, fS *FilterS, dynOpts []*utils.DynamicFloat64Opt) (dft float64, err error) { - var hasDefault bool - for _, opt := range dynOpts { // iterate through the options - if len(opt.FilterIDs) == 1 && opt.FilterIDs[0] == utils.MetaDefault { - hasDefault = true - dft = opt.Value - continue - } - if pass, err := fS.Pass(ctx, tnt, opt.FilterIDs, ev); err != nil { // check if the filter is passing for the DataProvider and return the option if it does - return 0, err - } else if pass { - return opt.Value, nil - } - } - if !hasDefault { - err = utils.ErrNotFound - } - return // return the option or NOT_FOUND if there are no options or none of the filters pass -} - // GetFloat64Opts checks the specified option names in order among the keys in APIOpts returning the first value it finds as float64, otherwise it // returns the config option if at least one filter passes or NOT_FOUND if none of them do func GetFloat64Opts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *FilterS, dynOpts []*utils.DynamicFloat64Opt, @@ -76,27 +55,6 @@ func GetFloat64Opts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *Fi return // return the option or NOT_FOUND if there are no options or none of the filters pass } -// FilterDurationCfgOpts returns the option as time.Duration if the filters match -func FilterDurationCfgOpts(ctx *context.Context, tnt string, ev utils.DataProvider, fS *FilterS, dynOpts []*utils.DynamicDurationOpt) (dft time.Duration, err error) { - var hasDefault bool - for _, opt := range dynOpts { // iterate through the options - if len(opt.FilterIDs) == 1 && opt.FilterIDs[0] == utils.MetaDefault { - hasDefault = true - dft = opt.Value - continue - } - if pass, err := fS.Pass(ctx, tnt, opt.FilterIDs, ev); err != nil { // check if the filter is passing for the DataProvider and return the option if it does - return 0, err - } else if pass { - return opt.Value, nil - } - } - if !hasDefault { - err = utils.ErrNotFound - } - return // return the option or NOT_FOUND if there are no options or none of the filters pass -} - // GetDurationOpts 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 config option if at least one filter passes or NOT_FOUND if none of them do func GetDurationOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *FilterS, dynOpts []*utils.DynamicDurationOpt, @@ -126,27 +84,6 @@ func GetDurationOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *F return // return the option or NOT_FOUND if there are no options or none of the filters pass } -// FilterStringCfgOpts returns the option as string if the filters match -func FilterStringCfgOpts(ctx *context.Context, tnt string, ev utils.DataProvider, fS *FilterS, dynOpts []*utils.DynamicStringOpt) (dft string, err error) { - var hasDefault bool - for _, opt := range dynOpts { // iterate through the options - if len(opt.FilterIDs) == 1 && opt.FilterIDs[0] == utils.MetaDefault { - hasDefault = true - dft = opt.Value - continue - } - if pass, err := fS.Pass(ctx, tnt, opt.FilterIDs, ev); err != nil { // check if the filter is passing for the DataProvider and return the option if it does - return utils.EmptyString, err - } else if pass { - return opt.Value, nil - } - } - if !hasDefault { - err = utils.ErrNotFound - } - return // return the option or NOT_FOUND if there are no options or none of the filters pass -} - // GetStringOpts checks the specified option names in order among the keys in APIOpts returning the first value it finds as string, otherwise it // returns the config option if at least one filter passes or NOT_FOUND if none of them do func GetStringOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *FilterS, dynOpts []*utils.DynamicStringOpt, @@ -208,27 +145,6 @@ func GetTimeOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *Filte return utils.ParseTimeDetectLayout(cfgOptStr, tmz) // return the option or NOT_FOUND if there are no options or none of the filters pass } -// FilterStringSliceCfgOpts returns the option as []string if the filters match -func FilterStringSliceCfgOpts(ctx *context.Context, tnt string, ev utils.DataProvider, fS *FilterS, dynOpts []*utils.DynamicStringSliceOpt) (dft []string, err error) { - var hasDefault bool - for _, opt := range dynOpts { // iterate through the options - if len(opt.FilterIDs) == 1 && opt.FilterIDs[0] == utils.MetaDefault { - hasDefault = true - dft = opt.Value - continue - } - if pass, err := fS.Pass(ctx, tnt, opt.FilterIDs, ev); err != nil { // check if the filter is passing for the DataProvider and return the option if it does - return nil, err - } else if pass { - return opt.Value, nil - } - } - if !hasDefault { - err = utils.ErrNotFound - } - return // return the option or NOT_FOUND if there are no options or none of the filters pass -} - // GetStringSliceOpts checks the specified option names in order among the keys in APIOpts returning the first value it finds as []string, otherwise it // returns the config option if at least one filter passes or NOT_FOUND if none of them do func GetStringSliceOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *FilterS, dynOpts []*utils.DynamicStringSliceOpt, @@ -258,27 +174,6 @@ func GetStringSliceOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS return // return the option or NOT_FOUND if there are no options or none of the filters pass } -// FilterIntCfgOpts returns the option as int if the filters match -func FilterIntCfgOpts(ctx *context.Context, tnt string, ev utils.DataProvider, fS *FilterS, dynOpts []*utils.DynamicIntOpt) (dft int, err error) { - var hasDefault bool - for _, opt := range dynOpts { // iterate through the options - if len(opt.FilterIDs) == 1 && opt.FilterIDs[0] == utils.MetaDefault { - hasDefault = true - dft = opt.Value - continue - } - if pass, err := fS.Pass(ctx, tnt, opt.FilterIDs, ev); err != nil { // check if the filter is passing for the DataProvider and return the option if it does - return 0, err - } else if pass { - return opt.Value, nil - } - } - if !hasDefault { - err = utils.ErrNotFound - } - return // return the option or NOT_FOUND if there are no options or none of the filters pass -} - // GetIntOpts checks the specified option names in order among the keys in APIOpts returning the first value it finds as int, otherwise it // returns the config option if at least one filter passes or NOT_FOUND if none of them do func GetIntOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *FilterS, dynOpts []*utils.DynamicIntOpt, @@ -312,27 +207,6 @@ func GetIntOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *Filter return // return the option or NOT_FOUND if there are no options or none of the filters pass } -// FilterBoolCfgOpts returns the option as bool if the filters match -func FilterBoolCfgOpts(ctx *context.Context, tnt string, ev utils.DataProvider, fS *FilterS, dynOpts []*utils.DynamicBoolOpt) (dft bool, err error) { - var hasDefault bool - for _, opt := range dynOpts { // iterate through the options - if len(opt.FilterIDs) == 1 && opt.FilterIDs[0] == utils.MetaDefault { - hasDefault = true - dft = opt.Value - continue - } - if pass, err := fS.Pass(ctx, tnt, opt.FilterIDs, ev); err != nil { // check if the filter is passing for the DataProvider and return the option if it does - return false, err - } else if pass { - return opt.Value, nil - } - } - if !hasDefault { - err = utils.ErrNotFound - } - return // return the option or NOT_FOUND if there are no options or none of the filters pass -} - // GetBoolOpts checks the specified option names in order among the keys in APIOpts returning the first value it finds as bool, otherwise it // returns the config option if at least one filter passes or NOT_FOUND if none of them do func GetBoolOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *FilterS, dynOpts []*utils.DynamicBoolOpt, @@ -362,27 +236,6 @@ func GetBoolOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *Filte return // return the option or NOT_FOUND if there are no options or none of the filters pass } -// FilterDurationCfgOpts returns the option as time.Duration if the filters match -func FilterDecimalBigCfgOpts(ctx *context.Context, tnt string, ev utils.DataProvider, fS *FilterS, dynOpts []*utils.DynamicDecimalBigOpt) (dft *decimal.Big, err error) { - var hasDefault bool - for _, opt := range dynOpts { // iterate through the options - if len(opt.FilterIDs) == 1 && opt.FilterIDs[0] == utils.MetaDefault { - hasDefault = true - dft = opt.Value - continue - } - if pass, err := fS.Pass(ctx, tnt, opt.FilterIDs, ev); err != nil { // check if the filter is passing for the DataProvider and return the option if it does - return nil, err - } else if pass { - return opt.Value, nil - } - } - if !hasDefault { - err = utils.ErrNotFound - } - return // return the option or NOT_FOUND if there are no options or none of the filters pass -} - // GetDecimalBigOpts checks the specified option names in order among the keys in APIOpts returning the first value it finds as *decimal.Big, otherwise it // returns the config option if at least one filter passes or NOT_FOUND if none of them do func GetDecimalBigOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *FilterS, dynOpts []*utils.DynamicDecimalBigOpt, @@ -412,27 +265,6 @@ func GetDecimalBigOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS return // return the option or NOT_FOUND if there are no options or none of the filters pass } -// FilterInterfaceCfgOpts returns the option as interface{} if the filters match -func FilterInterfaceCfgOpts(ctx *context.Context, tnt string, ev utils.DataProvider, fS *FilterS, dynOpts []*utils.DynamicInterfaceOpt) (dft interface{}, err error) { - var hasDefault bool - for _, opt := range dynOpts { // iterate through the options - if len(opt.FilterIDs) == 1 && opt.FilterIDs[0] == utils.MetaDefault { - hasDefault = true - dft = opt.Value - continue - } - if pass, err := fS.Pass(ctx, tnt, opt.FilterIDs, ev); err != nil { // check if the filter is passing for the DataProvider and return the option if it does - return false, err - } else if pass { - return opt.Value, nil - } - } - if !hasDefault { - err = utils.ErrNotFound - } - return // return the option or NOT_FOUND if there are no options or none of the filters pass -} - // GetInterfaceOpts checks the specified option names in order among the keys in APIOpts returning the first value it finds as interface{}, otherwise it // returns the config option if at least one filter passes or NOT_FOUND if none of them do func GetInterfaceOpts(ctx *context.Context, tnt string, ev *utils.CGREvent, fS *FilterS, dynOpts []*utils.DynamicInterfaceOpt, diff --git a/utils/apitpdata_test.go b/utils/apitpdata_test.go index 51858f34e..8361c694a 100644 --- a/utils/apitpdata_test.go +++ b/utils/apitpdata_test.go @@ -21,8 +21,6 @@ import ( "reflect" "testing" "time" - - "github.com/ericlagergren/decimal" ) func TestPaginatorPaginateStringSlice(t *testing.T) { @@ -506,123 +504,6 @@ func TestNewAttrReloadCacheWithOpts(t *testing.T) { } } -func TestStartTimeNow(t *testing.T) { - ev := &CGREvent{ - Tenant: "*req.CGRID", - ID: "", - Event: map[string]interface{}{}, - APIOpts: map[string]interface{}{ - OptsRatesRateProfileIDs: []string{"123", "456", "789"}, - }, - } - timpulet1 := time.Now() - result, err := ev.StartTime(MetaNow, "", OptsRatesStartTime, MetaStartTime) - timpulet2 := time.Now() - if err != nil { - t.Errorf("Expected , received <%+v>", err) - } - if result.Before(timpulet1) && result.After(timpulet2) { - t.Errorf("Expected between <%+v> and <%+v>, received <%+v>", timpulet1, timpulet2, result) - } -} - -func TestStartTime(t *testing.T) { - ev := &CGREvent{ - Tenant: "*req.CGRID", - ID: "", - Event: map[string]interface{}{}, - APIOpts: map[string]interface{}{ - "*ratesStartTime": "2018-01-07T17:00:10Z", - OptsRatesRateProfileIDs: []string{"123", "456", "789"}, - }, - } - if result, err := ev.StartTime(MetaNow, "", OptsRatesStartTime, MetaStartTime); err != nil { - t.Errorf("Expected , received <%+v>", err) - } else if !reflect.DeepEqual(result.String(), "2018-01-07 17:00:10 +0000 UTC") { - t.Errorf("Expected <2018-01-07 17:00:10 +0000 UTC> , received <%+v>", result) - } -} - -func TestStartTime2(t *testing.T) { - ev := &CGREvent{ - Tenant: "cgrates.org", - ID: "TestEvent", - Event: map[string]interface{}{}, - APIOpts: map[string]interface{}{}, - } - if result, err := ev.StartTime("2018-01-07T17:00:10Z", "", OptsRatesStartTime, MetaStartTime); err != nil { - t.Errorf("Expected , received <%+v>", err) - } else if !reflect.DeepEqual(result.String(), "2018-01-07 17:00:10 +0000 UTC") { - t.Errorf("Expected <2018-01-07 17:00:10 +0000 UTC> , received <%+v>", result) - } -} - -func TestStartTimeError(t *testing.T) { - ev := &CGREvent{ - Tenant: "*req.CGRID", - ID: "", - Event: map[string]interface{}{}, - APIOpts: map[string]interface{}{ - "*ratesStartTime": "start", - OptsRatesRateProfileIDs: []string{"123", "456", "789"}, - }, - } - _, err := ev.StartTime(MetaNow, "", OptsRatesStartTime, MetaStartTime) - if err == nil && err.Error() != "received , received <%+v>", err) - } -} - -func TestUsageMinute(t *testing.T) { - ev := &CGREvent{ - Tenant: "*req.CGRID", - ID: "", - Event: map[string]interface{}{}, - APIOpts: map[string]interface{}{ - OptsRatesRateProfileIDs: []string{"123", "456", "789"}, - }, - } - if rcv, err := ev.OptsAsDecimal(decimal.New(int64(60*time.Second), 0), OptsRatesUsage, MetaUsage); err != nil { - t.Error(err) - } else if !reflect.DeepEqual(decimal.New(int64(time.Minute), 0), rcv) { - t.Errorf("Expected %+v, received %+v", decimal.New(int64(time.Minute), 0), rcv) - } -} - -func TestUsageError(t *testing.T) { - ev := &CGREvent{ - Tenant: "*req.CGRID", - ID: "", - Event: map[string]interface{}{}, - APIOpts: map[string]interface{}{ - "*ratesUsage": "start", - OptsRatesRateProfileIDs: []string{"123", "456", "789"}, - }, - } - _, err := ev.OptsAsDecimal(decimal.New(int64(time.Minute), 0), OptsRatesUsage, MetaUsage) - if err == nil && err.Error() != "received , received <%+v>", err) - } -} - -func TestUsage(t *testing.T) { - ev := &CGREvent{ - Tenant: "*req.CGRID", - ID: "", - Event: map[string]interface{}{}, - APIOpts: map[string]interface{}{ - "*ratesUsage": "2m10s", - OptsRatesRateProfileIDs: []string{"123", "456", "789"}, - }, - } - - if result, err := ev.OptsAsDecimal(decimal.New(int64(time.Minute), 0), OptsRatesUsage, MetaUsage); err != nil { - t.Error(err) - } else if !reflect.DeepEqual(result.String(), "130000000000") { - t.Errorf("Expected <130000000000> , received <%+v>", result.String()) - } -} - func TestNewTPBalanceCostIncrement(t *testing.T) { incrementStr := "20" fixedFeeStr := "10" @@ -701,21 +582,6 @@ func TestBalanceUnitFactor(t *testing.T) { } } -func TestATDUsage(t *testing.T) { - ev := &CGREvent{ - ID: "testID", - APIOpts: map[string]interface{}{ - OptsRatesUsage: true, - }, - } - - _, err := ev.OptsAsDecimal(decimal.New(int64(time.Minute), 0), OptsRatesUsage, MetaUsage) - expected := "cannot convert field: bool to decimal.Big" - if err == nil || err.Error() != expected { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, err.Error()) - } -} - func TestActivationIntervalEquals(t *testing.T) { aI := &ActivationInterval{ ActivationTime: time.Time{}, @@ -732,36 +598,6 @@ func TestActivationIntervalEquals(t *testing.T) { } } -func TestIntervalStart(t *testing.T) { - args := &CGREvent{ - APIOpts: map[string]interface{}{ - OptsRatesIntervalStart: "1ns", - OptsRatesRateProfileIDs: []string{"RP_1001"}, - }, - } - rcv, err := args.OptsAsDecimal(decimal.New(0, 0), OptsRatesIntervalStart) - exp := new(decimal.Big).SetUint64(1) - if err != nil { - t.Error(err) - } else if !reflect.DeepEqual(rcv, exp) { - t.Errorf("Expected %v but received %v", rcv, exp) - } -} - -func TestIntervalStartDefault(t *testing.T) { - args := &CGREvent{ - APIOpts: map[string]interface{}{ - OptsRatesRateProfileIDs: []string{"RP_1001"}, - }, - } - rcv, err := args.OptsAsDecimal(decimal.New(0, 0), OptsRatesIntervalStart) - exp := new(decimal.Big).SetUint64(0) - if err != nil { - t.Error(err) - } else if !reflect.DeepEqual(rcv, exp) { - t.Errorf("Expected %v but received %v", rcv, exp) - } -} func TestNewAttrReloadCacheWithOptsFromMap(t *testing.T) { excluded := NewStringSet([]string{MetaAPIBan, MetaLoadIDs}) mp := make(map[string][]string) diff --git a/utils/cgrevent.go b/utils/cgrevent.go index 4dd7e4bb2..dfe6358c7 100644 --- a/utils/cgrevent.go +++ b/utils/cgrevent.go @@ -21,8 +21,6 @@ package utils import ( "strings" "time" - - "github.com/ericlagergren/decimal" ) // CGREvent is a generic event processed by CGR services @@ -86,80 +84,6 @@ func (ev *CGREvent) FieldAsDuration(fldName string) (d time.Duration, err error) return IfaceAsDuration(iface) } -// OptsAsDecimal returns an option as decimal.Big instance -func (ev *CGREvent) OptsAsDecimal(defaultValue *decimal.Big, optNames ...string) (*decimal.Big, error) { - for _, optName := range optNames { - if iface, has := ev.APIOpts[optName]; has { - return IfaceAsBig(iface) - } - } - return defaultValue, nil -} - -// OptsAsString returns an option as string -func (ev *CGREvent) OptsAsString(defaultValue string, optNames ...string) string { - for _, optName := range optNames { - if iface, has := ev.APIOpts[optName]; has { - return IfaceAsString(iface) - } - } - return defaultValue -} - -// OptsAsInt returns an option as int -func (ev *CGREvent) OptsAsInt(defaultValue int, optNames ...string) (opt int, err error) { - for _, optName := range optNames { - if iface, has := ev.APIOpts[optName]; has { - var value int64 - if value, err = IfaceAsTInt64(iface); err != nil { - return 0, err - } - return int(value), nil - } - } - return defaultValue, nil -} - -// OptsAsDuration returns an option as time.Duration -func (ev *CGREvent) OptsAsDuration(defaultValue time.Duration, optNames ...string) (time.Duration, error) { - for _, optName := range optNames { - if iface, has := ev.APIOpts[optName]; has { - return IfaceAsDuration(iface) - } - } - return defaultValue, nil -} - -// OptsAsFloat64 returns an option as float64 -func (ev *CGREvent) OptsAsFloat64(defaultValue float64, optNames ...string) (float64, error) { - for _, optName := range optNames { - if iface, has := ev.APIOpts[optName]; has { - return IfaceAsFloat64(iface) - } - } - return defaultValue, nil -} - -// OptsAsStringSlice returns an option as []string -func (ev *CGREvent) OptsAsStringSlice(defaultValue []string, optNames ...string) ([]string, error) { - for _, optName := range optNames { - if iface, has := ev.APIOpts[optName]; has { - return IfaceAsStringSlice(iface) - } - } - return defaultValue, nil -} - -// OptsAsInterface returns an option as float64 -func (ev *CGREvent) OptsAsInterface(defaultValue interface{}, optNames ...string) interface{} { - for _, optName := range optNames { - if iface, has := ev.APIOpts[optName]; has { - return iface - } - } - return defaultValue -} - func (ev *CGREvent) Clone() (clned *CGREvent) { clned = &CGREvent{ Tenant: ev.Tenant, @@ -269,16 +193,6 @@ func NMAsCGREvent(nM *OrderedNavigableMap, tnt string, pathSep string, opts MapS return } -// StartTime returns the event time used to check active rate profiles -func (args *CGREvent) StartTime(configSTime, tmz string, optNames ...string) (time.Time, error) { - for _, optName := range optNames { - if iface, has := args.APIOpts[optName]; has { - return IfaceAsTime(iface, tmz) - } - } - return ParseTimeDetectLayout(configSTime, tmz) -} - // SetCloneable sets if the args should be clonned on internal connections func (attr *CGREvent) SetCloneable(rpcCloneable bool) { attr.clnb = rpcCloneable diff --git a/utils/cgrevent_test.go b/utils/cgrevent_test.go index f7dbc5529..3b19c2376 100644 --- a/utils/cgrevent_test.go +++ b/utils/cgrevent_test.go @@ -22,8 +22,6 @@ import ( "strings" "testing" "time" - - "github.com/ericlagergren/decimal" ) func TestCGREventHasField(t *testing.T) { @@ -335,37 +333,6 @@ func TestCGREventOptAsInt64(t *testing.T) { } } -func TestCGREventOptAsDurationNoOpts(t *testing.T) { - ev := &CGREvent{} - - expdur := decimal.New(int64(time.Minute), 0) - received, err := ev.OptsAsDecimal(decimal.New(int64(time.Minute), 0), "testString") - - if err != nil { - t.Error(err) - } else if !reflect.DeepEqual(expdur, received) { - t.Errorf("expected: <%+v>, \nreceived: <%+v>", expdur, received) - } -} - -func TestCGREventOptAsDuration(t *testing.T) { - ev := &CGREvent{ - APIOpts: map[string]interface{}{ - "*usage": 30, - }, - } - - received, err := ev.OptsAsDecimal(decimal.New(int64(72*time.Hour), 0), "*usage") - if err != nil { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", nil, err) - } - expected := decimal.New(int64(30*time.Nanosecond), 0) - - if !reflect.DeepEqual(received, expected) { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, received) - } -} - func TestCGREventAsDataProvider(t *testing.T) { ev := &CGREvent{ APIOpts: map[string]interface{}{