mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-20 22:58:44 +05:00
Adding costWithRates skel to Rates
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user