mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Refactored engine/stats_test.go
This commit is contained in:
committed by
Dan Christian Bogos
parent
d20a1de9ed
commit
c40deb37c9
@@ -26,12 +26,39 @@ import (
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
cloneExpTimeStats time.Time
|
||||
expTimeStats = time.Now().Add(20 * time.Minute)
|
||||
statService *StatService
|
||||
dmSTS *DataManager
|
||||
sqps = []*StatQueueProfile{
|
||||
func TestNewStatService(t *testing.T) {
|
||||
testIntDB := NewInternalDB(nil, nil, true)
|
||||
testDM := NewDataManager(testIntDB, config.CgrConfig().CacheCfg(), nil)
|
||||
testCgrCfg := config.NewDefaultCGRConfig()
|
||||
testFltrS := &FilterS{dm: testDM, cfg: testCgrCfg}
|
||||
expStruct := &StatService{
|
||||
dm: testDM,
|
||||
filterS: testFltrS,
|
||||
cgrcfg: testCgrCfg,
|
||||
storedStatQueues: make(utils.StringSet),
|
||||
}
|
||||
result, err := NewStatService(testDM, testCgrCfg, testFltrS, nil)
|
||||
if err != nil {
|
||||
t.Errorf("\nExpecting <nil>,\n Received <%+v>", err)
|
||||
}
|
||||
if !reflect.DeepEqual(expStruct.dm, result.dm) {
|
||||
t.Errorf("\nExpecting <%+v>,\n Received <%+v>", expStruct.dm, result.dm)
|
||||
}
|
||||
if !reflect.DeepEqual(expStruct.filterS, result.filterS) {
|
||||
t.Errorf("\nExpecting <%+v>,\n Received <%+v>", expStruct.filterS, result.filterS)
|
||||
}
|
||||
if !reflect.DeepEqual(expStruct.cgrcfg, result.cgrcfg) {
|
||||
t.Errorf("\nExpecting <%+v>,\n Received <%+v>", expStruct.cgrcfg, result.cgrcfg)
|
||||
}
|
||||
if !reflect.DeepEqual(expStruct.storedStatQueues, expStruct.storedStatQueues) {
|
||||
t.Errorf("\nExpecting <%+v>,\n Received <%+v>", expStruct.storedStatQueues, expStruct.storedStatQueues)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchingStatQueuesForEvent(t *testing.T) {
|
||||
var statService *StatService
|
||||
var dmSTS *DataManager
|
||||
sqps := []*StatQueueProfile{
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatQueueProfile1",
|
||||
@@ -93,12 +120,12 @@ var (
|
||||
MinItems: 1,
|
||||
},
|
||||
}
|
||||
stqs = []*StatQueue{
|
||||
stqs := []*StatQueue{
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfile1", sqPrfl: sqps[0]},
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfile2", sqPrfl: sqps[1]},
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfilePrefix", sqPrfl: sqps[2]},
|
||||
}
|
||||
statsEvs = []*StatsArgsProcessEvent{
|
||||
statsEvs := []*StatsArgsProcessEvent{
|
||||
{
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
@@ -139,9 +166,6 @@ var (
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func TestStatQueuesPopulateService(t *testing.T) {
|
||||
defaultCfg := config.NewDefaultCGRConfig()
|
||||
data := NewInternalDB(nil, nil, true)
|
||||
dmSTS = NewDataManager(data, config.CgrConfig().CacheCfg(), nil)
|
||||
@@ -154,9 +178,6 @@ func TestStatQueuesPopulateService(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatQueuesAddFilters(t *testing.T) {
|
||||
fltrSts1 := &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_STATS_1",
|
||||
@@ -223,9 +244,6 @@ func TestStatQueuesAddFilters(t *testing.T) {
|
||||
},
|
||||
}
|
||||
dmSTS.SetFilter(fltrSts3, true)
|
||||
}
|
||||
|
||||
func TestStatQueuesPopulateStatsService(t *testing.T) {
|
||||
for _, statQueueProfile := range sqps {
|
||||
dmSTS.SetStatQueueProfile(statQueueProfile, true)
|
||||
}
|
||||
@@ -241,9 +259,6 @@ func TestStatQueuesPopulateStatsService(t *testing.T) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", sqp, tempStat)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatQueuesMatchingStatQueuesForEvent(t *testing.T) {
|
||||
msq, err := statService.matchingStatQueuesForEvent(statsEvs[0].Tenant, statsEvs[0].StatIDs, statsEvs[0].Time,
|
||||
utils.MapStorage{utils.MetaReq: statsEvs[0].Event, utils.MetaOpts: statsEvs[0].Opts})
|
||||
if err != nil {
|
||||
@@ -283,6 +298,209 @@ func TestStatQueuesMatchingStatQueuesForEvent(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStatQueuesProcessEvent(t *testing.T) {
|
||||
var statService *StatService
|
||||
var dmSTS *DataManager
|
||||
sqps := []*StatQueueProfile{
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatQueueProfile1",
|
||||
FilterIDs: []string{"FLTR_STATS_1"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
QueueLength: 10,
|
||||
TTL: 10 * time.Second,
|
||||
Metrics: []*MetricWithFilters{
|
||||
{
|
||||
MetricID: "*sum#Usage",
|
||||
},
|
||||
},
|
||||
ThresholdIDs: []string{},
|
||||
Blocker: false,
|
||||
Stored: true,
|
||||
Weight: 20,
|
||||
MinItems: 1,
|
||||
},
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatQueueProfile2",
|
||||
FilterIDs: []string{"FLTR_STATS_2"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
QueueLength: 10,
|
||||
TTL: 10 * time.Second,
|
||||
Metrics: []*MetricWithFilters{
|
||||
{
|
||||
MetricID: "*sum#Usage",
|
||||
},
|
||||
},
|
||||
ThresholdIDs: []string{},
|
||||
Blocker: false,
|
||||
Stored: true,
|
||||
Weight: 20,
|
||||
MinItems: 1,
|
||||
},
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatQueueProfilePrefix",
|
||||
FilterIDs: []string{"FLTR_STATS_3"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
QueueLength: 10,
|
||||
TTL: 10 * time.Second,
|
||||
Metrics: []*MetricWithFilters{
|
||||
{
|
||||
MetricID: "*sum#Usage",
|
||||
},
|
||||
},
|
||||
ThresholdIDs: []string{},
|
||||
Blocker: false,
|
||||
Stored: true,
|
||||
Weight: 20,
|
||||
MinItems: 1,
|
||||
},
|
||||
}
|
||||
stqs := []*StatQueue{
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfile1", sqPrfl: sqps[0]},
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfile2", sqPrfl: sqps[1]},
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfilePrefix", sqPrfl: sqps[2]},
|
||||
}
|
||||
statsEvs := []*StatsArgsProcessEvent{
|
||||
{
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event1",
|
||||
Event: map[string]interface{}{
|
||||
"Stats": "StatQueueProfile1",
|
||||
utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC),
|
||||
"UsageInterval": "1s",
|
||||
"PddInterval": "1s",
|
||||
"Weight": "9.0",
|
||||
utils.Usage: 135 * time.Second,
|
||||
utils.Cost: 123.0,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event2",
|
||||
Event: map[string]interface{}{
|
||||
"Stats": "StatQueueProfile2",
|
||||
utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC),
|
||||
"UsageInterval": "1s",
|
||||
"PddInterval": "1s",
|
||||
"Weight": "15.0",
|
||||
utils.Usage: 45 * time.Second,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event3",
|
||||
Event: map[string]interface{}{
|
||||
"Stats": "StatQueueProfilePrefix",
|
||||
utils.Usage: 30 * time.Second,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
defaultCfg := config.NewDefaultCGRConfig()
|
||||
data := NewInternalDB(nil, nil, true)
|
||||
dmSTS = NewDataManager(data, config.CgrConfig().CacheCfg(), nil)
|
||||
|
||||
defaultCfg.StatSCfg().StoreInterval = 1
|
||||
defaultCfg.StatSCfg().StringIndexedFields = nil
|
||||
defaultCfg.StatSCfg().PrefixIndexedFields = nil
|
||||
statService, err = NewStatService(dmSTS, defaultCfg,
|
||||
&FilterS{dm: dmSTS, cfg: defaultCfg}, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
fltrSts1 := &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_STATS_1",
|
||||
Rules: []*FilterRule{
|
||||
{
|
||||
Type: utils.MetaString,
|
||||
Element: "~*req.Stats",
|
||||
Values: []string{"StatQueueProfile1"},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: "~*req.UsageInterval",
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Usage,
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Weight,
|
||||
Values: []string{"9.0"},
|
||||
},
|
||||
},
|
||||
}
|
||||
dmSTS.SetFilter(fltrSts1, true)
|
||||
fltrSts2 := &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_STATS_2",
|
||||
Rules: []*FilterRule{
|
||||
{
|
||||
Type: utils.MetaString,
|
||||
Element: "~*req.Stats",
|
||||
Values: []string{"StatQueueProfile2"},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: "~*req.PddInterval",
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Usage,
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Weight,
|
||||
Values: []string{"15.0"},
|
||||
},
|
||||
},
|
||||
}
|
||||
dmSTS.SetFilter(fltrSts2, true)
|
||||
fltrSts3 := &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_STATS_3",
|
||||
Rules: []*FilterRule{
|
||||
{
|
||||
Type: utils.MetaPrefix,
|
||||
Element: "~*req.Stats",
|
||||
Values: []string{"StatQueueProfilePrefix"},
|
||||
},
|
||||
},
|
||||
}
|
||||
dmSTS.SetFilter(fltrSts3, true)
|
||||
for _, statQueueProfile := range sqps {
|
||||
dmSTS.SetStatQueueProfile(statQueueProfile, true)
|
||||
}
|
||||
for _, statQueue := range stqs {
|
||||
dmSTS.SetStatQueue(statQueue, nil, 0, nil, 0, true)
|
||||
}
|
||||
//Test each statQueueProfile from cache
|
||||
for _, sqp := range sqps {
|
||||
if tempStat, err := dmSTS.GetStatQueueProfile(sqp.Tenant,
|
||||
sqp.ID, true, false, utils.NonTransactional); err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
} else if !reflect.DeepEqual(sqp, tempStat) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", sqp, tempStat)
|
||||
}
|
||||
}
|
||||
stq := map[string]string{}
|
||||
reply := []string{}
|
||||
expected := []string{"StatQueueProfile1"}
|
||||
@@ -323,6 +541,209 @@ func TestStatQueuesProcessEvent(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStatQueuesMatchWithIndexFalse(t *testing.T) {
|
||||
var statService *StatService
|
||||
var dmSTS *DataManager
|
||||
sqps := []*StatQueueProfile{
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatQueueProfile1",
|
||||
FilterIDs: []string{"FLTR_STATS_1"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
QueueLength: 10,
|
||||
TTL: 10 * time.Second,
|
||||
Metrics: []*MetricWithFilters{
|
||||
{
|
||||
MetricID: "*sum#Usage",
|
||||
},
|
||||
},
|
||||
ThresholdIDs: []string{},
|
||||
Blocker: false,
|
||||
Stored: true,
|
||||
Weight: 20,
|
||||
MinItems: 1,
|
||||
},
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatQueueProfile2",
|
||||
FilterIDs: []string{"FLTR_STATS_2"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
QueueLength: 10,
|
||||
TTL: 10 * time.Second,
|
||||
Metrics: []*MetricWithFilters{
|
||||
{
|
||||
MetricID: "*sum#Usage",
|
||||
},
|
||||
},
|
||||
ThresholdIDs: []string{},
|
||||
Blocker: false,
|
||||
Stored: true,
|
||||
Weight: 20,
|
||||
MinItems: 1,
|
||||
},
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatQueueProfilePrefix",
|
||||
FilterIDs: []string{"FLTR_STATS_3"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
QueueLength: 10,
|
||||
TTL: 10 * time.Second,
|
||||
Metrics: []*MetricWithFilters{
|
||||
{
|
||||
MetricID: "*sum#Usage",
|
||||
},
|
||||
},
|
||||
ThresholdIDs: []string{},
|
||||
Blocker: false,
|
||||
Stored: true,
|
||||
Weight: 20,
|
||||
MinItems: 1,
|
||||
},
|
||||
}
|
||||
stqs := []*StatQueue{
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfile1", sqPrfl: sqps[0]},
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfile2", sqPrfl: sqps[1]},
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfilePrefix", sqPrfl: sqps[2]},
|
||||
}
|
||||
statsEvs := []*StatsArgsProcessEvent{
|
||||
{
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event1",
|
||||
Event: map[string]interface{}{
|
||||
"Stats": "StatQueueProfile1",
|
||||
utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC),
|
||||
"UsageInterval": "1s",
|
||||
"PddInterval": "1s",
|
||||
"Weight": "9.0",
|
||||
utils.Usage: 135 * time.Second,
|
||||
utils.Cost: 123.0,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event2",
|
||||
Event: map[string]interface{}{
|
||||
"Stats": "StatQueueProfile2",
|
||||
utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC),
|
||||
"UsageInterval": "1s",
|
||||
"PddInterval": "1s",
|
||||
"Weight": "15.0",
|
||||
utils.Usage: 45 * time.Second,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event3",
|
||||
Event: map[string]interface{}{
|
||||
"Stats": "StatQueueProfilePrefix",
|
||||
utils.Usage: 30 * time.Second,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
defaultCfg := config.NewDefaultCGRConfig()
|
||||
data := NewInternalDB(nil, nil, true)
|
||||
dmSTS = NewDataManager(data, config.CgrConfig().CacheCfg(), nil)
|
||||
|
||||
defaultCfg.StatSCfg().StoreInterval = 1
|
||||
defaultCfg.StatSCfg().StringIndexedFields = nil
|
||||
defaultCfg.StatSCfg().PrefixIndexedFields = nil
|
||||
statService, err = NewStatService(dmSTS, defaultCfg,
|
||||
&FilterS{dm: dmSTS, cfg: defaultCfg}, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
fltrSts1 := &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_STATS_1",
|
||||
Rules: []*FilterRule{
|
||||
{
|
||||
Type: utils.MetaString,
|
||||
Element: "~*req.Stats",
|
||||
Values: []string{"StatQueueProfile1"},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: "~*req.UsageInterval",
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Usage,
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Weight,
|
||||
Values: []string{"9.0"},
|
||||
},
|
||||
},
|
||||
}
|
||||
dmSTS.SetFilter(fltrSts1, true)
|
||||
fltrSts2 := &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_STATS_2",
|
||||
Rules: []*FilterRule{
|
||||
{
|
||||
Type: utils.MetaString,
|
||||
Element: "~*req.Stats",
|
||||
Values: []string{"StatQueueProfile2"},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: "~*req.PddInterval",
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Usage,
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Weight,
|
||||
Values: []string{"15.0"},
|
||||
},
|
||||
},
|
||||
}
|
||||
dmSTS.SetFilter(fltrSts2, true)
|
||||
fltrSts3 := &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_STATS_3",
|
||||
Rules: []*FilterRule{
|
||||
{
|
||||
Type: utils.MetaPrefix,
|
||||
Element: "~*req.Stats",
|
||||
Values: []string{"StatQueueProfilePrefix"},
|
||||
},
|
||||
},
|
||||
}
|
||||
dmSTS.SetFilter(fltrSts3, true)
|
||||
for _, statQueueProfile := range sqps {
|
||||
dmSTS.SetStatQueueProfile(statQueueProfile, true)
|
||||
}
|
||||
for _, statQueue := range stqs {
|
||||
dmSTS.SetStatQueue(statQueue, nil, 0, nil, 0, true)
|
||||
}
|
||||
//Test each statQueueProfile from cache
|
||||
for _, sqp := range sqps {
|
||||
if tempStat, err := dmSTS.GetStatQueueProfile(sqp.Tenant,
|
||||
sqp.ID, true, false, utils.NonTransactional); err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
} else if !reflect.DeepEqual(sqp, tempStat) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", sqp, tempStat)
|
||||
}
|
||||
}
|
||||
statService.cgrcfg.StatSCfg().IndexedSelects = false
|
||||
msq, err := statService.matchingStatQueuesForEvent(statsEvs[0].Tenant, statsEvs[0].StatIDs, statsEvs[0].Time,
|
||||
utils.MapStorage{utils.MetaReq: statsEvs[0].Event, utils.MetaOpts: statsEvs[0].Opts})
|
||||
@@ -361,7 +782,211 @@ func TestStatQueuesMatchWithIndexFalse(t *testing.T) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", stqs[2].sqPrfl, msq[0].sqPrfl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatQueuesV1ProcessEvent(t *testing.T) {
|
||||
var statService *StatService
|
||||
var dmSTS *DataManager
|
||||
sqps := []*StatQueueProfile{
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatQueueProfile1",
|
||||
FilterIDs: []string{"FLTR_STATS_1"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
QueueLength: 10,
|
||||
TTL: 10 * time.Second,
|
||||
Metrics: []*MetricWithFilters{
|
||||
{
|
||||
MetricID: "*sum#Usage",
|
||||
},
|
||||
},
|
||||
ThresholdIDs: []string{},
|
||||
Blocker: false,
|
||||
Stored: true,
|
||||
Weight: 20,
|
||||
MinItems: 1,
|
||||
},
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatQueueProfile2",
|
||||
FilterIDs: []string{"FLTR_STATS_2"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
QueueLength: 10,
|
||||
TTL: 10 * time.Second,
|
||||
Metrics: []*MetricWithFilters{
|
||||
{
|
||||
MetricID: "*sum#Usage",
|
||||
},
|
||||
},
|
||||
ThresholdIDs: []string{},
|
||||
Blocker: false,
|
||||
Stored: true,
|
||||
Weight: 20,
|
||||
MinItems: 1,
|
||||
},
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatQueueProfilePrefix",
|
||||
FilterIDs: []string{"FLTR_STATS_3"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
QueueLength: 10,
|
||||
TTL: 10 * time.Second,
|
||||
Metrics: []*MetricWithFilters{
|
||||
{
|
||||
MetricID: "*sum#Usage",
|
||||
},
|
||||
},
|
||||
ThresholdIDs: []string{},
|
||||
Blocker: false,
|
||||
Stored: true,
|
||||
Weight: 20,
|
||||
MinItems: 1,
|
||||
},
|
||||
}
|
||||
stqs := []*StatQueue{
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfile1", sqPrfl: sqps[0]},
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfile2", sqPrfl: sqps[1]},
|
||||
{Tenant: "cgrates.org", ID: "StatQueueProfilePrefix", sqPrfl: sqps[2]},
|
||||
}
|
||||
statsEvs := []*StatsArgsProcessEvent{
|
||||
{
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event1",
|
||||
Event: map[string]interface{}{
|
||||
"Stats": "StatQueueProfile1",
|
||||
utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC),
|
||||
"UsageInterval": "1s",
|
||||
"PddInterval": "1s",
|
||||
"Weight": "9.0",
|
||||
utils.Usage: 135 * time.Second,
|
||||
utils.Cost: 123.0,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event2",
|
||||
Event: map[string]interface{}{
|
||||
"Stats": "StatQueueProfile2",
|
||||
utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC),
|
||||
"UsageInterval": "1s",
|
||||
"PddInterval": "1s",
|
||||
"Weight": "15.0",
|
||||
utils.Usage: 45 * time.Second,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "event3",
|
||||
Event: map[string]interface{}{
|
||||
"Stats": "StatQueueProfilePrefix",
|
||||
utils.Usage: 30 * time.Second,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
defaultCfg := config.NewDefaultCGRConfig()
|
||||
data := NewInternalDB(nil, nil, true)
|
||||
dmSTS = NewDataManager(data, config.CgrConfig().CacheCfg(), nil)
|
||||
|
||||
defaultCfg.StatSCfg().StoreInterval = 1
|
||||
defaultCfg.StatSCfg().StringIndexedFields = nil
|
||||
defaultCfg.StatSCfg().PrefixIndexedFields = nil
|
||||
statService, err = NewStatService(dmSTS, defaultCfg,
|
||||
&FilterS{dm: dmSTS, cfg: defaultCfg}, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
fltrSts1 := &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_STATS_1",
|
||||
Rules: []*FilterRule{
|
||||
{
|
||||
Type: utils.MetaString,
|
||||
Element: "~*req.Stats",
|
||||
Values: []string{"StatQueueProfile1"},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: "~*req.UsageInterval",
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Usage,
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Weight,
|
||||
Values: []string{"9.0"},
|
||||
},
|
||||
},
|
||||
}
|
||||
dmSTS.SetFilter(fltrSts1, true)
|
||||
fltrSts2 := &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_STATS_2",
|
||||
Rules: []*FilterRule{
|
||||
{
|
||||
Type: utils.MetaString,
|
||||
Element: "~*req.Stats",
|
||||
Values: []string{"StatQueueProfile2"},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: "~*req.PddInterval",
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Usage,
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Weight,
|
||||
Values: []string{"15.0"},
|
||||
},
|
||||
},
|
||||
}
|
||||
dmSTS.SetFilter(fltrSts2, true)
|
||||
fltrSts3 := &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_STATS_3",
|
||||
Rules: []*FilterRule{
|
||||
{
|
||||
Type: utils.MetaPrefix,
|
||||
Element: "~*req.Stats",
|
||||
Values: []string{"StatQueueProfilePrefix"},
|
||||
},
|
||||
},
|
||||
}
|
||||
dmSTS.SetFilter(fltrSts3, true)
|
||||
for _, statQueueProfile := range sqps {
|
||||
dmSTS.SetStatQueueProfile(statQueueProfile, true)
|
||||
}
|
||||
for _, statQueue := range stqs {
|
||||
dmSTS.SetStatQueue(statQueue, nil, 0, nil, 0, true)
|
||||
}
|
||||
//Test each statQueueProfile from cache
|
||||
for _, sqp := range sqps {
|
||||
if tempStat, err := dmSTS.GetStatQueueProfile(sqp.Tenant,
|
||||
sqp.ID, true, false, utils.NonTransactional); err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
} else if !reflect.DeepEqual(sqp, tempStat) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", sqp, tempStat)
|
||||
}
|
||||
}
|
||||
sqPrf := &StatQueueProfile{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatQueueProfile3",
|
||||
|
||||
Reference in New Issue
Block a user