Added statsQueue unmarshal function for json

This commit is contained in:
Trial97
2021-07-13 09:19:36 +03:00
committed by Dan Christian Bogos
parent eddabfb985
commit b2a432d996
2 changed files with 71 additions and 0 deletions

View File

@@ -19,8 +19,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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
}

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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))
}
}