From d6bb574e6518e2c1ddb95df0ae80042e10202077 Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 20 Oct 2017 21:04:50 +0200 Subject: [PATCH] Adding time comparison in GreaterThan --- apier/v1/thresholds_it_test.go | 12 +++++++++--- data/tariffplans/tutorial/Thresholds.csv | 2 ++ utils/consts.go | 5 +++++ utils/reflect.go | 18 +++++++++++++++++- utils/reflect_test.go | 17 +++++++++++++++++ 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/apier/v1/thresholds_it_test.go b/apier/v1/thresholds_it_test.go index 166c0ae17..659c19c47 100644 --- a/apier/v1/thresholds_it_test.go +++ b/apier/v1/thresholds_it_test.go @@ -46,15 +46,18 @@ var tEvs = []*engine.ThresholdEvent{ Tenant: "cgrates.org", ID: "event1", Fields: map[string]interface{}{ + utils.EventType: utils.BalanceStatus, utils.EventSource: utils.AccountService, utils.ACCOUNT: "1002", utils.BalanceType: utils.MONETARY, utils.BalanceID: utils.META_DEFAULT, - utils.BalanceValue: 12.3}}, + utils.BalanceValue: 12.3, + utils.ExpiryTime: "2009-11-10T23:00:00Z"}}, &engine.ThresholdEvent{ // hitting THD_STATS_1 Tenant: "cgrates.org", ID: "event2", Fields: map[string]interface{}{ + utils.EventType: utils.StatUpdate, utils.EventSource: utils.StatService, utils.StatID: "Stats1", utils.ACCOUNT: "1002", @@ -69,6 +72,7 @@ var tEvs = []*engine.ThresholdEvent{ Tenant: "cgrates.org", ID: "event3", Fields: map[string]interface{}{ + utils.EventType: utils.StatUpdate, utils.EventSource: utils.StatService, utils.StatID: "STATS_HOURLY_DE", utils.ACCOUNT: "1002", @@ -80,6 +84,7 @@ var tEvs = []*engine.ThresholdEvent{ Tenant: "cgrates.org", ID: "event4", Fields: map[string]interface{}{ + utils.EventType: utils.StatUpdate, utils.EventSource: utils.StatService, utils.StatID: "STATS_DAILY_DE", utils.ACCOUNT: "1002", @@ -90,6 +95,7 @@ var tEvs = []*engine.ThresholdEvent{ Tenant: "cgrates.org", ID: "event5", Fields: map[string]interface{}{ + utils.EventType: utils.ResourceUpdate, utils.EventSource: utils.ResourceS, utils.ACCOUNT: "1002", utils.ResourceID: "RES_GRP_1", @@ -172,7 +178,7 @@ func testV1TSFromFolder(t *testing.T) { func testV1TSGetThresholds(t *testing.T) { var tIDs []string - expectedIDs := []string{"THD_RES_1", "THD_STATS_2", "THD_STATS_1", "THD_ACNT_BALANCE_1", "THD_STATS_3"} + expectedIDs := []string{"THD_RES_1", "THD_STATS_2", "THD_STATS_1", "THD_ACNT_BALANCE_1", "THD_ACNT_EXPIRED", "THD_STATS_3"} if err := tSv1Rpc.Call("ThresholdSV1.GetThresholdIDs", "cgrates.org", &tIDs); err != nil { t.Error(err) } else if len(expectedIDs) != len(tIDs) { @@ -224,7 +230,7 @@ func testV1TSProcessEvent(t *testing.T) { func testV1TSGetThresholdsAfterProcess(t *testing.T) { var tIDs []string - expectedIDs := []string{"THD_RES_1", "THD_STATS_2", "THD_STATS_1", "THD_ACNT_BALANCE_1"} + expectedIDs := []string{"THD_RES_1", "THD_STATS_2", "THD_STATS_1", "THD_ACNT_BALANCE_1", "THD_ACNT_EXPIRED"} if err := tSv1Rpc.Call("ThresholdSV1.GetThresholdIDs", "cgrates.org", &tIDs); err != nil { t.Error(err) } else if len(expectedIDs) != len(tIDs) { // THD_STATS_3 is not reccurent, so it was removed diff --git a/data/tariffplans/tutorial/Thresholds.csv b/data/tariffplans/tutorial/Thresholds.csv index 54b36a0f3..902fec01e 100644 --- a/data/tariffplans/tutorial/Thresholds.csv +++ b/data/tariffplans/tutorial/Thresholds.csv @@ -2,6 +2,8 @@ cgrates.org,THD_ACNT_BALANCE_1,*string,Account,1001;1002,2014-07-29T15:00:00Z,true,1s,false,10,LOG_WARNING, cgrates.org,THD_ACNT_BALANCE_1,*string,EventSource,AccountS,,,,,,, cgrates.org,THD_ACNT_BALANCE_1,*gte,BalanceValue,10.0,,,,,,, +cgrates.org,THD_ACNT_EXPIRED,*string,Account,1001;1002,2014-07-29T15:00:00Z,true,1s,false,10,LOG_WARNING, +cgrates.org,THD_ACNT_EXPIRED,*gte,ExpiryTime,*now,,,,,,, cgrates.org,THD_STATS_1,*string,EventSource,StatS,2014-07-29T15:00:00Z,true,1s,false,10,LOG_WARNING, cgrates.org,THD_STATS_1,*lt,ASR,40.0,,,,,,, cgrates.org,THD_STATS_1,*lt,ACD,3m,,,,,,, diff --git a/utils/consts.go b/utils/consts.go index ccd9355a3..21889608a 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -474,6 +474,11 @@ const ( ResourceS = "ResourceS" CacheThresholdProfiles = "threshold_profiles" CacheThresholds = "thresholds" + AccountStatus = "AccountStatus" + BalanceStatus = "BalanceStatus" + StatUpdate = "StatUpdate" + ResourceUpdate = "ResourceUpdate" + ExpiryTime = "ExpiryTime" ) func buildCacheInstRevPrefixes() { diff --git a/utils/reflect.go b/utils/reflect.go index 38488bb5f..15039f1cc 100644 --- a/utils/reflect.go +++ b/utils/reflect.go @@ -74,6 +74,10 @@ func StringToInterface(s string) interface{} { if d, err := time.ParseDuration(s); err == nil { return d } + // time.Time + if t, err := ParseTimeDetectLayout(s, "Local"); err == nil { + return t + } // string return s } @@ -200,8 +204,20 @@ func GreaterThan(item, oItem interface{}, orEqual bool) (gte bool, err error) { } else { gte = valItm.Int() > valOtItm.Int() } + case reflect.Struct: + tVal, ok := valItm.Interface().(time.Time) + tOVal, oOK := valOtItm.Interface().(time.Time) + if !ok || !oOK { + return false, fmt.Errorf("cannot cast struct to time: %v, %v", ok, oOK) + } + if orEqual { + gte = tVal == tOVal + } + if !gte { + gte = tVal.After(tOVal) + } default: // unsupported comparison - err = fmt.Errorf("unsupported type: %v", typItem) + err = fmt.Errorf("unsupported comparison type: %v, kind: %v", typItem, typItem.Kind()) } return } diff --git a/utils/reflect_test.go b/utils/reflect_test.go index 98a6beabb..bd6878d66 100644 --- a/utils/reflect_test.go +++ b/utils/reflect_test.go @@ -180,6 +180,23 @@ func TestGreaterThan(t *testing.T) { } else if gte { t.Error("should be less than") } + if gte, err := GreaterThan(time.Duration(2*time.Second), + time.Duration(1*time.Second), false); err != nil { + t.Error(err) + } else if !gte { + t.Error("should be greater than") + } + now := time.Now() + if gte, err := GreaterThan(now.Add(time.Second), now, false); err != nil { + t.Error(err) + } else if !gte { + t.Error("should be greater than") + } + if gte, err := GreaterThan(now, now, true); err != nil { + t.Error(err) + } else if !gte { + t.Error("should be equal") + } } func TestStringToInterface(t *testing.T) {