From 31881ea41c0fe67f404fffedfefe35fc49d1dd63 Mon Sep 17 00:00:00 2001 From: DanB Date: Mon, 26 May 2014 12:58:00 +0200 Subject: [PATCH] StoredCdr.FormatUsage now considers Data, StoredCdr.MangleDataUsage to properly export CDR objects --- cdre/fixedwidth.go | 2 +- utils/storedcdr.go | 12 +++++++++++- utils/storedcdr_test.go | 25 ++++++++++++++++++++----- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/cdre/fixedwidth.go b/cdre/fixedwidth.go index 2aad0f6b0..30b7ba9f3 100644 --- a/cdre/fixedwidth.go +++ b/cdre/fixedwidth.go @@ -121,7 +121,7 @@ func (fwv *FixedWidthCdrWriter) cdrFieldValue(cdr *utils.StoredCdr, cfgHdr, layo case utils.COST: cdrVal = cdr.FormatCost(fwv.costShiftDigits, fwv.roundDecimals) case utils.USAGE: - cdrVal = cdr.FormatDuration(layout) + cdrVal = cdr.FormatUsage(layout) case utils.SETUP_TIME: cdrVal = cdr.SetupTime.Format(layout) case utils.ANSWER_TIME: // Format time based on layout diff --git a/utils/storedcdr.go b/utils/storedcdr.go index 14979063d..ad721446c 100644 --- a/utils/storedcdr.go +++ b/utils/storedcdr.go @@ -50,6 +50,13 @@ type StoredCdr struct { Cost float64 } +// Should only be used for display purposes, bad otherwise. +func (storedCdr *StoredCdr) MangleDataUsage() { + if IsSliceMember([]string{DATA, SMS}, storedCdr.TOR) { + storedCdr.Usage = time.Duration(int(Round(storedCdr.Usage.Seconds(), 0, ROUNDING_MIDDLE))) // 0.1 should be reflected as 1 and not 0 + } +} + // Return cost as string, formated with number of decimals configured func (storedCdr *StoredCdr) FormatCost(shiftDecimals, roundDecimals int) string { cost := storedCdr.Cost @@ -60,7 +67,10 @@ func (storedCdr *StoredCdr) FormatCost(shiftDecimals, roundDecimals int) string } // Rounds up so 0.8 seconds will become 1 -func (storedCdr *StoredCdr) FormatDuration(layout string) string { +func (storedCdr *StoredCdr) FormatUsage(layout string) string { + if IsSliceMember([]string{DATA, SMS}, storedCdr.TOR) { + return strconv.FormatFloat(Round(storedCdr.Usage.Seconds(), 0, ROUNDING_MIDDLE), 'f', -1, 64) + } switch layout { case HOURS: return strconv.FormatFloat(math.Ceil(storedCdr.Usage.Hours()), 'f', -1, 64) diff --git a/utils/storedcdr_test.go b/utils/storedcdr_test.go index 58c957934..4d81a8890 100644 --- a/utils/storedcdr_test.go +++ b/utils/storedcdr_test.go @@ -78,6 +78,17 @@ func TestFieldAsString(t *testing.T) { } } +func TestMangleDataUsage(t *testing.T) { + cdr := StoredCdr{TOR: DATA, Usage: time.Duration(1640113000000000)} + if cdr.MangleDataUsage(); cdr.Usage != time.Duration(1640113) { + t.Error("Unexpected usage after mangling: ", cdr.Usage) + } + cdr = StoredCdr{TOR: VOICE, Usage: time.Duration(1640113000000000)} + if cdr.MangleDataUsage(); cdr.Usage != time.Duration(1640113000000000) { + t.Error("Unexpected usage after mangling: ", cdr.Usage) + } +} + func TestFormatCost(t *testing.T) { cdr := StoredCdr{Cost: 1.01} if cdr.FormatCost(0, 4) != "1.0100" { @@ -98,13 +109,17 @@ func TestFormatCost(t *testing.T) { } } -func TestFormatDuration(t *testing.T) { +func TestFormatUsage(t *testing.T) { cdr := StoredCdr{Usage: time.Duration(10) * time.Second} - if cdr.FormatDuration(SECONDS) != "10" { - t.Error("Wrong duration format: ", cdr.FormatDuration(SECONDS)) + if cdr.FormatUsage(SECONDS) != "10" { + t.Error("Wrong usage format: ", cdr.FormatUsage(SECONDS)) } - if cdr.FormatDuration("default") != "10000000000" { - t.Error("Wrong duration format: ", cdr.FormatDuration("default")) + if cdr.FormatUsage("default") != "10000000000" { + t.Error("Wrong usage format: ", cdr.FormatUsage("default")) + } + cdr = StoredCdr{TOR: DATA, Usage: time.Duration(1640113000000000)} + if cdr.FormatUsage("default") != "1640113" { + t.Error("Wrong usage format: ", cdr.FormatUsage("default")) } }