This commit is contained in:
TeoV
2018-09-17 10:29:29 -04:00
committed by Dan Christian Bogos
parent 19961ab9f7
commit 5003eecd71
6 changed files with 148 additions and 18 deletions

View File

@@ -157,14 +157,14 @@ func (self *CsvRecordsProcessor) recordToStoredCdr(record []string, cdrcCfg *con
cdrFldCfg.Value = config.NewRSRParsersMustCompile("~"+strconv.Itoa(len(record)-1), true) // in case of flatstore, last element will be the duration computed by us
}
}
var fieldVal string
fldVals := make(map[string]string)
switch cdrFldCfg.Type {
case utils.META_COMPOSED:
out, err := cdrFldCfg.Value.ParseDataProvider(csvProvider, utils.NestingSep)
if err != nil {
return nil, err
}
fieldVal = out
fldVals[cdrFldCfg.FieldId] += out
case utils.MetaUnixTimestamp:
out, err := cdrFldCfg.Value.ParseDataProvider(csvProvider, utils.NestingSep)
if err != nil {
@@ -174,13 +174,13 @@ func (self *CsvRecordsProcessor) recordToStoredCdr(record []string, cdrcCfg *con
if err != nil {
return nil, err
}
fieldVal += strconv.Itoa(int(t.Unix()))
fldVals[cdrFldCfg.FieldId] += strconv.Itoa(int(t.Unix()))
case utils.META_HTTP_POST:
lazyHttpFields = append(lazyHttpFields, cdrFldCfg) // Will process later so we can send an estimation of storedCdr to http server
default:
return nil, fmt.Errorf("Unsupported field type: %s", cdrFldCfg.Type)
}
if err := storedCdr.ParseFieldValue(cdrFldCfg.FieldId, fieldVal, self.timezone); err != nil {
if err := storedCdr.ParseFieldValue(cdrFldCfg.FieldId, fldVals[cdrFldCfg.FieldId], self.timezone); err != nil {
return nil, err
}
}

View File

@@ -171,25 +171,25 @@ func (self *FwvRecordsProcessor) recordToStoredCdr(record string, cdrcCfg *confi
continue // Not passes filters, ignore this CDR
}
}
var fieldVal string
fldVals := make(map[string]string)
switch cdrFldCfg.Type {
case utils.META_COMPOSED:
out, err := cdrFldCfg.Value.ParseDataProvider(fwvProvider, utils.NestingSep)
if err != nil {
return nil, err
}
fieldVal = out
fldVals[cdrFldCfg.FieldId] += out
case utils.META_HTTP_POST:
lazyHttpFields = append(lazyHttpFields, cdrFldCfg) // Will process later so we can send an estimation of storedCdr to http server
default:
//return nil, fmt.Errorf("Unsupported field type: %s", cdrFldCfg.Type)
continue // Don't do anything for unsupported fields
}
if fieldVal, err = utils.FmtFieldWidth(cdrFldCfg.Tag, fieldVal, cdrFldCfg.Width,
if fldVals[cdrFldCfg.FieldId], err = utils.FmtFieldWidth(cdrFldCfg.Tag, fldVals[cdrFldCfg.FieldId], cdrFldCfg.Width,
cdrFldCfg.Strip, cdrFldCfg.Padding, cdrFldCfg.Mandatory); err != nil {
return nil, err
}
if err := storedCdr.ParseFieldValue(cdrFldCfg.FieldId, fieldVal, self.timezone); err != nil {
if err := storedCdr.ParseFieldValue(cdrFldCfg.FieldId, fldVals[cdrFldCfg.FieldId], self.timezone); err != nil {
return nil, err
}
}

View File

@@ -302,3 +302,89 @@ func TestXmlIT3KillEngine(t *testing.T) {
t.Error(err)
}
}
// Begin tests for cdrc xml with new filters
func TestXmlIT4InitConfig(t *testing.T) {
var err error
xmlCfgPath = path.Join(*dataDir, "conf", "samples", "cdrcxmlwithfilter")
if xmlCfg, err = config.NewCGRConfigFromFolder(xmlCfgPath); err != nil {
t.Fatal("Got config error: ", err.Error())
}
}
// InitDb so we can rely on count
func TestXmlIT4InitCdrDb(t *testing.T) {
if err := engine.InitStorDb(xmlCfg); err != nil {
t.Fatal(err)
}
}
func TestXmlIT4CreateCdrDirs(t *testing.T) {
for _, cdrcProfiles := range xmlCfg.CdrcProfiles {
for _, cdrcInst := range cdrcProfiles {
for _, dir := range []string{cdrcInst.CdrInDir, cdrcInst.CdrOutDir} {
if err := os.RemoveAll(dir); err != nil {
t.Fatal("Error removing folder: ", dir, err)
}
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal("Error creating folder: ", dir, err)
}
}
if cdrcInst.ID == "msw_xml2" { // Initialize the folders to check later
xmlPathIn1 = cdrcInst.CdrInDir
xmlPathOut1 = cdrcInst.CdrOutDir
}
}
}
}
func TestXmlIT4StartEngine(t *testing.T) {
if _, err := engine.StopStartEngine(xmlCfgPath, *waitRater); err != nil {
t.Fatal(err)
}
}
// Connect rpc client to rater
func TestXmlIT4RpcConn(t *testing.T) {
var err error
cdrcXmlRPC, err = jsonrpc.Dial("tcp", xmlCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal("Could not connect to rater: ", err.Error())
}
}
// The default scenario, out of cdrc defined in .cfg file
func TestXmlIT4HandleCdr1File(t *testing.T) {
fileName := "file1.xml"
tmpFilePath := path.Join("/tmp", fileName)
if err := ioutil.WriteFile(tmpFilePath, []byte(xmlContent), 0644); err != nil {
t.Fatal(err.Error())
}
if err := os.Rename(tmpFilePath, path.Join(xmlPathIn1, fileName)); err != nil {
t.Fatal("Error moving file to processing directory: ", err)
}
}
func TestXmlIT4ProcessedFiles(t *testing.T) {
time.Sleep(time.Duration(2**waitRater) * time.Millisecond)
if outContent1, err := ioutil.ReadFile(path.Join(xmlPathOut1, "file1.xml")); err != nil {
t.Error(err)
} else if xmlContent != string(outContent1) {
t.Errorf("Expecting: %q, received: %q", xmlContent, string(outContent1))
}
}
func TestXmlIT4AnalyseCDRs(t *testing.T) {
var reply []*engine.ExternalCDR
if err := cdrcXmlRPC.Call("ApierV2.GetCdrs", utils.RPCCDRsFilter{}, &reply); err != nil {
t.Error("Unexpected error: ", err.Error())
} else if len(reply) != 2 {
t.Error("Unexpected number of CDRs returned: ", len(reply))
}
}
func TestXmlIT4KillEngine(t *testing.T) {
if err := engine.KillEngine(*waitRater); err != nil {
t.Error(err)
}
}

