From 6de1f132d8aba9244280d5cf0e18e61faded6b77 Mon Sep 17 00:00:00 2001 From: Edwardro22 Date: Tue, 1 Aug 2017 19:58:50 +0300 Subject: [PATCH] modified ResLimiter --- .../mysql/create_tariffplan_tables.sql | 5 +- .../postgres/create_tariffplan_tables.sql | 5 +- data/tariffplans/testtp/Stats.csv | 4 +- data/tariffplans/tutorial/Stats.csv | 4 +- engine/loader_csv_test.go | 7 ++- engine/model_helpers.go | 18 +++---- engine/model_helpers_test.go | 18 +++---- engine/models.go | 11 ++--- engine/onstor_it_test.go | 4 +- engine/reslimiter.go | 2 +- engine/reslimiter_test.go | 49 ++----------------- engine/stordb_it_test.go | 21 ++++---- utils/apitpdata.go | 5 +- 13 files changed, 50 insertions(+), 103 deletions(-) diff --git a/data/storage/mysql/create_tariffplan_tables.sql b/data/storage/mysql/create_tariffplan_tables.sql index 843007d40..95bb2e682 100644 --- a/data/storage/mysql/create_tariffplan_tables.sql +++ b/data/storage/mysql/create_tariffplan_tables.sql @@ -410,7 +410,7 @@ CREATE TABLE tp_resource_limits ( `blocker` BOOLEAN NOT NULL, `stored` BOOLEAN NOT NULL, `weight` decimal(8,2) NOT NULL, - `action_trigger_ids` varchar(64) NOT NULL, + `thresholds` varchar(64) NOT NULL, `created_at` TIMESTAMP, PRIMARY KEY (`id`), KEY `tpid` (`tpid`), @@ -433,11 +433,10 @@ CREATE TABLE tp_stats ( `queue_length` int(11) NOT NULL, `ttl` varchar(32) NOT NULL, `metrics` varchar(64) NOT NULL, - `store` BOOLEAN NOT NULL, - `thresholds` varchar(64) NOT NULL, `blocker` BOOLEAN NOT NULL, `stored` BOOLEAN NOT NULL, `weight` decimal(8,2) NOT NULL, + `thresholds` varchar(64) NOT NULL, `created_at` TIMESTAMP, PRIMARY KEY (`id`), KEY `tpid` (`tpid`), diff --git a/data/storage/postgres/create_tariffplan_tables.sql b/data/storage/postgres/create_tariffplan_tables.sql index 3c9f34e6b..1c5add6dd 100644 --- a/data/storage/postgres/create_tariffplan_tables.sql +++ b/data/storage/postgres/create_tariffplan_tables.sql @@ -406,7 +406,7 @@ CREATE TABLE tp_resource_limits ( "blocker" BOOLEAN NOT NULL, "stored" BOOLEAN NOT NULL, "weight" NUMERIC(8,2) NOT NULL, - "action_trigger_ids" varchar(64) NOT NULL, + "thresholds" varchar(64) NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE ); CREATE INDEX tp_resource_limits_idx ON tp_resource_limits (tpid); @@ -429,11 +429,10 @@ CREATE TABLE tp_stats ( "queue_length" INTEGER NOT NULL, "ttl" varchar(32) NOT NULL, "metrics" varchar(64) NOT NULL, - "store" BOOLEAN NOT NULL, - "thresholds" varchar(64) NOT NULL, "blocker" BOOLEAN NOT NULL, "stored" BOOLEAN NOT NULL, "weight" decimal(8,2) NOT NULL, + "thresholds" varchar(64) NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE ); CREATE INDEX tp_stats_idx ON tp_stats (tpid); diff --git a/data/tariffplans/testtp/Stats.csv b/data/tariffplans/testtp/Stats.csv index 3df799fc6..2013a7bc6 100755 --- a/data/tariffplans/testtp/Stats.csv +++ b/data/tariffplans/testtp/Stats.csv @@ -1,2 +1,2 @@ -#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Store,Thresholds,Blocker,Stored,Weight -Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,THRESH1;THRESH2,true,true,20 +#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Blocker,Stored,Weight,Thresholds +Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,true,20,THRESH1;THRESH2 diff --git a/data/tariffplans/tutorial/Stats.csv b/data/tariffplans/tutorial/Stats.csv index 3df799fc6..2013a7bc6 100755 --- a/data/tariffplans/tutorial/Stats.csv +++ b/data/tariffplans/tutorial/Stats.csv @@ -1,2 +1,2 @@ -#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Store,Thresholds,Blocker,Stored,Weight -Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,THRESH1;THRESH2,true,true,20 +#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Blocker,Stored,Weight,Thresholds +Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,true,20,THRESH1;THRESH2 diff --git a/engine/loader_csv_test.go b/engine/loader_csv_test.go index 1a45df1f0..e33944675 100755 --- a/engine/loader_csv_test.go +++ b/engine/loader_csv_test.go @@ -267,15 +267,15 @@ cgrates.org,mas,true,another,value,10 ` resLimits = ` -#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,TTL,Limit,AllocationMessage,Weight,ActionTriggers +#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,TTL,Limit,AllocationMessage,Weight,Thresholds ResGroup21,*string,HdrAccount,1001;1002,2014-07-29T15:00:00Z,1s,2,call,true,true,10, ResGroup21,*string_prefix,HdrDestination,10;20,,,,,,,, ResGroup21,*rsr_fields,,HdrSubject(~^1.*1$);HdrDestination(1002),,,,,,,, ResGroup22,*destinations,HdrDestination,DST_FS,2014-07-29T15:00:00Z,3600s,2,premium_call,true,true,10, ` stats = ` -#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Store,Thresholds,Weight -Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,THRESH1;THRESH2,true,true,20 +#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Blocker,Stored,Weight,Thresholds +Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,true,20,THRESH1;THRESH2 ` ) @@ -1436,7 +1436,6 @@ func TestLoadStats(t *testing.T) { QueueLength: 100, TTL: "1s", Metrics: []string{"*asr", "*acd", "*acc"}, - Store: true, Thresholds: []string{"THRESH1", "THRESH2"}, Blocker: true, Stored: true, diff --git a/engine/model_helpers.go b/engine/model_helpers.go index f07607133..bca41976c 100755 --- a/engine/model_helpers.go +++ b/engine/model_helpers.go @@ -1847,8 +1847,11 @@ func (tps TpResourceLimits) AsTPResourceLimits() (result []*utils.TPResourceLimi rl.ActivationInterval.ActivationTime = aiSplt[0] } } - if tp.ActionTriggerIds != "" { - rl.ActionTriggerIDs = append(rl.ActionTriggerIDs, strings.Split(tp.ActionTriggerIds, utils.INFIELD_SEP)...) + if tp.Thresholds != "" { + trshSplt := strings.Split(tp.Thresholds, utils.INFIELD_SEP) + for _, trsh := range trshSplt { + rl.Thresholds = append(rl.Thresholds, trsh) + } } if tp.FilterType != "" { rl.Filters = append(rl.Filters, &utils.TPRequestFilter{ @@ -1889,11 +1892,11 @@ func APItoModelResourceLimit(rl *utils.TPResourceLimit) (mdls TpResourceLimits) mdl.ActivationInterval += utils.INFIELD_SEP + rl.ActivationInterval.ExpiryTime } } - for i, atid := range rl.ActionTriggerIDs { + for i, val := range rl.Thresholds { if i != 0 { - mdl.ActionTriggerIds = mdl.ActionTriggerIds + utils.INFIELD_SEP + atid + mdl.Thresholds = mdl.Thresholds + utils.INFIELD_SEP + val } else { - mdl.ActionTriggerIds = atid + mdl.Thresholds = val } } } @@ -1971,9 +1974,6 @@ func (tps TpStatsS) AsTPStats() (result []*utils.TPStats) { st.Metrics = append(st.Metrics, metr) } } - if tp.Store != false { - st.Store = tp.Store - } if tp.Thresholds != "" { trshSplt := strings.Split(tp.Thresholds, utils.INFIELD_SEP) for _, trsh := range trshSplt { @@ -2025,7 +2025,6 @@ func APItoModelStats(st *utils.TPStats) (mdls TpStatsS) { mdl.Stored = st.Stored mdl.Weight = st.Weight mdl.QueueLength = st.QueueLength - mdl.Store = st.Store for _, val := range st.Metrics { mdl.Metrics = mdl.Metrics + utils.INFIELD_SEP + val } @@ -2059,7 +2058,6 @@ func APItoTPStats(tpST *utils.TPStats, timezone string) (st *StatsQueue, err err st = &StatsQueue{ ID: tpST.ID, QueueLength: tpST.QueueLength, - Store: tpST.Store, Weight: tpST.Weight, Blocker: tpST.Blocker, Stored: tpST.Stored, diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index 56ddbf3e7..9c51f579a 100644 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -680,14 +680,14 @@ func TestTpResourceLimitsAsTPResourceLimits(t *testing.T) { Blocker: false, Weight: 10.0, Limit: "45", - ActionTriggerIds: "WARN_RES1;WARN_RES2"}, + Thresholds: "WARN_RES1;WARN_RES2"}, &TpResourceLimit{ Tpid: "TEST_TPID", Tag: "ResGroup1", FilterType: MetaStringPrefix, FilterFieldName: "Category", FilterFieldValues: "call;inbound_call", - ActionTriggerIds: "WARN3"}, + Thresholds: "WARN3"}, &TpResourceLimit{ Tpid: "TEST_TPID", Tag: "ResGroup2", @@ -719,11 +719,11 @@ func TestTpResourceLimitsAsTPResourceLimits(t *testing.T) { ActivationInterval: &utils.TPActivationInterval{ ActivationTime: tps[0].ActivationInterval, }, - Stored: tps[0].Stored, - Blocker: tps[0].Blocker, - Weight: tps[0].Weight, - Limit: tps[0].Limit, - ActionTriggerIDs: []string{"WARN_RES1", "WARN_RES2", "WARN3"}, + Stored: tps[0].Stored, + Blocker: tps[0].Blocker, + Weight: tps[0].Weight, + Limit: tps[0].Limit, + Thresholds: []string{"WARN_RES1", "WARN_RES2", "WARN3"}, }, &utils.TPResourceLimit{ TPid: tps[2].Tpid, @@ -808,7 +808,6 @@ func TestTPStatsAsTPStats(t *testing.T) { QueueLength: 100, TTL: "1s", Metrics: "*asr;*acd;*acc", - Store: true, Thresholds: "THRESH1;THRESH2", Stored: false, Blocker: false, @@ -832,7 +831,6 @@ func TestTPStatsAsTPStats(t *testing.T) { QueueLength: tps[0].QueueLength, TTL: tps[0].TTL, Metrics: []string{"*asr", "*acd", "*acc"}, - Store: tps[0].Store, Thresholds: []string{"THRESH1", "THRESH2"}, Stored: tps[0].Stored, Blocker: tps[0].Blocker, @@ -856,7 +854,6 @@ func TestAPItoTPStats(t *testing.T) { QueueLength: 100, TTL: "1s", Metrics: []string{"*asr", "*acd", "*acc"}, - Store: true, Thresholds: []string{"THRESH1", "THRESH2"}, Stored: false, Blocker: false, @@ -866,7 +863,6 @@ func TestAPItoTPStats(t *testing.T) { eTPs := &StatsQueue{ID: tps.ID, QueueLength: tps.QueueLength, Metrics: []string{"*asr", "*acd", "*acc"}, - Store: tps.Store, Thresholds: []string{"THRESH1", "THRESH2"}, Filters: make([]*RequestFilter, len(tps.Filters)), Stored: tps.Stored, diff --git a/engine/models.go b/engine/models.go index 9ac3afc10..c76e280c4 100755 --- a/engine/models.go +++ b/engine/models.go @@ -464,7 +464,7 @@ type TpResourceLimit struct { Blocker bool `index:"8" re:""` Stored bool `index:"9" re:""` Weight float64 `index:"10" re:"\d+\.?\d*"` - ActionTriggerIds string `index:"11" re:""` + Thresholds string `index:"11" re:""` CreatedAt time.Time } @@ -489,10 +489,9 @@ type TpStats struct { QueueLength int `index:"5" re:""` TTL string `index:"6" re:""` Metrics string `index:"7" re:""` - Store bool `index:"8" re:""` - Thresholds string `index:"9" re:""` - Blocker bool `index:"10" re:""` - Stored bool `index:"11" re:""` - Weight float64 `index:"12" re:"\d+\.?\d*"` + Blocker bool `index:"8" re:""` + Stored bool `index:"9" re:""` + Weight float64 `index:"10" re:"\d+\.?\d*"` + Thresholds string `index:"11" re:""` CreatedAt time.Time } diff --git a/engine/onstor_it_test.go b/engine/onstor_it_test.go index 6a1c28fbd..cb20f7c3e 100644 --- a/engine/onstor_it_test.go +++ b/engine/onstor_it_test.go @@ -741,7 +741,7 @@ func testOnStorITCacheResourceLimit(t *testing.T) { ActivationInterval: &utils.ActivationInterval{ActivationTime: time.Date(2014, 7, 3, 13, 43, 0, 0, time.UTC).Local()}, ExpiryTime: time.Date(2015, 7, 3, 13, 43, 0, 0, time.UTC).Local(), Limit: 1, - ActionTriggers: make(ActionTriggers, 0), + Thresholds: []string{"TEST_ACTIONS"}, UsageTTL: time.Duration(1 * time.Millisecond), Usage: make(map[string]*ResourceUsage), } @@ -1681,7 +1681,7 @@ func testOnStorITCRUDResourceLimit(t *testing.T) { ActivationInterval: &utils.ActivationInterval{ActivationTime: time.Date(2014, 7, 3, 13, 43, 0, 0, time.UTC).Local()}, ExpiryTime: time.Date(2015, 7, 3, 13, 43, 0, 0, time.UTC).Local(), Limit: 1, - ActionTriggers: make(ActionTriggers, 0), + Thresholds: []string{"TEST_ACTIONS"}, UsageTTL: time.Duration(1 * time.Millisecond), Usage: make(map[string]*ResourceUsage), } diff --git a/engine/reslimiter.go b/engine/reslimiter.go index 4bc4cb3d1..33478ae75 100755 --- a/engine/reslimiter.go +++ b/engine/reslimiter.go @@ -46,7 +46,7 @@ type ResourceLimit struct { Stored bool Weight float64 // Weight to sort the ResourceLimits Limit float64 // Limit value - ActionTriggers ActionTriggers // Thresholds to check after changing Limit + Thresholds []string // Thresholds to check after changing Limit UsageTTL time.Duration // Expire usage after this duration AllocationMessage string // message returned by the winning resourceLimit on allocation Usage map[string]*ResourceUsage // Keep a record of usage, bounded with timestamps so we can expire too long records diff --git a/engine/reslimiter_test.go b/engine/reslimiter_test.go index 464ca69b5..73d0b1703 100644 --- a/engine/reslimiter_test.go +++ b/engine/reslimiter_test.go @@ -64,28 +64,8 @@ func TestResourceLimitRecordUsage(t *testing.T) { ExpiryTime: time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC), Weight: 100, Limit: 2, - ActionTriggers: ActionTriggers{ - &ActionTrigger{ - // ID string // original csv tag - // UniqueID string // individual id - ThresholdType: utils.TRIGGER_MAX_EVENT_COUNTER, - ThresholdValue: 2, - // Recurrent bool // reset excuted flag each run - // MinSleep time.Duration // Minimum duration between two executions in case of recurrent triggers - // ExpirationDate time.Time - // ActivationDate time.Time - Balance: &BalanceFilter{ - Type: utils.StringPointer(utils.MONETARY), - Directions: utils.StringMapPointer(utils.NewStringMap(utils.OUT)), - }, - ActionsID: "TEST_ACTIONS", - // Weight float64 - // ActionsID string - // MinQueuedItems int // Trigger actions only if this number is hit (stats only) - // Executed bool - // LastExecutionTime time.Time - }, - }, + Thresholds: []string{"TEST_ACTIONS"}, + UsageTTL: time.Duration(1 * time.Millisecond), AllocationMessage: "ALLOC", Usage: map[string]*ResourceUsage{ @@ -179,29 +159,8 @@ func TestRLSort(t *testing.T) { ExpiryTime: time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC), Weight: 50, Limit: 2, - ActionTriggers: ActionTriggers{ - &ActionTrigger{ - // ID string // original csv tag - // UniqueID string // individual id - ThresholdType: utils.TRIGGER_MAX_EVENT_COUNTER, - ThresholdValue: 2, - // Recurrent bool // reset excuted flag each run - // MinSleep time.Duration // Minimum duration between two executions in case of recurrent triggers - // ExpirationDate time.Time - // ActivationDate time.Time - Balance: &BalanceFilter{ - Type: utils.StringPointer(utils.MONETARY), - Directions: utils.StringMapPointer(utils.NewStringMap(utils.OUT)), - }, - ActionsID: "TEST_ACTIONS", - // Weight float64 - // ActionsID string - // MinQueuedItems int // Trigger actions only if this number is hit (stats only) - // Executed bool - // LastExecutionTime time.Time - }, - }, - UsageTTL: time.Duration(1 * time.Millisecond), + Thresholds: []string{"TEST_ACTIONS"}, + UsageTTL: time.Duration(1 * time.Millisecond), // AllocationMessage: "ALLOC2", Usage: map[string]*ResourceUsage{ ru2.ID: ru2, diff --git a/engine/stordb_it_test.go b/engine/stordb_it_test.go index e0f206a73..b91fbdbd5 100755 --- a/engine/stordb_it_test.go +++ b/engine/stordb_it_test.go @@ -1428,11 +1428,11 @@ func testStorDBitCRUDTpResourceLimits(t *testing.T) { //WRITE var snd = []*utils.TPResourceLimit{ &utils.TPResourceLimit{ - TPid: "testTPid", - ID: "testTag1", - Weight: 0.0, - Limit: "test", - ActionTriggerIDs: []string{"1x", "2x"}, + TPid: "testTPid", + ID: "testTag1", + Weight: 0.0, + Limit: "test", + Thresholds: []string{"1x", "2x"}, Filters: []*utils.TPRequestFilter{ &utils.TPRequestFilter{ Type: "filtertype", @@ -1447,7 +1447,7 @@ func testStorDBitCRUDTpResourceLimits(t *testing.T) { ActivationInterval: &utils.TPActivationInterval{ActivationTime: "test"}, Weight: 0.0, Limit: "test", - ActionTriggerIDs: []string{"1x", "2x"}, + Thresholds: []string{"1x", "2x"}, Filters: []*utils.TPRequestFilter{ &utils.TPRequestFilter{ Type: "filtertype", @@ -1479,8 +1479,8 @@ func testStorDBitCRUDTpResourceLimits(t *testing.T) { if !(reflect.DeepEqual(snd[0].Limit, rcv[0].Limit) || reflect.DeepEqual(snd[0].Limit, rcv[1].Limit)) { t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].Limit, rcv[0].Limit, rcv[1].Limit) } - if !(reflect.DeepEqual(snd[0].ActionTriggerIDs, rcv[0].ActionTriggerIDs) || reflect.DeepEqual(snd[0].ActionTriggerIDs, rcv[1].ActionTriggerIDs)) { - t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].ActionTriggerIDs, rcv[0].ActionTriggerIDs, rcv[1].ActionTriggerIDs) + if !(reflect.DeepEqual(snd[0].Thresholds, rcv[0].Thresholds) || reflect.DeepEqual(snd[0].Thresholds, rcv[1].Thresholds)) { + t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].Thresholds, rcv[0].Thresholds, rcv[1].Thresholds) } for i, _ := range snd[0].Filters { if !(reflect.DeepEqual(snd[0].Filters[i], rcv[0].Filters[i]) || reflect.DeepEqual(snd[0].Filters[i], rcv[1].Filters[i])) { @@ -1514,8 +1514,8 @@ func testStorDBitCRUDTpResourceLimits(t *testing.T) { if !(reflect.DeepEqual(snd[0].Limit, rcv[0].Limit) || reflect.DeepEqual(snd[0].Limit, rcv[1].Limit)) { t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].Limit, rcv[0].Limit, rcv[1].Limit) } - if !(reflect.DeepEqual(snd[0].ActionTriggerIDs, rcv[0].ActionTriggerIDs) || reflect.DeepEqual(snd[0].ActionTriggerIDs, rcv[1].ActionTriggerIDs)) { - t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].ActionTriggerIDs, rcv[0].ActionTriggerIDs, rcv[1].ActionTriggerIDs) + if !(reflect.DeepEqual(snd[0].Thresholds, rcv[0].Thresholds) || reflect.DeepEqual(snd[0].Thresholds, rcv[1].Thresholds)) { + t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].Thresholds, rcv[0].Thresholds, rcv[1].Thresholds) } } // REMOVE @@ -1551,7 +1551,6 @@ func testStorDBitCRUDTpStats(t *testing.T) { QueueLength: 100, TTL: "1s", Metrics: []string{"*asr", "*acd", "*acc"}, - Store: true, Thresholds: []string{"THRESH1", "THRESH2"}, Weight: 20.0, }, diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 0dccfe0e3..46f5d3090 100755 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -1274,7 +1274,7 @@ type TPResourceLimit struct { Blocker bool // blocker flag to stop processing on filters matched Stored bool Weight float64 // Weight to sort the ResourceLimits - ActionTriggerIDs []string // Thresholds to check after changing Limit + Thresholds []string // Thresholds to check after changing Limit } type TPRequestFilter struct { @@ -1336,9 +1336,8 @@ type TPStats struct { QueueLength int TTL string Metrics []string - Store bool - Thresholds []string Blocker bool // blocker flag to stop processing on filters matched Stored bool Weight float64 + Thresholds []string }