mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-25 09:08:45 +05:00
Remove CDRE and add in EventExporter support for specific Exporters
This commit is contained in:
committed by
Dan Christian Bogos
parent
2379533337
commit
3faa3a10f4
555
engine/cdre.go
555
engine/cdre.go
@@ -1,555 +0,0 @@
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package engine
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
metaExportID = "*export_id"
|
||||
metaTimeNow = "*time_now"
|
||||
metaFirstCDRAtime = "*first_cdr_atime"
|
||||
metaLastCDRAtime = "*last_cdr_atime"
|
||||
metaNrCDRs = "*cdrs_number"
|
||||
metaDurCDRs = "*cdrs_duration"
|
||||
metaSMSUsage = "*sms_usage"
|
||||
metaMMSUsage = "*mms_usage"
|
||||
metaGenericUsage = "*generic_usage"
|
||||
metaDataUsage = "*data_usage"
|
||||
metaCostCDRs = "*cdrs_cost"
|
||||
)
|
||||
|
||||
// NewCDRExporter returns a new CDRExporter
|
||||
func NewCDRExporter(cdrs []*CDR, exportTemplate *config.CdreCfg, exportFormat, exportPath, fallbackPath, exportID string,
|
||||
synchronous bool, attempts int, fieldSeparator rune,
|
||||
httpSkipTLSCheck bool, attrsConns []string, filterS *FilterS) (*CDRExporter, error) {
|
||||
if len(cdrs) == 0 { // Nothing to export
|
||||
return nil, nil
|
||||
}
|
||||
cdre := &CDRExporter{
|
||||
cdrs: cdrs,
|
||||
exportTemplate: exportTemplate,
|
||||
exportFormat: exportFormat,
|
||||
exportPath: exportPath,
|
||||
fallbackPath: fallbackPath,
|
||||
exportID: exportID,
|
||||
synchronous: synchronous,
|
||||
attempts: attempts,
|
||||
fieldSeparator: fieldSeparator,
|
||||
httpSkipTLSCheck: httpSkipTLSCheck,
|
||||
negativeExports: make(map[string]string),
|
||||
attrsConns: attrsConns,
|
||||
filterS: filterS,
|
||||
}
|
||||
return cdre, nil
|
||||
}
|
||||
|
||||
// CDRExporter used to export the CDRs
|
||||
type CDRExporter struct {
|
||||
sync.RWMutex
|
||||
cdrs []*CDR
|
||||
exportTemplate *config.CdreCfg
|
||||
exportFormat string
|
||||
exportPath string
|
||||
fallbackPath string // folder where we save failed CDRs
|
||||
exportID string // Unique identifier or this export
|
||||
synchronous bool
|
||||
attempts int
|
||||
fieldSeparator rune
|
||||
httpSkipTLSCheck bool
|
||||
|
||||
header, trailer []string // Header and Trailer fields
|
||||
content [][]string // Rows of cdr fields
|
||||
|
||||
firstCdrATime, lastCdrATime time.Time
|
||||
numberOfRecords int
|
||||
totalDuration, totalDataUsage, totalSmsUsage,
|
||||
totalMmsUsage, totalGenericUsage time.Duration
|
||||
totalCost float64
|
||||
firstExpOrderID, lastExpOrderID int64
|
||||
positiveExports []string // CGRIDs of successfully exported CDRs
|
||||
negativeExports map[string]string // CGRIDs of failed exports
|
||||
|
||||
attrsConns []string
|
||||
filterS *FilterS
|
||||
}
|
||||
|
||||
// Handle various meta functions used in header/trailer
|
||||
func (cdre *CDRExporter) metaHandler(tag, arg string) (string, error) {
|
||||
switch tag {
|
||||
case metaExportID:
|
||||
return cdre.exportID, nil
|
||||
case metaTimeNow:
|
||||
return time.Now().Format(arg), nil
|
||||
case metaFirstCDRAtime:
|
||||
return cdre.firstCdrATime.Format(arg), nil
|
||||
case metaLastCDRAtime:
|
||||
return cdre.lastCdrATime.Format(arg), nil
|
||||
case metaNrCDRs:
|
||||
return strconv.Itoa(cdre.numberOfRecords), nil
|
||||
case metaDurCDRs: // ToDo: remove this because they are useless
|
||||
cdr := &CDR{ToR: utils.VOICE, Usage: cdre.totalDuration}
|
||||
return cdr.FieldAsString(&config.RSRParser{Rules: utils.DynamicDataPrefix + utils.Usage})
|
||||
case metaSMSUsage:
|
||||
cdr := &CDR{ToR: utils.SMS, Usage: cdre.totalDuration}
|
||||
return cdr.FieldAsString(&config.RSRParser{Rules: utils.DynamicDataPrefix + utils.Usage})
|
||||
case metaMMSUsage:
|
||||
cdr := &CDR{ToR: utils.MMS, Usage: cdre.totalDuration}
|
||||
return cdr.FieldAsString(&config.RSRParser{Rules: utils.DynamicDataPrefix + utils.Usage})
|
||||
case metaGenericUsage:
|
||||
cdr := &CDR{ToR: utils.GENERIC, Usage: cdre.totalDuration}
|
||||
return cdr.FieldAsString(&config.RSRParser{Rules: utils.DynamicDataPrefix + utils.Usage})
|
||||
case metaDataUsage:
|
||||
cdr := &CDR{ToR: utils.DATA, Usage: cdre.totalDuration}
|
||||
return cdr.FieldAsString(&config.RSRParser{Rules: utils.DynamicDataPrefix + utils.Usage})
|
||||
case metaCostCDRs:
|
||||
return strconv.FormatFloat(utils.Round(cdre.totalCost,
|
||||
globalRoundingDecimals, utils.ROUNDING_MIDDLE), 'f', -1, 64), nil
|
||||
default:
|
||||
return "", fmt.Errorf("Unsupported METATAG: %s", tag)
|
||||
}
|
||||
}
|
||||
|
||||
// Compose and cache the header
|
||||
func (cdre *CDRExporter) composeHeader() (err error) {
|
||||
for _, cfgFld := range cdre.exportTemplate.Fields {
|
||||
if !strings.HasPrefix(cfgFld.Path, utils.MetaHdr) {
|
||||
continue
|
||||
}
|
||||
if len(cfgFld.Filters) != 0 {
|
||||
//check filter if pass
|
||||
}
|
||||
var outVal string
|
||||
switch cfgFld.Type {
|
||||
case utils.META_FILLER:
|
||||
out, err := cfgFld.Value.ParseValue(utils.EmptyString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outVal = out
|
||||
cfgFld.Padding = utils.MetaRight
|
||||
case utils.META_CONSTANT:
|
||||
out, err := cfgFld.Value.ParseValue(utils.EmptyString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outVal = out
|
||||
case utils.META_HANDLER:
|
||||
out, err := cfgFld.Value.ParseValue(utils.EmptyString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outVal, err = cdre.metaHandler(out, cfgFld.Layout)
|
||||
default:
|
||||
return fmt.Errorf("Unsupported field type: %s", cfgFld.Type)
|
||||
}
|
||||
if err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<CdreFw> Cannot export CDR header, field %s, error: %s", cfgFld.Tag, err.Error()))
|
||||
return err
|
||||
}
|
||||
fmtOut := outVal
|
||||
if fmtOut, err = utils.FmtFieldWidth(cfgFld.Tag, outVal, cfgFld.Width, cfgFld.Strip, cfgFld.Padding, cfgFld.Mandatory); err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<CdreFw> Cannot export CDR header, field %s, error: %s", cfgFld.Tag, err.Error()))
|
||||
return err
|
||||
}
|
||||
cdre.Lock()
|
||||
cdre.header = append(cdre.header, fmtOut)
|
||||
cdre.Unlock()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Compose and cache the trailer
|
||||
func (cdre *CDRExporter) composeTrailer() (err error) {
|
||||
for _, cfgFld := range cdre.exportTemplate.Fields {
|
||||
if !strings.HasPrefix(cfgFld.Path, utils.MetaTrl) {
|
||||
continue
|
||||
}
|
||||
if len(cfgFld.Filters) != 0 {
|
||||
//check filter if pass
|
||||
}
|
||||
var outVal string
|
||||
switch cfgFld.Type {
|
||||
case utils.META_FILLER:
|
||||
out, err := cfgFld.Value.ParseValue(utils.EmptyString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outVal = out
|
||||
cfgFld.Padding = utils.MetaRight
|
||||
case utils.META_CONSTANT:
|
||||
out, err := cfgFld.Value.ParseValue(utils.EmptyString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outVal = out
|
||||
case utils.META_HANDLER:
|
||||
out, err := cfgFld.Value.ParseValue(utils.EmptyString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outVal, err = cdre.metaHandler(out, cfgFld.Layout)
|
||||
default:
|
||||
return fmt.Errorf("Unsupported field type: %s", cfgFld.Type)
|
||||
}
|
||||
if err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<CdreFw> Cannot export CDR trailer, field: %s, error: %s", cfgFld.Tag, err.Error()))
|
||||
return err
|
||||
}
|
||||
fmtOut := outVal
|
||||
if fmtOut, err = utils.FmtFieldWidth(cfgFld.Tag, outVal, cfgFld.Width, cfgFld.Strip, cfgFld.Padding, cfgFld.Mandatory); err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<CdreFw> Cannot export CDR trailer, field: %s, error: %s", cfgFld.Tag, err.Error()))
|
||||
return err
|
||||
}
|
||||
cdre.Lock()
|
||||
cdre.trailer = append(cdre.trailer, fmtOut)
|
||||
cdre.Unlock()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cdre *CDRExporter) postCdr(cdr *CDR) (err error) {
|
||||
var body interface{}
|
||||
switch cdre.exportFormat {
|
||||
case utils.MetaHTTPjsonCDR, utils.MetaAMQPjsonCDR:
|
||||
if body, err = json.Marshal(cdr); err != nil {
|
||||
return
|
||||
}
|
||||
case utils.MetaHTTPjsonMap, utils.MetaAMQPjsonMap, utils.MetaAMQPV1jsonMap, utils.MetaSQSjsonMap, utils.MetaKafkajsonMap, utils.MetaS3jsonMap:
|
||||
var expMp map[string]string
|
||||
if expMp, err = cdr.AsExportMap(cdre.exportTemplate.Fields, cdre.httpSkipTLSCheck, nil, cdre.filterS); err != nil {
|
||||
return
|
||||
}
|
||||
if body, err = json.Marshal(expMp); err != nil {
|
||||
return
|
||||
}
|
||||
case utils.MetaHTTPPost:
|
||||
var expMp map[string]string
|
||||
if expMp, err = cdr.AsExportMap(cdre.exportTemplate.Fields, cdre.httpSkipTLSCheck, nil, cdre.filterS); err != nil {
|
||||
return
|
||||
}
|
||||
vals := url.Values{}
|
||||
for fld, val := range expMp {
|
||||
vals.Set(fld, val)
|
||||
}
|
||||
body = vals
|
||||
default:
|
||||
return fmt.Errorf("unsupported exportFormat: <%s>", cdre.exportFormat)
|
||||
}
|
||||
switch cdre.exportFormat {
|
||||
case utils.MetaHTTPjsonCDR, utils.MetaHTTPjsonMap, utils.MetaHTTPjson, utils.MetaHTTPPost:
|
||||
var pstr *HTTPPoster
|
||||
pstr, err = NewHTTPPoster(config.CgrConfig().GeneralCfg().HttpSkipTlsVerify,
|
||||
config.CgrConfig().GeneralCfg().ReplyTimeout, cdre.exportPath,
|
||||
utils.PosterTransportContentTypes[cdre.exportFormat], cdre.attempts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = pstr.Post(body, utils.EmptyString)
|
||||
case utils.MetaAMQPjsonCDR, utils.MetaAMQPjsonMap:
|
||||
err = PostersCache.PostAMQP(cdre.exportPath, cdre.attempts, body.([]byte))
|
||||
case utils.MetaAMQPV1jsonMap:
|
||||
err = PostersCache.PostAMQPv1(cdre.exportPath, cdre.attempts, body.([]byte))
|
||||
case utils.MetaSQSjsonMap:
|
||||
err = PostersCache.PostSQS(cdre.exportPath, cdre.attempts, body.([]byte))
|
||||
case utils.MetaKafkajsonMap:
|
||||
err = PostersCache.PostKafka(cdre.exportPath, cdre.attempts, body.([]byte), utils.ConcatenatedKey(cdr.CGRID, cdr.RunID))
|
||||
case utils.MetaS3jsonMap:
|
||||
err = PostersCache.PostS3(cdre.exportPath, cdre.attempts, body.([]byte), utils.ConcatenatedKey(cdr.CGRID, cdr.RunID))
|
||||
}
|
||||
if err != nil && cdre.fallbackPath != utils.META_NONE {
|
||||
AddFailedPost(cdre.exportPath, cdre.exportFormat, utils.CDRPoster, body)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Write individual cdr into content buffer, build stats
|
||||
func (cdre *CDRExporter) processCDR(cdr *CDR) (err error) {
|
||||
if cdr.ExtraFields == nil { // Avoid assignment in nil map if not initialized
|
||||
cdr.ExtraFields = make(map[string]string)
|
||||
}
|
||||
// send the cdr to be processed by attributeS
|
||||
if cdre.exportTemplate.AttributeSContext != utils.EmptyString {
|
||||
if len(cdre.attrsConns) == 0 {
|
||||
return errors.New("no connection to AttributeS")
|
||||
}
|
||||
cdrEv := cdr.AsCGREvent()
|
||||
args := &AttrArgsProcessEvent{
|
||||
Context: utils.StringPointer(utils.FirstNonEmpty(
|
||||
utils.IfaceAsString(cdrEv.Event[utils.OptsContext]),
|
||||
cdre.exportTemplate.AttributeSContext)),
|
||||
CGREvent: cdrEv,
|
||||
}
|
||||
var evReply AttrSProcessEventReply
|
||||
if err = connMgr.Call(cdre.attrsConns, nil,
|
||||
utils.AttributeSv1ProcessEvent,
|
||||
args, &evReply); err != nil {
|
||||
return
|
||||
}
|
||||
if len(evReply.AlteredFields) != 0 {
|
||||
if err = cdr.UpdateFromCGREvent(evReply.CGREvent, evReply.AlteredFields); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch cdre.exportFormat {
|
||||
case utils.MetaFileFWV, utils.MetaFileCSV:
|
||||
var cdrRow []string
|
||||
cdrRow, err = cdr.AsExportRecord(cdre.exportTemplate.Fields, cdre.httpSkipTLSCheck, cdre.cdrs, cdre.filterS)
|
||||
if len(cdrRow) == 0 && err == nil { // No CDR data, most likely no configuration fields defined
|
||||
return
|
||||
}
|
||||
cdre.Lock()
|
||||
cdre.content = append(cdre.content, cdrRow)
|
||||
cdre.Unlock()
|
||||
default: // attempt posting CDR
|
||||
err = cdre.postCdr(cdr)
|
||||
}
|
||||
if err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<CDRE> Cannot export CDR with CGRID: %s and runid: %s, error: %s", cdr.CGRID, cdr.RunID, err.Error()))
|
||||
return
|
||||
}
|
||||
// Done with writing content, compute stats here
|
||||
cdre.Lock()
|
||||
defer cdre.Unlock()
|
||||
if cdre.firstCdrATime.IsZero() || cdr.AnswerTime.Before(cdre.firstCdrATime) {
|
||||
cdre.firstCdrATime = cdr.AnswerTime
|
||||
}
|
||||
if cdr.AnswerTime.After(cdre.lastCdrATime) {
|
||||
cdre.lastCdrATime = cdr.AnswerTime
|
||||
}
|
||||
cdre.numberOfRecords++
|
||||
if cdr.ToR == utils.VOICE { // Only count duration for non data cdrs
|
||||
cdre.totalDuration += cdr.Usage
|
||||
}
|
||||
if cdr.ToR == utils.SMS { // Count usage for SMS
|
||||
cdre.totalSmsUsage += cdr.Usage
|
||||
}
|
||||
if cdr.ToR == utils.MMS { // Count usage for MMS
|
||||
cdre.totalMmsUsage += cdr.Usage
|
||||
}
|
||||
if cdr.ToR == utils.GENERIC { // Count usage for GENERIC
|
||||
cdre.totalGenericUsage += cdr.Usage
|
||||
}
|
||||
if cdr.ToR == utils.DATA { // Count usage for DATA
|
||||
cdre.totalDataUsage += cdr.Usage
|
||||
}
|
||||
if cdr.Cost != -1 {
|
||||
cdre.totalCost += cdr.Cost
|
||||
cdre.totalCost = utils.Round(cdre.totalCost, globalRoundingDecimals, utils.ROUNDING_MIDDLE)
|
||||
}
|
||||
if cdre.firstExpOrderID > cdr.OrderID || cdre.firstExpOrderID == 0 {
|
||||
cdre.firstExpOrderID = cdr.OrderID
|
||||
}
|
||||
if cdre.lastExpOrderID < cdr.OrderID {
|
||||
cdre.lastExpOrderID = cdr.OrderID
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// processCDRs proccess every cdr
|
||||
func (cdre *CDRExporter) processCDRs() (err error) {
|
||||
var wg sync.WaitGroup
|
||||
isSync := cdre.exportTemplate.Synchronous ||
|
||||
utils.SliceHasMember([]string{utils.MetaFileCSV, utils.MetaFileFWV}, cdre.exportTemplate.ExportFormat)
|
||||
for _, cdr := range cdre.cdrs {
|
||||
if cdr == nil || len(cdr.CGRID) == 0 { // CDR needs to exist and it's CGRID needs to be populated
|
||||
continue
|
||||
}
|
||||
if len(cdre.exportTemplate.Filters) != 0 {
|
||||
cgrDp := utils.MapStorage{
|
||||
utils.MetaReq: cdr.AsMapStringIface(),
|
||||
utils.MetaEC: cdr.CostDetails,
|
||||
}
|
||||
if pass, err := cdre.filterS.Pass(cdre.exportTemplate.Tenant,
|
||||
cdre.exportTemplate.Filters, cgrDp); err != nil {
|
||||
return err
|
||||
} else if !pass {
|
||||
continue // Not passes filters, ignore this CDR
|
||||
}
|
||||
}
|
||||
if isSync {
|
||||
wg.Add(1) // wait for synchronous or file ones since these need to be done before continuing
|
||||
}
|
||||
go func(cdre *CDRExporter, cdr *CDR) {
|
||||
if err := cdre.processCDR(cdr); err != nil {
|
||||
cdre.Lock()
|
||||
cdre.negativeExports[cdr.CGRID] = err.Error()
|
||||
cdre.Unlock()
|
||||
} else {
|
||||
cdre.Lock()
|
||||
cdre.positiveExports = append(cdre.positiveExports, cdr.CGRID)
|
||||
cdre.Unlock()
|
||||
}
|
||||
if isSync {
|
||||
wg.Done()
|
||||
}
|
||||
}(cdre, cdr)
|
||||
}
|
||||
wg.Wait()
|
||||
return
|
||||
}
|
||||
|
||||
// Simple write method
|
||||
func (cdre *CDRExporter) writeOut(ioWriter io.Writer) (err error) {
|
||||
cdre.Lock()
|
||||
defer cdre.Unlock()
|
||||
if len(cdre.header) != 0 {
|
||||
for _, fld := range append(cdre.header, "\n") {
|
||||
if _, err = io.WriteString(ioWriter, fld); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, cdrContent := range cdre.content {
|
||||
for _, cdrFld := range append(cdrContent, "\n") {
|
||||
if _, err = io.WriteString(ioWriter, cdrFld); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(cdre.trailer) != 0 {
|
||||
for _, fld := range append(cdre.trailer, "\n") {
|
||||
if _, err = io.WriteString(ioWriter, fld); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// csvWriter specific method
|
||||
func (cdre *CDRExporter) writeCsv(csvWriter *csv.Writer) (err error) {
|
||||
csvWriter.Comma = cdre.fieldSeparator
|
||||
cdre.RLock()
|
||||
defer cdre.RUnlock()
|
||||
if len(cdre.header) != 0 {
|
||||
if err = csvWriter.Write(cdre.header); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
for _, cdrContent := range cdre.content {
|
||||
if err = csvWriter.Write(cdrContent); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(cdre.trailer) != 0 {
|
||||
if err = csvWriter.Write(cdre.trailer); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
csvWriter.Flush()
|
||||
return
|
||||
}
|
||||
|
||||
// ExportCDRs exports the given CDRs
|
||||
func (cdre *CDRExporter) ExportCDRs() (err error) {
|
||||
if err = cdre.processCDRs(); err != nil {
|
||||
return
|
||||
}
|
||||
if utils.SliceHasMember([]string{utils.MetaFileCSV, utils.MetaFileFWV}, cdre.exportFormat) { // files are written after processing all CDRs
|
||||
cdre.RLock()
|
||||
contLen := len(cdre.content)
|
||||
cdre.RUnlock()
|
||||
if contLen == 0 {
|
||||
return
|
||||
}
|
||||
if err = cdre.composeHeader(); err != nil {
|
||||
return
|
||||
}
|
||||
if err = cdre.composeTrailer(); err != nil {
|
||||
return
|
||||
}
|
||||
var expFormat string
|
||||
switch cdre.exportFormat {
|
||||
case utils.MetaFileFWV:
|
||||
expFormat = "fwv"
|
||||
case utils.MetaFileCSV:
|
||||
expFormat = "csv"
|
||||
default:
|
||||
expFormat = cdre.exportFormat
|
||||
}
|
||||
expPath := cdre.exportPath
|
||||
if len(filepath.Ext(expPath)) == 0 { // verify extension from exportPath (if have extension is file else is directory)
|
||||
fileName := fmt.Sprintf("cdre_%s.%s", utils.UUIDSha1Prefix(), expFormat)
|
||||
expPath = path.Join(expPath, fileName)
|
||||
}
|
||||
var fileOut *os.File
|
||||
if fileOut, err = os.Create(expPath); err != nil {
|
||||
return
|
||||
}
|
||||
defer fileOut.Close()
|
||||
if cdre.exportFormat == utils.MetaFileCSV {
|
||||
return cdre.writeCsv(csv.NewWriter(fileOut))
|
||||
}
|
||||
return cdre.writeOut(fileOut)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// FirstOrderID returns the first exported Cdr OrderId
|
||||
func (cdre *CDRExporter) FirstOrderID() int64 {
|
||||
return cdre.firstExpOrderID
|
||||
}
|
||||
|
||||
// LastOrderID return the last exported Cdr OrderId
|
||||
func (cdre *CDRExporter) LastOrderID() int64 {
|
||||
return cdre.lastExpOrderID
|
||||
}
|
||||
|
||||
// TotalCost returns the total cost in the exported cdrs
|
||||
func (cdre *CDRExporter) TotalCost() float64 {
|
||||
return cdre.totalCost
|
||||
}
|
||||
|
||||
// TotalExportedCdrs returns the number of exported CDRs
|
||||
func (cdre *CDRExporter) TotalExportedCdrs() int {
|
||||
return cdre.numberOfRecords
|
||||
}
|
||||
|
||||
// PositiveExports returns the successfully exported CGRIDs
|
||||
func (cdre *CDRExporter) PositiveExports() []string {
|
||||
cdre.RLock()
|
||||
defer cdre.RUnlock()
|
||||
return cdre.positiveExports
|
||||
}
|
||||
|
||||
// NegativeExports returns the failed exported CGRIDs together with the reason
|
||||
func (cdre *CDRExporter) NegativeExports() map[string]string {
|
||||
cdre.RLock()
|
||||
defer cdre.RUnlock()
|
||||
return cdre.negativeExports
|
||||
}
|
||||
@@ -1,530 +0,0 @@
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestCsvCdrWriter(t *testing.T) {
|
||||
writer := &bytes.Buffer{}
|
||||
cfg, _ := config.NewDefaultCGRConfig()
|
||||
storedCdr1 := &CDR{
|
||||
CGRID: utils.Sha1("dsafdsaf", time.Unix(1383813745, 0).UTC().String()),
|
||||
ToR: utils.VOICE, OriginID: "dsafdsaf", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Unix(1383813745, 0).UTC(),
|
||||
AnswerTime: time.Unix(1383813746, 0).UTC(),
|
||||
Usage: time.Duration(10) * time.Second,
|
||||
RunID: utils.MetaDefault, Cost: 1.01,
|
||||
ExtraFields: map[string]string{"extra1": "val_extra1",
|
||||
"extra2": "val_extra2", "extra3": "val_extra3"},
|
||||
}
|
||||
cdre, err := NewCDRExporter([]*CDR{storedCdr1},
|
||||
cfg.CdreProfiles[utils.MetaDefault], utils.MetaFileCSV, "", "", "firstexport",
|
||||
true, 1, utils.CSV_SEP, cfg.GeneralCfg().HttpSkipTlsVerify, nil, nil)
|
||||
if err != nil {
|
||||
t.Error("Unexpected error received: ", err)
|
||||
}
|
||||
if err = cdre.processCDRs(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeHeader(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeTrailer(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
csvWriter := csv.NewWriter(writer)
|
||||
if err := cdre.writeCsv(csvWriter); err != nil {
|
||||
t.Error("Unexpected error: ", err)
|
||||
}
|
||||
expected := `dbafe9c8614c785a65aabd116dd3959c3c56f7f6,*default,*voice,dsafdsaf,*rated,cgrates.org,call,1001,1001,1002,2013-11-07T08:42:25Z,2013-11-07T08:42:26Z,10s,1.0100`
|
||||
result := strings.TrimSpace(writer.String())
|
||||
if result != expected {
|
||||
t.Errorf("Expected: \n%s \n received: \n%s.", expected, result)
|
||||
}
|
||||
if cdre.TotalCost() != 1.01 {
|
||||
t.Error("Unexpected TotalCost: ", cdre.TotalCost())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlternativeFieldSeparator(t *testing.T) {
|
||||
writer := &bytes.Buffer{}
|
||||
cfg, _ := config.NewDefaultCGRConfig()
|
||||
storedCdr1 := &CDR{
|
||||
CGRID: utils.Sha1("dsafdsaf", time.Unix(1383813745, 0).UTC().String()),
|
||||
ToR: utils.VOICE, OriginID: "dsafdsaf", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Unix(1383813745, 0).UTC(),
|
||||
AnswerTime: time.Unix(1383813746, 0).UTC(),
|
||||
Usage: time.Duration(10) * time.Second,
|
||||
RunID: utils.MetaDefault, Cost: 1.01,
|
||||
ExtraFields: map[string]string{"extra1": "val_extra1",
|
||||
"extra2": "val_extra2", "extra3": "val_extra3"},
|
||||
}
|
||||
cdre, err := NewCDRExporter([]*CDR{storedCdr1}, cfg.CdreProfiles[utils.MetaDefault],
|
||||
utils.MetaFileCSV, "", "", "firstexport", true, 1, '|',
|
||||
cfg.GeneralCfg().HttpSkipTlsVerify, nil, nil)
|
||||
if err != nil {
|
||||
t.Error("Unexpected error received: ", err)
|
||||
}
|
||||
if err = cdre.processCDRs(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeHeader(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeTrailer(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
csvWriter := csv.NewWriter(writer)
|
||||
if err := cdre.writeCsv(csvWriter); err != nil {
|
||||
t.Error("Unexpected error: ", err)
|
||||
}
|
||||
expected := `dbafe9c8614c785a65aabd116dd3959c3c56f7f6|*default|*voice|dsafdsaf|*rated|cgrates.org|call|1001|1001|1002|2013-11-07T08:42:25Z|2013-11-07T08:42:26Z|10s|1.0100`
|
||||
result := strings.TrimSpace(writer.String())
|
||||
if result != expected {
|
||||
t.Errorf("Expected: \n%s received: \n%s.", expected, result)
|
||||
}
|
||||
if cdre.TotalCost() != 1.01 {
|
||||
t.Error("Unexpected TotalCost: ", cdre.TotalCost())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportVoiceWithConvert(t *testing.T) {
|
||||
writer := &bytes.Buffer{}
|
||||
cfg, _ := config.NewDefaultCGRConfig()
|
||||
cdreCfg := cfg.CdreProfiles[utils.MetaDefault]
|
||||
cdreCfg.Fields = []*config.FCTemplate{
|
||||
{
|
||||
Tag: "*exp.ToR",
|
||||
Path: "*exp.ToR",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"ToR", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.OriginID",
|
||||
Path: "*exp.OriginID",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"OriginID", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.RequestType",
|
||||
Path: "*exp.RequestType",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"RequestType", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Tenant",
|
||||
Path: "*exp.Tenant",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Tenant", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Category",
|
||||
Path: "*exp.Category",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Category", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Account",
|
||||
Path: "*exp.Account",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Account", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Destination",
|
||||
Path: "*exp.Destination",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Destination", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.AnswerTime",
|
||||
Path: "*exp.AnswerTime",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"AnswerTime", utils.INFIELD_SEP),
|
||||
Layout: "2006-01-02T15:04:05Z07:00"},
|
||||
{
|
||||
Tag: "*exp.UsageVoice",
|
||||
Path: "*exp.UsageVoice",
|
||||
Type: "*composed",
|
||||
Filters: []string{"*string:~*req.ToR:*voice"},
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Usage{*duration_seconds}", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.UsageData",
|
||||
Path: "*exp.UsageData",
|
||||
Type: "*composed",
|
||||
Filters: []string{"*string:~*req.ToR:*data"},
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Usage{*duration_nanoseconds}", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.UsageSMS",
|
||||
Path: "*exp.UsageSMS",
|
||||
Type: "*composed",
|
||||
Filters: []string{"*string:~*req.ToR:*sms"},
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Usage{*duration_nanoseconds}", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Cost",
|
||||
Path: "*exp.Cost",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Cost", utils.INFIELD_SEP),
|
||||
RoundingDecimals: utils.IntPointer(5)},
|
||||
}
|
||||
cdrVoice := &CDR{
|
||||
CGRID: utils.Sha1("dsafdsaf", time.Unix(1383813745, 0).UTC().String()),
|
||||
ToR: utils.VOICE, OriginID: "dsafdsaf", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Unix(1383813745, 0).UTC(),
|
||||
AnswerTime: time.Unix(1383813746, 0).UTC(),
|
||||
Usage: time.Duration(10) * time.Second,
|
||||
RunID: utils.MetaDefault, Cost: 1.01,
|
||||
ExtraFields: map[string]string{"extra1": "val_extra1",
|
||||
"extra2": "val_extra2", "extra3": "val_extra3"},
|
||||
}
|
||||
cdrData := &CDR{
|
||||
CGRID: utils.Sha1("abcdef", time.Unix(1383813745, 0).UTC().String()),
|
||||
ToR: utils.DATA, OriginID: "abcdef", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Unix(1383813745, 0).UTC(),
|
||||
AnswerTime: time.Unix(1383813746, 0).UTC(),
|
||||
Usage: time.Duration(10) * time.Nanosecond,
|
||||
RunID: utils.MetaDefault, Cost: 0.012,
|
||||
ExtraFields: map[string]string{"extra1": "val_extra1",
|
||||
"extra2": "val_extra2", "extra3": "val_extra3"},
|
||||
}
|
||||
cdrSMS := &CDR{
|
||||
CGRID: utils.Sha1("sdfwer", time.Unix(1383813745, 0).UTC().String()),
|
||||
ToR: utils.SMS, OriginID: "sdfwer", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Unix(1383813745, 0).UTC(),
|
||||
AnswerTime: time.Unix(1383813746, 0).UTC(),
|
||||
Usage: time.Duration(1),
|
||||
RunID: utils.MetaDefault, Cost: 0.15,
|
||||
ExtraFields: map[string]string{"extra1": "val_extra1",
|
||||
"extra2": "val_extra2", "extra3": "val_extra3"},
|
||||
}
|
||||
cdre, err := NewCDRExporter([]*CDR{cdrVoice, cdrData, cdrSMS}, cdreCfg,
|
||||
utils.MetaFileCSV, "", "", "firstexport",
|
||||
true, 1, '|', true, nil, &FilterS{cfg: cfg})
|
||||
if err != nil {
|
||||
t.Error("Unexpected error received: ", err)
|
||||
}
|
||||
if err = cdre.processCDRs(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeHeader(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeTrailer(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
csvWriter := csv.NewWriter(writer)
|
||||
if err := cdre.writeCsv(csvWriter); err != nil {
|
||||
t.Error("Unexpected error: ", err)
|
||||
}
|
||||
expected := `*sms|sdfwer|*rated|cgrates.org|call|1001|1002|2013-11-07T08:42:26Z|1|0.15000
|
||||
*voice|dsafdsaf|*rated|cgrates.org|call|1001|1002|2013-11-07T08:42:26Z|10|1.01000
|
||||
*data|abcdef|*rated|cgrates.org|call|1001|1002|2013-11-07T08:42:26Z|10|0.01200`
|
||||
result := strings.TrimSpace(writer.String())
|
||||
if len(result) != len(expected) { // export is async, cannot check order
|
||||
t.Errorf("expected: \n%s received: \n%s.", expected, result)
|
||||
}
|
||||
if cdre.TotalCost() != 1.172 {
|
||||
t.Error("unexpected TotalCost: ", cdre.TotalCost())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportWithFilter(t *testing.T) {
|
||||
writer := &bytes.Buffer{}
|
||||
cfg, _ := config.NewDefaultCGRConfig()
|
||||
cdreCfg := cfg.CdreProfiles[utils.MetaDefault]
|
||||
cdreCfg.Filters = []string{"*string:~*req.Tenant:cgrates.org"}
|
||||
cdreCfg.Fields = []*config.FCTemplate{
|
||||
{
|
||||
Tag: "*exp.ToR",
|
||||
Path: "*exp.ToR",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"ToR", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.OriginID",
|
||||
Path: "*exp.OriginID",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"OriginID", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.RequestType",
|
||||
Path: "*exp.RequestType",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"RequestType", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Tenant",
|
||||
Path: "*exp.Tenant",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Tenant", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Category",
|
||||
Path: "*exp.Category",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Category", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Account",
|
||||
Path: "*exp.Account",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Account", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Destination",
|
||||
Path: "*exp.Destination",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Destination", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.AnswerTime",
|
||||
Path: "*exp.AnswerTime",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"AnswerTime", utils.INFIELD_SEP),
|
||||
Layout: "2006-01-02T15:04:05Z07:00"},
|
||||
{
|
||||
Tag: "*exp.UsageVoice",
|
||||
Path: "*exp.UsageVoice",
|
||||
Type: "*composed",
|
||||
Filters: []string{"*string:~*req.ToR:*voice"},
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Usage{*duration_seconds}", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.UsageData",
|
||||
Path: "*exp.UsageData",
|
||||
Type: "*composed",
|
||||
Filters: []string{"*string:~*req.ToR:*data"},
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Usage{*duration_nanoseconds}", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.UsageSMS",
|
||||
Path: "*exp.UsageSMS",
|
||||
Type: "*composed",
|
||||
Filters: []string{"*string:~*req.ToR:*sms"},
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Usage{*duration_nanoseconds}", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Cost",
|
||||
Path: "*exp.Cost",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Cost", utils.INFIELD_SEP),
|
||||
RoundingDecimals: utils.IntPointer(5)},
|
||||
}
|
||||
cdrVoice := &CDR{
|
||||
CGRID: utils.Sha1("dsafdsaf", time.Unix(1383813745, 0).UTC().String()),
|
||||
ToR: utils.VOICE, OriginID: "dsafdsaf", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Unix(1383813745, 0).UTC(),
|
||||
AnswerTime: time.Unix(1383813746, 0).UTC(),
|
||||
Usage: time.Duration(10) * time.Second,
|
||||
RunID: utils.MetaDefault, Cost: 1.01,
|
||||
ExtraFields: map[string]string{"extra1": "val_extra1",
|
||||
"extra2": "val_extra2", "extra3": "val_extra3"},
|
||||
}
|
||||
cdrData := &CDR{
|
||||
CGRID: utils.Sha1("abcdef", time.Unix(1383813745, 0).UTC().String()),
|
||||
ToR: utils.DATA, OriginID: "abcdef", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "AnotherTenant", Category: "call", //for data CDR use different Tenant
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Unix(1383813745, 0).UTC(),
|
||||
AnswerTime: time.Unix(1383813746, 0).UTC(),
|
||||
Usage: time.Duration(10) * time.Nanosecond,
|
||||
RunID: utils.MetaDefault, Cost: 0.012,
|
||||
ExtraFields: map[string]string{"extra1": "val_extra1",
|
||||
"extra2": "val_extra2", "extra3": "val_extra3"},
|
||||
}
|
||||
cdrSMS := &CDR{
|
||||
CGRID: utils.Sha1("sdfwer", time.Unix(1383813745, 0).UTC().String()),
|
||||
ToR: utils.SMS, OriginID: "sdfwer", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Unix(1383813745, 0).UTC(),
|
||||
AnswerTime: time.Unix(1383813746, 0).UTC(),
|
||||
Usage: time.Duration(1),
|
||||
RunID: utils.MetaDefault, Cost: 0.15,
|
||||
ExtraFields: map[string]string{"extra1": "val_extra1",
|
||||
"extra2": "val_extra2", "extra3": "val_extra3"},
|
||||
}
|
||||
cdre, err := NewCDRExporter([]*CDR{cdrVoice, cdrData, cdrSMS}, cdreCfg,
|
||||
utils.MetaFileCSV, "", "", "firstexport",
|
||||
true, 1, '|', true, nil, &FilterS{cfg: cfg})
|
||||
if err != nil {
|
||||
t.Error("Unexpected error received: ", err)
|
||||
}
|
||||
if err = cdre.processCDRs(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeHeader(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeTrailer(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
csvWriter := csv.NewWriter(writer)
|
||||
if err := cdre.writeCsv(csvWriter); err != nil {
|
||||
t.Error("Unexpected error: ", err)
|
||||
}
|
||||
expected := `*sms|sdfwer|*rated|cgrates.org|call|1001|1002|2013-11-07T08:42:26Z|1|0.15000
|
||||
*voice|dsafdsaf|*rated|cgrates.org|call|1001|1002|2013-11-07T08:42:26Z|10|1.01000`
|
||||
result := strings.TrimSpace(writer.String())
|
||||
if len(result) != len(expected) { // export is async, cannot check order
|
||||
t.Errorf("expected: \n%s received: \n%s.", expected, result)
|
||||
}
|
||||
if cdre.TotalCost() != 1.16 {
|
||||
t.Error("unexpected TotalCost: ", cdre.TotalCost())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportWithFilter2(t *testing.T) {
|
||||
writer := &bytes.Buffer{}
|
||||
cfg, _ := config.NewDefaultCGRConfig()
|
||||
cdreCfg := cfg.CdreProfiles[utils.MetaDefault]
|
||||
cdreCfg.Filters = []string{"*string:~*req.Tenant:cgrates.org", "*lte:~*req.Cost:0.5"}
|
||||
cdreCfg.Fields = []*config.FCTemplate{
|
||||
{
|
||||
Tag: "*exp.ToR",
|
||||
Path: "*exp.ToR",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"ToR", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.OriginID",
|
||||
Path: "*exp.OriginID",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"OriginID", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.RequestType",
|
||||
Path: "*exp.RequestType",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"RequestType", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Tenant",
|
||||
Path: "*exp.Tenant",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Tenant", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Category",
|
||||
Path: "*exp.Category",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Category", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Account",
|
||||
Path: "*exp.Account",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Account", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Destination",
|
||||
Path: "*exp.Destination",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Destination", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.AnswerTime",
|
||||
Path: "*exp.AnswerTime",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"AnswerTime", utils.INFIELD_SEP),
|
||||
Layout: "2006-01-02T15:04:05Z07:00"},
|
||||
{
|
||||
Tag: "*exp.UsageVoice",
|
||||
Path: "*exp.UsageVoice",
|
||||
Type: "*composed",
|
||||
Filters: []string{"*string:~*req.ToR:*voice"},
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Usage{*duration_seconds}", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.UsageData",
|
||||
Path: "*exp.UsageData",
|
||||
Type: "*composed",
|
||||
Filters: []string{"*string:~*req.ToR:*data"},
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Usage{*duration_nanoseconds}", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.UsageSMS",
|
||||
Path: "*exp.UsageSMS",
|
||||
Type: "*composed",
|
||||
Filters: []string{"*string:~*req.ToR:*sms"},
|
||||
Value: config.NewRSRParsersMustCompile("~*req.Usage{*duration_nanoseconds}", utils.INFIELD_SEP)},
|
||||
{
|
||||
Tag: "*exp.Cost",
|
||||
Path: "*exp.Cost",
|
||||
Type: "*composed",
|
||||
Value: config.NewRSRParsersMustCompile(utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep+"Cost", utils.INFIELD_SEP),
|
||||
RoundingDecimals: utils.IntPointer(5)},
|
||||
}
|
||||
cdrVoice := &CDR{
|
||||
CGRID: utils.Sha1("dsafdsaf", time.Unix(1383813745, 0).UTC().String()),
|
||||
ToR: utils.VOICE, OriginID: "dsafdsaf", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Unix(1383813745, 0).UTC(),
|
||||
AnswerTime: time.Unix(1383813746, 0).UTC(),
|
||||
Usage: time.Duration(10) * time.Second,
|
||||
RunID: utils.MetaDefault, Cost: 1.01,
|
||||
ExtraFields: map[string]string{"extra1": "val_extra1",
|
||||
"extra2": "val_extra2", "extra3": "val_extra3"},
|
||||
}
|
||||
cdrData := &CDR{
|
||||
CGRID: utils.Sha1("abcdef", time.Unix(1383813745, 0).UTC().String()),
|
||||
ToR: utils.DATA, OriginID: "abcdef", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "AnotherTenant", Category: "call", //for data CDR use different Tenant
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Unix(1383813745, 0).UTC(),
|
||||
AnswerTime: time.Unix(1383813746, 0).UTC(),
|
||||
Usage: time.Duration(10) * time.Nanosecond,
|
||||
RunID: utils.MetaDefault, Cost: 0.012,
|
||||
ExtraFields: map[string]string{"extra1": "val_extra1",
|
||||
"extra2": "val_extra2", "extra3": "val_extra3"},
|
||||
}
|
||||
cdrSMS := &CDR{
|
||||
CGRID: utils.Sha1("sdfwer", time.Unix(1383813745, 0).UTC().String()),
|
||||
ToR: utils.SMS, OriginID: "sdfwer", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Unix(1383813745, 0).UTC(),
|
||||
AnswerTime: time.Unix(1383813746, 0).UTC(),
|
||||
Usage: time.Duration(1),
|
||||
RunID: utils.MetaDefault, Cost: 0.15,
|
||||
ExtraFields: map[string]string{"extra1": "val_extra1",
|
||||
"extra2": "val_extra2", "extra3": "val_extra3"},
|
||||
}
|
||||
cdre, err := NewCDRExporter([]*CDR{cdrVoice, cdrData, cdrSMS}, cdreCfg,
|
||||
utils.MetaFileCSV, "", "", "firstexport",
|
||||
true, 1, '|', true, nil, &FilterS{cfg: cfg})
|
||||
if err != nil {
|
||||
t.Error("Unexpected error received: ", err)
|
||||
}
|
||||
if err = cdre.processCDRs(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeHeader(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeTrailer(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
csvWriter := csv.NewWriter(writer)
|
||||
if err := cdre.writeCsv(csvWriter); err != nil {
|
||||
t.Error("Unexpected error: ", err)
|
||||
}
|
||||
expected := `*sms|sdfwer|*rated|cgrates.org|call|1001|1002|2013-11-07T08:42:26Z|1|0.15000`
|
||||
result := strings.TrimSpace(writer.String())
|
||||
if len(result) != len(expected) { // export is async, cannot check order
|
||||
t.Errorf("expected: \n%s received: \n%s.", expected, result)
|
||||
}
|
||||
if cdre.TotalCost() != 0.15 {
|
||||
t.Error("unexpected TotalCost: ", cdre.TotalCost())
|
||||
}
|
||||
}
|
||||
@@ -1,444 +0,0 @@
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
var contentJsnCfgFlds = []*config.FcTemplateJsonCfg{
|
||||
{
|
||||
Tag: utils.StringPointer("TypeOfRecord"),
|
||||
Path: utils.StringPointer("*hdr.TypeOfRecord"),
|
||||
Type: utils.StringPointer(utils.META_CONSTANT),
|
||||
Value: utils.StringPointer("10"),
|
||||
Width: utils.IntPointer(2)},
|
||||
{
|
||||
Tag: utils.StringPointer("Filler1"),
|
||||
Path: utils.StringPointer("*hdr.Filler1"),
|
||||
Type: utils.StringPointer(utils.META_FILLER),
|
||||
Width: utils.IntPointer(3)},
|
||||
{
|
||||
Tag: utils.StringPointer("DistributorCode"),
|
||||
Path: utils.StringPointer("*hdr.DistributorCode"),
|
||||
Type: utils.StringPointer(utils.META_CONSTANT),
|
||||
Value: utils.StringPointer("VOI"),
|
||||
Width: utils.IntPointer(3)},
|
||||
{
|
||||
Tag: utils.StringPointer("FileSeqNr"),
|
||||
Path: utils.StringPointer("*hdr.FileSeqNr"),
|
||||
Type: utils.StringPointer(utils.META_HANDLER),
|
||||
Value: utils.StringPointer(metaExportID),
|
||||
Width: utils.IntPointer(5),
|
||||
Strip: utils.StringPointer(utils.MetaRight),
|
||||
Padding: utils.StringPointer(utils.MetaZeroLeft)},
|
||||
{
|
||||
Tag: utils.StringPointer("LastCdr"),
|
||||
Path: utils.StringPointer("*hdr.LastCdr"),
|
||||
Type: utils.StringPointer(utils.META_HANDLER),
|
||||
Width: utils.IntPointer(12),
|
||||
Value: utils.StringPointer(metaLastCDRAtime),
|
||||
Layout: utils.StringPointer("020106150400")},
|
||||
{
|
||||
Tag: utils.StringPointer("FileCreationfTime"),
|
||||
Path: utils.StringPointer("*hdr.FileCreationfTime"),
|
||||
Type: utils.StringPointer(utils.META_HANDLER),
|
||||
Value: utils.StringPointer(metaTimeNow),
|
||||
Width: utils.IntPointer(12),
|
||||
Layout: utils.StringPointer("020106150400")},
|
||||
{
|
||||
Tag: utils.StringPointer("FileVersion"),
|
||||
Path: utils.StringPointer("*hdr.FileVersion"),
|
||||
Type: utils.StringPointer(utils.META_CONSTANT),
|
||||
Value: utils.StringPointer("01"),
|
||||
Width: utils.IntPointer(2)},
|
||||
{
|
||||
Tag: utils.StringPointer("Filler2"),
|
||||
Path: utils.StringPointer("*hdr.Filler2"),
|
||||
Type: utils.StringPointer(utils.META_FILLER),
|
||||
Width: utils.IntPointer(105)},
|
||||
{
|
||||
Tag: utils.StringPointer("TypeOfRecord"),
|
||||
Path: utils.StringPointer("*exp.TypeOfRecord"),
|
||||
Type: utils.StringPointer(utils.META_CONSTANT),
|
||||
Value: utils.StringPointer("20"),
|
||||
Width: utils.IntPointer(2)},
|
||||
{
|
||||
Tag: utils.StringPointer("Account"),
|
||||
Path: utils.StringPointer("*exp.Account"),
|
||||
Type: utils.StringPointer(utils.META_COMPOSED),
|
||||
Value: utils.StringPointer("~" + utils.Account),
|
||||
Width: utils.IntPointer(12),
|
||||
Strip: utils.StringPointer(utils.MetaLeft),
|
||||
Padding: utils.StringPointer(utils.MetaRight)},
|
||||
{
|
||||
Tag: utils.StringPointer("Subject"),
|
||||
Path: utils.StringPointer("*exp.Subject"),
|
||||
Type: utils.StringPointer(utils.META_COMPOSED),
|
||||
Value: utils.StringPointer("~" + utils.Subject),
|
||||
Width: utils.IntPointer(5),
|
||||
Strip: utils.StringPointer(utils.MetaRight),
|
||||
Padding: utils.StringPointer(utils.MetaRight)},
|
||||
{
|
||||
Tag: utils.StringPointer("CLI"),
|
||||
Path: utils.StringPointer("*exp.CLI"),
|
||||
Type: utils.StringPointer(utils.META_COMPOSED),
|
||||
Width: utils.IntPointer(15),
|
||||
Value: utils.StringPointer("cli"),
|
||||
Strip: utils.StringPointer(utils.MetaXRight),
|
||||
Padding: utils.StringPointer(utils.MetaRight)},
|
||||
{
|
||||
Tag: utils.StringPointer("Destination"),
|
||||
Path: utils.StringPointer("*exp.Destination"),
|
||||
Type: utils.StringPointer(utils.META_COMPOSED),
|
||||
Value: utils.StringPointer("~" + utils.Destination),
|
||||
Width: utils.IntPointer(24),
|
||||
Strip: utils.StringPointer(utils.MetaXRight),
|
||||
Padding: utils.StringPointer(utils.MetaRight)},
|
||||
{
|
||||
Tag: utils.StringPointer("ToR"),
|
||||
Path: utils.StringPointer("*exp.ToR"),
|
||||
Type: utils.StringPointer(utils.META_CONSTANT),
|
||||
Value: utils.StringPointer("02"),
|
||||
Width: utils.IntPointer(2)},
|
||||
{
|
||||
Tag: utils.StringPointer("SubtypeTOR"),
|
||||
Path: utils.StringPointer("*exp.SubtypeTOR"),
|
||||
Type: utils.StringPointer(utils.META_CONSTANT),
|
||||
Value: utils.StringPointer("11"),
|
||||
Padding: utils.StringPointer(utils.MetaRight),
|
||||
Width: utils.IntPointer(4)},
|
||||
{
|
||||
Tag: utils.StringPointer("SetupTime"),
|
||||
Path: utils.StringPointer("*exp.SetupTime"),
|
||||
Type: utils.StringPointer(utils.META_COMPOSED),
|
||||
Value: utils.StringPointer("~" + utils.SetupTime),
|
||||
Width: utils.IntPointer(12),
|
||||
Strip: utils.StringPointer(utils.MetaRight),
|
||||
Padding: utils.StringPointer(utils.MetaRight),
|
||||
Layout: utils.StringPointer("020106150400")},
|
||||
{
|
||||
Tag: utils.StringPointer("Duration"),
|
||||
Path: utils.StringPointer("*exp.Duration"),
|
||||
Type: utils.StringPointer(utils.META_COMPOSED),
|
||||
Value: utils.StringPointer("~" + utils.Usage),
|
||||
Width: utils.IntPointer(6),
|
||||
Strip: utils.StringPointer(utils.MetaRight),
|
||||
Padding: utils.StringPointer(utils.MetaRight),
|
||||
Layout: utils.StringPointer(utils.SECONDS)},
|
||||
{
|
||||
Tag: utils.StringPointer("DataVolume"),
|
||||
Path: utils.StringPointer("*exp.DataVolume"),
|
||||
Type: utils.StringPointer(utils.META_FILLER),
|
||||
Width: utils.IntPointer(6)},
|
||||
{
|
||||
Tag: utils.StringPointer("TaxCode"),
|
||||
Path: utils.StringPointer("*exp.TaxCode"),
|
||||
Type: utils.StringPointer(utils.META_CONSTANT),
|
||||
Value: utils.StringPointer("1"),
|
||||
Width: utils.IntPointer(1)},
|
||||
{
|
||||
Tag: utils.StringPointer("OperatorCode"),
|
||||
Path: utils.StringPointer("*exp.OperatorCode"),
|
||||
Type: utils.StringPointer(utils.META_COMPOSED),
|
||||
Value: utils.StringPointer("opercode"),
|
||||
Width: utils.IntPointer(2),
|
||||
Strip: utils.StringPointer(utils.MetaRight),
|
||||
Padding: utils.StringPointer(utils.MetaRight)},
|
||||
{
|
||||
Tag: utils.StringPointer("ProductId"),
|
||||
Path: utils.StringPointer("*exp.ProductId"),
|
||||
Type: utils.StringPointer(utils.META_COMPOSED),
|
||||
Value: utils.StringPointer("~productid"),
|
||||
Width: utils.IntPointer(5),
|
||||
Strip: utils.StringPointer(utils.MetaRight),
|
||||
Padding: utils.StringPointer(utils.MetaRight)},
|
||||
{
|
||||
Tag: utils.StringPointer("NetworkId"),
|
||||
Path: utils.StringPointer("*exp.NetworkId"),
|
||||
Type: utils.StringPointer(utils.META_CONSTANT),
|
||||
Value: utils.StringPointer("3"),
|
||||
Width: utils.IntPointer(1)},
|
||||
{
|
||||
Tag: utils.StringPointer("CallId"),
|
||||
Path: utils.StringPointer("*exp.CallId"),
|
||||
Type: utils.StringPointer(utils.META_COMPOSED),
|
||||
Value: utils.StringPointer("~" + utils.OriginID),
|
||||
Width: utils.IntPointer(16),
|
||||
Padding: utils.StringPointer(utils.MetaRight)},
|
||||
{
|
||||
Tag: utils.StringPointer("Filler"),
|
||||
Path: utils.StringPointer("*exp.Filler"),
|
||||
Type: utils.StringPointer(utils.META_FILLER),
|
||||
Width: utils.IntPointer(8)},
|
||||
{
|
||||
Tag: utils.StringPointer("Filler"),
|
||||
Path: utils.StringPointer("*exp.Filler"),
|
||||
Type: utils.StringPointer(utils.META_FILLER),
|
||||
Width: utils.IntPointer(8)},
|
||||
{
|
||||
Tag: utils.StringPointer("TerminationCode"),
|
||||
Path: utils.StringPointer("*exp.TerminationCode"),
|
||||
Type: utils.StringPointer(utils.META_COMPOSED),
|
||||
Value: utils.StringPointer("~operator;~product"),
|
||||
Width: utils.IntPointer(5),
|
||||
Strip: utils.StringPointer(utils.MetaRight),
|
||||
Padding: utils.StringPointer(utils.MetaRight)},
|
||||
{
|
||||
Tag: utils.StringPointer("Cost"),
|
||||
Path: utils.StringPointer("*exp.Cost"),
|
||||
Type: utils.StringPointer(utils.META_COMPOSED),
|
||||
Width: utils.IntPointer(9),
|
||||
Value: utils.StringPointer("~" + utils.COST),
|
||||
Padding: utils.StringPointer(utils.MetaZeroLeft),
|
||||
Rounding_decimals: utils.IntPointer(5)},
|
||||
{
|
||||
Tag: utils.StringPointer("DestinationPrivacy"),
|
||||
Path: utils.StringPointer("*exp.DestinationPrivacy"),
|
||||
Type: utils.StringPointer(utils.MetaMaskedDestination),
|
||||
Width: utils.IntPointer(1)},
|
||||
{
|
||||
Tag: utils.StringPointer("TypeOfRecord"),
|
||||
Path: utils.StringPointer("*trl.TypeOfRecord"),
|
||||
Type: utils.StringPointer(utils.META_CONSTANT),
|
||||
Value: utils.StringPointer("90"),
|
||||
Width: utils.IntPointer(2)},
|
||||
{
|
||||
Tag: utils.StringPointer("Filler1"),
|
||||
Path: utils.StringPointer("*trl.Filler1"),
|
||||
Type: utils.StringPointer(utils.META_FILLER),
|
||||
Width: utils.IntPointer(3)},
|
||||
{
|
||||
Tag: utils.StringPointer("DistributorCode"),
|
||||
Path: utils.StringPointer("*trl.DistributorCode"),
|
||||
Type: utils.StringPointer(utils.META_CONSTANT),
|
||||
Value: utils.StringPointer("VOI"),
|
||||
Width: utils.IntPointer(3)},
|
||||
{
|
||||
Tag: utils.StringPointer("FileSeqNr"),
|
||||
Path: utils.StringPointer("*trl.FileSeqNr"),
|
||||
Type: utils.StringPointer(utils.META_HANDLER),
|
||||
Value: utils.StringPointer(metaExportID),
|
||||
Width: utils.IntPointer(5),
|
||||
Strip: utils.StringPointer(utils.MetaRight),
|
||||
Padding: utils.StringPointer(utils.MetaZeroLeft)},
|
||||
{
|
||||
Tag: utils.StringPointer("NumberOfRecords"),
|
||||
Path: utils.StringPointer("*trl.NumberOfRecords"),
|
||||
Type: utils.StringPointer(utils.META_HANDLER),
|
||||
Value: utils.StringPointer(metaNrCDRs),
|
||||
Width: utils.IntPointer(6),
|
||||
Padding: utils.StringPointer(utils.MetaZeroLeft)},
|
||||
{
|
||||
Tag: utils.StringPointer("CdrsDuration"),
|
||||
Path: utils.StringPointer("*trl.CdrsDuration"),
|
||||
Type: utils.StringPointer(utils.META_HANDLER),
|
||||
Value: utils.StringPointer(metaDurCDRs),
|
||||
Width: utils.IntPointer(8),
|
||||
Padding: utils.StringPointer(utils.MetaZeroLeft),
|
||||
Layout: utils.StringPointer(utils.SECONDS)},
|
||||
{
|
||||
Tag: utils.StringPointer("FirstCdrTime"),
|
||||
Path: utils.StringPointer("*trl.FirstCdrTime"),
|
||||
Type: utils.StringPointer(utils.META_HANDLER),
|
||||
Width: utils.IntPointer(12),
|
||||
Value: utils.StringPointer(metaFirstCDRAtime),
|
||||
Layout: utils.StringPointer("020106150400")},
|
||||
{
|
||||
Tag: utils.StringPointer("LastCdrTime"),
|
||||
Path: utils.StringPointer("*trl.LastCdrTime"),
|
||||
Type: utils.StringPointer(utils.META_HANDLER),
|
||||
Width: utils.IntPointer(12),
|
||||
Value: utils.StringPointer(metaLastCDRAtime),
|
||||
Layout: utils.StringPointer("020106150400")},
|
||||
{
|
||||
Tag: utils.StringPointer("Filler2"),
|
||||
Path: utils.StringPointer("*trl.Filler2"),
|
||||
Type: utils.StringPointer(utils.META_FILLER),
|
||||
Width: utils.IntPointer(93)},
|
||||
}
|
||||
|
||||
var contentCfgFlds []*config.FCTemplate
|
||||
|
||||
// Write one CDR and test it's results only for content buffer
|
||||
func TestWriteCdr(t *testing.T) {
|
||||
var err error
|
||||
wrBuf := &bytes.Buffer{}
|
||||
cfg, _ := config.NewDefaultCGRConfig()
|
||||
if contentCfgFlds, err = config.FCTemplatesFromFCTemplatesJsonCfg(contentJsnCfgFlds, utils.INFIELD_SEP); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
cdreCfg := &config.CdreCfg{
|
||||
ExportFormat: utils.MetaFileFWV,
|
||||
Fields: contentCfgFlds,
|
||||
}
|
||||
cdr := &CDR{
|
||||
CGRID: utils.Sha1("dsafdsaf", time.Date(2013, 11, 7, 8, 42, 20, 0, time.UTC).String()),
|
||||
ToR: utils.VOICE, OrderID: 1, OriginID: "dsafdsaf", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Date(2013, 11, 7, 8, 42, 20, 0, time.UTC),
|
||||
AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC),
|
||||
Usage: time.Duration(10) * time.Second,
|
||||
RunID: utils.MetaDefault, Cost: 2.34567,
|
||||
ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
}
|
||||
|
||||
cdre, err := NewCDRExporter([]*CDR{cdr}, cdreCfg, utils.MetaFileFWV, "", "", "fwv_1",
|
||||
true, 1, '|', cfg.GeneralCfg().HttpSkipTlsVerify, nil, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.processCDRs(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeHeader(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeTrailer(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
eHeader := "10 VOIfwv_107111308420018011511340001 \n"
|
||||
eContentOut := "201001 1001 1002 0211 07111308420010 1 3dsafdsaf 0002.34570\n"
|
||||
eTrailer := "90 VOIfwv_100000100000010071113084200071113084200 \n"
|
||||
if err := cdre.writeOut(wrBuf); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
allOut := wrBuf.String()
|
||||
eAllOut := eHeader + eContentOut + eTrailer
|
||||
if math.Mod(float64(len(allOut)), 145) != 0 {
|
||||
t.Errorf("Unexpected export content length %d, have output \n%q, \n expecting: \n%q",
|
||||
len(allOut), allOut, eAllOut)
|
||||
} else if len(allOut) != len(eAllOut) {
|
||||
t.Errorf("Output does not match expected length. Have output \n%q, \n expecting: \n%q",
|
||||
allOut, eAllOut)
|
||||
}
|
||||
// Test stats
|
||||
if !cdre.firstCdrATime.Equal(cdr.AnswerTime) {
|
||||
t.Error("Unexpected firstCdrATime in stats: ", cdre.firstCdrATime)
|
||||
} else if !cdre.lastCdrATime.Equal(cdr.AnswerTime) {
|
||||
t.Error("Unexpected lastCdrATime in stats: ", cdre.lastCdrATime)
|
||||
} else if cdre.numberOfRecords != 1 {
|
||||
t.Error("Unexpected number of records in the stats: ", cdre.numberOfRecords)
|
||||
} else if cdre.totalDuration != cdr.Usage {
|
||||
t.Error("Unexpected total duration in the stats: ", cdre.totalDuration)
|
||||
} else if cdre.totalCost != utils.Round(cdr.Cost, 5, utils.ROUNDING_MIDDLE) {
|
||||
t.Error("Unexpected total cost in the stats: ", cdre.totalCost)
|
||||
}
|
||||
|
||||
if cdre.FirstOrderID() != 1 {
|
||||
t.Error("Unexpected FirstOrderId", cdre.FirstOrderID())
|
||||
}
|
||||
if cdre.LastOrderID() != 1 {
|
||||
t.Error("Unexpected LastOrderId", cdre.LastOrderID())
|
||||
}
|
||||
if cdre.TotalCost() != utils.Round(cdr.Cost, 5, utils.ROUNDING_MIDDLE) {
|
||||
t.Error("Unexpected TotalCost: ", cdre.TotalCost())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteCdrs(t *testing.T) {
|
||||
wrBuf := &bytes.Buffer{}
|
||||
cdreCfg := &config.CdreCfg{
|
||||
ExportFormat: utils.MetaFileFWV,
|
||||
Fields: contentCfgFlds,
|
||||
}
|
||||
cdr1 := &CDR{CGRID: utils.Sha1("aaa1", time.Date(2013, 11, 7, 8, 42, 20, 0, time.UTC).String()),
|
||||
ToR: utils.VOICE, OrderID: 2, OriginID: "aaa1", OriginHost: "192.168.1.1",
|
||||
RequestType: utils.META_RATED, Tenant: "cgrates.org",
|
||||
Category: "call", Account: "1001", Subject: "1001", Destination: "1010",
|
||||
SetupTime: time.Date(2013, 11, 7, 8, 42, 20, 0, time.UTC),
|
||||
AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC),
|
||||
Usage: time.Duration(10) * time.Second, RunID: utils.MetaDefault, Cost: 2.25,
|
||||
ExtraFields: map[string]string{"productnumber": "12341", "fieldextr2": "valextr2"},
|
||||
}
|
||||
cdr2 := &CDR{CGRID: utils.Sha1("aaa2", time.Date(2013, 11, 7, 7, 42, 20, 0, time.UTC).String()),
|
||||
ToR: utils.VOICE, OrderID: 4, OriginID: "aaa2", OriginHost: "192.168.1.2",
|
||||
RequestType: utils.META_PREPAID, Tenant: "cgrates.org",
|
||||
Category: "call", Account: "1002", Subject: "1002", Destination: "1011",
|
||||
SetupTime: time.Date(2013, 11, 7, 7, 42, 20, 0, time.UTC),
|
||||
AnswerTime: time.Date(2013, 11, 7, 7, 42, 26, 0, time.UTC),
|
||||
Usage: time.Duration(5) * time.Minute,
|
||||
RunID: utils.MetaDefault, Cost: 1.40001,
|
||||
ExtraFields: map[string]string{"productnumber": "12342", "fieldextr2": "valextr2"},
|
||||
}
|
||||
cdr3 := &CDR{}
|
||||
cdr4 := &CDR{CGRID: utils.Sha1("aaa3", time.Date(2013, 11, 7, 9, 42, 18, 0, time.UTC).String()),
|
||||
ToR: utils.VOICE, OrderID: 3, OriginID: "aaa4", OriginHost: "192.168.1.4",
|
||||
RequestType: utils.META_POSTPAID, Tenant: "cgrates.org",
|
||||
Category: "call", Account: "1004", Subject: "1004", Destination: "1013",
|
||||
SetupTime: time.Date(2013, 11, 7, 9, 42, 18, 0, time.UTC),
|
||||
AnswerTime: time.Date(2013, 11, 7, 9, 42, 26, 0, time.UTC),
|
||||
Usage: time.Duration(20) * time.Second,
|
||||
RunID: utils.MetaDefault, Cost: 2.34567,
|
||||
ExtraFields: map[string]string{"productnumber": "12344", "fieldextr2": "valextr2"},
|
||||
}
|
||||
cfg, _ := config.NewDefaultCGRConfig()
|
||||
cdre, err := NewCDRExporter([]*CDR{cdr1, cdr2, cdr3, cdr4}, cdreCfg,
|
||||
utils.MetaFileFWV, "", "", "fwv_1", true, 1, utils.CSV_SEP,
|
||||
cfg.GeneralCfg().HttpSkipTlsVerify, nil, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.processCDRs(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeHeader(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = cdre.composeTrailer(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := cdre.writeOut(wrBuf); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(wrBuf.String()) != 725 {
|
||||
t.Error("Output buffer does not contain expected info. Expecting len: 725, got: ", len(wrBuf.String()))
|
||||
}
|
||||
// Test stats
|
||||
if !cdre.firstCdrATime.Equal(cdr2.AnswerTime) {
|
||||
t.Error("Unexpected firstCdrATime in stats: ", cdre.firstCdrATime)
|
||||
}
|
||||
if !cdre.lastCdrATime.Equal(cdr4.AnswerTime) {
|
||||
t.Error("Unexpected lastCdrATime in stats: ", cdre.lastCdrATime)
|
||||
}
|
||||
if cdre.numberOfRecords != 3 {
|
||||
t.Error("Unexpected number of records in the stats: ", cdre.numberOfRecords)
|
||||
}
|
||||
if cdre.totalDuration != time.Duration(330)*time.Second {
|
||||
t.Error("Unexpected total duration in the stats: ", cdre.totalDuration)
|
||||
}
|
||||
if cdre.totalCost != 5.99568 {
|
||||
t.Error("Unexpected total cost in the stats: ", cdre.totalCost)
|
||||
}
|
||||
if cdre.FirstOrderID() != 2 {
|
||||
t.Error("Unexpected FirstOrderId", cdre.FirstOrderID())
|
||||
}
|
||||
if cdre.LastOrderID() != 4 {
|
||||
t.Error("Unexpected LastOrderId", cdre.LastOrderID())
|
||||
}
|
||||
if cdre.TotalCost() != 5.99568 {
|
||||
t.Error("Unexpected TotalCost: ", cdre.TotalCost())
|
||||
}
|
||||
}
|
||||
@@ -399,29 +399,8 @@ func (cdrS *CDRServer) statSProcessEvent(cgrEv *utils.CGREventWithArgDispatcher)
|
||||
return
|
||||
}
|
||||
|
||||
// exportCDRs will export the CDRs received
|
||||
func (cdrS *CDRServer) exportCDRs(cdrs []*CDR) (err error) {
|
||||
for _, exportID := range cdrS.cgrCfg.CdrsCfg().OnlineCDRExports {
|
||||
expTpl := cdrS.cgrCfg.CdreProfiles[exportID] // not checking for existence of profile since this should be done in a higher layer
|
||||
var cdre *CDRExporter
|
||||
if cdre, err = NewCDRExporter(cdrs, expTpl, expTpl.ExportFormat,
|
||||
expTpl.ExportPath, cdrS.cgrCfg.GeneralCfg().FailedPostsDir,
|
||||
"CDRSReplication", expTpl.Synchronous, expTpl.Attempts,
|
||||
expTpl.FieldSeparator, cdrS.cgrCfg.GeneralCfg().HttpSkipTlsVerify,
|
||||
cdrS.cgrCfg.CdrsCfg().AttributeSConns, cdrS.filterS); err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<CDRS> Building CDRExporter for online exports got error: <%s>", err.Error()))
|
||||
continue
|
||||
}
|
||||
if err = cdre.ExportCDRs(); err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<CDRS> Replicating CDR: %+v, got error: <%s>", cdrs, err.Error()))
|
||||
continue
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// eeSProcessEvent will process the event with the EEs component
|
||||
func (cdrS *CDRServer) eeSProcessEvent(cgrEv *utils.CGREventWithOpts) (err error) {
|
||||
func (cdrS *CDRServer) eeSProcessEvent(cgrEv *utils.CGREventWithIDs) (err error) {
|
||||
var reply string
|
||||
if err = cdrS.connMgr.Call(cdrS.cgrCfg.CdrsCfg().EEsConns, nil,
|
||||
utils.EventExporterSv1ProcessEvent,
|
||||
@@ -583,19 +562,17 @@ func (cdrS *CDRServer) processEvent(ev *utils.CGREventWithOpts,
|
||||
}
|
||||
var partiallyExecuted bool // from here actions are optional and a general error is returned
|
||||
if export {
|
||||
if len(cdrS.cgrCfg.CdrsCfg().OnlineCDRExports) != 0 {
|
||||
if err = cdrS.exportCDRs(cdrs); err != nil {
|
||||
utils.Logger.Warning(
|
||||
fmt.Sprintf("<%s> error: <%s> exporting CDRs %+v",
|
||||
utils.CDRs, err.Error(), cdrs))
|
||||
partiallyExecuted = true
|
||||
}
|
||||
}
|
||||
if len(cdrS.cgrCfg.CdrsCfg().EEsConns) != 0 {
|
||||
for _, cgrEv := range cgrEvs {
|
||||
evWithOpts := &utils.CGREventWithOpts{
|
||||
CGREvent: cgrEv.CGREvent,
|
||||
ArgDispatcher: cgrEv.ArgDispatcher}
|
||||
evWithOpts := &utils.CGREventWithIDs{
|
||||
CGREventWithOpts: &utils.CGREventWithOpts{
|
||||
CGREvent: cgrEv.CGREvent,
|
||||
ArgDispatcher: cgrEv.ArgDispatcher,
|
||||
},
|
||||
}
|
||||
if len(cdrS.cgrCfg.CdrsCfg().OnlineCDRExports) != 0 {
|
||||
evWithOpts.IDs = cdrS.cgrCfg.CdrsCfg().OnlineCDRExports
|
||||
}
|
||||
if err = cdrS.eeSProcessEvent(evWithOpts); err != nil {
|
||||
utils.Logger.Warning(
|
||||
fmt.Sprintf("<%s> error: <%s> exporting cdr %+v",
|
||||
@@ -717,7 +694,7 @@ func (cdrS *CDRServer) V1ProcessCDR(cdr *CDRWithOpts, reply *string) (err error)
|
||||
!cdr.PreRated, // rate the CDR if is not PreRated
|
||||
cdrS.cgrCfg.CdrsCfg().StoreCdrs,
|
||||
false, // no rerate
|
||||
(len(cdrS.cgrCfg.CdrsCfg().OnlineCDRExports) != 0 || len(cdrS.cgrCfg.CdrsCfg().EEsConns) != 0),
|
||||
len(cdrS.cgrCfg.CdrsCfg().OnlineCDRExports) != 0 || len(cdrS.cgrCfg.CdrsCfg().EEsConns) != 0,
|
||||
len(cdrS.cgrCfg.CdrsCfg().ThresholdSConns) != 0,
|
||||
len(cdrS.cgrCfg.CdrsCfg().StatSConns) != 0); err != nil {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user