StoredCdr.FormatUsage now considers Data, StoredCdr.MangleDataUsage to properly export CDR objects

This commit is contained in:
DanB
2014-05-26 12:58:00 +02:00
parent e6aaf24a14
commit 31881ea41c
3 changed files with 32 additions and 7 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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"))
}
}