mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-14 20:59:53 +05:00
Added tests for CompressFactor
This commit is contained in:
committed by
Dan Christian Bogos
parent
19d3cf4fd9
commit
33a243d3f4
@@ -85,20 +85,19 @@ func testDMitCRUDStatQueue(t *testing.T) {
|
||||
sq := &StatQueue{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "testDMitCRUDStatQueue",
|
||||
SQItems: []struct {
|
||||
EventID string // Bounded to the original StatEvent
|
||||
ExpiryTime *time.Time // Used to auto-expire events
|
||||
}{{EventID: "cgrates.org:ev1", ExpiryTime: eTime},
|
||||
SQItems: []SQItem{
|
||||
{EventID: "cgrates.org:ev1", ExpiryTime: eTime},
|
||||
{EventID: "cgrates.org:ev2", ExpiryTime: eTime},
|
||||
{EventID: "cgrates.org:ev3", ExpiryTime: eTime}},
|
||||
{EventID: "cgrates.org:ev3", ExpiryTime: eTime},
|
||||
},
|
||||
SQMetrics: map[string]StatMetric{
|
||||
utils.MetaASR: &StatASR{
|
||||
Answered: 2,
|
||||
Count: 3,
|
||||
Events: map[string]*AnsweredWithCompress{
|
||||
"cgrates.org:ev1": &AnsweredWithCompress{Answered: 1},
|
||||
"cgrates.org:ev2": &AnsweredWithCompress{Answered: 1},
|
||||
"cgrates.org:ev3": &AnsweredWithCompress{Answered: 0},
|
||||
Events: map[string]*StatWithCompress{
|
||||
"cgrates.org:ev1": &StatWithCompress{Stat: 1},
|
||||
"cgrates.org:ev2": &StatWithCompress{Stat: 1},
|
||||
"cgrates.org:ev3": &StatWithCompress{Stat: 0},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -56,12 +56,9 @@ type MetricWithFilters struct {
|
||||
// NewStoredStatQueue initiates a StoredStatQueue out of StatQueue
|
||||
func NewStoredStatQueue(sq *StatQueue, ms Marshaler) (sSQ *StoredStatQueue, err error) {
|
||||
sSQ = &StoredStatQueue{
|
||||
Tenant: sq.Tenant,
|
||||
ID: sq.ID,
|
||||
SQItems: make([]struct {
|
||||
EventID string
|
||||
ExpiryTime *time.Time
|
||||
}, len(sq.SQItems)),
|
||||
Tenant: sq.Tenant,
|
||||
ID: sq.ID,
|
||||
SQItems: make([]SQItem, len(sq.SQItems)),
|
||||
SQMetrics: make(map[string][]byte, len(sq.SQMetrics)),
|
||||
MinItems: sq.MinItems,
|
||||
}
|
||||
@@ -80,12 +77,9 @@ func NewStoredStatQueue(sq *StatQueue, ms Marshaler) (sSQ *StoredStatQueue, err
|
||||
|
||||
// StoredStatQueue differs from StatQueue due to serialization of SQMetrics
|
||||
type StoredStatQueue struct {
|
||||
Tenant string
|
||||
ID string
|
||||
SQItems []struct {
|
||||
EventID string // Bounded to the original utils.CGREvent
|
||||
ExpiryTime *time.Time // Used to auto-expire events
|
||||
}
|
||||
Tenant string
|
||||
ID string
|
||||
SQItems []SQItem
|
||||
SQMetrics map[string][]byte
|
||||
MinItems int
|
||||
}
|
||||
@@ -98,12 +92,9 @@ func (ssq *StoredStatQueue) SqID() string {
|
||||
// AsStatQueue converts into StatQueue unmarshaling SQMetrics
|
||||
func (ssq *StoredStatQueue) AsStatQueue(ms Marshaler) (sq *StatQueue, err error) {
|
||||
sq = &StatQueue{
|
||||
Tenant: ssq.Tenant,
|
||||
ID: ssq.ID,
|
||||
SQItems: make([]struct {
|
||||
EventID string
|
||||
ExpiryTime *time.Time
|
||||
}, len(ssq.SQItems)),
|
||||
Tenant: ssq.Tenant,
|
||||
ID: ssq.ID,
|
||||
SQItems: make([]SQItem, len(ssq.SQItems)),
|
||||
SQMetrics: make(map[string]StatMetric, len(ssq.SQMetrics)),
|
||||
MinItems: ssq.MinItems,
|
||||
}
|
||||
@@ -122,20 +113,22 @@ func (ssq *StoredStatQueue) AsStatQueue(ms Marshaler) (sq *StatQueue, err error)
|
||||
return
|
||||
}
|
||||
|
||||
type SQItem struct {
|
||||
EventID string // Bounded to the original utils.CGREvent
|
||||
ExpiryTime *time.Time // Used to auto-expire events
|
||||
}
|
||||
|
||||
// StatQueue represents an individual stats instance
|
||||
type StatQueue struct {
|
||||
sync.RWMutex // protect the elements from within
|
||||
Tenant string
|
||||
ID string
|
||||
SQItems []struct {
|
||||
EventID string // Bounded to the original utils.CGREvent
|
||||
ExpiryTime *time.Time // Used to auto-expire events
|
||||
}
|
||||
SQMetrics map[string]StatMetric
|
||||
MinItems int
|
||||
sqPrfl *StatQueueProfile
|
||||
dirty *bool // needs save
|
||||
ttl *time.Duration // timeToLeave, picked on each init
|
||||
SQItems []SQItem
|
||||
SQMetrics map[string]StatMetric
|
||||
MinItems int
|
||||
sqPrfl *StatQueueProfile
|
||||
dirty *bool // needs save
|
||||
ttl *time.Duration // timeToLeave, picked on each init
|
||||
}
|
||||
|
||||
// SqID will compose the unique identifier for the StatQueue out of Tenant and ID
|
||||
|
||||
@@ -52,9 +52,9 @@ func TestStatRemEventWithID(t *testing.T) {
|
||||
utils.MetaASR: &StatASR{
|
||||
Answered: 1,
|
||||
Count: 2,
|
||||
Events: map[string]*AnsweredWithCompress{
|
||||
"cgrates.org:TestRemEventWithID_1": &AnsweredWithCompress{Answered: 1},
|
||||
"cgrates.org:TestRemEventWithID_2": &AnsweredWithCompress{Answered: 0},
|
||||
Events: map[string]*StatWithCompress{
|
||||
"cgrates.org:TestRemEventWithID_1": &StatWithCompress{Stat: 1},
|
||||
"cgrates.org:TestRemEventWithID_2": &StatWithCompress{Stat: 0},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -95,23 +95,17 @@ func TestStatRemExpired(t *testing.T) {
|
||||
utils.MetaASR: &StatASR{
|
||||
Answered: 2,
|
||||
Count: 3,
|
||||
Events: map[string]*AnsweredWithCompress{
|
||||
"cgrates.org:TestStatRemExpired_1": &AnsweredWithCompress{Answered: 1},
|
||||
"cgrates.org:TestStatRemExpired_2": &AnsweredWithCompress{Answered: 0},
|
||||
"cgrates.org:TestStatRemExpired_3": &AnsweredWithCompress{Answered: 1},
|
||||
Events: map[string]*StatWithCompress{
|
||||
"cgrates.org:TestStatRemExpired_1": &StatWithCompress{Stat: 1},
|
||||
"cgrates.org:TestStatRemExpired_2": &StatWithCompress{Stat: 0},
|
||||
"cgrates.org:TestStatRemExpired_3": &StatWithCompress{Stat: 1},
|
||||
},
|
||||
},
|
||||
},
|
||||
SQItems: []struct {
|
||||
EventID string
|
||||
ExpiryTime *time.Time
|
||||
}{
|
||||
struct {
|
||||
EventID string // Bounded to the original StatEvent
|
||||
ExpiryTime *time.Time // Used to auto-expire events
|
||||
}{"cgrates.org:TestStatRemExpired_1", utils.TimePointer(time.Now())},
|
||||
{"cgrates.org:TestStatRemExpired_2", utils.TimePointer(time.Now())},
|
||||
{"cgrates.org:TestStatRemExpired_3", utils.TimePointer(time.Now().Add(time.Duration(time.Minute)))},
|
||||
SQItems: []SQItem{
|
||||
SQItem{"cgrates.org:TestStatRemExpired_1", utils.TimePointer(time.Now())},
|
||||
SQItem{"cgrates.org:TestStatRemExpired_2", utils.TimePointer(time.Now())},
|
||||
SQItem{"cgrates.org:TestStatRemExpired_3", utils.TimePointer(time.Now().Add(time.Duration(time.Minute)))},
|
||||
},
|
||||
}
|
||||
asrMetric := sq.SQMetrics[utils.MetaASR].(*StatASR)
|
||||
@@ -134,10 +128,7 @@ func TestStatRemOnQueueLength(t *testing.T) {
|
||||
sqPrfl: &StatQueueProfile{
|
||||
QueueLength: 2,
|
||||
},
|
||||
SQItems: []struct {
|
||||
EventID string
|
||||
ExpiryTime *time.Time
|
||||
}{
|
||||
SQItems: []SQItem{
|
||||
{"cgrates.org:TestStatRemExpired_1", nil},
|
||||
},
|
||||
}
|
||||
@@ -145,10 +136,7 @@ func TestStatRemOnQueueLength(t *testing.T) {
|
||||
if len(sq.SQItems) != 1 {
|
||||
t.Errorf("wrong items: %+v", sq.SQItems)
|
||||
}
|
||||
sq.SQItems = []struct {
|
||||
EventID string
|
||||
ExpiryTime *time.Time
|
||||
}{
|
||||
sq.SQItems = []SQItem{
|
||||
{"cgrates.org:TestStatRemExpired_1", nil},
|
||||
{"cgrates.org:TestStatRemExpired_2", nil},
|
||||
}
|
||||
@@ -159,10 +147,7 @@ func TestStatRemOnQueueLength(t *testing.T) {
|
||||
t.Errorf("wrong item in SQItems: %+v", sq.SQItems[0])
|
||||
}
|
||||
sq.sqPrfl.QueueLength = -1
|
||||
sq.SQItems = []struct {
|
||||
EventID string
|
||||
ExpiryTime *time.Time
|
||||
}{
|
||||
sq.SQItems = []SQItem{
|
||||
{"cgrates.org:TestStatRemExpired_1", nil},
|
||||
{"cgrates.org:TestStatRemExpired_2", nil},
|
||||
{"cgrates.org:TestStatRemExpired_3", nil},
|
||||
@@ -179,8 +164,8 @@ func TestStatAddStatEvent(t *testing.T) {
|
||||
utils.MetaASR: &StatASR{
|
||||
Answered: 1,
|
||||
Count: 1,
|
||||
Events: map[string]*AnsweredWithCompress{
|
||||
"cgrates.org:TestStatRemExpired_1": &AnsweredWithCompress{Answered: 1},
|
||||
Events: map[string]*StatWithCompress{
|
||||
"cgrates.org:TestStatRemExpired_1": &StatWithCompress{Stat: 1},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -1452,20 +1452,19 @@ func testOnStorITStatQueue(t *testing.T) {
|
||||
sq := &StatQueue{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "Test_StatQueue",
|
||||
SQItems: []struct {
|
||||
EventID string // Bounded to the original StatEvent
|
||||
ExpiryTime *time.Time // Used to auto-expire events
|
||||
}{{EventID: "cgrates.org:ev1", ExpiryTime: eTime},
|
||||
SQItems: []SQItem{
|
||||
{EventID: "cgrates.org:ev1", ExpiryTime: eTime},
|
||||
{EventID: "cgrates.org:ev2", ExpiryTime: eTime},
|
||||
{EventID: "cgrates.org:ev3", ExpiryTime: eTime}},
|
||||
{EventID: "cgrates.org:ev3", ExpiryTime: eTime},
|
||||
},
|
||||
SQMetrics: map[string]StatMetric{
|
||||
utils.MetaASR: &StatASR{
|
||||
Answered: 2,
|
||||
Count: 3,
|
||||
Events: map[string]*AnsweredWithCompress{
|
||||
"cgrates.org:ev1": &AnsweredWithCompress{Answered: 1},
|
||||
"cgrates.org:ev2": &AnsweredWithCompress{Answered: 1},
|
||||
"cgrates.org:ev3": &AnsweredWithCompress{Answered: 0},
|
||||
Events: map[string]*StatWithCompress{
|
||||
"cgrates.org:ev1": &StatWithCompress{Stat: 1},
|
||||
"cgrates.org:ev2": &StatWithCompress{Stat: 1},
|
||||
"cgrates.org:ev3": &StatWithCompress{Stat: 0},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -1495,10 +1494,10 @@ func testOnStorITStatQueue(t *testing.T) {
|
||||
utils.MetaASR: &StatASR{
|
||||
Answered: 3,
|
||||
Count: 3,
|
||||
Events: map[string]*AnsweredWithCompress{
|
||||
"cgrates.org:ev1": &AnsweredWithCompress{Answered: 1},
|
||||
"cgrates.org:ev2": &AnsweredWithCompress{Answered: 1},
|
||||
"cgrates.org:ev3": &AnsweredWithCompress{Answered: 1},
|
||||
Events: map[string]*StatWithCompress{
|
||||
"cgrates.org:ev1": &StatWithCompress{Stat: 1},
|
||||
"cgrates.org:ev2": &StatWithCompress{Stat: 1},
|
||||
"cgrates.org:ev3": &StatWithCompress{Stat: 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -30,6 +30,18 @@ import (
|
||||
//to be moved in utils
|
||||
const STATS_NA = -1.0
|
||||
|
||||
// ACDHelper structure
|
||||
type DurationWithCompress struct {
|
||||
Duration time.Duration
|
||||
CompressFactor int
|
||||
}
|
||||
|
||||
// ACDHelper structure
|
||||
type StatWithCompress struct {
|
||||
Stat float64
|
||||
CompressFactor int
|
||||
}
|
||||
|
||||
// NewStatMetric instantiates the StatMetric
|
||||
// cfg serves as general purpose container to pass config options to metric
|
||||
func NewStatMetric(metricID string, minItems int, filterIDs []string) (sm StatMetric, err error) {
|
||||
@@ -71,22 +83,16 @@ type StatMetric interface {
|
||||
}
|
||||
|
||||
func NewASR(minItems int, extraParams string, filterIDs []string) (StatMetric, error) {
|
||||
return &StatASR{Events: make(map[string]*AnsweredWithCompress),
|
||||
return &StatASR{Events: make(map[string]*StatWithCompress),
|
||||
MinItems: minItems, FilterIDs: filterIDs}, nil
|
||||
}
|
||||
|
||||
// ASRHelper structure
|
||||
type AnsweredWithCompress struct {
|
||||
Answered float64
|
||||
CompressFactor int
|
||||
}
|
||||
|
||||
// ASR implements AverageSuccessRatio metric
|
||||
type StatASR struct {
|
||||
FilterIDs []string
|
||||
Answered float64
|
||||
Count int64
|
||||
Events map[string]*AnsweredWithCompress // map[EventTenantID]Answered
|
||||
Events map[string]*StatWithCompress // map[EventTenantID]Answered
|
||||
MinItems int
|
||||
val *float64 // cached ASR value
|
||||
}
|
||||
@@ -135,9 +141,9 @@ func (asr *StatASR) AddEvent(ev *utils.CGREvent) (err error) {
|
||||
}
|
||||
|
||||
if val, has := asr.Events[ev.ID]; !has {
|
||||
asr.Events[ev.ID] = &AnsweredWithCompress{Answered: float64(answered)}
|
||||
asr.Events[ev.ID] = &StatWithCompress{Stat: float64(answered), CompressFactor: 1}
|
||||
} else {
|
||||
val.Answered = (val.Answered*float64(val.CompressFactor) + float64(answered)) / float64(val.CompressFactor)
|
||||
val.Stat = (val.Stat*float64(val.CompressFactor) + float64(answered)) / float64(val.CompressFactor+1)
|
||||
val.CompressFactor = val.CompressFactor + 1
|
||||
}
|
||||
asr.Count += 1
|
||||
@@ -154,15 +160,15 @@ func (asr *StatASR) RemEvent(evID string) (err error) {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
ans := 0
|
||||
if val.Answered >= 0.5 {
|
||||
if val.Stat > 0.5 {
|
||||
ans = 1
|
||||
asr.Answered -= 1
|
||||
}
|
||||
asr.Count -= 1
|
||||
if val.CompressFactor <= 0 {
|
||||
if val.CompressFactor <= 1 {
|
||||
delete(asr.Events, evID)
|
||||
} else {
|
||||
val.Answered = (val.Answered*float64(val.CompressFactor) - float64(ans)) / (float64(val.CompressFactor - 1))
|
||||
val.Stat = (val.Stat*float64(val.CompressFactor) - float64(ans)) / (float64(val.CompressFactor - 1))
|
||||
val.CompressFactor = val.CompressFactor - 1
|
||||
}
|
||||
asr.val = nil
|
||||
@@ -188,12 +194,6 @@ func NewACD(minItems int, extraParams string, filterIDs []string) (StatMetric, e
|
||||
return &StatACD{Events: make(map[string]*DurationWithCompress), MinItems: minItems, FilterIDs: filterIDs}, nil
|
||||
}
|
||||
|
||||
// ACDHelper structure
|
||||
type DurationWithCompress struct {
|
||||
Duration time.Duration
|
||||
CompressFactor int
|
||||
}
|
||||
|
||||
// ACD implements AverageCallDuration metric
|
||||
type StatACD struct {
|
||||
FilterIDs []string
|
||||
@@ -248,7 +248,7 @@ func (acd *StatACD) AddEvent(ev *utils.CGREvent) (err error) {
|
||||
}
|
||||
acd.Sum += dur
|
||||
if val, has := acd.Events[ev.ID]; !has {
|
||||
acd.Events[ev.ID] = &DurationWithCompress{Duration: dur}
|
||||
acd.Events[ev.ID] = &DurationWithCompress{Duration: dur, CompressFactor: 1}
|
||||
} else {
|
||||
val.Duration = time.Duration((float64(val.Duration.Nanoseconds())*float64(val.CompressFactor) + float64(dur.Nanoseconds())) / float64(val.CompressFactor+1))
|
||||
val.CompressFactor = val.CompressFactor + 1
|
||||
@@ -267,7 +267,7 @@ func (acd *StatACD) RemEvent(evID string) (err error) {
|
||||
acd.Sum -= val.Duration
|
||||
}
|
||||
acd.Count -= 1
|
||||
if val.CompressFactor <= 0 {
|
||||
if val.CompressFactor <= 1 {
|
||||
delete(acd.Events, evID)
|
||||
} else {
|
||||
val.CompressFactor = val.CompressFactor - 1
|
||||
@@ -346,7 +346,7 @@ func (tcd *StatTCD) AddEvent(ev *utils.CGREvent) (err error) {
|
||||
}
|
||||
tcd.Sum += dur
|
||||
if val, has := tcd.Events[ev.ID]; !has {
|
||||
tcd.Events[ev.ID] = &DurationWithCompress{Duration: dur}
|
||||
tcd.Events[ev.ID] = &DurationWithCompress{Duration: dur, CompressFactor: 1}
|
||||
} else {
|
||||
val.Duration = time.Duration((float64(val.Duration.Nanoseconds())*float64(val.CompressFactor) + float64(dur.Nanoseconds())) / float64(val.CompressFactor+1))
|
||||
val.CompressFactor = val.CompressFactor + 1
|
||||
@@ -365,7 +365,7 @@ func (tcd *StatTCD) RemEvent(evID string) (err error) {
|
||||
tcd.Sum -= val.Duration
|
||||
}
|
||||
tcd.Count -= 1
|
||||
if val.CompressFactor <= 0 {
|
||||
if val.CompressFactor <= 1 {
|
||||
delete(tcd.Events, evID)
|
||||
} else {
|
||||
val.CompressFactor = val.CompressFactor - 1
|
||||
@@ -391,12 +391,6 @@ func NewACC(minItems int, extraParams string, filterIDs []string) (StatMetric, e
|
||||
return &StatACC{Events: make(map[string]*StatWithCompress), MinItems: minItems, FilterIDs: filterIDs}, nil
|
||||
}
|
||||
|
||||
// ACDHelper structure
|
||||
type StatWithCompress struct {
|
||||
Stat float64
|
||||
CompressFactor int
|
||||
}
|
||||
|
||||
// ACC implements AverageCallCost metric
|
||||
type StatACC struct {
|
||||
FilterIDs []string
|
||||
@@ -448,7 +442,7 @@ func (acc *StatACC) AddEvent(ev *utils.CGREvent) (err error) {
|
||||
}
|
||||
acc.Sum += cost
|
||||
if val, has := acc.Events[ev.ID]; !has {
|
||||
acc.Events[ev.ID] = &StatWithCompress{Stat: cost}
|
||||
acc.Events[ev.ID] = &StatWithCompress{Stat: cost, CompressFactor: 1}
|
||||
} else {
|
||||
val.Stat = (val.Stat*float64(val.CompressFactor) + cost) / float64(val.CompressFactor+1)
|
||||
val.CompressFactor = val.CompressFactor + 1
|
||||
@@ -465,7 +459,7 @@ func (acc *StatACC) RemEvent(evID string) (err error) {
|
||||
}
|
||||
acc.Sum -= cost.Stat
|
||||
acc.Count -= 1
|
||||
if cost.CompressFactor <= 0 {
|
||||
if cost.CompressFactor <= 1 {
|
||||
delete(acc.Events, evID)
|
||||
} else {
|
||||
cost.CompressFactor = cost.CompressFactor - 1
|
||||
@@ -542,7 +536,7 @@ func (tcc *StatTCC) AddEvent(ev *utils.CGREvent) (err error) {
|
||||
}
|
||||
tcc.Sum += cost
|
||||
if val, has := tcc.Events[ev.ID]; !has {
|
||||
tcc.Events[ev.ID] = &StatWithCompress{Stat: cost}
|
||||
tcc.Events[ev.ID] = &StatWithCompress{Stat: cost, CompressFactor: 1}
|
||||
} else {
|
||||
val.Stat = (val.Stat*float64(val.CompressFactor) + cost) / float64(val.CompressFactor+1)
|
||||
val.CompressFactor = val.CompressFactor + 1
|
||||
@@ -561,7 +555,7 @@ func (tcc *StatTCC) RemEvent(evID string) (err error) {
|
||||
tcc.Sum -= cost.Stat
|
||||
}
|
||||
tcc.Count -= 1
|
||||
if cost.CompressFactor <= 0 {
|
||||
if cost.CompressFactor <= 1 {
|
||||
delete(tcc.Events, evID)
|
||||
} else {
|
||||
cost.CompressFactor = cost.CompressFactor - 1
|
||||
@@ -641,7 +635,7 @@ func (pdd *StatPDD) AddEvent(ev *utils.CGREvent) (err error) {
|
||||
}
|
||||
pdd.Sum += dur
|
||||
if val, has := pdd.Events[ev.ID]; !has {
|
||||
pdd.Events[ev.ID] = &DurationWithCompress{Duration: dur}
|
||||
pdd.Events[ev.ID] = &DurationWithCompress{Duration: dur, CompressFactor: 1}
|
||||
} else {
|
||||
val.Duration = time.Duration((float64(val.Duration.Nanoseconds())*float64(val.CompressFactor) + float64(dur.Nanoseconds())) / float64(val.CompressFactor+1))
|
||||
val.CompressFactor = val.CompressFactor + 1
|
||||
@@ -660,7 +654,7 @@ func (pdd *StatPDD) RemEvent(evID string) (err error) {
|
||||
pdd.Sum -= val.Duration
|
||||
}
|
||||
pdd.Count -= 1
|
||||
if val.CompressFactor <= 0 {
|
||||
if val.CompressFactor <= 1 {
|
||||
delete(pdd.Events, evID)
|
||||
} else {
|
||||
val.CompressFactor = val.CompressFactor - 1
|
||||
@@ -812,7 +806,7 @@ func (sum *StatSum) AddEvent(ev *utils.CGREvent) (err error) {
|
||||
}
|
||||
sum.Sum += val
|
||||
if v, has := sum.Events[ev.ID]; !has {
|
||||
sum.Events[ev.ID] = &StatWithCompress{Stat: val}
|
||||
sum.Events[ev.ID] = &StatWithCompress{Stat: val, CompressFactor: 1}
|
||||
} else {
|
||||
v.Stat = (v.Stat*float64(v.CompressFactor) + val) / float64(v.CompressFactor+1)
|
||||
v.CompressFactor = v.CompressFactor + 1
|
||||
@@ -831,7 +825,7 @@ func (sum *StatSum) RemEvent(evID string) (err error) {
|
||||
sum.Sum -= val.Stat
|
||||
}
|
||||
sum.Count -= 1
|
||||
if val.CompressFactor <= 0 {
|
||||
if val.CompressFactor <= 1 {
|
||||
delete(sum.Events, evID)
|
||||
} else {
|
||||
val.CompressFactor = val.CompressFactor - 1
|
||||
@@ -910,7 +904,7 @@ func (avg *StatAverage) AddEvent(ev *utils.CGREvent) (err error) {
|
||||
}
|
||||
avg.Sum += val
|
||||
if v, has := avg.Events[ev.ID]; !has {
|
||||
avg.Events[ev.ID] = &StatWithCompress{Stat: val}
|
||||
avg.Events[ev.ID] = &StatWithCompress{Stat: val, CompressFactor: 1}
|
||||
} else {
|
||||
v.Stat = (v.Stat*float64(v.CompressFactor) + val) / float64(v.CompressFactor+1)
|
||||
v.CompressFactor = v.CompressFactor + 1
|
||||
@@ -929,7 +923,7 @@ func (avg *StatAverage) RemEvent(evID string) (err error) {
|
||||
avg.Sum -= val.Stat
|
||||
}
|
||||
avg.Count -= 1
|
||||
if val.CompressFactor <= 0 {
|
||||
if val.CompressFactor <= 1 {
|
||||
delete(avg.Events, evID)
|
||||
} else {
|
||||
val.CompressFactor = val.CompressFactor - 1
|
||||
|
||||
@@ -74,6 +74,101 @@ func TestASRGetStringValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestASRGetStringValue2(t *testing.T) {
|
||||
asr, _ := NewASR(2, "", []string{})
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{
|
||||
"AnswerTime": time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC)}}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_2"}
|
||||
ev4 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1"}
|
||||
asr.AddEvent(ev)
|
||||
asr.AddEvent(ev2)
|
||||
if strVal := asr.GetStringValue(""); strVal != "50%" {
|
||||
t.Errorf("wrong asr value: %s", strVal)
|
||||
}
|
||||
asr.AddEvent(ev2)
|
||||
if strVal := asr.GetStringValue(""); strVal != "33.33333%" {
|
||||
t.Errorf("wrong asr value: %s", strVal)
|
||||
}
|
||||
asr.AddEvent(ev4)
|
||||
if strVal := asr.GetStringValue(""); strVal != "25%" {
|
||||
t.Errorf("wrong asr value: %s", strVal)
|
||||
}
|
||||
asr.RemEvent(ev4.ID)
|
||||
asr.RemEvent(ev2.ID)
|
||||
if strVal := asr.GetStringValue(""); strVal != "50%" {
|
||||
t.Errorf("wrong asr value: %s", strVal)
|
||||
}
|
||||
asr.RemEvent(ev2.ID)
|
||||
asr.AddEvent(ev)
|
||||
if strVal := asr.GetStringValue(""); strVal != "100%" {
|
||||
t.Errorf("wrong asr value: %s", strVal)
|
||||
}
|
||||
}
|
||||
|
||||
func TestASRGetStringValue3(t *testing.T) {
|
||||
asr := &StatASR{Events: make(map[string]*StatWithCompress),
|
||||
MinItems: 2, FilterIDs: []string{}}
|
||||
expected := &StatASR{
|
||||
Events: map[string]*StatWithCompress{
|
||||
"EVENT_1": &StatWithCompress{Stat: 1, CompressFactor: 1},
|
||||
"EVENT_2": &StatWithCompress{Stat: 0, CompressFactor: 1},
|
||||
},
|
||||
MinItems: 2,
|
||||
FilterIDs: []string{},
|
||||
Answered: 1,
|
||||
Count: 2,
|
||||
}
|
||||
expected.GetStringValue("")
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{
|
||||
"AnswerTime": time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC)}}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_2"}
|
||||
ev4 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1"}
|
||||
asr.AddEvent(ev)
|
||||
asr.AddEvent(ev2)
|
||||
if strVal := asr.GetStringValue(""); strVal != "50%" {
|
||||
t.Errorf("wrong asr value: %s", strVal)
|
||||
}
|
||||
if !reflect.DeepEqual(*expected, *asr) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(asr))
|
||||
}
|
||||
asr.AddEvent(ev2)
|
||||
asr.AddEvent(ev4)
|
||||
v := expected.Events["EVENT_1"]
|
||||
v.Stat = 0.5
|
||||
v.CompressFactor = 2
|
||||
v = expected.Events["EVENT_2"]
|
||||
v.Stat = 0
|
||||
v.CompressFactor = 2
|
||||
expected.Count = 4
|
||||
expected.val = nil
|
||||
expected.GetStringValue("")
|
||||
if strVal := asr.GetStringValue(""); strVal != "25%" {
|
||||
t.Errorf("wrong asr value: %s", strVal)
|
||||
}
|
||||
if !reflect.DeepEqual(*expected, *asr) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(asr))
|
||||
}
|
||||
asr.RemEvent(ev4.ID)
|
||||
asr.RemEvent(ev2.ID)
|
||||
v = expected.Events["EVENT_1"]
|
||||
v.Stat = 1
|
||||
v.CompressFactor = 1
|
||||
v = expected.Events["EVENT_2"]
|
||||
v.Stat = 0
|
||||
v.CompressFactor = 1
|
||||
expected.Count = 2
|
||||
expected.val = nil
|
||||
expected.GetStringValue("")
|
||||
if strVal := asr.GetStringValue(""); strVal != "50%" {
|
||||
t.Errorf("wrong asr value: %s", strVal)
|
||||
}
|
||||
if !reflect.DeepEqual(*expected, *asr) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(asr))
|
||||
}
|
||||
}
|
||||
|
||||
func TestASRGetValue(t *testing.T) {
|
||||
asr, _ := NewASR(2, "", []string{})
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
@@ -186,6 +281,75 @@ func TestACDGetStringValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestACDGetStringValue2(t *testing.T) {
|
||||
acd, _ := NewACD(2, "", []string{})
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{utils.Usage: time.Duration(2 * time.Minute)}}
|
||||
if err := acd.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_2",
|
||||
Event: map[string]interface{}{"Usage": time.Duration(1 * time.Minute)}}
|
||||
acd.AddEvent(ev2)
|
||||
if strVal := acd.GetStringValue(""); strVal != "1m30s" {
|
||||
t.Errorf("wrong acd value: %s", strVal)
|
||||
}
|
||||
acd.AddEvent(ev2)
|
||||
acd.AddEvent(ev2)
|
||||
if strVal := acd.GetStringValue(""); strVal != "1m15s" {
|
||||
t.Errorf("wrong acd value: %s", strVal)
|
||||
}
|
||||
acd.RemEvent(ev2.ID)
|
||||
if strVal := acd.GetStringValue(""); strVal != "1m20s" {
|
||||
t.Errorf("wrong acd value: %s", strVal)
|
||||
}
|
||||
}
|
||||
|
||||
func TestACDGetStringValue3(t *testing.T) {
|
||||
acd := &StatACD{Events: make(map[string]*DurationWithCompress), MinItems: 2, FilterIDs: []string{}}
|
||||
expected := &StatACD{
|
||||
Events: map[string]*DurationWithCompress{
|
||||
"EVENT_1": &DurationWithCompress{Duration: 2*time.Minute + 30*time.Second, CompressFactor: 2},
|
||||
"EVENT_3": &DurationWithCompress{Duration: time.Minute, CompressFactor: 1},
|
||||
},
|
||||
MinItems: 2,
|
||||
FilterIDs: []string{},
|
||||
Count: 3,
|
||||
Sum: 6 * time.Minute,
|
||||
}
|
||||
expected.GetStringValue("")
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{utils.Usage: time.Duration(2 * time.Minute)}}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{utils.Usage: time.Duration(3 * time.Minute)}}
|
||||
ev3 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_3",
|
||||
Event: map[string]interface{}{"Usage": time.Duration(1 * time.Minute)}}
|
||||
if err := acd.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := acd.AddEvent(ev2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := acd.AddEvent(ev3); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
acd.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *acd) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(acd))
|
||||
}
|
||||
v := expected.Events[ev1.ID]
|
||||
v.CompressFactor = 1
|
||||
expected.Count = 2
|
||||
expected.Sum = 3*time.Minute + 30*time.Second
|
||||
expected.val = nil
|
||||
expected.GetStringValue("")
|
||||
acd.RemEvent(ev1.ID)
|
||||
acd.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *acd) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(acd))
|
||||
}
|
||||
}
|
||||
|
||||
func TestACDGetFloat64Value(t *testing.T) {
|
||||
acd, _ := NewACD(2, "", []string{})
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
@@ -356,6 +520,75 @@ func TestTCDGetStringValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCDGetStringValue2(t *testing.T) {
|
||||
tcd, _ := NewTCD(2, "", []string{})
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{utils.Usage: time.Duration(2 * time.Minute)}}
|
||||
if err := tcd.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_2",
|
||||
Event: map[string]interface{}{"Usage": time.Duration(1 * time.Minute)}}
|
||||
tcd.AddEvent(ev2)
|
||||
if strVal := tcd.GetStringValue(""); strVal != "3m0s" {
|
||||
t.Errorf("wrong tcd value: %s", strVal)
|
||||
}
|
||||
tcd.AddEvent(ev2)
|
||||
tcd.AddEvent(ev2)
|
||||
if strVal := tcd.GetStringValue(""); strVal != "5m0s" {
|
||||
t.Errorf("wrong tcd value: %s", strVal)
|
||||
}
|
||||
tcd.RemEvent(ev2.ID)
|
||||
if strVal := tcd.GetStringValue(""); strVal != "4m0s" {
|
||||
t.Errorf("wrong tcd value: %s", strVal)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCDGetStringValue3(t *testing.T) {
|
||||
tcd := &StatTCD{Events: make(map[string]*DurationWithCompress), MinItems: 2, FilterIDs: []string{}}
|
||||
expected := &StatTCD{
|
||||
Events: map[string]*DurationWithCompress{
|
||||
"EVENT_1": &DurationWithCompress{Duration: 2*time.Minute + 30*time.Second, CompressFactor: 2},
|
||||
"EVENT_3": &DurationWithCompress{Duration: time.Minute, CompressFactor: 1},
|
||||
},
|
||||
MinItems: 2,
|
||||
FilterIDs: []string{},
|
||||
Count: 3,
|
||||
Sum: 6 * time.Minute,
|
||||
}
|
||||
expected.GetStringValue("")
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{utils.Usage: time.Duration(2 * time.Minute)}}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{utils.Usage: time.Duration(3 * time.Minute)}}
|
||||
ev3 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_3",
|
||||
Event: map[string]interface{}{"Usage": time.Duration(1 * time.Minute)}}
|
||||
if err := tcd.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := tcd.AddEvent(ev2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := tcd.AddEvent(ev3); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tcd.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *tcd) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(tcd))
|
||||
}
|
||||
v := expected.Events[ev1.ID]
|
||||
v.CompressFactor = 1
|
||||
expected.Count = 2
|
||||
expected.Sum = 3*time.Minute + 30*time.Second
|
||||
expected.val = nil
|
||||
expected.GetStringValue("")
|
||||
tcd.RemEvent(ev1.ID)
|
||||
tcd.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *tcd) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(tcd))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCDGetFloat64Value(t *testing.T) {
|
||||
tcd, _ := NewTCD(2, "", []string{})
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
@@ -517,6 +750,75 @@ func TestACCGetStringValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestACCGetStringValue2(t *testing.T) {
|
||||
acc, _ := NewACC(2, "", []string{})
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 12.3}}
|
||||
if err := acc.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_2",
|
||||
Event: map[string]interface{}{"Cost": 18.3}}
|
||||
acc.AddEvent(ev2)
|
||||
if strVal := acc.GetStringValue(""); strVal != "15.3" {
|
||||
t.Errorf("wrong acc value: %s", strVal)
|
||||
}
|
||||
acc.AddEvent(ev2)
|
||||
acc.AddEvent(ev2)
|
||||
if strVal := acc.GetStringValue(""); strVal != "16.8" {
|
||||
t.Errorf("wrong acc value: %s", strVal)
|
||||
}
|
||||
acc.RemEvent(ev2.ID)
|
||||
if strVal := acc.GetStringValue(""); strVal != "16.3" {
|
||||
t.Errorf("wrong acc value: %s", strVal)
|
||||
}
|
||||
}
|
||||
|
||||
func TestACCGetStringValue3(t *testing.T) {
|
||||
acc := &StatACC{Events: make(map[string]*StatWithCompress), MinItems: 2, FilterIDs: []string{}}
|
||||
expected := &StatACC{
|
||||
Events: map[string]*StatWithCompress{
|
||||
"EVENT_1": &StatWithCompress{Stat: 12.2, CompressFactor: 2},
|
||||
"EVENT_3": &StatWithCompress{Stat: 18.3, CompressFactor: 1},
|
||||
},
|
||||
MinItems: 2,
|
||||
FilterIDs: []string{},
|
||||
Count: 3,
|
||||
Sum: 42.7,
|
||||
}
|
||||
expected.GetStringValue("")
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 18.2}}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 6.2}}
|
||||
ev3 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_3",
|
||||
Event: map[string]interface{}{"Cost": 18.3}}
|
||||
if err := acc.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := acc.AddEvent(ev2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := acc.AddEvent(ev3); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
acc.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *acc) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(acc))
|
||||
}
|
||||
v := expected.Events[ev1.ID]
|
||||
v.CompressFactor = 1
|
||||
expected.Count = 2
|
||||
expected.Sum = expected.Sum - 12.2
|
||||
expected.val = nil
|
||||
expected.GetStringValue("")
|
||||
acc.RemEvent(ev1.ID)
|
||||
acc.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *acc) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(acc))
|
||||
}
|
||||
}
|
||||
|
||||
func TestACCGetValue(t *testing.T) {
|
||||
acc, _ := NewACC(2, "", []string{})
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
@@ -618,6 +920,75 @@ func TestTCCGetStringValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCCGetStringValue2(t *testing.T) {
|
||||
tcc, _ := NewTCC(2, "", []string{})
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 12.3}}
|
||||
if err := tcc.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_2",
|
||||
Event: map[string]interface{}{"Cost": 18.3}}
|
||||
tcc.AddEvent(ev2)
|
||||
if strVal := tcc.GetStringValue(""); strVal != "30.6" {
|
||||
t.Errorf("wrong tcc value: %s", strVal)
|
||||
}
|
||||
tcc.AddEvent(ev2)
|
||||
tcc.AddEvent(ev2)
|
||||
if strVal := tcc.GetStringValue(""); strVal != "67.2" {
|
||||
t.Errorf("wrong tcc value: %s", strVal)
|
||||
}
|
||||
tcc.RemEvent(ev2.ID)
|
||||
if strVal := tcc.GetStringValue(""); strVal != "48.9" {
|
||||
t.Errorf("wrong tcc value: %s", strVal)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCCGetStringValue3(t *testing.T) {
|
||||
tcc := &StatTCC{Events: make(map[string]*StatWithCompress), MinItems: 2, FilterIDs: []string{}}
|
||||
expected := &StatTCC{
|
||||
Events: map[string]*StatWithCompress{
|
||||
"EVENT_1": &StatWithCompress{Stat: 12.2, CompressFactor: 2},
|
||||
"EVENT_3": &StatWithCompress{Stat: 18.3, CompressFactor: 1},
|
||||
},
|
||||
MinItems: 2,
|
||||
FilterIDs: []string{},
|
||||
Count: 3,
|
||||
Sum: 42.7,
|
||||
}
|
||||
expected.GetStringValue("")
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 18.2}}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 6.2}}
|
||||
ev3 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_3",
|
||||
Event: map[string]interface{}{"Cost": 18.3}}
|
||||
if err := tcc.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := tcc.AddEvent(ev2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := tcc.AddEvent(ev3); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tcc.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *tcc) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(tcc))
|
||||
}
|
||||
v := expected.Events[ev1.ID]
|
||||
v.CompressFactor = 1
|
||||
expected.Count = 2
|
||||
expected.Sum = expected.Sum - 12.2
|
||||
expected.val = nil
|
||||
expected.GetStringValue("")
|
||||
tcc.RemEvent(ev1.ID)
|
||||
tcc.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *tcc) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(tcc))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCCGetValue(t *testing.T) {
|
||||
tcc, _ := NewTCC(2, "", []string{})
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
@@ -732,6 +1103,75 @@ func TestPDDGetStringValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDDGetStringValue2(t *testing.T) {
|
||||
pdd, _ := NewPDD(2, "", []string{})
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{utils.PDD: time.Duration(2 * time.Minute)}}
|
||||
if err := pdd.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_2",
|
||||
Event: map[string]interface{}{utils.PDD: time.Duration(1 * time.Minute)}}
|
||||
pdd.AddEvent(ev2)
|
||||
if strVal := pdd.GetStringValue(""); strVal != "1m30s" {
|
||||
t.Errorf("wrong pdd value: %s", strVal)
|
||||
}
|
||||
pdd.AddEvent(ev2)
|
||||
pdd.AddEvent(ev2)
|
||||
if strVal := pdd.GetStringValue(""); strVal != "1m15s" {
|
||||
t.Errorf("wrong pdd value: %s", strVal)
|
||||
}
|
||||
pdd.RemEvent(ev2.ID)
|
||||
if strVal := pdd.GetStringValue(""); strVal != "1m20s" {
|
||||
t.Errorf("wrong pdd value: %s", strVal)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDDGetStringValue3(t *testing.T) {
|
||||
pdd := &StatPDD{Events: make(map[string]*DurationWithCompress), MinItems: 2, FilterIDs: []string{}}
|
||||
expected := &StatPDD{
|
||||
Events: map[string]*DurationWithCompress{
|
||||
"EVENT_1": &DurationWithCompress{Duration: 2*time.Minute + 30*time.Second, CompressFactor: 2},
|
||||
"EVENT_3": &DurationWithCompress{Duration: time.Minute, CompressFactor: 1},
|
||||
},
|
||||
MinItems: 2,
|
||||
FilterIDs: []string{},
|
||||
Count: 3,
|
||||
Sum: 6 * time.Minute,
|
||||
}
|
||||
expected.GetStringValue("")
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{utils.PDD: time.Duration(2 * time.Minute)}}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{utils.PDD: time.Duration(3 * time.Minute)}}
|
||||
ev3 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_3",
|
||||
Event: map[string]interface{}{utils.PDD: time.Duration(1 * time.Minute)}}
|
||||
if err := pdd.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := pdd.AddEvent(ev2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := pdd.AddEvent(ev3); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
pdd.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *pdd) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(pdd))
|
||||
}
|
||||
v := expected.Events[ev1.ID]
|
||||
v.CompressFactor = 1
|
||||
expected.Count = 2
|
||||
expected.Sum = 3*time.Minute + 30*time.Second
|
||||
expected.val = nil
|
||||
expected.GetStringValue("")
|
||||
pdd.RemEvent(ev1.ID)
|
||||
pdd.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *pdd) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(pdd))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDDGetFloat64Value(t *testing.T) {
|
||||
pdd, _ := NewPDD(2, "", []string{})
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
@@ -1066,6 +1506,76 @@ func TestStatSumGetStringValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatSumGetStringValue2(t *testing.T) {
|
||||
statSum, _ := NewStatSum(2, "Cost", []string{})
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 12.3}}
|
||||
if err := statSum.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_2",
|
||||
Event: map[string]interface{}{"Cost": 18.3}}
|
||||
statSum.AddEvent(ev2)
|
||||
if strVal := statSum.GetStringValue(""); strVal != "30.6" {
|
||||
t.Errorf("wrong statSum value: %s", strVal)
|
||||
}
|
||||
statSum.AddEvent(ev2)
|
||||
statSum.AddEvent(ev2)
|
||||
if strVal := statSum.GetStringValue(""); strVal != "67.2" {
|
||||
t.Errorf("wrong statSum value: %s", strVal)
|
||||
}
|
||||
statSum.RemEvent(ev2.ID)
|
||||
if strVal := statSum.GetStringValue(""); strVal != "48.9" {
|
||||
t.Errorf("wrong statSum value: %s", strVal)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatSumGetStringValue3(t *testing.T) {
|
||||
statSum := &StatSum{Events: make(map[string]*StatWithCompress), MinItems: 2, FilterIDs: []string{}, FieldName: "Cost"}
|
||||
expected := &StatSum{
|
||||
Events: map[string]*StatWithCompress{
|
||||
"EVENT_1": &StatWithCompress{Stat: 12.2, CompressFactor: 2},
|
||||
"EVENT_3": &StatWithCompress{Stat: 18.3, CompressFactor: 1},
|
||||
},
|
||||
MinItems: 2,
|
||||
FilterIDs: []string{},
|
||||
FieldName: "Cost",
|
||||
Count: 3,
|
||||
Sum: 42.7,
|
||||
}
|
||||
expected.GetStringValue("")
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 18.2}}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 6.2}}
|
||||
ev3 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_3",
|
||||
Event: map[string]interface{}{"Cost": 18.3}}
|
||||
if err := statSum.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := statSum.AddEvent(ev2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := statSum.AddEvent(ev3); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
statSum.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *statSum) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(statSum))
|
||||
}
|
||||
v := expected.Events[ev1.ID]
|
||||
v.CompressFactor = 1
|
||||
expected.Count = 2
|
||||
expected.Sum = expected.Sum - 12.2
|
||||
expected.val = nil
|
||||
expected.GetStringValue("")
|
||||
statSum.RemEvent(ev1.ID)
|
||||
statSum.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *statSum) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(statSum))
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatAverageGetFloat64Value(t *testing.T) {
|
||||
statAvg, _ := NewStatAverage(2, "Cost", []string{})
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
@@ -1172,6 +1682,76 @@ func TestStatAverageGetStringValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatAverageGetStringValue2(t *testing.T) {
|
||||
statAvg, _ := NewStatAverage(2, "Cost", []string{})
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 12.3}}
|
||||
if err := statAvg.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_2",
|
||||
Event: map[string]interface{}{"Cost": 18.3}}
|
||||
statAvg.AddEvent(ev2)
|
||||
if strVal := statAvg.GetStringValue(""); strVal != "15.3" {
|
||||
t.Errorf("wrong statAvg value: %s", strVal)
|
||||
}
|
||||
statAvg.AddEvent(ev2)
|
||||
statAvg.AddEvent(ev2)
|
||||
if strVal := statAvg.GetStringValue(""); strVal != "16.8" {
|
||||
t.Errorf("wrong statAvg value: %s", strVal)
|
||||
}
|
||||
statAvg.RemEvent(ev2.ID)
|
||||
if strVal := statAvg.GetStringValue(""); strVal != "16.3" {
|
||||
t.Errorf("wrong statAvg value: %s", strVal)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatAverageGetStringValue3(t *testing.T) {
|
||||
statAvg := &StatAverage{Events: make(map[string]*StatWithCompress), MinItems: 2, FilterIDs: []string{}, FieldName: "Cost"}
|
||||
expected := &StatAverage{
|
||||
Events: map[string]*StatWithCompress{
|
||||
"EVENT_1": &StatWithCompress{Stat: 12.2, CompressFactor: 2},
|
||||
"EVENT_3": &StatWithCompress{Stat: 18.3, CompressFactor: 1},
|
||||
},
|
||||
MinItems: 2,
|
||||
FilterIDs: []string{},
|
||||
FieldName: "Cost",
|
||||
Count: 3,
|
||||
Sum: 42.7,
|
||||
}
|
||||
expected.GetStringValue("")
|
||||
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 18.2}}
|
||||
ev2 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
Event: map[string]interface{}{"Cost": 6.2}}
|
||||
ev3 := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_3",
|
||||
Event: map[string]interface{}{"Cost": 18.3}}
|
||||
if err := statAvg.AddEvent(ev1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := statAvg.AddEvent(ev2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := statAvg.AddEvent(ev3); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
statAvg.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *statAvg) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(statAvg))
|
||||
}
|
||||
v := expected.Events[ev1.ID]
|
||||
v.CompressFactor = 1
|
||||
expected.Count = 2
|
||||
expected.Sum = expected.Sum - 12.2
|
||||
expected.val = nil
|
||||
expected.GetStringValue("")
|
||||
statAvg.RemEvent(ev1.ID)
|
||||
statAvg.GetStringValue("")
|
||||
if !reflect.DeepEqual(*expected, *statAvg) {
|
||||
t.Errorf("Expected: %s , received: %s", utils.ToJSON(expected), utils.ToJSON(statAvg))
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatDistinctGetFloat64Value(t *testing.T) {
|
||||
statDistinct, _ := NewStatDistinct(2, "Usage", []string{})
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
@@ -1287,7 +1867,7 @@ func TestASRMarshal(t *testing.T) {
|
||||
"AnswerTime": time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC)}}
|
||||
asr.AddEvent(ev)
|
||||
var nasr StatASR
|
||||
expected := []byte(`{"FilterIDs":["*string:Account:1001"],"Answered":1,"Count":1,"Events":{"EVENT_1":{"Answered":1,"CompressFactor":0}},"MinItems":2}`)
|
||||
expected := []byte(`{"FilterIDs":["*string:Account:1001"],"Answered":1,"Count":1,"Events":{"EVENT_1":{"Stat":1,"CompressFactor":1}},"MinItems":2}`)
|
||||
if b, err := asr.Marshal(&jMarshaler); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, b) {
|
||||
@@ -1307,7 +1887,7 @@ func TestACDMarshal(t *testing.T) {
|
||||
"Usage": time.Duration(10 * time.Second)}}
|
||||
acd.AddEvent(ev)
|
||||
var nacd StatACD
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":10000000000,"Count":1,"Events":{"EVENT_1":{"Duration":10000000000,"CompressFactor":0}},"MinItems":2}`)
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":10000000000,"Count":1,"Events":{"EVENT_1":{"Duration":10000000000,"CompressFactor":1}},"MinItems":2}`)
|
||||
if b, err := acd.Marshal(&jMarshaler); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, b) {
|
||||
@@ -1327,7 +1907,7 @@ func TestTCDMarshal(t *testing.T) {
|
||||
"Usage": time.Duration(10 * time.Second)}}
|
||||
tcd.AddEvent(ev)
|
||||
var ntcd StatTCD
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":10000000000,"Count":1,"Events":{"EVENT_1":{"Duration":10000000000,"CompressFactor":0}},"MinItems":2}`)
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":10000000000,"Count":1,"Events":{"EVENT_1":{"Duration":10000000000,"CompressFactor":1}},"MinItems":2}`)
|
||||
if b, err := tcd.Marshal(&jMarshaler); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, b) {
|
||||
@@ -1347,7 +1927,7 @@ func TestACCMarshal(t *testing.T) {
|
||||
"Cost": "12.3"}}
|
||||
acc.AddEvent(ev)
|
||||
var nacc StatACC
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":12.3,"Count":1,"Events":{"EVENT_1":{"Stat":12.3,"CompressFactor":0}},"MinItems":2}`)
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":12.3,"Count":1,"Events":{"EVENT_1":{"Stat":12.3,"CompressFactor":1}},"MinItems":2}`)
|
||||
if b, err := acc.Marshal(&jMarshaler); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, b) {
|
||||
@@ -1367,7 +1947,7 @@ func TestTCCMarshal(t *testing.T) {
|
||||
"Cost": "12.3"}}
|
||||
tcc.AddEvent(ev)
|
||||
var ntcc StatTCC
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":12.3,"Count":1,"Events":{"EVENT_1":{"Stat":12.3,"CompressFactor":0}},"MinItems":2}`)
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":12.3,"Count":1,"Events":{"EVENT_1":{"Stat":12.3,"CompressFactor":1}},"MinItems":2}`)
|
||||
if b, err := tcc.Marshal(&jMarshaler); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, b) {
|
||||
@@ -1388,7 +1968,7 @@ func TestPDDMarshal(t *testing.T) {
|
||||
utils.PDD: time.Duration(5 * time.Second)}}
|
||||
pdd.AddEvent(ev)
|
||||
var ntdd StatPDD
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":5000000000,"Count":1,"Events":{"EVENT_1":{"Duration":5000000000,"CompressFactor":0}},"MinItems":2}`)
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":5000000000,"Count":1,"Events":{"EVENT_1":{"Duration":5000000000,"CompressFactor":1}},"MinItems":2}`)
|
||||
if b, err := pdd.Marshal(&jMarshaler); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, b) {
|
||||
@@ -1433,7 +2013,7 @@ func TestStatSumMarshal(t *testing.T) {
|
||||
utils.Destination: "1002"}}
|
||||
statSum.AddEvent(ev)
|
||||
var nstatSum StatSum
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":20,"Count":1,"Events":{"EVENT_1":{"Stat":20,"CompressFactor":0}},"MinItems":2,"FieldName":"Cost"}`)
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":20,"Count":1,"Events":{"EVENT_1":{"Stat":20,"CompressFactor":1}},"MinItems":2,"FieldName":"Cost"}`)
|
||||
if b, err := statSum.Marshal(&jMarshaler); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, b) {
|
||||
@@ -1456,7 +2036,7 @@ func TestStatAverageMarshal(t *testing.T) {
|
||||
utils.Destination: "1002"}}
|
||||
statAvg.AddEvent(ev)
|
||||
var nstatAvg StatAverage
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":20,"Count":1,"Events":{"EVENT_1":{"Stat":20,"CompressFactor":0}},"MinItems":2,"FieldName":"Cost"}`)
|
||||
expected := []byte(`{"FilterIDs":[],"Sum":20,"Count":1,"Events":{"EVENT_1":{"Stat":20,"CompressFactor":1}},"MinItems":2,"FieldName":"Cost"}`)
|
||||
if b, err := statAvg.Marshal(&jMarshaler); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, b) {
|
||||
|
||||
@@ -154,14 +154,9 @@ func TestRemakeQueue(t *testing.T) {
|
||||
sq := &engine.StatQueue{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "StatsID",
|
||||
SQItems: []struct {
|
||||
EventID string
|
||||
ExpiryTime *time.Time
|
||||
}{
|
||||
{
|
||||
EventID: "ev1",
|
||||
},
|
||||
},
|
||||
SQItems: []engine.SQItem{{
|
||||
EventID: "ev1",
|
||||
}},
|
||||
SQMetrics: map[string]engine.StatMetric{
|
||||
"*tcc": nil,
|
||||
"*sum:Usage": nil,
|
||||
|
||||
Reference in New Issue
Block a user