From bb576a24eafd52dfc331d2221edfd6494d7becc9 Mon Sep 17 00:00:00 2001 From: TeoV Date: Tue, 19 May 2020 18:18:48 +0300 Subject: [PATCH] Update combimed function for CDR to use filterS --- engine/cdr.go | 31 +++++++++--------- engine/cdr_test.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 16 deletions(-) diff --git a/engine/cdr.go b/engine/cdr.go index ceab23018..a1b771604 100644 --- a/engine/cdr.go +++ b/engine/cdr.go @@ -288,25 +288,24 @@ func (cdr *CDR) String() string { // combimedCdrFieldVal groups together CDRs with same CGRID and combines their values matching filter field ID func (cdr *CDR) combimedCdrFieldVal(cfgCdrFld *config.FCTemplate, groupCDRs []*CDR, filterS *FilterS) (string, error) { - var combinedVal string // Will result as combination of the field values, filters must match - for _, filterRule := range cfgCdrFld.Value { - pairingVal, err := cdr.FieldAsString(filterRule) - if err != nil { - return "", err + var combimedVal string // Will result as combination of the field values, filters must match + + for _, grpCDR := range groupCDRs { + if cdr.CGRID != grpCDR.CGRID { + continue // We only care about cdrs with same primary cdr behind } - for _, grpCDR := range groupCDRs { - if cdr.CGRID != grpCDR.CGRID { - continue // We only care about cdrs with same primary cdr behind - } - if valStr, err := grpCDR.FieldAsString(filterRule); err != nil { - return "", err - } else if valStr != pairingVal { // First CDR with field equal with ours - continue - } - combinedVal += grpCDR.FieldsAsString(cfgCdrFld.Value) + if pass, err := filterS.Pass(grpCDR.Tenant, cfgCdrFld.Filters, utils.MapStorage{ + utils.MetaReq: grpCDR.AsMapStringIface(), + utils.MetaEC: grpCDR.CostDetails, + }); err != nil { + return utils.EmptyString, err + } else if !pass { + continue } + combimedVal += grpCDR.FieldsAsString(cfgCdrFld.Value) + } - return combinedVal, nil + return combimedVal, nil } // Extracts the value specified by cfgHdr out of cdr, used for export values diff --git a/engine/cdr_test.go b/engine/cdr_test.go index 0ba52bf20..1912db1c2 100644 --- a/engine/cdr_test.go +++ b/engine/cdr_test.go @@ -1149,3 +1149,85 @@ func TestCDRexportFieldValue(t *testing.T) { } } + +func TestCDRcombimedCdrFieldVal(t *testing.T) { + cdr := &CDR{ + CGRID: utils.Sha1("dsafdsaf", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), + OrderID: 123, + ToR: utils.VOICE, + OriginID: "dsafdsaf", + OriginHost: "192.168.1.1", + Source: utils.UNIT_TEST, + RequestType: utils.META_RATED, + Tenant: "cgrates.org", + Category: "call", + Account: "1001", + Subject: "1001", + Destination: "+4986517174963", + SetupTime: time.Date(2013, 11, 7, 8, 42, 20, 0, time.UTC), + AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), + RunID: utils.MetaDefault, + Usage: time.Duration(10) * time.Second, + Cost: 1.32165, + ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, + } + groupCDRs := []*CDR{ + cdr, + &CDR{ + CGRID: utils.Sha1("dsafdsaf", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), + OrderID: 124, + ToR: utils.VOICE, + OriginID: "dsafdsaf", + OriginHost: "192.168.1.1", + Source: utils.UNIT_TEST, + RequestType: utils.META_RATED, + Tenant: "cgrates.org", + Category: "call", + Account: "1001", + Subject: "1001", + Destination: "+4986517174963", + SetupTime: time.Date(2013, 11, 7, 8, 42, 20, 0, time.UTC), + AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), + RunID: "testRun1", + Usage: time.Duration(10) * time.Second, + Cost: 1.22, + }, + &CDR{ + CGRID: utils.Sha1("dsafdsaf", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), + OrderID: 125, + ToR: utils.VOICE, + OriginID: "dsafdsaf", + OriginHost: "192.168.1.1", + Source: utils.UNIT_TEST, + RequestType: utils.META_RATED, + Tenant: "cgrates.org", + Category: "call", + Account: "1001", + Subject: "1001", + Destination: "+4986517174963", + SetupTime: time.Date(2013, 11, 7, 8, 42, 20, 0, time.UTC), + AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), + RunID: "testRun2", + Usage: time.Duration(10) * time.Second, + Cost: 1.764, + }, + } + + tpFld := &config.FCTemplate{ + Tag: "TestCombiMed", + Type: utils.META_COMBIMED, + Filters: []string{"*string:~*req.RunID:testRun1"}, + Value: config.NewRSRParsersMustCompile("~*req.Cost", true, utils.INFIELD_SEP), + } + cfg, err := config.NewDefaultCGRConfig() + if err != nil { + t.Errorf("Error: %+v", err) + } + + if out, err := cdr.combimedCdrFieldVal(tpFld, groupCDRs, &FilterS{cfg: cfg}); err != nil { + t.Error(err) + } else if out != "1.22" { + t.Errorf("Expected : %+v, received: %+v", "1.22", out) + } + +}