mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
more refactorings
This commit is contained in:
@@ -463,7 +463,7 @@ func main() {
|
||||
}
|
||||
|
||||
if cfg.CDRStatsEnabled {
|
||||
cdrStats = NewStats(accountDb)
|
||||
cdrStats = engine.NewStats(accountDb)
|
||||
server.RpcRegister(cdrStats)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user