diff --git a/apier/cdrstatsv1_local_test.go b/apier/cdrstatsv1_local_test.go index 66db4c813..773003146 100644 --- a/apier/cdrstatsv1_local_test.go +++ b/apier/cdrstatsv1_local_test.go @@ -140,26 +140,26 @@ func TestCDRStatsLclPostCdrs(t *testing.T) { storedCdrs := []*utils.StoredCdr{ &utils.StoredCdr{CgrId: utils.Sha1("dsafdsafa", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderId: 123, TOR: utils.VOICE, AccId: "dsafdsaf", CdrHost: "192.168.1.1", CdrSource: "test", ReqType: "rated", Direction: "*out", Tenant: "cgrates.org", - Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), - AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), MediationRunId: utils.DEFAULT_RUNID, + Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Now(), + AnswerTime: time.Now(), MediationRunId: utils.DEFAULT_RUNID, Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01, RatedAccount: "dan", RatedSubject: "dan", }, &utils.StoredCdr{CgrId: utils.Sha1("dsafdsafb", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderId: 123, TOR: utils.VOICE, AccId: "dsafdsaf", CdrHost: "192.168.1.1", CdrSource: "test", ReqType: "rated", Direction: "*out", Tenant: "cgrates.org", - Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), - AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), MediationRunId: utils.DEFAULT_RUNID, + Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Now(), + AnswerTime: time.Now(), MediationRunId: utils.DEFAULT_RUNID, Usage: time.Duration(5) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01, RatedAccount: "dan", RatedSubject: "dan", }, &utils.StoredCdr{CgrId: utils.Sha1("dsafdsafc", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderId: 123, TOR: utils.VOICE, AccId: "dsafdsaf", CdrHost: "192.168.1.1", CdrSource: "test", ReqType: "rated", Direction: "*out", Tenant: "cgrates.org", - Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), + Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Now(), AnswerTime: time.Now(), MediationRunId: utils.DEFAULT_RUNID, Usage: time.Duration(30) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01, RatedAccount: "dan", RatedSubject: "dan", }, &utils.StoredCdr{CgrId: utils.Sha1("dsafdsafd", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderId: 123, TOR: utils.VOICE, AccId: "dsafdsaf", CdrHost: "192.168.1.1", CdrSource: "test", ReqType: "rated", Direction: "*out", Tenant: "cgrates.org", - Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), AnswerTime: time.Time{}, + Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Now(), AnswerTime: time.Time{}, MediationRunId: utils.DEFAULT_RUNID, Usage: time.Duration(0) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01, RatedAccount: "dan", RatedSubject: "dan", }, diff --git a/engine/stats_test.go b/engine/stats_test.go index 360d19698..13146a03c 100644 --- a/engine/stats_test.go +++ b/engine/stats_test.go @@ -25,7 +25,7 @@ import ( "github.com/cgrates/cgrates/utils" ) -func TestStatsInit(t *testing.T) { +func TestStatsQueueInit(t *testing.T) { sq := NewStatsQueue(&CdrStats{Metrics: []string{ASR, ACC}}) if len(sq.metrics) != 2 { t.Error("Expected 2 metrics got ", len(sq.metrics)) @@ -180,3 +180,229 @@ func TestAcceptCdr(t *testing.T) { t.Error("Should have accepted thif CDR: %+v", cdr) } } + +func TestStatsQueueIds(t *testing.T) { + cdrStats := NewStats(dataStorage) + ids := []string{} + if err := cdrStats.GetQueueIds(0, &ids); err != nil { + t.Error("Error getting queue ids: ", err) + } + result := len(ids) + expected := 2 + if result != expected { + t.Errorf("Error loading stats queues. Expected %v was %v", expected, result) + } +} + +func TestStatsAppendCdr(t *testing.T) { + cdrStats := NewStats(dataStorage) + cdr := &utils.StoredCdr{ + Tenant: "cgrates.org", + Category: "call", + AnswerTime: time.Now(), + SetupTime: time.Now(), + Usage: 10 * time.Second, + Cost: 10, + } + err := cdrStats.AppendCDR(cdr, nil) + if err != nil { + t.Error("Error appending cdr to stats: ", err) + } + if len(cdrStats.queues["CDRST1"].cdrs) != 0 || + len(cdrStats.queues["CDRST2"].cdrs) != 1 { + t.Error("Error appending cdr to queue: ", len(cdrStats.queues["CDRST2"].cdrs)) + } +} + +func TestStatsGetValues(t *testing.T) { + cdrStats := NewStats(dataStorage) + cdr := &utils.StoredCdr{ + Tenant: "cgrates.org", + Category: "call", + AnswerTime: time.Now(), + SetupTime: time.Now(), + Usage: 10 * time.Second, + Cost: 10, + } + cdrStats.AppendCDR(cdr, nil) + cdr = &utils.StoredCdr{ + Tenant: "cgrates.org", + Category: "call", + AnswerTime: time.Now(), + SetupTime: time.Now(), + Usage: 2 * time.Second, + Cost: 4, + } + cdrStats.AppendCDR(cdr, nil) + valMap := make(map[string]float64) + if err := cdrStats.GetValues("CDRST2", &valMap); err != nil { + t.Error("Error getting metric values: ", err) + } + if len(valMap) != 2 || valMap["ACD"] != 6 || valMap["ASR"] != 100 { + t.Error("Error on metric map: ", valMap) + } +} + +func TestStatsReloadQueues(t *testing.T) { + cdrStats := NewStats(dataStorage) + cdr := &utils.StoredCdr{ + Tenant: "cgrates.org", + Category: "call", + AnswerTime: time.Now(), + SetupTime: time.Now(), + Usage: 10 * time.Second, + Cost: 10, + } + cdrStats.AppendCDR(cdr, nil) + if err := cdrStats.ReloadQueues(nil, nil); err != nil { + t.Error("Error reloading queues: ", err) + } + ids := []string{} + if err := cdrStats.GetQueueIds(0, &ids); err != nil { + t.Error("Error getting queue ids: ", err) + } + result := len(ids) + expected := 2 + if result != expected { + t.Errorf("Error loading stats queues. Expected %v was %v", expected, result) + } + valMap := make(map[string]float64) + if err := cdrStats.GetValues("CDRST2", &valMap); err != nil { + t.Error("Error getting metric values: ", err) + } + if len(valMap) != 2 || valMap["ACD"] != 0 || valMap["ASR"] != 0 { + t.Error("Error on metric map: ", valMap) + } +} + +func TestStatsReloadQueuesWithDefault(t *testing.T) { + cdrStats := NewStats(dataStorage) + cdrStats.AddQueue(&CdrStats{ + Id: utils.META_DEFAULT, + }, nil) + cdr := &utils.StoredCdr{ + Tenant: "cgrates.org", + Category: "call", + AnswerTime: time.Now(), + SetupTime: time.Now(), + Usage: 10 * time.Second, + Cost: 10, + } + cdrStats.AppendCDR(cdr, nil) + + if err := cdrStats.ReloadQueues(nil, nil); err != nil { + t.Error("Error reloading queues: ", err) + } + ids := []string{} + if err := cdrStats.GetQueueIds(0, &ids); err != nil { + t.Error("Error getting queue ids: ", err) + } + result := len(ids) + expected := 3 + if result != expected { + t.Errorf("Error loading stats queues. Expected %v was %v", expected, result) + } + valMap := make(map[string]float64) + if err := cdrStats.GetValues("CDRST2", &valMap); err != nil { + t.Error("Error getting metric values: ", err) + } + if len(valMap) != 2 || valMap["ACD"] != 0 || valMap["ASR"] != 0 { + t.Error("Error on metric map: ", valMap) + } +} + +func TestStatsReloadQueuesWithIds(t *testing.T) { + cdrStats := NewStats(dataStorage) + cdr := &utils.StoredCdr{ + Tenant: "cgrates.org", + Category: "call", + AnswerTime: time.Now(), + SetupTime: time.Now(), + Usage: 10 * time.Second, + Cost: 10, + } + cdrStats.AppendCDR(cdr, nil) + if err := cdrStats.ReloadQueues([]string{"CDRST1"}, nil); err != nil { + t.Error("Error reloading queues: ", err) + } + ids := []string{} + if err := cdrStats.GetQueueIds(0, &ids); err != nil { + t.Error("Error getting queue ids: ", err) + } + result := len(ids) + expected := 2 + if result != expected { + t.Errorf("Error loading stats queues. Expected %v was %v", expected, result) + } + valMap := make(map[string]float64) + if err := cdrStats.GetValues("CDRST2", &valMap); err != nil { + t.Error("Error getting metric values: ", err) + } + if len(valMap) != 2 || valMap["ACD"] != 10 || valMap["ASR"] != 100 { + t.Error("Error on metric map: ", valMap) + } +} + +func TestStatsResetQueues(t *testing.T) { + cdrStats := NewStats(dataStorage) + cdr := &utils.StoredCdr{ + Tenant: "cgrates.org", + Category: "call", + AnswerTime: time.Now(), + SetupTime: time.Now(), + Usage: 10 * time.Second, + Cost: 10, + } + cdrStats.AppendCDR(cdr, nil) + if err := cdrStats.ResetQueues(nil, nil); err != nil { + t.Error("Error reloading queues: ", err) + } + ids := []string{} + if err := cdrStats.GetQueueIds(0, &ids); err != nil { + t.Error("Error getting queue ids: ", err) + } + result := len(ids) + expected := 2 + if result != expected { + t.Errorf("Error loading stats queues. Expected %v was %v", expected, result) + } + valMap := make(map[string]float64) + if err := cdrStats.GetValues("CDRST2", &valMap); err != nil { + t.Error("Error getting metric values: ", err) + } + if len(valMap) != 2 || valMap["ACD"] != 0 || valMap["ASR"] != 0 { + t.Error("Error on metric map: ", valMap) + } +} + +func TestStatsResetQueuesWithIds(t *testing.T) { + cdrStats := NewStats(dataStorage) + cdr := &utils.StoredCdr{ + Tenant: "cgrates.org", + Category: "call", + AnswerTime: time.Now(), + SetupTime: time.Now(), + Usage: 10 * time.Second, + Cost: 10, + } + cdrStats.AppendCDR(cdr, nil) + if err := cdrStats.ResetQueues([]string{"CDRST1"}, nil); err != nil { + t.Error("Error reloading queues: ", err) + } + ids := []string{} + if err := cdrStats.GetQueueIds(0, &ids); err != nil { + t.Error("Error getting queue ids: ", err) + } + result := len(ids) + expected := 2 + if result != expected { + t.Errorf("Error loading stats queues. Expected %v was %v", expected, result) + } + valMap := make(map[string]float64) + if err := cdrStats.GetValues("CDRST2", &valMap); err != nil { + t.Error("Error getting metric values: ", err) + } + if len(valMap) != 2 || valMap["ACD"] != 10 || valMap["ASR"] != 100 { + t.Error("Error on metric map: ", valMap) + } +} diff --git a/engine/storage_map.go b/engine/storage_map.go index 606e7ce83..fdad137d4 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -526,7 +526,7 @@ func (ms *MapStorage) SetCdrStats(cs *CdrStats) error { } func (ms *MapStorage) GetCdrStats(key string) (cs *CdrStats, err error) { - if values, ok := ms.dict[key]; ok { + if values, ok := ms.dict[CDR_STATS_PREFIX+key]; ok { err = ms.ms.Unmarshal(values, &cs) } else { return nil, errors.New(utils.ERR_NOT_FOUND) @@ -539,7 +539,7 @@ func (ms *MapStorage) GetAllCdrStats() (css []*CdrStats, err error) { if !strings.HasPrefix(key, CDR_STATS_PREFIX) { continue } - var cs *CdrStats + cs := &CdrStats{} err = ms.ms.Unmarshal(value, cs) css = append(css, cs) }