Stats metric with GetValue method

This commit is contained in:
DanB
2017-07-28 19:13:14 +02:00
parent 5cc4b3c2e2
commit 204f42798c
4 changed files with 53 additions and 7 deletions

View File

@@ -38,6 +38,10 @@ func (acd *ACD) GetStringValue(fmtOpts string) (val string) {
return
}
func (acd *ACD) GetValue() (v interface{}) {
return
}
func (acd *ACD) AddEvent(ev engine.StatsEvent) (err error) {
return
}

View File

@@ -40,13 +40,21 @@ func (asr *ASR) GetStringValue(fmtOpts string) (valStr string) {
if asr.Count == 0 {
return utils.NOT_AVAILABLE
}
val := utils.Round((asr.Answered / asr.Count * 100),
val := asr.GetValue().(float64)
return fmt.Sprintf("%v%%", val) // %v will automatically limit the number of decimals printed
}
func (asr *ASR) GetValue() (v interface{}) {
if asr.Count == 0 {
return engine.STATS_NA
}
return utils.Round((asr.Answered / asr.Count * 100),
config.CgrConfig().RoundingDecimals, utils.ROUNDING_MIDDLE)
return fmt.Sprintf("%v%%", val)
}
func (asr *ASR) AddEvent(ev engine.StatsEvent) (err error) {
if at, err := ev.AnswerTime(config.CgrConfig().DefaultTimezone); err != nil {
if at, err := ev.AnswerTime(config.CgrConfig().DefaultTimezone); err != nil &&
err != utils.ErrNotFound {
return err
} else if !at.IsZero() {
asr.Answered += 1
@@ -56,7 +64,8 @@ func (asr *ASR) AddEvent(ev engine.StatsEvent) (err error) {
}
func (asr *ASR) RemEvent(ev engine.StatsEvent) (err error) {
if at, err := ev.AnswerTime(config.CgrConfig().DefaultTimezone); err != nil {
if at, err := ev.AnswerTime(config.CgrConfig().DefaultTimezone); err != nil &&
err != utils.ErrNotFound {
return err
} else if !at.IsZero() {
asr.Answered -= 1

View File

@@ -22,15 +22,47 @@ import (
"time"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
func TestASRAddRemEvent(t *testing.T) {
func TestASRGetStringValue(t *testing.T) {
asr, _ := NewASR()
if strVal := asr.GetStringValue(""); strVal != utils.NOT_AVAILABLE {
t.Errorf("wrong asr value: %s", strVal)
}
asr.AddEvent(
engine.StatsEvent{
"AnswerTime": time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC)})
if strVal := asr.GetStringValue(""); strVal != "100%" {
t.Errorf("wrong asr value: %s", strVal)
}
asr.AddEvent(engine.StatsEvent{})
asr.AddEvent(engine.StatsEvent{})
if strVal := asr.GetStringValue(""); strVal != "33.33333%" {
t.Errorf("wrong asr value: %s", strVal)
}
asr.RemEvent(engine.StatsEvent{})
if strVal := asr.GetStringValue(""); strVal != "50%" {
t.Errorf("wrong asr value: %s", strVal)
}
}
func TestASRGetValue(t *testing.T) {
asr, _ := NewASR()
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)
if v := asr.GetValue(); v != 100.0 {
t.Errorf("wrong asr value: %f", v)
}
asr.AddEvent(engine.StatsEvent{})
asr.AddEvent(engine.StatsEvent{})
if v := asr.GetValue(); v != 33.33333 {
t.Errorf("wrong asr value: %f", v)
}
asr.RemEvent(engine.StatsEvent{})
if v := asr.GetValue(); v != 50.0 {
t.Errorf("wrong asr value: %f", v)
}
}

View File

@@ -41,6 +41,7 @@ func NewStatsMetric(metricID string) (sm StatsMetric, err error) {
// StatsMetric is the interface which a metric should implement
type StatsMetric interface {
GetStringValue(fmtOpts string) (val string)
GetValue() interface{}
AddEvent(ev engine.StatsEvent) error
RemEvent(ev engine.StatsEvent) error
GetMarshaled(ms engine.Marshaler) (vals []byte, err error)