From c6e96e3f0e11a1589bec446e5b5b9cb0dad63a15 Mon Sep 17 00:00:00 2001 From: DanB Date: Tue, 22 Aug 2017 16:33:38 +0200 Subject: [PATCH] EventCost fixes --- engine/eventcost.go | 14 +++----------- engine/eventcost_test.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/engine/eventcost.go b/engine/eventcost.go index 6295df25b..716447a86 100644 --- a/engine/eventcost.go +++ b/engine/eventcost.go @@ -19,7 +19,6 @@ package engine import ( "errors" - "fmt" "time" "github.com/cgrates/cgrates/utils" @@ -462,7 +461,6 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error if ec.Usage == nil { ec.GetUsage() } - fmt.Printf("Trim, atUsage: %v, eventCostUsage: %v\n", atUsage, ec.Usage) origECUsage := ec.GetUsage() if atUsage >= *ec.Usage { return // no trim @@ -505,7 +503,7 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error } else if lastActiveCIl.CompressFactor == 0 { return nil, errors.New("ChargingInterval with 0 compressFactor") } - srplusEC.Charges = ec.Charges[*lastActiveCIlIdx+1:] + srplusEC.Charges = append(srplusEC.Charges, ec.Charges[*lastActiveCIlIdx+1:]...) // direct assignment will wrongly reference later ec.Charges = ec.Charges[:*lastActiveCIlIdx+1] ec.Usage = nil ec.Cost = nil @@ -522,7 +520,6 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error if laCF == 0 { return nil, errors.New("cannot detect last active CompressFactor in ChargingInterval") } - if laCF != lastActiveCIl.CompressFactor { srplsCIl := lastActiveCIl.Clone() srplsCIl.CompressFactor = lastActiveCIl.CompressFactor - laCF @@ -532,8 +529,8 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error ec.Cost = nil } } - if atUsage != ec.GetUsage()+*lastActiveCIl.TotalUsage() { // lastInterval covering more than needed, need split - atUsage -= (ec.GetUsage() - *lastActiveCIl.TotalUsage()) // remaining duration to cover in increments of the last charging interval + if atUsage != ec.GetUsage() { // lastInterval covering more than needed, need split + atUsage -= (ec.GetUsage() - *lastActiveCIl.Usage()) // remaining duration to cover in increments of the last charging interval // find out last increment covering duration var lastActiveCItIdx *int var incrementsUsage time.Duration @@ -577,13 +574,8 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error srplsIncrement := lastIncrement.Clone() srplsIncrement.CompressFactor = srplsIncrement.CompressFactor - laItCF srplsIncrements = append([]*ChargingIncrement{srplsIncrement}, srplsIncrements...) // prepend the surplus out of compress - lastIncrement.CompressFactor = laItCF - ec.Usage = nil - ec.Cost = nil - } } - if len(srplsIncrements) != 0 { // partially covering, need trim if lastActiveCIl.CompressFactor > 1 { // ChargingInterval not covering in full, need to split it lastActiveCIl.CompressFactor -= 1 diff --git a/engine/eventcost_test.go b/engine/eventcost_test.go index badab8e31..5a51b1ce8 100644 --- a/engine/eventcost_test.go +++ b/engine/eventcost_test.go @@ -1169,13 +1169,49 @@ func TestECTrimMUsage(t *testing.T) { time.Duration(538 * time.Second), 3.23}, {time.Duration(120 * time.Second), time.Duration(120 * time.Second), 0.87, time.Duration(480 * time.Second), 2.65}, + {time.Duration(121 * time.Second), time.Duration(121 * time.Second), 0.88, + time.Duration(479 * time.Second), 2.64}, + {time.Duration(180 * time.Second), time.Duration(180 * time.Second), 1.47, + time.Duration(420 * time.Second), 2.05}, + {time.Duration(250 * time.Second), time.Duration(250 * time.Second), 2.17, + time.Duration(350 * time.Second), 1.35}, + {time.Duration(299 * time.Second), time.Duration(299 * time.Second), 2.66, + time.Duration(301 * time.Second), 0.86}, + {time.Duration(300 * time.Second), time.Duration(300 * time.Second), 2.67, + time.Duration(300 * time.Second), 0.85}, + {time.Duration(302 * time.Second), time.Duration(302 * time.Second), 2.67, + time.Duration(298 * time.Second), 0.85}, + {time.Duration(310 * time.Second), time.Duration(310 * time.Second), 2.67, + time.Duration(290 * time.Second), 0.85}, + {time.Duration(316 * time.Second), time.Duration(320 * time.Second), 2.68, + time.Duration(280 * time.Second), 0.84}, + {time.Duration(320 * time.Second), time.Duration(320 * time.Second), 2.68, + time.Duration(280 * time.Second), 0.84}, + {time.Duration(321 * time.Second), time.Duration(330 * time.Second), 2.69, + time.Duration(270 * time.Second), 0.83}, + {time.Duration(330 * time.Second), time.Duration(330 * time.Second), 2.69, + time.Duration(270 * time.Second), 0.83}, + {time.Duration(331 * time.Second), time.Duration(331 * time.Second), 2.695, + time.Duration(269 * time.Second), 0.825}, + {time.Duration(359 * time.Second), time.Duration(359 * time.Second), 2.835, + time.Duration(241 * time.Second), 0.685}, + {time.Duration(360 * time.Second), time.Duration(360 * time.Second), 2.84, + time.Duration(240 * time.Second), 0.68}, + {time.Duration(376 * time.Second), time.Duration(380 * time.Second), 2.85, + time.Duration(220 * time.Second), 0.67}, + {time.Duration(391 * time.Second), time.Duration(391 * time.Second), 2.865, + time.Duration(209 * time.Second), 0.655}, + {time.Duration(479 * time.Second), time.Duration(479 * time.Second), 3.175, + time.Duration(121 * time.Second), 0.345}, + {time.Duration(599 * time.Second), time.Duration(599 * time.Second), 3.515, + time.Duration(1 * time.Second), 0.005}, } for _, tC := range testCases { t.Run(fmt.Sprintf("AtUsage:%s", tC.atUsage), func(t *testing.T) { var ec, srplsEC *EventCost ec = testEC.Clone() if srplsEC, err = ec.Trim(tC.atUsage); err != nil { - t.Error(err) + t.Fatal(err) } if ec.GetUsage() != tC.ecUsage { t.Errorf("Wrongly trimmed EC: %s", utils.ToIJSON(ec))