From 2aa1387b58bfcb54390b7449342ac29be5ac1120 Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 30 Oct 2020 13:37:29 +0100 Subject: [PATCH] Adding costWithRates skel to Rates --- rates/librates.go | 24 +++++++++++++++++ rates/librates_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/rates/librates.go b/rates/librates.go index 2dfe08561..325d84647 100644 --- a/rates/librates.go +++ b/rates/librates.go @@ -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 +} diff --git a/rates/librates_test.go b/rates/librates_test.go index 1491c6760..bf5f5c81a 100644 --- a/rates/librates_test.go +++ b/rates/librates_test.go @@ -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) + } +}