View File

@@ -64,6 +64,31 @@
{"tag": "Usage", "field_id": "Usage", "type": "*handler", "handler_id": "*substract_usage", "value": "~File.CDRs.Call.ReleaseTime;|;~File.CDRs.Call.ConnectTime", "mandatory": true}
],
},
{
"id": "msw_xml2", // identifier of the CDRC runner
"enabled": true, // enable CDR client functionality
"cdr_format": "xml", // CDR file format <csv|freeswitch_csv|fwv|opensips_flatstore|partial_csv.
"cdr_in_dir": "/tmp/cdrcxmlwithfilters3/xmlit3/in",
"cdr_out_dir": "/tmp/cdrcxmlwithfilters3/xmlit3/out",
"cdr_path": "File.CDRs.Call", // path towards one CDR element in case of XML CDRs
"cdr_source_id": "zw_cfs1", // free form field, tag identifying the source of the CDRs within CDRS database
"content_fields":[ // import content_fields template, id will match internally CDR field, in case of .csv value will be represented by index of the field value
{"tag": "TOR", "field_id": "ToR", "type": "*composed", "value": "*voice", "mandatory": true},
{"tag": "OriginID", "field_id": "OriginID", "type": "*composed", "value": "~File.CDRs.Call.SignalingInfo.PChargingVector.icidvalue", "mandatory": true},
{"tag": "OriginID2", "field_id": "OriginID", "type": "*composed", "value": "~File.CDRs.Call.OrigParty.SIPCallId:s/^(.*)@.*/$1/", "mandatory": true},
{"tag": "OriginHost", "field_id": "OriginHost", "type": "*composed", "value": "~File.CDRs.Call.OrigParty.SIPCallId:s/^.*@(.*)/$1/", "mandatory": true},
{"tag": "RequestType", "field_id": "RequestType", "type": "*composed", "value": "*rated", "mandatory": true},
{"tag": "Direction", "field_id": "Direction", "type": "*composed", "value": "*out", "mandatory": true},
{"tag": "Tenant", "field_id": "Tenant", "type": "*composed", "value": "XX.liquid.tel", "mandatory": true},
{"tag": "Category", "field_id": "Category", "type": "*composed", "value": "call", "mandatory": true},
{"tag": "Account", "field_id": "Account", "type": "*composed", "value": "~File.CDRs.Call.OrigParty.SubscriberAddr", "mandatory": true},
{"tag": "Subject", "field_id": "Subject", "type": "*composed", "value": "~File.CDRs.Call.OrigParty.SubscriberAddr", "mandatory": true},
{"tag": "Destination", "field_id": "Destination", "type": "*composed", "value": "~File.CDRs.Call.RoutingInfo.DestAddr", "mandatory": true},
{"tag": "SetupTime", "field_id": "SetupTime", "type": "*composed", "value": "~File.CDRs.Call.RingingTime", "mandatory": true},
{"tag": "AnswerTime", "field_id": "AnswerTime", "type": "*composed", "value": "~File.CDRs.Call.ConnectTime", "mandatory": true},
{"tag": "Usage", "field_id": "Usage", "type": "*handler", "handler_id": "*substract_usage", "value": "~File.CDRs.Call.ReleaseTime;|;~File.CDRs.Call.ConnectTime", "mandatory": true}
],
},
],

