mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
added integration tests for trend event to thresholds,EEs && fixes
This commit is contained in:
committed by
Dan Christian Bogos
parent
0a89b131be
commit
69f4f08770
@@ -859,6 +859,7 @@ const CGRATES_CFG_JSON = `
|
||||
"store_interval": "",
|
||||
"stats_conns": [], // connections to StatS ,empty to disable stats functionality: <""|*internal|$rpc_conns_id>
|
||||
"scheduled_ids": {},
|
||||
"thresholds_conns": [],
|
||||
"ees_conns": [],
|
||||
"ees_exporter_ids": []
|
||||
},
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,6 +1,6 @@
|
||||
#Tenant[0],Id[1],Schedule[2],StatID[3],Metrics[4],TTL[5],QueueLength[6],MinItems[7],CorrelationType[8],Tolerance[9],Stored[10],ThresholdIDs[11]
|
||||
cgrates.org,TREND_1,@every 1s,Stats1_1,,-1,-1,1,*last,1,false,
|
||||
cgrates.org,TREND_2,@every 2s,Stats1_2,,-1,2,4,*last,1,false,
|
||||
cgrates.org,TREND_2,@every 3s,Stats1_2,,-1,2,4,*last,1,false,
|
||||
cgrates.org,TR_1min,@every 1m,Stats1_1,,-1,3,2,*average,0.15,false,
|
||||
tenant1,TR_5min,*/5 * * * *,Stat1,,-1,-1,8,*average,0.23,true,
|
||||
tenant1,TR_1hr,0 * * * *,Stat1,,-1,-1,8,*average,0.1,true,
|
||||
|
||||
|
@@ -125,7 +125,7 @@ func (t *Trend) Clone() (tC *Trend) {
|
||||
// thread safe since it should be used close to source
|
||||
func (t *Trend) Compile(cleanTtl time.Duration, qLength int) {
|
||||
t.cleanup(cleanTtl, qLength)
|
||||
if t.mTotals == nil { // indexes were not yet built
|
||||
if len(t.mTotals) == 0 { // indexes were not yet built
|
||||
t.computeIndexes()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,11 +99,7 @@ func (tS *TrendS) computeTrend(tP *TrendProfile) {
|
||||
if trnd.tPrfl == nil {
|
||||
trnd.tPrfl = tP
|
||||
}
|
||||
trnd.cleanup(tP.TTL, tP.QueueLength)
|
||||
|
||||
if len(trnd.mTotals) == 0 { // indexes were not yet built
|
||||
trnd.computeIndexes()
|
||||
}
|
||||
trnd.Compile(tP.TTL, tP.QueueLength)
|
||||
now := time.Now()
|
||||
var metrics []string
|
||||
if len(tP.Metrics) != 0 {
|
||||
@@ -137,7 +133,9 @@ func (tS *TrendS) computeTrend(tP *TrendProfile) {
|
||||
mWt.TrendLabel = trnd.getTrendLabel(mWt.TrendGrowth, tP.Tolerance)
|
||||
}
|
||||
trnd.Metrics[now][mWt.ID] = mWt
|
||||
trnd.indexesAppendMetric(mWt, now)
|
||||
}
|
||||
|
||||
if err = tS.storeTrend(trnd); err != nil {
|
||||
utils.Logger.Warning(
|
||||
fmt.Sprintf(
|
||||
@@ -178,6 +176,7 @@ func (tS *TrendS) processThresholds(trnd *Trend) (err error) {
|
||||
trnd.tPrfl.ThresholdIDs[0] == utils.MetaNone {
|
||||
return
|
||||
}
|
||||
thIDs = make([]string, len(trnd.tPrfl.ThresholdIDs))
|
||||
copy(thIDs, trnd.tPrfl.ThresholdIDs)
|
||||
}
|
||||
opts[utils.OptsThresholdsProfileIDs] = thIDs
|
||||
@@ -378,6 +377,17 @@ func (tS *TrendS) StopTrendS() {
|
||||
}
|
||||
}
|
||||
|
||||
func (tS *TrendS) Reload() {
|
||||
ctx := tS.crn.Stop()
|
||||
close(tS.trendStop)
|
||||
<-ctx.Done()
|
||||
<-tS.storingStopped
|
||||
tS.trendStop = make(chan struct{})
|
||||
tS.storingStopped = make(chan struct{})
|
||||
tS.crn.Start()
|
||||
go tS.asyncStoreTrends()
|
||||
}
|
||||
|
||||
// scheduleAutomaticQueries will schedule the queries at start/reload based on configured
|
||||
func (tS *TrendS) scheduleAutomaticQueries() error {
|
||||
schedData := make(map[string][]string)
|
||||
|
||||
@@ -179,13 +179,13 @@ func testScheduledTrends(t *testing.T) {
|
||||
},
|
||||
{
|
||||
TrendID: "TREND_2",
|
||||
Next: time.Now().Add(2 * time.Second),
|
||||
Next: time.Now().Add(3 * time.Second),
|
||||
},
|
||||
}
|
||||
|
||||
if err := trendAuQRpc.Call(context.Background(), utils.TrendSv1GetScheduledTrends, &utils.ArgScheduledTrends{TenantIDWithAPIOpts: utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org"}}, TrendIDPrefix: []string{"TREND"}}, &schedTrends); err != nil {
|
||||
t.Error(err)
|
||||
} else if diff := cmp.Diff(schedTrends, expTrends, cmpopts.EquateApproxTime(2*time.Second), cmpopts.IgnoreFields(utils.ScheduledTrend{}, "Prev")); diff != utils.EmptyString {
|
||||
} else if diff := cmp.Diff(schedTrends, expTrends, cmpopts.EquateApproxTime(3*time.Second), cmpopts.IgnoreFields(utils.ScheduledTrend{}, "Prev")); diff != utils.EmptyString {
|
||||
t.Errorf("unexpected scheduled trends (-want +got)\n%s", diff)
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,11 @@ package general_tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
@@ -55,7 +55,10 @@ func TestTrendSchedule(t *testing.T) {
|
||||
|
||||
"trends": {
|
||||
"enabled": true,
|
||||
"store_interval": "-1",
|
||||
"stats_conns":["*localhost"],
|
||||
"thresholds_conns": [],
|
||||
"ees_conns": [],
|
||||
},
|
||||
|
||||
"stats": {
|
||||
@@ -67,15 +70,48 @@ func TestTrendSchedule(t *testing.T) {
|
||||
"enabled": true,
|
||||
},
|
||||
|
||||
"thresholds": {
|
||||
"enabled": true,
|
||||
"store_interval": "-1"
|
||||
},
|
||||
|
||||
"ees": {
|
||||
"enabled": true,
|
||||
"attributes_conns":["*localhost"],
|
||||
"exporters": [
|
||||
{
|
||||
"id": "exporter1",
|
||||
"type": "*virt",
|
||||
"flags": [""],
|
||||
"attempts": 1,
|
||||
"synchronous": true,
|
||||
"filters":["*string:~*req.TrendID:TREND_1"],
|
||||
"fields":[
|
||||
{"tag": "TrendID", "path": "*uch.TrendID", "type": "*variable", "value": "~*req.TrendID"},
|
||||
{"tag": "EventType", "path": "*uch.EventType", "type": "*variable", "value": "~*opts.*eventType"},
|
||||
{"tag": "SumUsage", "path": "*uch.SumUsage", "type": "*variable", "value": "~*req.Metrics.*sum#~*req.Usage.Value"},
|
||||
{"tag": "AverageCallDuration", "path": "*uch.AverageCallDuration", "type": "*variable", "value": "~*req.Metrics.*acd.Value"},
|
||||
{"tag": "PDD", "path": "*uch.PDD", "type": "*variable", "value": "~*req.Metrics.*pdd.Value"},
|
||||
{"tag": "TotalCallDuration", "path": "*uch.TotalCallDuration", "type": "*variable", "value": "~*req.Metrics.*tcd.Value"},
|
||||
{"tag": "TotalCallCost", "path": "*uch.TotalCallCost", "type": "*variable", "value": "~*req.Metrics.*tcc.Value"},
|
||||
],
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
`
|
||||
tpFiles := map[string]string{
|
||||
utils.TrendsCsv: `#Tenant[0],Id[1],Schedule[2],StatID[3],Metrics[4],TTL[5],QueueLength[6],MinItems[7],CorrelationType[8],Tolerance[9],Stored[10],ThresholdIDs[11]
|
||||
cgrates.org,TREND_1,@every 1s,Stats1_1,,-1,-1,1,*last,1,false,
|
||||
cgrates.org,TREND_2,@every 1s,Stats1_2,,-1,-1,1,*last,1,false,`,
|
||||
cgrates.org,TREND_1,@every 1s,Stats1_1,,-1,-1,1,*last,1,false,Threshold1;Threshold2
|
||||
cgrates.org,TREND_2,@every 1s,Stats1_2,,-1,-1,1,*last,1,false,*none`,
|
||||
utils.StatsCsv: `#Tenant[0],Id[1],FilterIDs[2],ActivationInterval[3],QueueLength[4],TTL[5],MinItems[6],Metrics[7],MetricFilterIDs[8],Stored[9],Blocker[10],Weight[11],ThresholdIDs[12]
|
||||
cgrates.org,Stats1_1,*string:~*req.Account:1001,,,,,*tcc;*acd;*tcd,,,,,
|
||||
cgrates.org,Stats1_2,*string:~*req.Account:1002,,,,,*sum#~*req.Usage;*pdd,,,,,`}
|
||||
cgrates.org,Stats1_2,*string:~*req.Account:1002,,,,,*sum#~*req.Usage;*pdd,,,,,`,
|
||||
utils.ThresholdsCsv: `#Tenant[0],Id[1],FilterIDs[2],ActivationInterval[3],MaxHits[4],MinHits[5],MinSleep[6],Blocker[7],Weight[8],ActionIDs[9],Async[10]
|
||||
cgrates.org,Threshold1,*string:~*req.Metrics.*acd.ID:*acd,2024-07-29T15:00:00Z,-1,10,1s,false,10,,true
|
||||
cgrates.org,Threshold2,*string:~*req.Metrics.*pdd.ID:*pdd,2024-07-29T15:00:00Z,-1,10,1s,false,10,,true
|
||||
`}
|
||||
|
||||
ng := TestEngine{
|
||||
ConfigJSON: content,
|
||||
@@ -83,7 +119,8 @@ cgrates.org,Stats1_2,*string:~*req.Account:1002,,,,,*sum#~*req.Usage;*pdd,,,,,`}
|
||||
}
|
||||
|
||||
client, _ := ng.Run(t)
|
||||
t.Run("CheckTrendSchedule", func(t *testing.T) {
|
||||
var tr *engine.Trend
|
||||
t.Run("TrendSchedule", func(t *testing.T) {
|
||||
var scheduled int
|
||||
if err := client.Call(context.Background(), utils.TrendSv1ScheduleQueries,
|
||||
&utils.ArgScheduleTrendQueries{TrendIDs: []string{"TREND_1", "TREND_2"}, TenantIDWithAPIOpts: utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org"}}}, &scheduled); err != nil {
|
||||
@@ -102,27 +139,45 @@ cgrates.org,Stats1_2,*string:~*req.Account:1002,,,,,*sum#~*req.Usage;*pdd,,,,,`}
|
||||
Event: map[string]any{
|
||||
utils.AccountField: fmt.Sprintf("100%d", i),
|
||||
utils.AnswerTime: time.Date(2024, 8, 22, 14, 25, 0, 0, time.UTC),
|
||||
utils.Usage: time.Duration(rand.Intn(3600)+60) * time.Second,
|
||||
utils.Cost: rand.Float64()*20 + float64(i/10),
|
||||
utils.PDD: time.Duration(rand.Intn(10)+i) * time.Second,
|
||||
utils.Usage: time.Duration(1800+60) * time.Second,
|
||||
utils.Cost: 20 + float64(i/10),
|
||||
utils.PDD: time.Duration(10+i) * time.Second,
|
||||
}}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
time.Sleep(1 * time.Second)
|
||||
t.Run("TestGetTrend", func(t *testing.T) {
|
||||
var tr engine.Trend
|
||||
t.Run("GetTrend", func(t *testing.T) {
|
||||
// GetTrend without pagination parameters in args
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_1", TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(tr.RunTimes) != 1 && len(tr.Metrics) != 1 {
|
||||
} else if len(tr.RunTimes) != 1 || len(tr.Metrics) != 1 {
|
||||
t.Error("expected metrics to be calculated")
|
||||
}
|
||||
// GetTrend with RunIndexStart larger than RunTime length
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_2", RunIndexStart: 1, TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CheckThresholds", func(t *testing.T) {
|
||||
var td *engine.Threshold
|
||||
if err := client.Call(context.Background(), utils.ThresholdSv1GetThreshold,
|
||||
&utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "Threshold1"}}, &td); err != nil {
|
||||
t.Error(err)
|
||||
} else if td.Hits != 0 {
|
||||
t.Errorf("Threshold with id %s expected 0 hits, got %d", td.ID, td.Hits)
|
||||
}
|
||||
|
||||
if err := client.Call(context.Background(), utils.ThresholdSv1GetThreshold,
|
||||
&utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "Threshold2"}}, &td); err != nil {
|
||||
t.Error(err)
|
||||
} else if td.Hits != 0 {
|
||||
t.Errorf("Threshold with id %s expected 0 hits, got %d", td.ID, td.Hits)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ProcessStats", func(t *testing.T) {
|
||||
var reply []string
|
||||
for i := range 2 {
|
||||
@@ -134,42 +189,200 @@ cgrates.org,Stats1_2,*string:~*req.Account:1002,,,,,*sum#~*req.Usage;*pdd,,,,,`}
|
||||
utils.AccountField: fmt.Sprintf("100%d", i),
|
||||
utils.AnswerTime: time.Date(2024, 9, 22, 14, 25, 0, 0, time.UTC),
|
||||
utils.Usage: time.Duration(60) * time.Second / time.Duration(i),
|
||||
utils.Cost: rand.Float64() * 30 * float64(i),
|
||||
utils.PDD: time.Duration(rand.Intn(20)+i) * time.Second,
|
||||
utils.Cost: 30 * float64(i),
|
||||
utils.PDD: time.Duration(20+i) * time.Second,
|
||||
}}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CheckExportedTrends", func(t *testing.T) {
|
||||
var eventType any
|
||||
if err = client.Call(context.Background(), utils.CacheSv1GetItem, &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "cgrates.org",
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: utils.CacheUCH,
|
||||
ItemID: "TrendID",
|
||||
},
|
||||
}, &eventType); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var exporterID any
|
||||
if err = client.Call(context.Background(), utils.CacheSv1GetItem, &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "cgrates.org",
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: utils.CacheUCH,
|
||||
ItemID: "EventType",
|
||||
},
|
||||
}, &exporterID); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("TrendsSetConfig", func(t *testing.T) {
|
||||
var reply string
|
||||
if err := client.Call(context.Background(), utils.ConfigSv1SetConfig, &config.SetConfigArgs{
|
||||
Tenant: "cgrates.org",
|
||||
Config: map[string]any{
|
||||
"trends": map[string]any{
|
||||
"enabled": true,
|
||||
"stats_conns": []string{"*localhost"},
|
||||
"store_interval": "-1",
|
||||
"thresholds_conns": []string{"*localhost"},
|
||||
"ees_conns": []string{"*localhost"},
|
||||
"ees_exporter_ids": []string{"exporter1"},
|
||||
},
|
||||
},
|
||||
}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Errorf("Expected OK received: %s", reply)
|
||||
}
|
||||
})
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
t.Run("TestGetTrend", func(t *testing.T) {
|
||||
t.Run("GetTrends", func(t *testing.T) {
|
||||
// GetTrend with RunTimeEnd earlier than first scheduled time
|
||||
timeEnd := time.Now().Add(-3 * time.Second).Format(time.RFC3339)
|
||||
var tr engine.Trend
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_1", RunTimeEnd: timeEnd, TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_2", RunIndexEnd: 4, TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err != nil {
|
||||
// GetTrend with RunIndexEnd large than Runtimes length
|
||||
var tr *engine.Trend
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_2", RunIndexEnd: 3, TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(tr.RunTimes) != 2 && len(tr.Metrics) != 2 {
|
||||
} else if len(tr.RunTimes) != 2 || len(tr.Metrics) != 2 {
|
||||
t.Errorf("expected to 2 runtimes, got %d", len(tr.RunTimes))
|
||||
}
|
||||
|
||||
// GetTrend with RunTimeStart after the scheduled runtimes
|
||||
timeStart := time.Now().Add(1 * time.Second).Format(time.RFC3339)
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_2", RunTimeStart: timeStart, TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
t.Run("CheckThresholds", func(t *testing.T) {
|
||||
var td *engine.Threshold
|
||||
if err := client.Call(context.Background(), utils.ThresholdSv1GetThreshold,
|
||||
&utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "Threshold1"}}, &td); err != nil {
|
||||
t.Error(err)
|
||||
} else if td.Hits != 1 {
|
||||
t.Errorf("Threshold with id %s expected 1 hits, got %d", td.ID, td.Hits)
|
||||
}
|
||||
|
||||
if err := client.Call(context.Background(), utils.ThresholdSv1GetThreshold,
|
||||
&utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "Threshold2"}}, &td); err != nil {
|
||||
t.Error(err)
|
||||
} else if td.Hits != 0 {
|
||||
t.Errorf("Threshold with id %s expected 0 hits, got %d", td.ID, td.Hits)
|
||||
}
|
||||
})
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
t.Run("TestGetTrend", func(t *testing.T) {
|
||||
t.Run("GetTrends", func(t *testing.T) {
|
||||
// GetTrend with all correctParameters
|
||||
var tr *engine.Trend
|
||||
timeStart := time.Now().Add(-4 * time.Second).Format(time.RFC3339)
|
||||
timeEnd := time.Now().Add(-1 * time.Second).Format(time.RFC3339)
|
||||
var tr engine.Trend
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_1", RunIndexStart: 1, RunIndexEnd: 3, RunTimeStart: timeStart, RunTimeEnd: timeEnd, TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(tr.RunTimes) != 2 && len(tr.Metrics) != 1 {
|
||||
} else if len(tr.RunTimes) != 1 || len(tr.Metrics) != 1 {
|
||||
t.Errorf("expected to 2 runtimes, got %d", len(tr.RunTimes))
|
||||
}
|
||||
|
||||
// GetTrend with incorrect indexes
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_2", RunIndexStart: 5, RunIndexEnd: 3, TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
//GetTrend with incorrect runtime args
|
||||
timeEnd = time.Now().Add(-5 * time.Second).Format(time.RFC3339)
|
||||
timeStart = time.Now().Add(5 * time.Second).Format(time.RFC3339)
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_2", RunTimeStart: timeStart, RunTimeEnd: timeEnd, TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
var tr2 engine.Trend
|
||||
// GetTrend with both index args
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_1", RunIndexStart: 3, RunIndexEnd: 4, TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr2); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(tr2.RunTimes) != 1 || len(tr2.Metrics) != 1 {
|
||||
t.Errorf("expected to 2 runtimes, got %d", len(tr.RunTimes))
|
||||
}
|
||||
|
||||
// GetTrend without any pagination args
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_1", TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(tr.RunTimes) != 4 || len(tr.Metrics) != 4 {
|
||||
t.Errorf("expected to 4 runtimes, got %d", len(tr.RunTimes))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CheckExportedTrends", func(t *testing.T) {
|
||||
var eventType any
|
||||
if err = client.Call(context.Background(), utils.CacheSv1GetItem, &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "cgrates.org",
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: utils.CacheUCH,
|
||||
ItemID: "EventType",
|
||||
},
|
||||
}, &eventType); err != nil {
|
||||
t.Error(err)
|
||||
} else if eventType != utils.TrendUpdate {
|
||||
t.Errorf("expected %v, received %v", utils.StatUpdate, eventType)
|
||||
}
|
||||
|
||||
var trendID any
|
||||
if err = client.Call(context.Background(), utils.CacheSv1GetItem, &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "cgrates.org",
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: utils.CacheUCH,
|
||||
ItemID: utils.TrendID,
|
||||
},
|
||||
}, &trendID); err != nil {
|
||||
t.Error(err)
|
||||
} else if trendID != "TREND_1" {
|
||||
t.Errorf("expected %v, received %v", "STAT_EES", trendID)
|
||||
}
|
||||
|
||||
var averageCallCost any
|
||||
if err = client.Call(context.Background(), utils.CacheSv1GetItem, &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "cgrates.org",
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: utils.CacheUCH,
|
||||
ItemID: "AverageCallDuration",
|
||||
},
|
||||
}, &averageCallCost); err != nil {
|
||||
t.Error(err)
|
||||
} else if averageCallCost != "960000000000" {
|
||||
t.Errorf("expected %v, received %v", "960000000000", averageCallCost)
|
||||
}
|
||||
|
||||
var totalCallDuration any
|
||||
if err = client.Call(context.Background(), utils.CacheSv1GetItem, &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "cgrates.org",
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: utils.CacheUCH,
|
||||
ItemID: "TotalCallDuration",
|
||||
},
|
||||
}, &totalCallDuration); err != nil {
|
||||
t.Error(err)
|
||||
} else if totalCallDuration != "1920000000000" {
|
||||
t.Errorf("expected %v, received %v", "1920000000000", totalCallDuration)
|
||||
}
|
||||
var totalCallCost any
|
||||
if err = client.Call(context.Background(), utils.CacheSv1GetItem, &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "cgrates.org",
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: utils.CacheUCH,
|
||||
ItemID: "TotalCallCost",
|
||||
},
|
||||
}, &totalCallCost); err != nil {
|
||||
t.Error(err)
|
||||
} else if totalCallCost != "50" {
|
||||
t.Errorf("expected %v, received %v", "50", totalCallCost)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
245
general_tests/trends_stored_it_test.go
Normal file
245
general_tests/trends_stored_it_test.go
Normal file
@@ -0,0 +1,245 @@
|
||||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package general_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestTrendStore(t *testing.T) {
|
||||
var dbConfig string
|
||||
switch *utils.DBType {
|
||||
case utils.MetaMySQL:
|
||||
dbConfig = `
|
||||
"data_db": {
|
||||
"db_type": "redis",
|
||||
"db_port": 6379,
|
||||
"db_name": "10",
|
||||
},
|
||||
|
||||
"stor_db": {
|
||||
"db_password": "CGRateS.org",
|
||||
},`
|
||||
case utils.MetaMongo:
|
||||
dbConfig = `
|
||||
"data_db": {
|
||||
"db_type": "mongo",
|
||||
"db_name": "10",
|
||||
"db_port": 27017,
|
||||
},
|
||||
|
||||
"stor_db": {
|
||||
"db_type": "mongo",
|
||||
"db_name": "cgrates",
|
||||
"db_port": 27017,
|
||||
"db_password": "",
|
||||
},`
|
||||
case utils.MetaInternal, utils.MetaPostgres:
|
||||
t.SkipNow()
|
||||
default:
|
||||
t.Fatal("unsupported dbtype value")
|
||||
}
|
||||
content := `{
|
||||
|
||||
"general": {
|
||||
"log_level": 7,
|
||||
},
|
||||
` + dbConfig + `
|
||||
"trends": {
|
||||
"enabled": true,
|
||||
"store_interval": "2100ms",
|
||||
"stats_conns":["*localhost"],
|
||||
},
|
||||
|
||||
"stats": {
|
||||
"enabled": true,
|
||||
"store_interval": "-1",
|
||||
},
|
||||
|
||||
"apiers": {
|
||||
"enabled": true,
|
||||
},
|
||||
|
||||
}
|
||||
`
|
||||
tpFiles := map[string]string{
|
||||
utils.TrendsCsv: `#Tenant[0],Id[1],Schedule[2],StatID[3],Metrics[4],TTL[5],QueueLength[6],MinItems[7],CorrelationType[8],Tolerance[9],Stored[10],ThresholdIDs[11]
|
||||
cgrates.org,TREND_1,@every 1s,Stats1_1,,-1,-1,1,*last,1,false,`,
|
||||
utils.StatsCsv: `#Tenant[0],Id[1],FilterIDs[2],ActivationInterval[3],QueueLength[4],TTL[5],MinItems[6],Metrics[7],MetricFilterIDs[8],Stored[9],Blocker[10],Weight[11],ThresholdIDs[12]
|
||||
cgrates.org,Stats1_1,*string:~*req.Account:1001,,,,,*tcc;*acd;*tcd,,,,,`}
|
||||
|
||||
ng := TestEngine{
|
||||
ConfigJSON: content,
|
||||
TpFiles: tpFiles,
|
||||
}
|
||||
|
||||
client, _ := ng.Run(t)
|
||||
t.Run("TrendSchedule", func(t *testing.T) {
|
||||
var scheduled int
|
||||
if err := client.Call(context.Background(), utils.TrendSv1ScheduleQueries,
|
||||
&utils.ArgScheduleTrendQueries{TrendIDs: []string{"TREND_1"}, TenantIDWithAPIOpts: utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org"}}}, &scheduled); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if scheduled != 1 {
|
||||
t.Errorf("expected 1, got %d", scheduled)
|
||||
}
|
||||
})
|
||||
t.Run("ProcessStats", func(t *testing.T) {
|
||||
var reply []string
|
||||
if err := client.Call(context.Background(), utils.StatSv1ProcessEvent, &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event1",
|
||||
Event: map[string]any{
|
||||
utils.AccountField: "1001",
|
||||
utils.AnswerTime: time.Date(2024, 8, 22, 14, 25, 0, 0, time.UTC),
|
||||
utils.Usage: time.Duration(1800+60) * time.Second,
|
||||
utils.Cost: 20 + float64(10),
|
||||
utils.PDD: time.Duration(10 * time.Second),
|
||||
}}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
})
|
||||
time.Sleep(1 * time.Second)
|
||||
t.Run("GetTrendBeforeStoreInterval", func(t *testing.T) {
|
||||
var reply string
|
||||
if err := client.Call(context.Background(), utils.CacheSv1Clear, &utils.AttrCacheIDsWithAPIOpts{}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Calling CacheSv1.ReloadCache got reply: ", reply)
|
||||
}
|
||||
var tr *engine.Trend
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_1", TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
|
||||
time.Sleep(1200 * time.Millisecond)
|
||||
t.Run("GetTrendsAfterStoreInterval", func(t *testing.T) {
|
||||
var reply string
|
||||
if err := client.Call(context.Background(), utils.CacheSv1Clear, &utils.AttrCacheIDsWithAPIOpts{}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Calling CacheSv1.ReloadCache got reply: ", reply)
|
||||
}
|
||||
var trnd *engine.Trend
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_1", TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &trnd); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(trnd.RunTimes) != 1 || len(trnd.Metrics) != 1 {
|
||||
t.Errorf("expected to 1 runtimes, got %d", len(trnd.RunTimes))
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
t.Run("TrendsSetConfig", func(t *testing.T) {
|
||||
var reply string
|
||||
// setting store interval to 0
|
||||
if err := client.Call(context.Background(), utils.ConfigSv1SetConfig, &config.SetConfigArgs{
|
||||
Tenant: "cgrates.org",
|
||||
Config: map[string]any{
|
||||
"trends": map[string]any{
|
||||
"enabled": true,
|
||||
"stats_conns": []string{"*localhost"},
|
||||
"store_interval": "0",
|
||||
},
|
||||
},
|
||||
}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Errorf("Expected OK received: %s", reply)
|
||||
}
|
||||
})
|
||||
t.Run("TrendSchedule", func(t *testing.T) {
|
||||
var scheduled int
|
||||
if err := client.Call(context.Background(), utils.TrendSv1ScheduleQueries,
|
||||
&utils.ArgScheduleTrendQueries{TrendIDs: []string{"TREND_1"}, TenantIDWithAPIOpts: utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org"}}}, &scheduled); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if scheduled != 1 {
|
||||
t.Errorf("expected 1, got %d", scheduled)
|
||||
}
|
||||
})
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
t.Run("GetTrendsNotStored", func(t *testing.T) {
|
||||
var reply string
|
||||
if err := client.Call(context.Background(), utils.CacheSv1Clear, &utils.AttrCacheIDsWithAPIOpts{}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Calling CacheSv1.ReloadCache got reply: ", reply)
|
||||
}
|
||||
|
||||
var tr *engine.Trend
|
||||
// the trend will be not updated since storeinterval is set to 0
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_1", TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(tr.RunTimes) != 1 || len(tr.Metrics) != 1 {
|
||||
t.Errorf("expected to 1 runtimes, got %d", len(tr.RunTimes))
|
||||
}
|
||||
})
|
||||
t.Run("TrendsSetConfig", func(t *testing.T) {
|
||||
var reply string
|
||||
// setting store interval to -1
|
||||
if err := client.Call(context.Background(), utils.ConfigSv1SetConfig, &config.SetConfigArgs{
|
||||
Tenant: "cgrates.org",
|
||||
Config: map[string]any{
|
||||
"trends": map[string]any{
|
||||
"enabled": true,
|
||||
"stats_conns": []string{"*localhost"},
|
||||
"store_interval": "-1",
|
||||
},
|
||||
},
|
||||
}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Errorf("Expected OK received: %s", reply)
|
||||
}
|
||||
})
|
||||
t.Run("TrendSchedule", func(t *testing.T) {
|
||||
var scheduled int
|
||||
if err := client.Call(context.Background(), utils.TrendSv1ScheduleQueries,
|
||||
&utils.ArgScheduleTrendQueries{TrendIDs: []string{"TREND_1"}, TenantIDWithAPIOpts: utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org"}}}, &scheduled); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if scheduled != 1 {
|
||||
t.Errorf("expected 1, got %d", scheduled)
|
||||
}
|
||||
})
|
||||
time.Sleep(1 * time.Second)
|
||||
t.Run("GetTrendsStoredUnlimited", func(t *testing.T) {
|
||||
var reply string
|
||||
if err := client.Call(context.Background(), utils.CacheSv1Clear, &utils.AttrCacheIDsWithAPIOpts{}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Calling CacheSv1.ReloadCache got reply: ", reply)
|
||||
}
|
||||
var tr *engine.Trend
|
||||
// the trend will be updated since storeinterval is set to -1
|
||||
if err := client.Call(context.Background(), utils.TrendSv1GetTrend, &utils.ArgGetTrend{ID: "TREND_1", TenantWithAPIOpts: utils.TenantWithAPIOpts{Tenant: "cgrates.org"}}, &tr); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(tr.RunTimes) != 2 || len(tr.Metrics) != 2 {
|
||||
t.Errorf("expected to 2 runtimes, got %d", len(tr.RunTimes))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -100,6 +100,9 @@ func (trs *TrendService) Start() error {
|
||||
|
||||
// Reload handles the change of config
|
||||
func (tr *TrendService) Reload() (err error) {
|
||||
tr.Lock()
|
||||
tr.trs.Reload()
|
||||
tr.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -29,13 +29,6 @@ import (
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestTrendServiceReload(t *testing.T) {
|
||||
trendService := &TrendService{}
|
||||
if err := trendService.Reload(); err != nil {
|
||||
t.Errorf("Reload() expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewTrendService(t *testing.T) {
|
||||
|
||||
cfg := &config.CGRConfig{}
|
||||
|
||||
Reference in New Issue
Block a user