diff --git a/engine/libstats.go b/engine/libstats.go index 8bcc815ab..56313e58d 100644 --- a/engine/libstats.go +++ b/engine/libstats.go @@ -19,8 +19,10 @@ along with this program. If not, see package engine import ( + "encoding/json" "fmt" "sort" + "strings" "sync" "time" @@ -340,3 +342,54 @@ type StatQueues []*StatQueue func (sis StatQueues) Sort() { sort.Slice(sis, func(i, j int) bool { return sis[i].sqPrfl.Weight > sis[j].sqPrfl.Weight }) } + +// UnmarshalJSON here only to fully support json for StatQueue +func (sq *StatQueue) UnmarshalJSON(data []byte) (err error) { + var tmp struct { + Tenant string + ID string + SQItems []SQItem + SQMetrics map[string]json.RawMessage + } + if err = json.Unmarshal(data, &tmp); err != nil { + return + } + sq.Tenant = tmp.Tenant + sq.ID = tmp.ID + sq.SQItems = tmp.SQItems + sq.SQMetrics = make(map[string]StatMetric) + for metricID, val := range tmp.SQMetrics { + metricSplit := strings.Split(metricID, utils.HashtagSep) + var metric StatMetric + switch metricSplit[0] { + case utils.MetaASR: + metric = new(StatASR) + case utils.MetaACD: + metric = new(StatACD) + case utils.MetaTCD: + metric = new(StatTCD) + case utils.MetaACC: + metric = new(StatACC) + case utils.MetaTCC: + metric = new(StatTCC) + case utils.MetaPDD: + metric = new(StatPDD) + case utils.MetaDDC: + metric = new(StatDDC) + case utils.MetaSum: + metric = new(StatSum) + case utils.MetaAverage: + metric = new(StatAverage) + case utils.MetaDistinct: + metric = new(StatDistinct) + default: + return fmt.Errorf("unsupported metric type <%s>", metricSplit[0]) + } + if err = json.Unmarshal([]byte(val), metric); err != nil { + fmt.Println(1) + return + } + sq.SQMetrics[metricID] = metric + } + return +} diff --git a/engine/libstats_test.go b/engine/libstats_test.go index 5198ab5eb..2266ed783 100644 --- a/engine/libstats_test.go +++ b/engine/libstats_test.go @@ -18,6 +18,7 @@ along with this program. If not, see package engine import ( + "encoding/json" "fmt" "reflect" "testing" @@ -1224,3 +1225,20 @@ func TestLibstatsaddStatEventNoPass(t *testing.T) { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", exp, sq) } } + +func TestStatQueueJSONMarshall(t *testing.T) { + rply := new(StatQueue) + exp, err := NewStatQueue("cgrates.org", "STS", []*MetricWithFilters{ + {MetricID: utils.MetaASR}, + {MetricID: utils.MetaTCD}, + }, 1) + if err != nil { + t.Fatal(err) + } + if err = json.Unmarshal([]byte(utils.ToJSON(exp)), rply); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(rply, exp) { + t.Errorf("Expected: %s , received: %s", utils.ToJSON(exp), utils.ToJSON(rply)) + } + +}