View File

@@ -209,23 +209,23 @@ func (cdr *CDR) ParseFieldValue(fieldId, fieldVal, timezone string) error {
case utils.OriginHost: // overwrite if originHost is given from template
cdr.OriginHost = fieldVal
case utils.ToR:
cdr.ToR += fieldVal
cdr.ToR = fieldVal
case utils.RunID:
cdr.RunID += fieldVal
cdr.RunID = fieldVal
case utils.OriginID:
cdr.OriginID += fieldVal
cdr.OriginID = fieldVal
case utils.RequestType:
cdr.RequestType += fieldVal
cdr.RequestType = fieldVal
case utils.Tenant:
cdr.Tenant += fieldVal
cdr.Tenant = fieldVal
case utils.Category:
cdr.Category += fieldVal
cdr.Category = fieldVal
case utils.Account:
cdr.Account += fieldVal
cdr.Account = fieldVal
case utils.Subject:
cdr.Subject += fieldVal
cdr.Subject = fieldVal
case utils.Destination:
cdr.Destination += fieldVal
cdr.Destination = fieldVal
case utils.PreRated:
cdr.PreRated, _ = strconv.ParseBool(fieldVal)
case utils.SetupTime:
@@ -247,7 +247,7 @@ func (cdr *CDR) ParseFieldValue(fieldId, fieldVal, timezone string) error {
case utils.Partial:
cdr.Partial, _ = strconv.ParseBool(fieldVal)
default: // Extra fields will not match predefined so they all show up here
cdr.ExtraFields[fieldId] += fieldVal
cdr.ExtraFields[fieldId] = fieldVal
}
return nil
}

View File

@@ -954,3 +954,22 @@ func TestCDRUpdateFromCGREvent(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", cdr, eCDR)
}
}
func TestCDRParseFieldValue2(t *testing.T) {
cdr := new(CDR)
if err := cdr.ParseFieldValue(utils.RunID, "*default", ""); err != nil {
t.Error(err)
} else if cdr.RunID != "*default" {
t.Errorf("Received cdr: %+v", cdr)
}
if err := cdr.ParseFieldValue(utils.OriginID, "FirstID", ""); err != nil {
t.Error(err)
} else if cdr.OriginID != "FirstID" {
t.Errorf("Received cdr: %+v", cdr)
}
if err := cdr.ParseFieldValue(utils.OriginID, "SecondID", ""); err != nil {
t.Error(err)
} else if cdr.OriginID != "SecondID" {
t.Errorf("Received cdr: %+v", cdr)
}
}