mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
EventCost ComputeUsage -> GetUsage, ComputeCost -> GetCost
This commit is contained in:
@@ -197,9 +197,9 @@ func (ec *EventCost) Clone() (cln *EventCost) {
|
||||
|
||||
// Compute aggregates all the compute methods on EventCost
|
||||
func (ec *EventCost) Compute() {
|
||||
ec.ComputeUsage()
|
||||
ec.GetUsage()
|
||||
ec.ComputeEventCostUsageIndexes()
|
||||
ec.ComputeCost()
|
||||
ec.GetCost()
|
||||
}
|
||||
|
||||
// ResetCounters will reset all the computed cached values
|
||||
@@ -214,7 +214,7 @@ func (ec *EventCost) ResetCounters() {
|
||||
}
|
||||
|
||||
// ComputeCost iterates through Charges, computing EventCost.Cost
|
||||
func (ec *EventCost) ComputeCost() float64 {
|
||||
func (ec *EventCost) GetCost() float64 {
|
||||
if ec.Cost == nil {
|
||||
var cost float64
|
||||
for _, ci := range ec.Charges {
|
||||
@@ -227,7 +227,7 @@ func (ec *EventCost) ComputeCost() float64 {
|
||||
}
|
||||
|
||||
// ComputeUsage iterates through Charges, computing EventCost.Usage
|
||||
func (ec *EventCost) ComputeUsage() time.Duration {
|
||||
func (ec *EventCost) GetUsage() time.Duration {
|
||||
if ec.Usage == nil {
|
||||
var usage time.Duration
|
||||
for _, ci := range ec.Charges {
|
||||
@@ -251,7 +251,7 @@ func (ec *EventCost) ComputeEventCostUsageIndexes() {
|
||||
|
||||
func (ec *EventCost) AsCallCost() *CallCost {
|
||||
cc := &CallCost{
|
||||
Cost: ec.ComputeCost(), RatedUsage: ec.ComputeUsage().Seconds(),
|
||||
Cost: ec.GetCost(), RatedUsage: ec.GetUsage().Seconds(),
|
||||
AccountSummary: ec.AccountSummary}
|
||||
cc.Timespans = make(TimeSpans, len(ec.Charges))
|
||||
for i, cIl := range ec.Charges {
|
||||
@@ -436,9 +436,9 @@ func (ec *EventCost) RemoveStaleReferences() {
|
||||
// returns the srplusEC as separate EventCost
|
||||
func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error) {
|
||||
if ec.Usage == nil {
|
||||
ec.ComputeUsage()
|
||||
ec.GetUsage()
|
||||
}
|
||||
origECUsage := ec.ComputeUsage()
|
||||
origECUsage := ec.GetUsage()
|
||||
if atUsage >= *ec.Usage {
|
||||
return // no trim
|
||||
}
|
||||
@@ -464,7 +464,7 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error
|
||||
ec.ComputeEventCostUsageIndexes()
|
||||
}
|
||||
if cIl.usage == nil {
|
||||
ec.ComputeUsage()
|
||||
ec.GetUsage()
|
||||
}
|
||||
if *cIl.ecUsageIdx+*cIl.TotalUsage() >= atUsage {
|
||||
lastActiveCIlIdx = utils.IntPointer(i)
|
||||
@@ -571,12 +571,12 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error
|
||||
}
|
||||
}
|
||||
ec.ResetCounters()
|
||||
if usage := ec.ComputeUsage(); usage < atUsage {
|
||||
if usage := ec.GetUsage(); usage < atUsage {
|
||||
return nil, errors.New("usage of EventCost smaller than requested")
|
||||
}
|
||||
srplusEC.ResetCounters()
|
||||
srplusEC.StartTime = ec.StartTime.Add(ec.ComputeUsage())
|
||||
if srplsUsage := srplusEC.ComputeUsage(); srplsUsage > origECUsage-atUsage {
|
||||
srplusEC.StartTime = ec.StartTime.Add(ec.GetUsage())
|
||||
if srplsUsage := srplusEC.GetUsage(); srplsUsage > origECUsage-atUsage {
|
||||
return nil, errors.New("surplus EventCost too big")
|
||||
}
|
||||
// close surplus with missing cache
|
||||
|
||||
@@ -522,11 +522,11 @@ func TestNewEventCostFromCallCost(t *testing.T) {
|
||||
AccountSummary: acntSummary,
|
||||
}
|
||||
ec := NewEventCostFromCallCost(cc, "164b0422fdc6a5117031b427439482c6a4f90e41", utils.META_DEFAULT)
|
||||
if cost := ec.ComputeCost(); cost != cc.Cost {
|
||||
if cost := ec.GetCost(); cost != cc.Cost {
|
||||
t.Errorf("Expecting: %f, received: %f", cc.Cost, cost)
|
||||
}
|
||||
eUsage := time.Duration(int64(cc.RatedUsage * 1000000000))
|
||||
if usage := ec.ComputeUsage(); usage != eUsage {
|
||||
if usage := ec.GetUsage(); usage != eUsage {
|
||||
t.Errorf("Expecting: %v, received: %v", eUsage, usage)
|
||||
}
|
||||
if len(ec.Charges) != len(eEC.Charges) {
|
||||
@@ -1009,7 +1009,7 @@ func TestECTrimMiddle1(t *testing.T) {
|
||||
}
|
||||
|
||||
reqDuration := time.Duration(190 * time.Second)
|
||||
initDur := ec.ComputeUsage()
|
||||
initDur := ec.GetUsage()
|
||||
srplsEC, err := ec.Trim(reqDuration)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@@ -1018,7 +1018,7 @@ func TestECTrimMiddle1(t *testing.T) {
|
||||
t.Logf("\teEC: %s\n\tEC: %s\n\torigEC: %s\n", utils.ToJSON(eEC), utils.ToJSON(ec), utils.ToJSON(testEC))
|
||||
t.Errorf("Expecting request duration: %v, received: %v", reqDuration, *ec.Usage)
|
||||
}
|
||||
if srplsUsage := srplsEC.ComputeUsage(); srplsUsage != time.Duration(110*time.Second) {
|
||||
if srplsUsage := srplsEC.GetUsage(); srplsUsage != time.Duration(110*time.Second) {
|
||||
t.Errorf("Expecting surplus duration: %v, received: %v", initDur-reqDuration, srplsUsage)
|
||||
}
|
||||
if !reflect.DeepEqual(eEC, ec) {
|
||||
@@ -1034,55 +1034,55 @@ func TestECTrimMUsage(t *testing.T) {
|
||||
t.Logf("ec: %s", utils.ToJSON(ec))
|
||||
atUsage := time.Duration(5 * time.Second)
|
||||
srplsEC, _ := ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != atUsage {
|
||||
if ec.GetUsage() != atUsage {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(295*time.Second) {
|
||||
if srplsEC.GetUsage() != time.Duration(295*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
ec = testEC.Clone()
|
||||
atUsage = time.Duration(10 * time.Second)
|
||||
srplsEC, _ = ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != atUsage {
|
||||
if ec.GetUsage() != atUsage {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(290*time.Second) {
|
||||
if srplsEC.GetUsage() != time.Duration(290*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(srplsEC))
|
||||
}
|
||||
ec = testEC.Clone()
|
||||
atUsage = time.Duration(15 * time.Second)
|
||||
srplsEC, _ = ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != time.Duration(20*time.Second) {
|
||||
if ec.GetUsage() != time.Duration(20*time.Second) {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(280*time.Second) {
|
||||
if srplsEC.GetUsage() != time.Duration(280*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(srplsEC))
|
||||
}
|
||||
ec = testEC.Clone()
|
||||
atUsage = time.Duration(25 * time.Second)
|
||||
srplsEC, _ = ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != time.Duration(30*time.Second) {
|
||||
if ec.GetUsage() != time.Duration(30*time.Second) {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(270*time.Second) {
|
||||
if srplsEC.GetUsage() != time.Duration(270*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(srplsEC))
|
||||
}
|
||||
ec = testEC.Clone()
|
||||
atUsage = time.Duration(38 * time.Second)
|
||||
srplsEC, _ = ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != atUsage {
|
||||
if ec.GetUsage() != atUsage {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(262*time.Second) {
|
||||
if srplsEC.GetUsage() != time.Duration(262*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(srplsEC))
|
||||
}
|
||||
ec = testEC.Clone()
|
||||
atUsage = time.Duration(61 * time.Second)
|
||||
srplsEC, _ = ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != atUsage {
|
||||
if ec.GetUsage() != atUsage {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(239*time.Second) {
|
||||
if srplsEC.GetUsage() != time.Duration(239*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(srplsEC))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user