Add StatACD and add test for ACD and ASR

This commit is contained in:
TeoV
2017-09-25 14:11:55 +03:00
parent 13970f3688
commit be0d8b27d8
3 changed files with 154 additions and 29 deletions

View File

@@ -75,6 +75,22 @@ func (se StatEvent) AnswerTime(timezone string) (at time.Time, err error) {
return utils.ParseTimeDetectLayout(atStr, timezone)
}
// Usage returns the Usage of StatEvent
func (se StatEvent) Usage(timezone string) (at time.Duration, err error) {
usIf, has := se.Fields[utils.USAGE]
if !has {
return at, utils.ErrNotFound
}
if us, canCast := usIf.(time.Duration); canCast {
return us, nil
}
usStr, canCast := usIf.(string)
if !canCast {
return at, errors.New("cannot cast to string")
}
return utils.ParseDurationWithSecs(usStr)
}
// NewStoredStatQueue initiates a StoredStatQueue out of StatQueue
func NewStoredStatQueue(sq *StatQueue, ms Marshaler) (sSQ *StoredStatQueue, err error) {
sSQ = &StoredStatQueue{

View File

@@ -20,7 +20,6 @@ package engine
import (
"fmt"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
@@ -51,7 +50,7 @@ type StatMetric interface {
}
func NewASR() (StatMetric, error) {
return new(StatASR), nil
return &StatASR{Events: make(map[string]bool)}, nil
}
// ASR implements AverageSuccessRatio metric
@@ -135,38 +134,78 @@ func (asr *StatASR) LoadMarshaled(ms Marshaler, marshaled []byte) (err error) {
}
func NewACD() (StatMetric, error) {
return new(StatACD), nil
return &StatACD{Events: make(map[string]float64)}, nil
}
// ACD implements AverageCallDuration metric
type StatACD struct {
Sum time.Duration
Count int
Sum float64
Count float64
Events map[string]float64 // map[EventTenantID]Duration
val *float64 // cached ACD value
}
// getValue returns asr.val
func (acd *StatACD) getValue() float64 {
if acd.val == nil {
if acd.Count == 0 {
acd.val = utils.Float64Pointer(float64(STATS_NA))
} else {
acd.val = utils.Float64Pointer(utils.Round(acd.Sum/acd.Count,
config.CgrConfig().RoundingDecimals, utils.ROUNDING_MIDDLE))
}
}
return *acd.val
}
func (acd *StatACD) GetStringValue(fmtOpts string) (val string) {
return
if acd.Count == 0 {
return utils.NOT_AVAILABLE
}
return fmt.Sprintf("%+v", acd.getValue())
}
func (acd *StatACD) GetValue() (v interface{}) {
return
return acd.getValue()
}
func (acd *StatACD) GetFloat64Value() (v float64) {
return float64(STATS_NA)
return acd.getValue()
}
func (acd *StatACD) AddEvent(ev *StatEvent) (err error) {
var answered float64
if at, err := ev.AnswerTime(config.CgrConfig().DefaultTimezone); err != nil &&
err != utils.ErrNotFound {
return err
} else if !at.IsZero() {
duration, _ := ev.Usage(config.CgrConfig().DefaultTimezone)
answered = duration.Seconds()
acd.Sum += duration.Seconds()
}
acd.Events[ev.TenantID()] = answered
acd.Count += 1
acd.val = nil
return
}
func (acd *StatACD) RemEvent(evTenantID string) (err error) {
duration, has := acd.Events[evTenantID]
if !has {
return utils.ErrNotFound
}
if duration != 0 {
acd.Sum -= duration
}
acd.Count -= 1
delete(acd.Events, evTenantID)
acd.val = nil
return
}
func (acd *StatACD) Marshal(ms Marshaler) (marshaled []byte, err error) {
return
return ms.Marshal(acd)
}
func (acd *StatACD) LoadMarshaled(ms Marshaler, marshaled []byte) (err error) {
return
return ms.Unmarshal(marshaled, acd)
}

View File

@@ -17,39 +17,40 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package engine
/*
import (
"github.com/cgrates/cgrates/utils"
"testing"
"time"
"github.com/cgrates/cgrates/utils"
)
func TestASRGetStringValue(t *testing.T) {
asr, _ := NewASR()
ev := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_1",
Fields: map[string]interface{}{
"AnswerTime": time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC)}}
if strVal := asr.GetStringValue(""); strVal != utils.NOT_AVAILABLE {
t.Errorf("wrong asr value: %s", strVal)
}
ev := engine.StatsEvent{
"AnswerTime": time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC)}
asr.AddEvent(ev)
if strVal := asr.GetStringValue(""); strVal != "100%" {
t.Errorf("wrong asr value: %s", strVal)
}
asr.AddEvent(engine.StatsEvent{})
asr.AddEvent(engine.StatsEvent{})
ev2 := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_2"}
ev3 := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_3"}
asr.AddEvent(ev2)
asr.AddEvent(ev3)
if strVal := asr.GetStringValue(""); strVal != "33.33333%" {
t.Errorf("wrong asr value: %s", strVal)
}
asr.RemEvent(engine.StatsEvent{})
asr.RemEvent(ev3.TenantID())
if strVal := asr.GetStringValue(""); strVal != "50%" {
t.Errorf("wrong asr value: %s", strVal)
}
asr.RemEvent(ev)
asr.RemEvent(ev.TenantID())
if strVal := asr.GetStringValue(""); strVal != "0%" {
t.Errorf("wrong asr value: %s", strVal)
}
asr.RemEvent(engine.StatsEvent{})
asr.RemEvent(ev2.TenantID())
if strVal := asr.GetStringValue(""); strVal != utils.NOT_AVAILABLE {
t.Errorf("wrong asr value: %s", strVal)
}
@@ -58,29 +59,98 @@ func TestASRGetStringValue(t *testing.T) {
func TestASRGetValue(t *testing.T) {
asr, _ := NewASR()
ev := engine.StatsEvent{
"AnswerTime": time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
}
ev := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_1",
Fields: map[string]interface{}{
"AnswerTime": time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC)}}
asr.AddEvent(ev)
if v := asr.GetValue(); v != 100.0 {
t.Errorf("wrong asr value: %f", v)
}
asr.AddEvent(engine.StatsEvent{})
asr.AddEvent(engine.StatsEvent{})
ev2 := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_2"}
ev3 := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_3"}
asr.AddEvent(ev2)
asr.AddEvent(ev3)
if v := asr.GetValue(); v != 33.33333 {
t.Errorf("wrong asr value: %f", v)
}
asr.RemEvent(engine.StatsEvent{})
asr.RemEvent(ev3.TenantID())
if v := asr.GetValue(); v != 50.0 {
t.Errorf("wrong asr value: %f", v)
}
asr.RemEvent(ev)
asr.RemEvent(ev.TenantID())
if v := asr.GetValue(); v != 0.0 {
t.Errorf("wrong asr value: %f", v)
}
asr.RemEvent(engine.StatsEvent{})
asr.RemEvent(ev2.TenantID())
if v := asr.GetValue(); v != -1.0 {
t.Errorf("wrong asr value: %f", v)
}
}
*/
func TestACDGetStringValue(t *testing.T) {
acd, _ := NewACD()
ev := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_1",
Fields: map[string]interface{}{
"Usage": time.Duration(10 * time.Second),
"AnswerTime": time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
}}
if strVal := acd.GetStringValue(""); strVal != utils.NOT_AVAILABLE {
t.Errorf("wrong acd value: %s", strVal)
}
acd.AddEvent(ev)
if strVal := acd.GetStringValue(""); strVal != "10" {
t.Errorf("wrong acd value: %s", strVal)
}
ev2 := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_2"}
ev3 := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_3"}
acd.AddEvent(ev2)
acd.AddEvent(ev3)
if strVal := acd.GetStringValue(""); strVal != "3.33333" {
t.Errorf("wrong acd value: %s", strVal)
}
acd.RemEvent(ev3.TenantID())
if strVal := acd.GetStringValue(""); strVal != "5" {
t.Errorf("wrong acd value: %s", strVal)
}
acd.RemEvent(ev.TenantID())
if strVal := acd.GetStringValue(""); strVal != "0" {
t.Errorf("wrong acd value: %s", strVal)
}
acd.RemEvent(ev2.TenantID())
if strVal := acd.GetStringValue(""); strVal != utils.NOT_AVAILABLE {
t.Errorf("wrong acd value: %s", strVal)
}
}
func TestACDGetValue(t *testing.T) {
acd, _ := NewACD()
ev := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_1",
Fields: map[string]interface{}{
"AnswerTime": time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
"Usage": time.Duration(10 * time.Second)}}
acd.AddEvent(ev)
if v := acd.GetValue(); v != 10.0 {
t.Errorf("wrong asr value: %f", v)
}
ev2 := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_2"}
ev3 := &StatEvent{Tenant: "cgrates.org", ID: "EVENT_3"}
acd.AddEvent(ev2)
acd.AddEvent(ev3)
if v := acd.GetValue(); v != 3.33333 {
t.Errorf("wrong asr value: %f", v)
}
acd.RemEvent(ev3.TenantID())
if v := acd.GetValue(); v != 5.0 {
t.Errorf("wrong asr value: %f", v)
}
acd.RemEvent(ev.TenantID())
if v := acd.GetValue(); v != 0.0 {
t.Errorf("wrong asr value: %f", v)
}
acd.RemEvent(ev2.TenantID())
if v := acd.GetValue(); v != -1.0 {
t.Errorf("wrong asr value: %f", v)
}
}