From 98cdbb6dc0741b90bb23b518de6a67f386bdd248 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 31 Jul 2014 13:17:01 +0300 Subject: [PATCH 1/4] update cdr stats on engine start --- cmd/cgr-engine/cgr-engine.go | 5 +++++ engine/stats.go | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 8d3e86f1a..2736bbbb2 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -464,6 +464,11 @@ func main() { if cfg.CDRStatsEnabled { cdrStats = &engine.Stats{} + if css, err := accountDb.GetAllCdrStats(); err == nil { + cdrStats.UpdateQueues(css, nil) + } else { + engine.Logger.Err(fmt.Sprintf("Cannot load cdr stats: %v", err)) + } server.RpcRegister(cdrStats) } diff --git a/engine/stats.go b/engine/stats.go index 71b1f23a8..093d6f952 100644 --- a/engine/stats.go +++ b/engine/stats.go @@ -101,13 +101,17 @@ func NewProxyStats(addr string) (*ProxyStats, error) { } func (ps *ProxyStats) AddQueue(sq *StatsQueue, out *int) error { - return ps.Client.Call("Scribe.AddQueue", sq, out) + return ps.Client.Call("Stats.AddQueue", sq, out) } func (ps *ProxyStats) GetValues(sqID string, values *map[string]float64) error { - return ps.Client.Call("Scribe.GetValues", sqID, values) + return ps.Client.Call("Stats.GetValues", sqID, values) } func (ps *ProxyStats) AppendCDR(cdr *utils.StoredCdr, out *int) error { - return ps.Client.Call("Scribe.AppendCDR", cdr, out) + return ps.Client.Call("Stats.AppendCDR", cdr, out) } + +/*func (ps *ProxyStats) UpdateQueues(css []*CdrStats, out *int) error { + return ps.Client.Call("Stats.UpdateQueues", css, out) +}*/ From ebc972ed410d6c2b209c7eb68aa8c059278a43c3 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 31 Jul 2014 14:55:47 +0300 Subject: [PATCH 2/4] better stats methods --- cmd/cgr-engine/cgr-engine.go | 7 +------ engine/stats.go | 29 ++++++++++++++++++++--------- engine/stats_queue.go | 13 ++++++++----- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 2736bbbb2..5462a0b42 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -463,12 +463,7 @@ func main() { } if cfg.CDRStatsEnabled { - cdrStats = &engine.Stats{} - if css, err := accountDb.GetAllCdrStats(); err == nil { - cdrStats.UpdateQueues(css, nil) - } else { - engine.Logger.Err(fmt.Sprintf("Cannot load cdr stats: %v", err)) - } + cdrStats = NewStats(accountDb) server.RpcRegister(cdrStats) } diff --git a/engine/stats.go b/engine/stats.go index 093d6f952..e1d347580 100644 --- a/engine/stats.go +++ b/engine/stats.go @@ -20,6 +20,7 @@ package engine import ( "errors" + "fmt" "net/rpc" "sync" @@ -27,7 +28,7 @@ import ( ) type StatsInterface interface { - AddQueue(*StatsQueue, *int) error + AddQueue(*CdrStats, *int) error GetValues(string, *map[string]float64) error AppendCDR(*utils.StoredCdr, *int) error } @@ -37,16 +38,30 @@ type Stats struct { mux sync.RWMutex } -func (s *Stats) AddQueue(sq *StatsQueue, out *int) error { +func (s *Stats) AddQueue(cs *CdrStats, out *int) error { s.mux.Lock() defer s.mux.Unlock() if s.queues == nil { s.queues = make(map[string]*StatsQueue) } - s.queues[sq.conf.Id] = sq + if sq, exists := s.queues[cs.Id]; exists { + sq.UpdateConf(cs) + } else { + s.queues[cs.Id] = NewStatsQueue(cs) + } return nil } +func NewStats(accountDb AccountingStorage) *Stats { + cdrStats := &Stats{} + if css, err := accountDb.GetAllCdrStats(); err == nil { + cdrStats.UpdateQueues(css, nil) + } else { + Logger.Err(fmt.Sprintf("Cannot load cdr stats: %v", err)) + } + return cdrStats +} + func (s *Stats) GetValues(sqID string, values *map[string]float64) error { s.mux.RLock() defer s.mux.RUnlock() @@ -100,8 +115,8 @@ func NewProxyStats(addr string) (*ProxyStats, error) { return &ProxyStats{Client: client}, nil } -func (ps *ProxyStats) AddQueue(sq *StatsQueue, out *int) error { - return ps.Client.Call("Stats.AddQueue", sq, out) +func (ps *ProxyStats) AddQueue(cs *CdrStats, out *int) error { + return ps.Client.Call("Stats.AddQueue", cs, out) } func (ps *ProxyStats) GetValues(sqID string, values *map[string]float64) error { @@ -111,7 +126,3 @@ func (ps *ProxyStats) GetValues(sqID string, values *map[string]float64) error { func (ps *ProxyStats) AppendCDR(cdr *utils.StoredCdr, out *int) error { return ps.Client.Call("Stats.AppendCDR", cdr, out) } - -/*func (ps *ProxyStats) UpdateQueues(css []*CdrStats, out *int) error { - return ps.Client.Call("Stats.UpdateQueues", css, out) -}*/ diff --git a/engine/stats_queue.go b/engine/stats_queue.go index e3a1939b3..1654c87bc 100644 --- a/engine/stats_queue.go +++ b/engine/stats_queue.go @@ -45,17 +45,20 @@ func NewStatsQueue(conf *CdrStats) *StatsQueue { if conf == nil { return &StatsQueue{metrics: make(map[string]Metric)} } - sq := &StatsQueue{ - conf: conf, - metrics: make(map[string]Metric, len(conf.Metrics)), - } + sq := &StatsQueue{} + sq.UpdateConf(conf) + return sq +} + +func (sq *StatsQueue) UpdateConf(conf *CdrStats) { + sq.conf = conf + sq.metrics = make(map[string]Metric, len(conf.Metrics)) for _, m := range conf.Metrics { metric := CreateMetric(m) if metric != nil { sq.metrics[m] = metric } } - return sq } func (sq *StatsQueue) AppendCDR(cdr *utils.StoredCdr) { From d29512de022b5bbfc4a3953c3767be65b5d1d0ef Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 31 Jul 2014 15:00:56 +0300 Subject: [PATCH 3/4] moved AcceptCDR method into CdrStats --- engine/cdrstats.go | 83 ++++++++++++++++++++++++++++++++++++++++++- engine/stats_queue.go | 78 +--------------------------------------- engine/stats_test.go | 40 ++++++++++----------- 3 files changed, 103 insertions(+), 98 deletions(-) diff --git a/engine/cdrstats.go b/engine/cdrstats.go index 27edce584..60d2fcfeb 100644 --- a/engine/cdrstats.go +++ b/engine/cdrstats.go @@ -18,7 +18,12 @@ along with this program. If not, see package engine -import "time" +import ( + "strings" + "time" + + "github.com/cgrates/cgrates/utils" +) type CdrStats struct { Id string // Config id, unique per config instance @@ -43,3 +48,79 @@ type CdrStats struct { CostInterval []float64 // 2 or less items, (>=Cost, 0 { + if cdr.SetupTime.Before(cs.SetupInterval[0]) { + return false + } + if len(cs.SetupInterval) > 1 && (cdr.SetupTime.Equal(cs.SetupInterval[1]) || cdr.SetupTime.After(cs.SetupInterval[1])) { + return false + } + } + if len(cs.TOR) > 0 && !utils.IsSliceMember(cs.TOR, cdr.TOR) { + return false + } + if len(cs.CdrHost) > 0 && !utils.IsSliceMember(cs.CdrHost, cdr.CdrHost) { + return false + } + if len(cs.CdrSource) > 0 && !utils.IsSliceMember(cs.CdrSource, cdr.CdrSource) { + return false + } + if len(cs.ReqType) > 0 && !utils.IsSliceMember(cs.ReqType, cdr.ReqType) { + return false + } + if len(cs.Direction) > 0 && !utils.IsSliceMember(cs.Direction, cdr.Direction) { + return false + } + if len(cs.Tenant) > 0 && !utils.IsSliceMember(cs.Tenant, cdr.Tenant) { + return false + } + if len(cs.Category) > 0 && !utils.IsSliceMember(cs.Category, cdr.Category) { + return false + } + if len(cs.Account) > 0 && !utils.IsSliceMember(cs.Account, cdr.Account) { + return false + } + if len(cs.Subject) > 0 && !utils.IsSliceMember(cs.Subject, cdr.Subject) { + return false + } + if len(cs.DestinationPrefix) > 0 { + found := false + for _, prefix := range cs.DestinationPrefix { + if strings.HasPrefix(cdr.Destination, prefix) { + found = true + break + } + } + if !found { + return false + } + } + if len(cs.UsageInterval) > 0 { + if cdr.Usage < cs.UsageInterval[0] { + return false + } + if len(cs.UsageInterval) > 1 && cdr.Usage >= cs.UsageInterval[1] { + return false + } + } + if len(cs.MediationRunIds) > 0 && !utils.IsSliceMember(cs.MediationRunIds, cdr.MediationRunId) { + return false + } + if len(cs.CostInterval) > 0 { + if cdr.Cost < cs.CostInterval[0] { + return false + } + if len(cs.CostInterval) > 1 && cdr.Cost >= cs.CostInterval[1] { + return false + } + } + if len(cs.RatedAccount) > 0 && !utils.IsSliceMember(cs.RatedAccount, cdr.RatedAccount) { + return false + } + if len(cs.RatedSubject) > 0 && !utils.IsSliceMember(cs.RatedSubject, cdr.RatedSubject) { + return false + } + return true +} diff --git a/engine/stats_queue.go b/engine/stats_queue.go index 1654c87bc..72b911ee5 100644 --- a/engine/stats_queue.go +++ b/engine/stats_queue.go @@ -64,7 +64,7 @@ func (sq *StatsQueue) UpdateConf(conf *CdrStats) { func (sq *StatsQueue) AppendCDR(cdr *utils.StoredCdr) { sq.mux.Lock() defer sq.mux.Unlock() - if sq.acceptCDR(cdr) { + if sq.conf.AcceptCDR(cdr) { qcdr := sq.simplifyCDR(cdr) sq.cdrs = append(sq.cdrs, qcdr) sq.addToMetrics(qcdr) @@ -153,79 +153,3 @@ func (sq *StatsQueue) getStats() map[string]float64 { } return stat } - -func (sq *StatsQueue) acceptCDR(cdr *utils.StoredCdr) bool { - if len(sq.conf.SetupInterval) > 0 { - if cdr.SetupTime.Before(sq.conf.SetupInterval[0]) { - return false - } - if len(sq.conf.SetupInterval) > 1 && (cdr.SetupTime.Equal(sq.conf.SetupInterval[1]) || cdr.SetupTime.After(sq.conf.SetupInterval[1])) { - return false - } - } - if len(sq.conf.TOR) > 0 && !utils.IsSliceMember(sq.conf.TOR, cdr.TOR) { - return false - } - if len(sq.conf.CdrHost) > 0 && !utils.IsSliceMember(sq.conf.CdrHost, cdr.CdrHost) { - return false - } - if len(sq.conf.CdrSource) > 0 && !utils.IsSliceMember(sq.conf.CdrSource, cdr.CdrSource) { - return false - } - if len(sq.conf.ReqType) > 0 && !utils.IsSliceMember(sq.conf.ReqType, cdr.ReqType) { - return false - } - if len(sq.conf.Direction) > 0 && !utils.IsSliceMember(sq.conf.Direction, cdr.Direction) { - return false - } - if len(sq.conf.Tenant) > 0 && !utils.IsSliceMember(sq.conf.Tenant, cdr.Tenant) { - return false - } - if len(sq.conf.Category) > 0 && !utils.IsSliceMember(sq.conf.Category, cdr.Category) { - return false - } - if len(sq.conf.Account) > 0 && !utils.IsSliceMember(sq.conf.Account, cdr.Account) { - return false - } - if len(sq.conf.Subject) > 0 && !utils.IsSliceMember(sq.conf.Subject, cdr.Subject) { - return false - } - if len(sq.conf.DestinationPrefix) > 0 { - found := false - for _, prefix := range sq.conf.DestinationPrefix { - if strings.HasPrefix(cdr.Destination, prefix) { - found = true - break - } - } - if !found { - return false - } - } - if len(sq.conf.UsageInterval) > 0 { - if cdr.Usage < sq.conf.UsageInterval[0] { - return false - } - if len(sq.conf.UsageInterval) > 1 && cdr.Usage >= sq.conf.UsageInterval[1] { - return false - } - } - if len(sq.conf.MediationRunIds) > 0 && !utils.IsSliceMember(sq.conf.MediationRunIds, cdr.MediationRunId) { - return false - } - if len(sq.conf.CostInterval) > 0 { - if cdr.Cost < sq.conf.CostInterval[0] { - return false - } - if len(sq.conf.CostInterval) > 1 && cdr.Cost >= sq.conf.CostInterval[1] { - return false - } - } - if len(sq.conf.RatedAccount) > 0 && !utils.IsSliceMember(sq.conf.RatedAccount, cdr.RatedAccount) { - return false - } - if len(sq.conf.RatedSubject) > 0 && !utils.IsSliceMember(sq.conf.RatedSubject, cdr.RatedSubject) { - return false - } - return true -} diff --git a/engine/stats_test.go b/engine/stats_test.go index 933f43246..e28d2283f 100644 --- a/engine/stats_test.go +++ b/engine/stats_test.go @@ -100,83 +100,83 @@ func TestAcceptCDR(t *testing.T) { Cost: 10, } sq.conf = &CdrStats{} - if sq.acceptCDR(cdr) != true { + if sq.conf.AcceptCDR(cdr) != true { t.Error("Should have accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{TOR: []string{"test"}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{CdrHost: []string{"test"}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{CdrSource: []string{"test"}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{Direction: []string{"test"}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{Tenant: []string{"test"}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{Category: []string{"test"}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{Account: []string{"test"}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{Subject: []string{"test"}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{RatedAccount: []string{"test"}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{RatedSubject: []string{"test"}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{DestinationPrefix: []string{"test"}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{DestinationPrefix: []string{"test", "123"}} - if sq.acceptCDR(cdr) != true { + if sq.conf.AcceptCDR(cdr) != true { t.Error("Should have accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC)}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 42, 0, 0, time.UTC), time.Date(2014, 7, 3, 13, 43, 0, 0, time.UTC)}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 42, 0, 0, time.UTC)}} - if sq.acceptCDR(cdr) != true { + if sq.conf.AcceptCDR(cdr) != true { t.Error("Should have accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 42, 0, 0, time.UTC), time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC)}} - if sq.acceptCDR(cdr) != true { + if sq.conf.AcceptCDR(cdr) != true { t.Error("Should have accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{UsageInterval: []time.Duration{11 * time.Second}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{UsageInterval: []time.Duration{1 * time.Second, 10 * time.Second}} - if sq.acceptCDR(cdr) == true { + if sq.conf.AcceptCDR(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{UsageInterval: []time.Duration{10 * time.Second, 11 * time.Second}} - if sq.acceptCDR(cdr) != true { + if sq.conf.AcceptCDR(cdr) != true { t.Error("Should have accepted thif CDR: %+v", cdr) } } From d6ede3560a710ac35d8decce8323c8e4aa8fd1aa Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 31 Jul 2014 15:05:35 +0300 Subject: [PATCH 4/4] more refactorings --- cmd/cgr-engine/cgr-engine.go | 2 +- engine/cdrstats.go | 2 +- engine/stats_metrics.go | 16 ++++++------- engine/stats_queue.go | 24 ++++++++++---------- engine/stats_test.go | 44 ++++++++++++++++++------------------ 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 5462a0b42..5dc22f7fd 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -463,7 +463,7 @@ func main() { } if cfg.CDRStatsEnabled { - cdrStats = NewStats(accountDb) + cdrStats = engine.NewStats(accountDb) server.RpcRegister(cdrStats) } diff --git a/engine/cdrstats.go b/engine/cdrstats.go index 60d2fcfeb..2b35ccdcc 100644 --- a/engine/cdrstats.go +++ b/engine/cdrstats.go @@ -49,7 +49,7 @@ type CdrStats struct { Triggers ActionTriggerPriotityList } -func (cs *CdrStats) AcceptCDR(cdr *utils.StoredCdr) bool { +func (cs *CdrStats) AcceptCdr(cdr *utils.StoredCdr) bool { if len(cs.SetupInterval) > 0 { if cdr.SetupTime.Before(cs.SetupInterval[0]) { return false diff --git a/engine/stats_metrics.go b/engine/stats_metrics.go index e3633a6c2..007543f8e 100644 --- a/engine/stats_metrics.go +++ b/engine/stats_metrics.go @@ -21,8 +21,8 @@ package engine import "time" type Metric interface { - AddCDR(*QCDR) - RemoveCDR(*QCDR) + AddCdr(*QCdr) + RemoveCdr(*QCdr) GetValue() float64 } @@ -49,14 +49,14 @@ type ASRMetric struct { total float64 } -func (asr *ASRMetric) AddCDR(cdr *QCDR) { +func (asr *ASRMetric) AddCdr(cdr *QCdr) { if !cdr.AnswerTime.IsZero() { asr.answered += 1 } asr.total += 1 } -func (asr *ASRMetric) RemoveCDR(cdr *QCDR) { +func (asr *ASRMetric) RemoveCdr(cdr *QCdr) { if !cdr.AnswerTime.IsZero() { asr.answered -= 1 } @@ -74,14 +74,14 @@ type ACDMetric struct { count float64 } -func (acd *ACDMetric) AddCDR(cdr *QCDR) { +func (acd *ACDMetric) AddCdr(cdr *QCdr) { if !cdr.AnswerTime.IsZero() { acd.sum += cdr.Usage acd.count += 1 } } -func (acd *ACDMetric) RemoveCDR(cdr *QCDR) { +func (acd *ACDMetric) RemoveCdr(cdr *QCdr) { if !cdr.AnswerTime.IsZero() { acd.sum -= cdr.Usage acd.count -= 1 @@ -99,14 +99,14 @@ type ACCMetric struct { count float64 } -func (acc *ACCMetric) AddCDR(cdr *QCDR) { +func (acc *ACCMetric) AddCdr(cdr *QCdr) { if !cdr.AnswerTime.IsZero() && cdr.Cost >= 0 { acc.sum += cdr.Cost acc.count += 1 } } -func (acc *ACCMetric) RemoveCDR(cdr *QCDR) { +func (acc *ACCMetric) RemoveCdr(cdr *QCdr) { if !cdr.AnswerTime.IsZero() && cdr.Cost >= 0 { acc.sum -= cdr.Cost acc.count -= 1 diff --git a/engine/stats_queue.go b/engine/stats_queue.go index 72b911ee5..4f900ffa2 100644 --- a/engine/stats_queue.go +++ b/engine/stats_queue.go @@ -27,14 +27,14 @@ import ( ) type StatsQueue struct { - cdrs []*QCDR + cdrs []*QCdr conf *CdrStats metrics map[string]Metric mux sync.RWMutex } // Simplified cdr structure containing only the necessary info -type QCDR struct { +type QCdr struct { SetupTime time.Time AnswerTime time.Time Usage time.Duration @@ -64,11 +64,11 @@ func (sq *StatsQueue) UpdateConf(conf *CdrStats) { func (sq *StatsQueue) AppendCDR(cdr *utils.StoredCdr) { sq.mux.Lock() defer sq.mux.Unlock() - if sq.conf.AcceptCDR(cdr) { - qcdr := sq.simplifyCDR(cdr) + if sq.conf.AcceptCdr(cdr) { + qcdr := sq.simplifyCdr(cdr) sq.cdrs = append(sq.cdrs, qcdr) sq.addToMetrics(qcdr) - sq.purgeObsoleteCDRs() + sq.purgeObsoleteCdrs() // check for trigger stats := sq.getStats() sq.conf.Triggers.Sort() @@ -94,20 +94,20 @@ func (sq *StatsQueue) AppendCDR(cdr *utils.StoredCdr) { } } -func (sq *StatsQueue) addToMetrics(cdr *QCDR) { +func (sq *StatsQueue) addToMetrics(cdr *QCdr) { for _, metric := range sq.metrics { - metric.AddCDR(cdr) + metric.AddCdr(cdr) } } -func (sq *StatsQueue) removeFromMetrics(cdr *QCDR) { +func (sq *StatsQueue) removeFromMetrics(cdr *QCdr) { for _, metric := range sq.metrics { - metric.RemoveCDR(cdr) + metric.RemoveCdr(cdr) } } -func (sq *StatsQueue) simplifyCDR(cdr *utils.StoredCdr) *QCDR { - return &QCDR{ +func (sq *StatsQueue) simplifyCdr(cdr *utils.StoredCdr) *QCdr { + return &QCdr{ SetupTime: cdr.SetupTime, AnswerTime: cdr.AnswerTime, Usage: cdr.Usage, @@ -115,7 +115,7 @@ func (sq *StatsQueue) simplifyCDR(cdr *utils.StoredCdr) *QCDR { } } -func (sq *StatsQueue) purgeObsoleteCDRs() { +func (sq *StatsQueue) purgeObsoleteCdrs() { if sq.conf.QueueLength > 0 { currentLength := len(sq.cdrs) if currentLength > sq.conf.QueueLength { diff --git a/engine/stats_test.go b/engine/stats_test.go index e28d2283f..360d19698 100644 --- a/engine/stats_test.go +++ b/engine/stats_test.go @@ -71,7 +71,7 @@ func TestStatsSimplifyCDR(t *testing.T) { Cost: 10, } sq := &StatsQueue{} - qcdr := sq.simplifyCDR(cdr) + qcdr := sq.simplifyCdr(cdr) if cdr.SetupTime != qcdr.SetupTime || cdr.AnswerTime != qcdr.AnswerTime || cdr.Usage != qcdr.Usage || @@ -80,7 +80,7 @@ func TestStatsSimplifyCDR(t *testing.T) { } } -func TestAcceptCDR(t *testing.T) { +func TestAcceptCdr(t *testing.T) { sq := NewStatsQueue(nil) cdr := &utils.StoredCdr{ TOR: "tor", @@ -100,83 +100,83 @@ func TestAcceptCDR(t *testing.T) { Cost: 10, } sq.conf = &CdrStats{} - if sq.conf.AcceptCDR(cdr) != true { + if sq.conf.AcceptCdr(cdr) != true { t.Error("Should have accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{TOR: []string{"test"}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{CdrHost: []string{"test"}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{CdrSource: []string{"test"}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{Direction: []string{"test"}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{Tenant: []string{"test"}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{Category: []string{"test"}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{Account: []string{"test"}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{Subject: []string{"test"}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{RatedAccount: []string{"test"}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{RatedSubject: []string{"test"}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{DestinationPrefix: []string{"test"}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{DestinationPrefix: []string{"test", "123"}} - if sq.conf.AcceptCDR(cdr) != true { + if sq.conf.AcceptCdr(cdr) != true { t.Error("Should have accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC)}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 42, 0, 0, time.UTC), time.Date(2014, 7, 3, 13, 43, 0, 0, time.UTC)}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 42, 0, 0, time.UTC)}} - if sq.conf.AcceptCDR(cdr) != true { + if sq.conf.AcceptCdr(cdr) != true { t.Error("Should have accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{SetupInterval: []time.Time{time.Date(2014, 7, 3, 13, 42, 0, 0, time.UTC), time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC)}} - if sq.conf.AcceptCDR(cdr) != true { + if sq.conf.AcceptCdr(cdr) != true { t.Error("Should have accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{UsageInterval: []time.Duration{11 * time.Second}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{UsageInterval: []time.Duration{1 * time.Second, 10 * time.Second}} - if sq.conf.AcceptCDR(cdr) == true { + if sq.conf.AcceptCdr(cdr) == true { t.Error("Should have NOT accepted thif CDR: %+v", cdr) } sq.conf = &CdrStats{UsageInterval: []time.Duration{10 * time.Second, 11 * time.Second}} - if sq.conf.AcceptCDR(cdr) != true { + if sq.conf.AcceptCdr(cdr) != true { t.Error("Should have accepted thif CDR: %+v", cdr) } }