Adding time comparison in GreaterThan

This commit is contained in:
DanB
2017-10-20 21:04:50 +02:00
parent 74cef080ea
commit d6bb574e65
5 changed files with 50 additions and 4 deletions

View File

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

View File

@@ -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,,,,,,,
1 #Tenant[0] Id[1] FilterType[2] FilterFieldName[3] FilterFieldValues[4] ActivationInterval[5] Recurrent[6] MinSleep[7] Blocker[8] Weight[9] ActionIDs[10] Async[11]
2 cgrates.org THD_ACNT_BALANCE_1 *string Account 1001;1002 2014-07-29T15:00:00Z true 1s false 10 LOG_WARNING
3 cgrates.org THD_ACNT_BALANCE_1 *string EventSource AccountS
4 cgrates.org THD_ACNT_BALANCE_1 *gte BalanceValue 10.0
5 cgrates.org THD_ACNT_EXPIRED *string Account 1001;1002 2014-07-29T15:00:00Z true 1s false 10 LOG_WARNING
6 cgrates.org THD_ACNT_EXPIRED *gte ExpiryTime *now
7 cgrates.org THD_STATS_1 *string EventSource StatS 2014-07-29T15:00:00Z true 1s false 10 LOG_WARNING
8 cgrates.org THD_STATS_1 *lt ASR 40.0
9 cgrates.org THD_STATS_1 *lt ACD 3m

View File

@@ -474,6 +474,11 @@ const (
ResourceS = "ResourceS"
CacheThresholdProfiles = "threshold_profiles"
CacheThresholds = "thresholds"
AccountStatus = "AccountStatus"
BalanceStatus = "BalanceStatus"
StatUpdate = "StatUpdate"
ResourceUpdate = "ResourceUpdate"
ExpiryTime = "ExpiryTime"
)
func buildCacheInstRevPrefixes() {

View File

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

View File

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