Updated v1.ArgExportCDRs

This commit is contained in:
Trial97
2019-11-28 10:31:54 +02:00
parent ee0956d05e
commit f38b986e64
4 changed files with 56 additions and 40 deletions

View File

@@ -188,16 +188,9 @@ func (apier *ApierV1) ReloadCdreConfig(attrs AttrReloadConfig, reply *string) er
// ArgExportCDRs are the arguments passed to ExportCDRs method
type ArgExportCDRs struct {
ExportTemplate *string // Exported fields template <""|fld1,fld2|>
ExportFormat *string
ExportPath *string
Synchronous *bool
Attempts *int
FieldSeparator *string
ExportID *string // Optional exportid
ExportFileName *string // If provided the output filename will be set to this
Verbose bool // Disable CgrIds reporting in reply/ExportedCgrIds and reply/UnexportedCgrIds
utils.RPCCDRsFilter // Inherit the CDR filter attributes
ExportArgs map[string]interface{}
Verbose bool // Disable CgrIds reporting in reply/ExportedCgrIds and reply/UnexportedCgrIds
utils.RPCCDRsFilter // Inherit the CDR filter attributes
}
// RplExportedCDRs contain the reply of the ExportCDRs API
@@ -215,54 +208,60 @@ func (api *ApierV1) ExportCDRs(arg ArgExportCDRs, reply *RplExportedCDRs) (err e
cdreReloadStruct := <-api.Config.ConfigReloads[utils.CDRE] // Read the content of the channel, locking it
defer func() { api.Config.ConfigReloads[utils.CDRE] <- cdreReloadStruct }() // Unlock reloads at exit
exportTemplate := api.Config.CdreProfiles[utils.META_DEFAULT]
if arg.ExportTemplate != nil && len(*arg.ExportTemplate) != 0 { // Export template prefered, use it
if expTemplate, has := arg.ExportArgs[utils.ExportTemplate]; has {
var hasIt bool
if exportTemplate, hasIt = api.Config.CdreProfiles[*arg.ExportTemplate]; !hasIt {
if exportTemplate, hasIt = api.Config.CdreProfiles[utils.IfaceAsString(expTemplate)]; !hasIt {
return fmt.Errorf("%s:ExportTemplate", utils.ErrNotFound)
}
}
exportFormat := exportTemplate.ExportFormat
if arg.ExportFormat != nil && len(*arg.ExportFormat) != 0 {
exportFormat = strings.ToLower(*arg.ExportFormat)
if expformat, has := arg.ExportArgs[utils.ExportFormat]; has {
exportFormat = strings.ToLower(utils.IfaceAsString(expformat))
}
if !utils.CDRExportFormats.Has(exportFormat) {
return utils.NewErrMandatoryIeMissing("CdrFormat")
}
synchronous := exportTemplate.Synchronous
if arg.Synchronous != nil {
synchronous = *arg.Synchronous
if sync, has := arg.ExportArgs[utils.Synchronous]; has {
if synchronous, err = utils.IfaceAsBool(sync); err != nil {
return
}
}
attempts := exportTemplate.Attempts
if arg.Attempts != nil && *arg.Attempts != 0 {
attempts = *arg.Attempts
if ate, has := arg.ExportArgs[utils.Attempts]; has {
var atte int64
if atte, err = utils.IfaceAsTInt64(ate); err != nil {
return
}
attempts = int(atte)
}
fieldSep := exportTemplate.FieldSeparator
if arg.FieldSeparator != nil && len(*arg.FieldSeparator) != 0 {
fieldSep, _ = utf8.DecodeRuneInString(*arg.FieldSeparator)
if fieldS, has := arg.ExportArgs[utils.FieldSeparator]; has {
fieldSep, _ = utf8.DecodeRuneInString(utils.IfaceAsString(fieldS))
if fieldSep == utf8.RuneError {
return fmt.Errorf("%s:FieldSeparator:%s", utils.ErrServerError, "Invalid")
}
}
eDir := exportTemplate.ExportPath
if arg.ExportPath != nil && len(*arg.ExportPath) != 0 {
eDir = *arg.ExportPath
if expPath, has := arg.ExportArgs[utils.ExportPath]; has {
eDir = utils.IfaceAsString(expPath)
}
exportID := strconv.FormatInt(time.Now().Unix(), 10)
if arg.ExportID != nil && len(*arg.ExportID) != 0 {
exportID = *arg.ExportID
if expID, has := arg.ExportArgs[utils.ExportID]; has {
exportID = utils.IfaceAsString(expID)
}
var expFormat string
switch exportFormat {
case utils.MetaFileFWV:
expFormat = "fwv"
expFormat = utils.FWV
case utils.MetaFileCSV:
expFormat = "csv"
expFormat = utils.CSV
default:
expFormat = exportFormat
}
fileName := fmt.Sprintf("cdre_%s.%s", exportID, expFormat)
if arg.ExportFileName != nil && len(*arg.ExportFileName) != 0 {
fileName = *arg.ExportFileName
if expFn, has := arg.ExportArgs[utils.ExportFileName]; has {
fileName = utils.IfaceAsString(expFn)
}
var filePath string
switch exportFormat {

View File

@@ -157,8 +157,10 @@ func testCDReAddCDRs(t *testing.T) {
func testCDReExportCDRs(t *testing.T) {
attr := ArgExportCDRs{
ExportTemplate: utils.StringPointer("TemplateWithFilter"),
Verbose: true,
ExportArgs: map[string]interface{}{
utils.ExportTemplate: "TemplateWithFilter",
},
Verbose: true,
}
var rply *RplExportedCDRs
if err := cdreRPC.Call(utils.ApierV1ExportCDRs, attr, &rply); err != nil {
@@ -309,8 +311,10 @@ func testCDReAddAttributes(t *testing.T) {
func testCDReExportCDRsWithAttributes(t *testing.T) {
attr := ArgExportCDRs{
ExportTemplate: utils.StringPointer("TemplateWithAttributeS"),
Verbose: true,
ExportArgs: map[string]interface{}{
utils.ExportTemplate: "TemplateWithAttributeS",
},
Verbose: true,
}
var rply *RplExportedCDRs
if err := cdreRPC.Call(utils.ApierV1ExportCDRs, attr, &rply); err != nil {

View File

@@ -117,10 +117,12 @@ func testCDREGetCdrs(t *testing.T) {
func testCDREExportNotFound(t *testing.T) {
var replyExport v1.RplExportedCDRs
exportArgs := v1.ArgExportCDRs{
ExportPath: utils.StringPointer("/tmp"),
ExportFileName: utils.StringPointer("TestTutITExportCDR.csv"),
ExportTemplate: utils.StringPointer("TestTutITExportCDR"),
RPCCDRsFilter: utils.RPCCDRsFilter{},
ExportArgs: map[string]interface{}{
utils.ExportPath: "/tmp",
utils.ExportFileName: "TestTutITExportCDR.csv",
utils.ExportTemplate: "TestTutITExportCDR",
},
RPCCDRsFilter: utils.RPCCDRsFilter{},
}
if err := cdreRPC.Call(utils.ApierV1ExportCDRs, exportArgs,
&replyExport); err.Error() != utils.ErrNotFound.Error() {
@@ -151,10 +153,12 @@ func testCDREExport(t *testing.T) {
// time.Sleep(100 * time.Millisecond)
var replyExport v1.RplExportedCDRs
exportArgs := v1.ArgExportCDRs{
ExportPath: utils.StringPointer("/tmp"),
ExportFileName: utils.StringPointer("TestTutITExportCDR.csv"),
ExportTemplate: utils.StringPointer("TestTutITExportCDR"),
RPCCDRsFilter: utils.RPCCDRsFilter{},
ExportArgs: map[string]interface{}{
utils.ExportPath: "/tmp",
utils.ExportFileName: "TestTutITExportCDR.csv",
utils.ExportTemplate: "TestTutITExportCDR",
},
RPCCDRsFilter: utils.RPCCDRsFilter{},
}
if err := cdreRPC.Call(utils.ApierV1ExportCDRs, exportArgs, &replyExport); err != nil {
t.Error(err)

View File

@@ -188,6 +188,7 @@ const (
META_DEFAULT = "*default"
STATIC_VALUE_PREFIX = "^"
CSV = "csv"
FWV = "fwv"
MetaPartialCSV = "*partial_csv"
DRYRUN = "dry_run"
META_COMBIMED = "*combimed"
@@ -563,6 +564,14 @@ const (
MetaAct = "*act"
DestinationPrefix = "DestinationPrefix"
DestinationID = "DestinationID"
ExportTemplate = "ExportTemplate"
ExportFormat = "ExportFormat"
Synchronous = "Synchronous"
Attempts = "Attempts"
FieldSeparator = "FieldSeparator"
ExportPath = "ExportPath"
ExportID = "ExportID"
ExportFileName = "ExportFileName"
)
// Migrator Action