From d08b77fcb7d93f01694d9009e9e262cf91736ad8 Mon Sep 17 00:00:00 2001 From: DanB Date: Wed, 16 Apr 2014 19:22:57 +0200 Subject: [PATCH] Adding TotalCost to export stats --- apier/cdre.go | 6 ++++-- cdre/cdrexporter.go | 1 + cdre/csv.go | 7 +++++++ cdre/csv_test.go | 6 +++++- cdre/fixedwidth.go | 4 ++++ cdre/fixedwidth_test.go | 6 ++++++ utils/apitpdata.go | 1 + 7 files changed, 28 insertions(+), 3 deletions(-) diff --git a/apier/cdre.go b/apier/cdre.go index 84e8f1093..60b48d707 100644 --- a/apier/cdre.go +++ b/apier/cdre.go @@ -118,7 +118,8 @@ func (self *ApierV1) ExportCdrsToFile(attr utils.AttrExpFileCdrs, reply *utils.E } } csvWriter.Close() - *reply = utils.ExportedFileCdrs{ExportedFilePath: filePath, TotalRecords: len(cdrs), ExportedCgrIds: exportedIds, UnexportedCgrIds: unexportedIds, + *reply = utils.ExportedFileCdrs{ExportedFilePath: filePath, TotalRecords: len(cdrs), TotalCost: csvWriter.TotalCost(), + ExportedCgrIds: exportedIds, UnexportedCgrIds: unexportedIds, FirstOrderId: csvWriter.FirstOrderId(), LastOrderId: csvWriter.LastOrderId()} case utils.CDRE_FIXED_WIDTH: if len(exportDir) == 0 { @@ -155,7 +156,8 @@ func (self *ApierV1) ExportCdrsToFile(attr utils.AttrExpFileCdrs, reply *utils.E } } fww.Close() - *reply = utils.ExportedFileCdrs{ExportedFilePath: filePath, TotalRecords: len(cdrs), ExportedCgrIds: exportedIds, UnexportedCgrIds: unexportedIds, + *reply = utils.ExportedFileCdrs{ExportedFilePath: filePath, TotalRecords: len(cdrs), TotalCost: fww.TotalCost(), + ExportedCgrIds: exportedIds, UnexportedCgrIds: unexportedIds, FirstOrderId: fww.FirstOrderId(), LastOrderId: fww.LastOrderId()} } return nil diff --git a/cdre/cdrexporter.go b/cdre/cdrexporter.go index 00c361cd2..1c4c0aee9 100644 --- a/cdre/cdrexporter.go +++ b/cdre/cdrexporter.go @@ -25,6 +25,7 @@ import ( type CdrWriter interface { FirstOrderId() int64 LastOrderId() int64 + TotalCost() float64 WriteCdr(cdr *utils.StoredCdr) string Close() } diff --git a/cdre/csv.go b/cdre/csv.go index 9dfe80c52..e02989992 100644 --- a/cdre/csv.go +++ b/cdre/csv.go @@ -32,6 +32,7 @@ type CsvCdrWriter struct { maskLen int exportedFields []*utils.RSRField // The fields exported, order important firstExpOrderId, lastExpOrderId int64 + totalCost float64 // Cummulated cost of all the } func NewCsvCdrWriter(writer io.Writer, costShiftDigits, roundDecimals int, maskDestId string, maskLen int, exportedFields []*utils.RSRField) *CsvCdrWriter { @@ -47,6 +48,10 @@ func (csvwr *CsvCdrWriter) LastOrderId() int64 { return csvwr.lastExpOrderId } +func (csvwr *CsvCdrWriter) TotalCost() float64 { + return csvwr.totalCost +} + func (csvwr *CsvCdrWriter) WriteCdr(cdr *utils.StoredCdr) error { row := make([]string, len(csvwr.exportedFields)) for idx, fld := range csvwr.exportedFields { @@ -69,6 +74,8 @@ func (csvwr *CsvCdrWriter) WriteCdr(cdr *utils.StoredCdr) error { if csvwr.lastExpOrderId < cdr.OrderId { csvwr.lastExpOrderId = cdr.OrderId } + csvwr.totalCost += cdr.Cost + csvwr.totalCost = utils.Round(csvwr.totalCost, csvwr.roundDecimals, utils.ROUNDING_MIDDLE) return csvwr.writer.Write(row) } diff --git a/cdre/csv_test.go b/cdre/csv_test.go index 08b357456..309413a4a 100644 --- a/cdre/csv_test.go +++ b/cdre/csv_test.go @@ -32,7 +32,8 @@ func TestCsvCdrWriter(t *testing.T) { cfg, _ := config.NewDefaultCGRConfig() exportedFields := append(cfg.CdreExportedFields, &utils.RSRField{Id: "extra3"}, &utils.RSRField{Id: "dummy_extra"}, &utils.RSRField{Id: "extra1"}) csvCdrWriter := NewCsvCdrWriter(writer, 0, 4, "", -1, exportedFields) - ratedCdr := &utils.StoredCdr{CgrId: utils.Sha1("dsafdsaf", time.Unix(1383813745, 0).UTC().String()), AccId: "dsafdsaf", CdrHost: "192.168.1.1", ReqType: "rated", Direction: "*out", Tenant: "cgrates.org", + ratedCdr := &utils.StoredCdr{CgrId: utils.Sha1("dsafdsaf", time.Unix(1383813745, 0).UTC().String()), AccId: "dsafdsaf", CdrHost: "192.168.1.1", + ReqType: "rated", Direction: "*out", Tenant: "cgrates.org", TOR: "call", Account: "1001", Subject: "1001", Destination: "1002", SetupTime: time.Unix(1383813745, 0).UTC(), AnswerTime: time.Unix(1383813746, 0).UTC(), Duration: time.Duration(10) * time.Second, MediationRunId: utils.DEFAULT_RUNID, ExtraFields: map[string]string{"extra1": "val_extra1", "extra2": "val_extra2", "extra3": "val_extra3"}, Cost: 1.01, @@ -44,4 +45,7 @@ func TestCsvCdrWriter(t *testing.T) { if result != expected { t.Errorf("Expected: \n%s received: \n%s.", expected, result) } + if csvCdrWriter.TotalCost() != 1.01 { + t.Error("Unexpected TotalCost: ", csvCdrWriter.TotalCost()) + } } diff --git a/cdre/fixedwidth.go b/cdre/fixedwidth.go index 36455c7b7..d3a7f6715 100644 --- a/cdre/fixedwidth.go +++ b/cdre/fixedwidth.go @@ -172,6 +172,10 @@ func (fwv *FixedWidthCdrWriter) LastOrderId() int64 { return fwv.lastExpOrderId } +func (fwv *FixedWidthCdrWriter) TotalCost() float64 { + return fwv.totalCost +} + // Writes the header into it's buffer func (fwv *FixedWidthCdrWriter) ComposeHeader() error { header := "" diff --git a/cdre/fixedwidth_test.go b/cdre/fixedwidth_test.go index 6dc3a7d92..9cfb01597 100644 --- a/cdre/fixedwidth_test.go +++ b/cdre/fixedwidth_test.go @@ -130,6 +130,9 @@ func TestWriteCdr(t *testing.T) { if fwWriter.LastOrderId() != 1 { t.Error("Unexpected LastOrderId", fwWriter.LastOrderId()) } + if fwWriter.TotalCost() != utils.Round(cdr.Cost, fwWriter.roundDecimals, utils.ROUNDING_MIDDLE) { + t.Error("Unexpected TotalCost: ", fwWriter.TotalCost()) + } } func TestWriteCdrs(t *testing.T) { @@ -199,4 +202,7 @@ func TestWriteCdrs(t *testing.T) { if fwWriter.LastOrderId() != 4 { t.Error("Unexpected LastOrderId", fwWriter.LastOrderId()) } + if fwWriter.TotalCost() != 5.9957 { + t.Error("Unexpected TotalCost: ", fwWriter.TotalCost()) + } } diff --git a/utils/apitpdata.go b/utils/apitpdata.go index f66980635..ef2a64cef 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -347,6 +347,7 @@ type AttrExpFileCdrs struct { type ExportedFileCdrs struct { ExportedFilePath string // Full path to the newly generated export file TotalRecords int // Number of CDRs to be exported + TotalCost float64 // Sum of all costs in exported CDRs FirstOrderId, LastOrderId int64 // The order id of the last exported CDR ExportedCgrIds []string // List of successfuly exported cgrids in the file UnexportedCgrIds map[string]string // Map of errored CDRs, map key is cgrid, value will be the error string