mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Adding TotalCost to export stats
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
type CdrWriter interface {
|
||||
FirstOrderId() int64
|
||||
LastOrderId() int64
|
||||
TotalCost() float64
|
||||
WriteCdr(cdr *utils.StoredCdr) string
|
||||
Close()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 := ""
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user