From 69c5012020e488d57c9cf41d7a079e4b0714c493 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 30 Jun 2020 11:28:28 +0300 Subject: [PATCH] Updated MapEvent.AsCDR to initialize the CostDetails. Fixes #2247 --- .../cdrsonexpmaster_mongo/cdrsreplicationmaster.json | 10 +++++++++- .../cdrsonexpmaster_mysql/cdrsreplicationmaster.json | 10 +++++++++- engine/mapevent.go | 5 +++++ engine/mapevent_test.go | 11 ++++++++--- engine/safevent_test.go | 8 ++++++-- general_tests/cdrs_onlexp_it_test.go | 9 +++++++++ 6 files changed, 46 insertions(+), 7 deletions(-) diff --git a/data/conf/samples/cdrsonexpmaster_mongo/cdrsreplicationmaster.json b/data/conf/samples/cdrsonexpmaster_mongo/cdrsreplicationmaster.json index 25f101ba9..7c8a91451 100644 --- a/data/conf/samples/cdrsonexpmaster_mongo/cdrsreplicationmaster.json +++ b/data/conf/samples/cdrsonexpmaster_mongo/cdrsreplicationmaster.json @@ -32,7 +32,7 @@ "store_cdrs": false, // store cdrs in storDb "chargers_conns":["*internal"], "rals_conns": ["*internal"], - "online_cdr_exports": ["http_localhost", "amqp_localhost", "http_test_file", "amqp_test_file","aws_test_file","sqs_test_file","kafka_localhost","s3_test_file"], + "online_cdr_exports": ["http_localhost", "amqp_localhost", "http_test_file", "amqp_test_file","aws_test_file","sqs_test_file","kafka_localhost","s3_test_file", "eventcost_filter"], }, "chargers": { @@ -128,6 +128,14 @@ {"path": "*exp.CGRID", "type": "*variable", "value": "~*req.CGRID"}, ], }, + "eventcost_filter": { + "export_format": "*amqp_json_map", + "export_path": "amqp://guest:guest@wrongurl:25672/?queue_id=cgrates_cdrs", + "filters":["*string:~*ec.Cost:100"], + "fields": [ + {"path": "*exp.CGRID", "type": "*composed", "value": "~*req.CGRID"}, + ], + }, }, diff --git a/data/conf/samples/cdrsonexpmaster_mysql/cdrsreplicationmaster.json b/data/conf/samples/cdrsonexpmaster_mysql/cdrsreplicationmaster.json index b3f52a3ce..9200d6b83 100644 --- a/data/conf/samples/cdrsonexpmaster_mysql/cdrsreplicationmaster.json +++ b/data/conf/samples/cdrsonexpmaster_mysql/cdrsreplicationmaster.json @@ -30,7 +30,7 @@ "store_cdrs": false, // store cdrs in storDb "chargers_conns":["*internal"], "rals_conns": ["*internal"], - "online_cdr_exports": ["http_localhost", "amqp_localhost", "http_test_file", "amqp_test_file","aws_test_file","sqs_test_file","kafka_localhost","s3_test_file"], + "online_cdr_exports": ["http_localhost", "amqp_localhost", "http_test_file", "amqp_test_file","aws_test_file","sqs_test_file","kafka_localhost","s3_test_file", "eventcost_filter"], }, "chargers": { @@ -126,6 +126,14 @@ {"path": "*exp.CGRID", "type": "*variable", "value": "~*req.CGRID"}, ], }, + "eventcost_filter": { + "export_format": "*amqp_json_map", + "export_path": "amqp://guest:guest@wrongurl:25672/?queue_id=cgrates_cdrs", + "filters":["*string:~*ec.Cost:100"], + "fields": [ + {"path": "*exp.CGRID", "type": "*composed", "value": "~*req.CGRID"}, + ], + }, }, diff --git a/engine/mapevent.go b/engine/mapevent.go index 347605b5f..41959b564 100644 --- a/engine/mapevent.go +++ b/engine/mapevent.go @@ -265,6 +265,11 @@ func (me MapEvent) AsCDR(cfg *config.CGRConfig, tnt, tmz string) (cdr *CDR, err } } } + if cdr.CostDetails == nil { + cdr.CostDetails = NewBareEventCost() + } else { + cdr.CostDetails.initCache() + } if cfg != nil { cdr.AddDefaults(cfg) } diff --git a/engine/mapevent_test.go b/engine/mapevent_test.go index 52565c66d..742ead9c3 100644 --- a/engine/mapevent_test.go +++ b/engine/mapevent_test.go @@ -311,7 +311,7 @@ func TestMapEventAsMapString(t *testing.T) { func TestMapEventAsCDR(t *testing.T) { me := NewMapEvent(nil) - expected := &CDR{Cost: -1.0, ExtraFields: make(map[string]string)} + expected := &CDR{Cost: -1.0, ExtraFields: make(map[string]string), CostDetails: NewBareEventCost()} if rply, err := me.AsCDR(nil, utils.EmptyString, utils.EmptyString); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, rply) { @@ -330,6 +330,7 @@ func TestMapEventAsCDR(t *testing.T) { Tenant: cfg.GeneralCfg().DefaultTenant, Category: cfg.GeneralCfg().DefaultCategory, ExtraFields: make(map[string]string), + CostDetails: NewBareEventCost(), } if rply, err := me.AsCDR(cfg, utils.EmptyString, utils.EmptyString); err != nil { t.Error(err) @@ -371,7 +372,8 @@ func TestMapEventAsCDR(t *testing.T) { me = MapEvent{"ExtraField1": 5, "ExtraField2": "extra"} expected = &CDR{ - Cost: -1.0, + Cost: -1.0, + CostDetails: NewBareEventCost(), ExtraFields: map[string]string{ "ExtraField1": "5", "ExtraField2": "extra", @@ -403,6 +405,7 @@ func TestMapEventAsCDR(t *testing.T) { Tenant: cfg.GeneralCfg().DefaultTenant, Category: cfg.GeneralCfg().DefaultCategory, ExtraInfo: "ACCOUNT_NOT_FOUND", + CostDetails: NewBareEventCost(), } if rply, err := me.AsCDR(cfg, utils.EmptyString, utils.EmptyString); err != nil { t.Error(err) @@ -446,7 +449,8 @@ func TestMapEventAsCDR(t *testing.T) { "ExtraField1": "5", "ExtraField2": "extra", }, - ExtraInfo: "ACCOUNT_NOT_FOUND", + ExtraInfo: "ACCOUNT_NOT_FOUND", + CostDetails: NewBareEventCost(), } if rply, err := me.AsCDR(cfg, utils.EmptyString, utils.EmptyString); err != nil { t.Error(err) @@ -629,6 +633,7 @@ func TestMapEventAsCDR(t *testing.T) { }, }, } + ec1.initCache() me = MapEvent{ "ExtraField1": 5, "Source": 1001, diff --git a/engine/safevent_test.go b/engine/safevent_test.go index ed1760aae..432459b98 100644 --- a/engine/safevent_test.go +++ b/engine/safevent_test.go @@ -625,7 +625,7 @@ func TestSafEventAsMapString(t *testing.T) { func TestSafEventAsCDR(t *testing.T) { se := SafEvent{Me: NewMapEvent(nil)} - expected := &CDR{Cost: -1.0, ExtraFields: make(map[string]string)} + expected := &CDR{Cost: -1.0, ExtraFields: make(map[string]string), CostDetails: NewBareEventCost()} if rply, err := se.AsCDR(nil, utils.EmptyString, utils.EmptyString); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, rply) { @@ -644,6 +644,7 @@ func TestSafEventAsCDR(t *testing.T) { Tenant: cfg.GeneralCfg().DefaultTenant, Category: cfg.GeneralCfg().DefaultCategory, ExtraFields: make(map[string]string), + CostDetails: NewBareEventCost(), } if rply, err := se.AsCDR(cfg, utils.EmptyString, utils.EmptyString); err != nil { t.Error(err) @@ -676,7 +677,8 @@ func TestSafEventAsCDR(t *testing.T) { } se = SafEvent{Me: MapEvent{"ExtraField1": 5, "ExtraField2": "extra"}} expected = &CDR{ - Cost: -1.0, + Cost: -1.0, + CostDetails: NewBareEventCost(), ExtraFields: map[string]string{ "ExtraField1": "5", "ExtraField2": "extra", @@ -706,6 +708,7 @@ func TestSafEventAsCDR(t *testing.T) { RequestType: cfg.GeneralCfg().DefaultReqType, Tenant: cfg.GeneralCfg().DefaultTenant, Category: cfg.GeneralCfg().DefaultCategory, + CostDetails: NewBareEventCost(), } if rply, err := se.AsCDR(cfg, utils.EmptyString, utils.EmptyString); err != nil { t.Error(err) @@ -739,6 +742,7 @@ func TestSafEventAsCDR(t *testing.T) { ToR: utils.VOICE, RequestType: cfg.GeneralCfg().DefaultReqType, Category: cfg.GeneralCfg().DefaultCategory, + CostDetails: NewBareEventCost(), } if rply, err := se.AsCDR(cfg, "itsyscom.com", utils.EmptyString); err != nil { t.Error(err) diff --git a/general_tests/cdrs_onlexp_it_test.go b/general_tests/cdrs_onlexp_it_test.go index 51a2e5150..d90c92b47 100644 --- a/general_tests/cdrs_onlexp_it_test.go +++ b/general_tests/cdrs_onlexp_it_test.go @@ -214,6 +214,9 @@ func testCDRsOnExpDisableOnlineExport(t *testing.T) { RunID: utils.MetaDefault, Cost: 1.201, PreRated: true, + CostDetails: &engine.EventCost{ + Cost: utils.Float64Pointer(10), + }, } var reply string if err := cdrsMasterRpc.Call(utils.CDRsV1ProcessEvent, @@ -257,6 +260,9 @@ func testCDRsOnExpHttpCdrReplication(t *testing.T) { RunID: utils.MetaDefault, Cost: 1.201, PreRated: true, + CostDetails: &engine.EventCost{ + Cost: utils.Float64Pointer(10), + }, } var reply string if err := cdrsMasterRpc.Call(utils.CDRsV1ProcessEvent, @@ -385,6 +391,9 @@ func testCDRsOnExpAMQPReplication(t *testing.T) { RunID: utils.MetaDefault, Cost: 1.201, PreRated: true, + CostDetails: &engine.EventCost{ + Cost: utils.Float64Pointer(10), + }, } var reply string if err := cdrsMasterRpc.Call(utils.CDRsV1ProcessEvent,