From b44b439dcd159efb849b55e10f96d944a7c3cf32 Mon Sep 17 00:00:00 2001 From: DanB Date: Mon, 25 Sep 2017 18:51:20 +0200 Subject: [PATCH] StatS matching only fields in filters, integration tests for StatSV1.ProcessEvent --- apier/v1/stats_it_test.go | 46 +++++++++++++++++++++++++------------- apier/v1/tp_it_test.go | 5 ----- config/config_defaults.go | 8 +++---- config/config_json_test.go | 13 ++++++----- config/config_test.go | 10 ++++----- engine/libstats.go | 1 - engine/stats.go | 2 +- utils/consts.go | 8 +++---- 8 files changed, 51 insertions(+), 42 deletions(-) diff --git a/apier/v1/stats_it_test.go b/apier/v1/stats_it_test.go index 68d0adb30..e8c633147 100644 --- a/apier/v1/stats_it_test.go +++ b/apier/v1/stats_it_test.go @@ -71,7 +71,7 @@ var sTestsStatSV1 = []func(t *testing.T){ testV1STSRpcConn, testV1STSFromFolder, testV1STSGetStats, - //testV1STSProcessEvent, + testV1STSProcessEvent, //testV1STSGetStatQueueProfileBeforeSet, //testV1STSSetStatQueueProfile, //testV1STSGetStatQueueProfileAfterSet, @@ -79,7 +79,7 @@ var sTestsStatSV1 = []func(t *testing.T){ //testV1STSGetStatQueueProfileAfterUpdate, //testV1STSRemoveStatQueueProfile, //testV1STSGetStatQueueProfileAfterRemove, - //testV1STSStopEngine, + testV1STSStopEngine, } //Test start here @@ -150,7 +150,7 @@ func testV1STSGetStats(t *testing.T) { var metrics map[string]string expectedMetrics := map[string]string{ utils.MetaASR: utils.NOT_AVAILABLE, - utils.MetaACD: "", + utils.MetaACD: utils.NOT_AVAILABLE, } if err := stsV1Rpc.Call("StatSV1.GetQueueStringMetrics", &utils.TenantID{"cgrates.org", expectedIDs[0]}, &metrics); err != nil { @@ -160,32 +160,42 @@ func testV1STSGetStats(t *testing.T) { } } -/* - func testV1STSProcessEvent(t *testing.T) { var reply string if err := stsV1Rpc.Call("StatSV1.ProcessEvent", - engine.StatsEvent{ - utils.ID: "event1", - utils.ANSWER_TIME: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC)}, + engine.StatEvent{ + Tenant: "cgrates.org", + ID: "event1", + Fields: map[string]interface{}{ + utils.ACCOUNT: "1001", + utils.ANSWER_TIME: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + utils.USAGE: time.Duration(125 * time.Second)}}, &reply); err != nil { t.Error(err) } else if reply != utils.OK { t.Errorf("received reply: %s", reply) } if err := stsV1Rpc.Call("StatSV1.ProcessEvent", - engine.StatsEvent{ - utils.ID: "event2", - utils.ANSWER_TIME: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC)}, + engine.StatEvent{ + Tenant: "cgrates.org", + ID: "event2", + Fields: map[string]interface{}{ + utils.ACCOUNT: "1002", + utils.ANSWER_TIME: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + utils.USAGE: time.Duration(45 * time.Second)}}, &reply); err != nil { t.Error(err) } else if reply != utils.OK { t.Errorf("received reply: %s", reply) } if err := stsV1Rpc.Call("StatSV1.ProcessEvent", - engine.StatsEvent{ - utils.ID: "event3", - utils.SETUP_TIME: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC)}, + engine.StatEvent{ + Tenant: "cgrates.org", + ID: "event3", + Fields: map[string]interface{}{ + utils.ACCOUNT: "1002", + utils.SETUP_TIME: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + utils.USAGE: 0}}, &reply); err != nil { t.Error(err) } else if reply != utils.OK { @@ -193,16 +203,18 @@ func testV1STSProcessEvent(t *testing.T) { } expectedMetrics := map[string]string{ utils.MetaASR: "66.66667%", - utils.MetaACD: "", + utils.MetaACD: "0", } var metrics map[string]string - if err := stsV1Rpc.Call("StatSV1.GetQueueStringMetrics", "Stats1", &metrics); err != nil { + if err := stsV1Rpc.Call("StatSV1.GetQueueStringMetrics", &utils.TenantID{"cgrates.org", "STATS_1"}, &metrics); err != nil { t.Error(err) } else if !reflect.DeepEqual(expectedMetrics, metrics) { t.Errorf("expecting: %+v, received reply: %s", expectedMetrics, metrics) } } +/* + func testV1STSGetStatQueueProfileBeforeSet(t *testing.T) { var reply *engine.StatQueueProfile if err := stsV1Rpc.Call("ApierV1.GetStatQueueProfile", &AttrGetStatsCfg{ID: "SCFG1"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { @@ -300,6 +312,7 @@ func testV1STSGetStatQueueProfileAfterRemove(t *testing.T) { t.Error(err) } } +*/ func testV1STSStopEngine(t *testing.T) { if err := engine.KillEngine(100); err != nil { @@ -307,6 +320,7 @@ func testV1STSStopEngine(t *testing.T) { } } +/* // BenchmarkStatSV1SetEvent 5000 263437 ns/op func BenchmarkStatSV1SetEvent(b *testing.B) { if _, err := engine.StopStartEngine(stsV1CfgPath, 1000); err != nil { diff --git a/apier/v1/tp_it_test.go b/apier/v1/tp_it_test.go index b548b5152..65e9a54e4 100644 --- a/apier/v1/tp_it_test.go +++ b/apier/v1/tp_it_test.go @@ -73,7 +73,6 @@ func TestTPITPG(t *testing.T) { } func testTPInitCfg(t *testing.T) { - utils.Logger.Debug("init config") var err error tpCfgPath = path.Join(tpDataDir, "conf", "samples", tpConfigDIR) tpCfg, err = config.NewCGRConfigFromFolder(tpCfgPath) @@ -92,7 +91,6 @@ func testTPInitCfg(t *testing.T) { // Wipe out the cdr database func testTPResetStorDb(t *testing.T) { - utils.Logger.Debug("ResetStorDB") if err := engine.InitStorDb(tpCfg); err != nil { t.Fatal(err) } @@ -100,7 +98,6 @@ func testTPResetStorDb(t *testing.T) { // Start CGR Engine func testTPStartEngine(t *testing.T) { - utils.Logger.Debug("StartEngine") if _, err := engine.StopStartEngine(tpCfgPath, tpDelay); err != nil { t.Fatal(err) } @@ -108,7 +105,6 @@ func testTPStartEngine(t *testing.T) { // Connect rpc client to rater func testTPRpcConn(t *testing.T) { - utils.Logger.Debug("RPCCONN") var err error tpRPC, err = jsonrpc.Dial("tcp", tpCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed if err != nil { @@ -149,7 +145,6 @@ func testTPExportTPToFolder(t *testing.T) { } func testTPKillEngine(t *testing.T) { - utils.Logger.Debug("KillEngine") if err := engine.KillEngine(tpDelay); err != nil { t.Error(err) } diff --git a/config/config_defaults.go b/config/config_defaults.go index 0ce7301c9..153f4fa99 100755 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -29,7 +29,7 @@ const CGRATES_CFG_JSON = ` "general": { "instance_id": "", // identifier of this instance in the cluster, if empty it will be autogenerated - "logger":"*syslog", // controls the destination of logs <*syslog|*stdout> + "logger":"*syslog", // controls the destination of logs <*syslog|*stdout> "log_level": 6, // control the level of messages logged (0-emerg to 7-debug) "http_skip_tls_verify": false, // if enabled Http Client will accept any TLS certificate "rounding_decimals": 5, // system level precision for floats @@ -66,12 +66,12 @@ const CGRATES_CFG_JSON = ` "aliases": {"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, // aliases caching "reverse_aliases": {"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, // reverse aliases index caching "derived_chargers": {"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, // derived charging rule caching + "timings": {"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, // timings caching "resource_profiles": {"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, // control resource profiles caching "resources": {"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, // control resources caching "event_resources": {"limit": -1, "ttl": "1m", "static_ttl": false}, // matching resources to events - "timings": {"limit": -1, "ttl": "", "static_ttl": false, "precache": false}, // timings caching - "stats_queues": {"limit": -1, "ttl": "1m", "static_ttl": false, "precache": false}, // queues with metrics - "event_queues": {"limit": -1, "ttl": "1m", "static_ttl": false}, // matching queues to events + "statqueue_profiles": {"limit": -1, "ttl": "1m", "static_ttl": false, "precache": false}, // statqueue profiles + "statqueues": {"limit": -1, "ttl": "1m", "static_ttl": false, "precache": false}, // statqueues with metrics }, diff --git a/config/config_json_test.go b/config/config_json_test.go index ce60eca24..3fde3d27c 100755 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -110,6 +110,9 @@ func TestCacheJsonCfg(t *testing.T) { utils.CacheDerivedChargers: &CacheParamJsonCfg{Limit: utils.IntPointer(-1), Ttl: utils.StringPointer(""), Static_ttl: utils.BoolPointer(false), Precache: utils.BoolPointer(false)}, + utils.CacheTimings: &CacheParamJsonCfg{Limit: utils.IntPointer(-1), + Ttl: utils.StringPointer(""), Static_ttl: utils.BoolPointer(false), + Precache: utils.BoolPointer(false)}, utils.CacheResourceProfiles: &CacheParamJsonCfg{Limit: utils.IntPointer(-1), Ttl: utils.StringPointer(""), Static_ttl: utils.BoolPointer(false), Precache: utils.BoolPointer(false)}, @@ -118,14 +121,12 @@ func TestCacheJsonCfg(t *testing.T) { Precache: utils.BoolPointer(false)}, utils.CacheEventResources: &CacheParamJsonCfg{Limit: utils.IntPointer(-1), Ttl: utils.StringPointer("1m"), Static_ttl: utils.BoolPointer(false)}, - utils.CacheTimings: &CacheParamJsonCfg{Limit: utils.IntPointer(-1), - Ttl: utils.StringPointer(""), Static_ttl: utils.BoolPointer(false), - Precache: utils.BoolPointer(false)}, - utils.CacheStatSQueues: &CacheParamJsonCfg{Limit: utils.IntPointer(-1), + utils.CacheStatQueueProfiles: &CacheParamJsonCfg{Limit: utils.IntPointer(-1), + Ttl: utils.StringPointer("1m"), Static_ttl: utils.BoolPointer(false), + Precache: utils.BoolPointer(false)}, + utils.CacheStatQueues: &CacheParamJsonCfg{Limit: utils.IntPointer(-1), Ttl: utils.StringPointer("1m"), Static_ttl: utils.BoolPointer(false), Precache: utils.BoolPointer(false)}, - utils.CacheEventQueues: &CacheParamJsonCfg{Limit: utils.IntPointer(-1), - Ttl: utils.StringPointer("1m"), Static_ttl: utils.BoolPointer(false)}, } if gCfg, err := dfCgrJsonCfg.CacheJsonCfg(); err != nil { diff --git a/config/config_test.go b/config/config_test.go index a1a7a662b..8cf538ade 100755 --- a/config/config_test.go +++ b/config/config_test.go @@ -438,18 +438,18 @@ func TestCgrCfgJSONDefaultsCacheCFG(t *testing.T) { TTL: time.Duration(0), StaticTTL: false, Precache: false}, utils.CacheDerivedChargers: &CacheParamConfig{Limit: -1, TTL: time.Duration(0), StaticTTL: false, Precache: false}, + utils.CacheTimings: &CacheParamConfig{Limit: -1, + TTL: time.Duration(0), StaticTTL: false, Precache: false}, utils.CacheResourceProfiles: &CacheParamConfig{Limit: -1, TTL: time.Duration(0), StaticTTL: false, Precache: false}, utils.CacheResources: &CacheParamConfig{Limit: -1, TTL: time.Duration(0), StaticTTL: false, Precache: false}, utils.CacheEventResources: &CacheParamConfig{Limit: -1, TTL: time.Duration(1 * time.Minute), StaticTTL: false}, - utils.CacheTimings: &CacheParamConfig{Limit: -1, - TTL: time.Duration(0), StaticTTL: false, Precache: false}, - utils.CacheStatSQueues: &CacheParamConfig{Limit: -1, + utils.CacheStatQueueProfiles: &CacheParamConfig{Limit: -1, + TTL: time.Duration(1 * time.Minute), StaticTTL: false, Precache: false}, + utils.CacheStatQueues: &CacheParamConfig{Limit: -1, TTL: time.Duration(1 * time.Minute), StaticTTL: false, Precache: false}, - utils.CacheEventQueues: &CacheParamConfig{Limit: -1, - TTL: time.Duration(1 * time.Minute), StaticTTL: false}, } if !reflect.DeepEqual(eCacheCfg, cgrCfg.CacheConfig) { t.Errorf("received: %s, \nexpecting: %s", utils.ToJSON(eCacheCfg), utils.ToJSON(cgrCfg.CacheConfig)) diff --git a/engine/libstats.go b/engine/libstats.go index ce09b4d4a..3aa4f5027 100755 --- a/engine/libstats.go +++ b/engine/libstats.go @@ -36,7 +36,6 @@ type StatQueueProfile struct { QueueLength int TTL time.Duration Metrics []string // list of metrics to build - Store bool // store to DB Thresholds []string // list of thresholds to be checked after changes Blocker bool // blocker flag to stop processing on filters matched Stored bool diff --git a/engine/stats.go b/engine/stats.go index 00418245e..ed7e68ee2 100644 --- a/engine/stats.go +++ b/engine/stats.go @@ -154,7 +154,7 @@ func (sS *StatService) matchingStatQueuesForEvent(ev *StatEvent) (sqs StatQueues } passAllFilters := true for _, fltr := range sqPrfl.Filters { - if pass, err := fltr.Pass(ev, "", sS); err != nil { + if pass, err := fltr.Pass(ev.Fields, "", sS); err != nil { return nil, err } else if !pass { passAllFilters = false diff --git a/utils/consts.go b/utils/consts.go index 7332eb3fe..5bc451af6 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -59,8 +59,8 @@ var ( CacheResources: ResourcesPrefix, CacheEventResources: EventResourcesPrefix, CacheTimings: TimingsPrefix, - CacheStatSQueues: META_NONE, - CacheEventQueues: META_NONE, + CacheStatQueueProfiles: StatQueueProfilePrefix, + CacheStatQueues: StatQueuePrefix, } CachePrefixToInstance map[string]string // will be built on init ) @@ -446,9 +446,9 @@ const ( CostSource = "CostSource" ExtraInfo = "ExtraInfo" MetaPrefix = "*" - CacheStatSQueues = "stats_queues" - CacheEventQueues = "event_queues" CacheEventResources = "event_resources" + CacheStatQueueProfiles = "statqueue_profiles" + CacheStatQueues = "statqueues" EventResourcesPrefix = "ers_" MetaSysLog = "*syslog" MetaStdLog = "*stdout"