From f258a289f4639e25b97359049b4df43868597816 Mon Sep 17 00:00:00 2001 From: gezimbll Date: Fri, 1 Dec 2023 11:15:02 -0500 Subject: [PATCH] Updated statmetrics test && added comments --- apier/v1/stats_it_test.go | 7 ++++++ engine/statmetrics.go | 2 ++ engine/statmetrics_test.go | 47 +++----------------------------------- 3 files changed, 12 insertions(+), 44 deletions(-) diff --git a/apier/v1/stats_it_test.go b/apier/v1/stats_it_test.go index 48cc355c8..056ea7e14 100644 --- a/apier/v1/stats_it_test.go +++ b/apier/v1/stats_it_test.go @@ -963,6 +963,13 @@ func testV1STSProcessStatsOneEvent(t *testing.T) { } else if !reflect.DeepEqual(expectedMetrics, metrics) { t.Errorf("expecting: %+v, received reply: %s", expectedMetrics, metrics) } + var statQueue engine.StatQueue + if err := stsV1Rpc.Call(context.Background(), utils.StatSv1GetStatQueue, &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "StatQueueOne"}}, &statQueue); err != nil { + t.Error(err) + } else if len(statQueue.SQItems) != 0 { + t.Errorf("Expected 0, Received %v", len(statQueue.SQItems)) + } } func testV1STSStatsPing(t *testing.T) { diff --git a/engine/statmetrics.go b/engine/statmetrics.go index 1cc86fc84..9d5a050a0 100644 --- a/engine/statmetrics.go +++ b/engine/statmetrics.go @@ -166,6 +166,7 @@ func (asr *StatASR) AddEvent(evID string, ev utils.DataProvider) (err error) { return } +// AddOneEvent aggregates the metric value without storing the events func (asr *StatASR) AddOneEvent(ev utils.DataProvider) (err error) { var answered int if answered, err = asr.getFieldVal(ev); err != nil { @@ -179,6 +180,7 @@ func (asr *StatASR) AddOneEvent(ev utils.DataProvider) (err error) { return } +// RemEvent deletes a stored event and decrements statistics of the metric for recalculation func (asr *StatASR) RemEvent(evID string) (err error) { val, has := asr.Events[evID] if !has { diff --git a/engine/statmetrics_test.go b/engine/statmetrics_test.go index dc2df524a..f70a200b5 100644 --- a/engine/statmetrics_test.go +++ b/engine/statmetrics_test.go @@ -328,7 +328,6 @@ func TestASRAddOneEvent(t *testing.T) { Event: map[string]any{ "AnswerTime": time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC)}} ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_2"} - ev4 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1"} asr.AddOneEvent(utils.MapStorage{utils.MetaReq: ev.Event}) asr.AddOneEvent(utils.MapStorage{utils.MetaReq: ev2.Event}) if strVal := asr.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "50%" { @@ -338,12 +337,7 @@ func TestASRAddOneEvent(t *testing.T) { if strVal := asr.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "33.33333%" { t.Errorf("wrong asr value: %s", strVal) } - asr.AddOneEvent(utils.MapStorage{utils.MetaReq: ev4.Event}) - if strVal := asr.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "25%" { - t.Errorf("wrong asr value: %s", strVal) - } - asr.RemEvent(ev4.ID) - asr.RemEvent(ev2.ID) + asr.AddOneEvent(utils.MapStorage{utils.MetaReq: ev2.Event}) if strVal := asr.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "25%" { t.Errorf("wrong asr value: %s", strVal) } @@ -722,10 +716,7 @@ func TestACDAddOneEvent(t *testing.T) { if strVal := acd.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "1m15s" { t.Errorf("wrong acd value: %s", strVal) } - acd.RemEvent(ev2.ID) - if strVal := acd.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "1m15s" { - t.Errorf("wrong acd value: %s", strVal) - } + } func TestTCDGetStringValue(t *testing.T) { @@ -1071,10 +1062,6 @@ func TestTCDAddOneEvent(t *testing.T) { if strVal := tcd.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "5m0s" { t.Errorf("wrong tcd value: %s", strVal) } - tcd.RemEvent(ev2.ID) - if strVal := tcd.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "5m0s" { - t.Errorf("wrong tcd value: %s", strVal) - } } func TestACCGetStringValue(t *testing.T) { @@ -1370,10 +1357,6 @@ func TestACCAddOneEvent(t *testing.T) { if strVal := acc.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "16.8" { t.Errorf("wrong acc value: %s", strVal) } - acc.RemEvent(ev2.ID) - if strVal := acc.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "16.8" { - t.Errorf("wrong acc value: %s", strVal) - } } func TestTCCGetStringValue(t *testing.T) { @@ -1674,10 +1657,6 @@ func TestTCCAddOneEvent(t *testing.T) { if strVal := tcc.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "67.2" { t.Errorf("wrong tcc value: %s", strVal) } - tcc.RemEvent(ev2.ID) - if strVal := tcc.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "67.2" { - t.Errorf("wrong tcc value: %s", strVal) - } } func TestPDDGetStringValue(t *testing.T) { @@ -2041,10 +2020,6 @@ func TestPDDAddOneEvent(t *testing.T) { if strVal := pdd.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "1m15s" { t.Errorf("wrong pdd value: %s", strVal) } - pdd.RemEvent(ev2.ID) - if strVal := pdd.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "1m15s" { - t.Errorf("wrong pdd value: %s", strVal) - } } func TestDDCGetStringValue(t *testing.T) { @@ -2281,10 +2256,6 @@ func TestDDCAddOneEvent(t *testing.T) { if strVal := statDistinct.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "2" { t.Errorf("wrong statDistinct value: %s", strVal) } - statDistinct.RemEvent(ev.ID) - if strVal := statDistinct.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "2" { - t.Errorf("wrong statDistinct value: %s", strVal) - } } func TestStatSumGetFloat64Value(t *testing.T) { @@ -2585,10 +2556,6 @@ func TestStatSumAddOneEvent(t *testing.T) { if strVal := statSum.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "67.2" { t.Errorf("wrong statSum value: %s", strVal) } - statSum.RemEvent(ev2.ID) - if strVal := statSum.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "67.2" { - t.Errorf("wrong statSum value: %s", strVal) - } } func TestStatAverageGetFloat64Value(t *testing.T) { @@ -2888,10 +2855,6 @@ func TestStatAverageAddOneEvent(t *testing.T) { if strVal := statAvg.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "16.8" { t.Errorf("wrong statAvg value: %s", strVal) } - statAvg.RemEvent(ev2.ID) - if strVal := statAvg.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "16.8" { - t.Errorf("wrong statAvg value: %s", strVal) - } } func TestStatDistinctGetFloat64Value(t *testing.T) { @@ -3098,7 +3061,7 @@ func TestStatDistinctGetCompressFactor(t *testing.T) { } } -func TestStatDistinct(t *testing.T) { +func TestStatDistinctAddOneEvent(t *testing.T) { statDistinct, _ := NewStatDistinct(2, "~*req.Cost", []string{}) ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1", Event: map[string]any{"Cost": "20"}} @@ -3116,10 +3079,6 @@ func TestStatDistinct(t *testing.T) { if strVal := statDistinct.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "2" { t.Errorf("wrong statDistinct value: %s", strVal) } - statDistinct.RemEvent(ev.ID) - if strVal := statDistinct.GetStringValue(config.CgrConfig().GeneralCfg().RoundingDecimals); strVal != "2" { - t.Errorf("wrong statDistinct value: %s", strVal) - } } var jMarshaler JSONMarshaler