Adding costWithRates skel to Rates

This commit is contained in:
DanB
2020-10-30 13:37:29 +01:00
parent f26a26296d
commit 2aa1387b58
2 changed files with 85 additions and 0 deletions

View File

@@ -132,6 +132,7 @@ func orderRatesOnIntervals(aRts []*engine.Rate, sTime time.Time, usage time.Dura
rWw.add(rIt)
}
}
// sort the activation times
sortedATimes := make([]time.Time, len(rtIdx))
idxATimes := 0
@@ -175,3 +176,26 @@ func orderRatesOnIntervals(aRts []*engine.Rate, sTime time.Time, usage time.Dura
}
return
}
// costWithRates will give out the cost projection for the given orderedRates and usage
func costWithRates(rts []*orderedRate, usage time.Duration) (rtIvls []*engine.RateSInterval, err error) {
//var usageSIdx time.Duration // usageStart for one rate
for i, rt := range rts {
var usageEIdx time.Duration
if i != len(rts)-1 {
usageEIdx = rts[i+1].Duration
}
var iRts []*engine.IntervalRate
for _, iRt := range rt.IntervalRates {
if usageEIdx == 0 || iRt.IntervalStart < usageEIdx {
iRts = append(iRts, iRt)
}
}
//fmt.Printf("iRts: %+v\n", iRts)
if usageEIdx == 0 {
break
}
//usageSIdx = usageEIdx // continue for the next interval
}
return
}

View File

@@ -1574,3 +1574,64 @@ func TestOrderRatesOnIntervalStartLowerThanEndIdx(t *testing.T) {
t.Errorf("Expected %+v, received %+v", utils.ToJSON(expected), utils.ToJSON(ordRts))
}
}
func TestCostWithRates(t *testing.T) {
rt0 := &engine.Rate{
ID: "RATE0",
IntervalRates: []*engine.IntervalRate{
{
IntervalStart: time.Duration(0),
Unit: time.Duration(1 * time.Minute),
Increment: time.Duration(1 * time.Minute),
Value: 0.10,
},
{
IntervalStart: time.Duration(60 * time.Second),
Unit: time.Duration(1 * time.Minute),
Increment: time.Duration(1 * time.Second),
Value: 0.05,
},
},
}
rt0.Compile()
rt1 := &engine.Rate{
ID: "RATE1",
IntervalRates: []*engine.IntervalRate{
{
IntervalStart: time.Duration(0),
Unit: time.Duration(1 * time.Minute),
Increment: time.Duration(1 * time.Second),
Value: 0.20,
},
{
IntervalStart: time.Duration(2 * time.Minute),
Unit: time.Duration(1 * time.Minute),
Increment: time.Duration(1 * time.Second),
Value: 0.15,
},
},
}
rt1.Compile()
rts := []*orderedRate{
{
time.Duration(0),
rt0,
},
{
time.Duration(90 * time.Second),
rt1,
},
}
//eRtIvls := []*engine.RateSInterval{}
var eRtIvls []*engine.RateSInterval
if rtIvls, err := costWithRates(rts, time.Duration(130*time.Second)); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eRtIvls, rtIvls) {
t.Errorf("expecting: %+v, received: %+v", eRtIvls, rtIvls)
}
}