diff --git a/engine/callcost.go b/engine/callcost.go index 767efc8f1..391b2a33a 100644 --- a/engine/callcost.go +++ b/engine/callcost.go @@ -29,6 +29,7 @@ type CallCost struct { Direction, Category, Tenant, Subject, Account, Destination, TOR string Cost float64 Timespans TimeSpans + RatedUsage float64 deductConnectFee bool negativeConnectFee bool // the connect fee went negative on default balance maxCostDisconect bool @@ -61,6 +62,12 @@ func (cc *CallCost) GetDuration() (td time.Duration) { return } +func (cc *CallCost) UpdateRatedUsage() time.Duration { + totalDuration := cc.GetDuration() + cc.RatedUsage = totalDuration.Seconds() + return totalDuration +} + func (cc *CallCost) GetConnectFee() float64 { if len(cc.Timespans) == 0 || cc.Timespans[0].RateInterval == nil || diff --git a/engine/callcost_test.go b/engine/callcost_test.go index c9ce24f01..55b0eced8 100644 --- a/engine/callcost_test.go +++ b/engine/callcost_test.go @@ -50,6 +50,10 @@ func TestSingleResultMerge(t *testing.T) { if cc1.Cost != 122 { t.Errorf("Exdpected 120 was %v", cc1.Cost) } + d := cc1.UpdateRatedUsage() + if d != 2*time.Minute || cc1.RatedUsage != 120.0 { + t.Errorf("error updating rating usage: %v, %v", d, cc1.RatedUsage) + } } func TestMultipleResultMerge(t *testing.T) { diff --git a/engine/calldesc.go b/engine/calldesc.go index 46771baa4..c38b3be18 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -526,6 +526,7 @@ func (cd *CallDescriptor) getCost() (*CallCost, error) { cc.Cost = utils.Round(cc.Cost, roundingDecimals, roundingMethod) //utils.Logger.Info(fmt.Sprintf(" Get Cost: %s => %v", cd.GetKey(), cc)) cc.Timespans.Compress() + cc.UpdateRatedUsage() return cc, err } @@ -668,6 +669,7 @@ func (cd *CallDescriptor) debit(account *Account, dryRun bool, goNegative bool) return nil, err } cc.updateCost() + cc.UpdateRatedUsage() cc.Timespans.Compress() //log.Printf("OUT CC: ", cc) return diff --git a/engine/cdrs.go b/engine/cdrs.go index 457a5fcfd..51a9249e5 100644 --- a/engine/cdrs.go +++ b/engine/cdrs.go @@ -107,6 +107,7 @@ func (self *CdrServer) ProcessExternalCdr(eCDR *ExternalCDR) error { // RPC method, used to log callcosts to db func (self *CdrServer) LogCallCost(ccl *CallCostLog) error { + ccl.CallCost.UpdateRatedUsage() // make sure rated usage is updated if ccl.CheckDuplicate { _, err := self.guard.Guard(func() (interface{}, error) { cc, err := self.cdrDb.GetCallCostLog(ccl.CgrId, ccl.RunId)