New Equals Methods for RateProfile Cost + new rates field

This commit is contained in:
porosnicuadrian
2021-05-11 17:41:05 +03:00
committed by Dan Christian Bogos
parent f2e5c2f9c2
commit f6dbb2b782
6 changed files with 252 additions and 670 deletions

View File

@@ -160,16 +160,19 @@ func (aB *abstractBalance) debitAbstracts(usage *decimal.Big,
ec.Rating[ratingID] = &utils.RateSInterval{
Increments: []*utils.RateSIncrement{
{
Rate: &utils.Rate{
ID: utils.MetaCostIncrement,
IntervalRates: []*utils.IntervalRate{
{
FixedFee: costIcrm.FixedFee,
RecurrentFee: costIcrm.RecurrentFee,
/*
Rate: &utils.Rate{
ID: utils.MetaCostIncrement,
IntervalRates: []*utils.IntervalRate{
{
FixedFee: costIcrm.FixedFee,
RecurrentFee: costIcrm.RecurrentFee,
},
},
},
},
CompressFactor: 1,
CompressFactor: 1,
*/
},
},
CompressFactor: 1,

View File

@@ -105,15 +105,19 @@ func (cB *concreteBalance) debitAbstracts(aUnits *decimal.Big,
ec.Rating[ratingID] = &utils.RateSInterval{
Increments: []*utils.RateSIncrement{
{
Rate: &utils.Rate{
ID: utils.MetaCostIncrement,
IntervalRates: []*utils.IntervalRate{
{
FixedFee: costIcrm.FixedFee,
RecurrentFee: costIcrm.RecurrentFee,
/*
Rate: &utils.Rate{
ID: utils.MetaCostIncrement,
IntervalRates: []*utils.IntervalRate{
{
FixedFee: costIcrm.FixedFee,
RecurrentFee: costIcrm.RecurrentFee,
},
},
},
},
*/
CompressFactor: 1,
},
},

View File

@@ -1 +0,0 @@
mode: set

View File

@@ -639,27 +639,6 @@ func TestEventChargesEquals(t *testing.T) {
"GENUUID_RATING1": {
Increments: []*RateSIncrement{
{
Rate: &Rate{
ID: "*costIncrement",
FilterIDs: []string{"*string:~*req.Account:1003"},
ActivationTimes: "* * * * *",
Blocker: true,
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
Increment: NewDecimal(int64(time.Second), 0),
FixedFee: NewDecimal(0, 0),
RecurrentFee: NewDecimal(11, 1),
},
{
IntervalStart: NewDecimal(int64(time.Minute), 0),
Increment: NewDecimal(int64(2*time.Second), 0),
FixedFee: NewDecimal(1, 0),
RecurrentFee: NewDecimal(5, 1),
Unit: NewDecimal(8, 1),
},
},
},
Usage: NewDecimal(int64(time.Minute), 0),
IntervalRateIndex: 0,
CompressFactor: 1,
@@ -671,26 +650,7 @@ func TestEventChargesEquals(t *testing.T) {
"GENUUID_RATING2": {
Increments: []*RateSIncrement{
{
IncrementStart: NewDecimal(4, 2),
Rate: &Rate{
ID: "*costIncrement",
FilterIDs: []string{},
Weights: []*DynamicWeight{
{
FilterIDs: []string{"*string:~*req.Account:1002"},
Weight: 20,
},
{
Weight: 15,
},
},
IntervalRates: []*IntervalRate{
{
FixedFee: NewDecimal(5, 1),
RecurrentFee: NewDecimal(2, 1),
},
},
},
IncrementStart: NewDecimal(4, 2),
Usage: NewDecimal(int64(30*time.Second), 0),
IntervalRateIndex: 0,
CompressFactor: 1,
@@ -852,27 +812,6 @@ func TestEventChargesEquals(t *testing.T) {
"GENUUID_RATING1": {
Increments: []*RateSIncrement{
{
Rate: &Rate{
ID: "*costIncrement",
FilterIDs: []string{"*string:~*req.Account:1003"},
ActivationTimes: "* * * * *",
Blocker: true,
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
Increment: NewDecimal(int64(time.Second), 0),
FixedFee: NewDecimal(0, 0),
RecurrentFee: NewDecimal(11, 1),
},
{
IntervalStart: NewDecimal(int64(time.Minute), 0),
Increment: NewDecimal(int64(2*time.Second), 0),
FixedFee: NewDecimal(1, 0),
RecurrentFee: NewDecimal(5, 1),
Unit: NewDecimal(8, 1),
},
},
},
Usage: NewDecimal(int64(time.Minute), 0),
IntervalRateIndex: 0,
CompressFactor: 1,
@@ -884,26 +823,7 @@ func TestEventChargesEquals(t *testing.T) {
"GENUUID_RATING2": {
Increments: []*RateSIncrement{
{
IncrementStart: NewDecimal(4, 2),
Rate: &Rate{
ID: "*costIncrement",
FilterIDs: []string{},
Weights: []*DynamicWeight{
{
FilterIDs: []string{"*string:~*req.Account:1002"},
Weight: 20,
},
{
Weight: 15,
},
},
IntervalRates: []*IntervalRate{
{
FixedFee: NewDecimal(5, 1),
RecurrentFee: NewDecimal(2, 1),
},
},
},
IncrementStart: NewDecimal(4, 2),
Usage: NewDecimal(int64(30*time.Second), 0),
IntervalRateIndex: 0,
CompressFactor: 1,
@@ -1071,27 +991,6 @@ func TestEqualsExtEventCharges(t *testing.T) {
"GENUUID_RATING1": {
Increments: []*RateSIncrement{
{
Rate: &Rate{
ID: "*costIncrement",
FilterIDs: []string{"*string:~*req.Account:1003"},
ActivationTimes: "* * * * *",
Blocker: true,
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
Increment: NewDecimal(int64(time.Second), 0),
FixedFee: NewDecimal(0, 0),
RecurrentFee: NewDecimal(11, 1),
},
{
IntervalStart: NewDecimal(int64(time.Minute), 0),
Increment: NewDecimal(int64(2*time.Second), 0),
FixedFee: NewDecimal(1, 0),
RecurrentFee: NewDecimal(5, 1),
Unit: NewDecimal(8, 1),
},
},
},
Usage: NewDecimal(int64(time.Minute), 0),
IntervalRateIndex: 0,
CompressFactor: 1,
@@ -1103,26 +1002,7 @@ func TestEqualsExtEventCharges(t *testing.T) {
"GENUUID_RATING2": {
Increments: []*RateSIncrement{
{
IncrementStart: NewDecimal(4, 2),
Rate: &Rate{
ID: "*costIncrement",
FilterIDs: []string{},
Weights: []*DynamicWeight{
{
FilterIDs: []string{"*string:~*req.Account:1002"},
Weight: 20,
},
{
Weight: 15,
},
},
IntervalRates: []*IntervalRate{
{
FixedFee: NewDecimal(5, 1),
RecurrentFee: NewDecimal(2, 1),
},
},
},
IncrementStart: NewDecimal(4, 2),
Usage: NewDecimal(int64(30*time.Second), 0),
IntervalRateIndex: 0,
CompressFactor: 1,
@@ -1285,27 +1165,6 @@ func TestEqualsExtEventCharges(t *testing.T) {
"GENUUID_RATING1": {
Increments: []*ExtRateSIncrement{
{
Rate: &ExtRate{
ID: "*costIncrement",
FilterIDs: []string{"*string:~*req.Account:1003"},
ActivationTimes: "* * * * *",
Blocker: true,
IntervalRates: []*ExtIntervalRate{
{
IntervalStart: Float64Pointer(0),
Increment: Float64Pointer(float64(time.Second)),
FixedFee: Float64Pointer(0),
RecurrentFee: Float64Pointer(1.1),
},
{
IntervalStart: Float64Pointer(float64(time.Minute)),
Increment: Float64Pointer(float64(2 * time.Second)),
FixedFee: Float64Pointer(1),
RecurrentFee: Float64Pointer(0.5),
Unit: Float64Pointer(0.8),
},
},
},
Usage: Float64Pointer(float64(time.Minute)),
IntervalRateIndex: 0,
CompressFactor: 1,
@@ -1317,26 +1176,7 @@ func TestEqualsExtEventCharges(t *testing.T) {
"GENUUID_RATING2": {
Increments: []*ExtRateSIncrement{
{
IncrementStart: Float64Pointer(0.04),
Rate: &ExtRate{
ID: "*costIncrement",
FilterIDs: []string{},
Weights: []*DynamicWeight{
{
FilterIDs: []string{"*string:~*req.Account:1002"},
Weight: 20,
},
{
Weight: 15,
},
},
IntervalRates: []*ExtIntervalRate{
{
FixedFee: Float64Pointer(0.5),
RecurrentFee: Float64Pointer(0.2),
},
},
},
IncrementStart: Float64Pointer(0.04),
Usage: Float64Pointer(float64(30 * time.Second)),
IntervalRateIndex: 0,
CompressFactor: 1,

View File

@@ -411,7 +411,6 @@ func (rIl *ExtRateSInterval) Equals(nRil *ExtRateSInterval) (eq bool) {
type RateSIncrement struct {
IncrementStart *Decimal
Rate *Rate
IntervalRateIndex int
RateID string
CompressFactor int64
@@ -422,7 +421,6 @@ type RateSIncrement struct {
type ExtRateSIncrement struct {
IncrementStart *float64
Rate *ExtRate
IntervalRateIndex int
CompressFactor int64
Usage *float64
@@ -436,14 +434,6 @@ func (rI *RateSIncrement) AsExtRateSIncrement() (eRi *ExtRateSIncrement, err err
IntervalRateIndex: rI.IntervalRateIndex,
CompressFactor: rI.CompressFactor,
}
if rI.Rate != nil {
eRi.Rate = new(ExtRate)
if rt, err := rI.Rate.AsExtRate(); err != nil {
return nil, err
} else {
eRi.Rate = rt
}
}
if rI.IncrementStart != nil {
if fltIncrStart, ok := rI.IncrementStart.Big.Float64(); !ok {
return nil, errors.New("Cannot convert decimal IncrementStart into float64 ")
@@ -473,10 +463,7 @@ func (eRI *ExtRateSIncrement) Equals(extRI *ExtRateSIncrement) (eq bool) {
if !((eRI.Usage == nil && extRI.Usage == nil) ||
(eRI.Usage != nil && extRI.Usage != nil && *eRI.Usage == *extRI.Usage)) ||
!((eRI.IncrementStart == nil && extRI.IncrementStart == nil) ||
(eRI.IncrementStart != nil && extRI.IncrementStart != nil && *eRI.IncrementStart == *extRI.IncrementStart)) ||
(((eRI.Rate != nil && extRI.Rate == nil) ||
(eRI.Rate == nil && extRI.Rate != nil)) ||
(eRI.Rate != nil && extRI.Rate != nil && !eRI.Rate.Equals(extRI.Rate))) {
(eRI.IncrementStart != nil && extRI.IncrementStart != nil && *eRI.IncrementStart == *extRI.IncrementStart)) {
return
}
return eRI.CompressFactor == extRI.CompressFactor &&
@@ -515,10 +502,6 @@ func (rI *RateSIncrement) Equals(rtIn *RateSIncrement) (eq bool) {
rI.IncrementStart != nil && rtIn.IncrementStart == nil ||
(rI.IncrementStart != nil && rtIn.IncrementStart != nil &&
rI.IncrementStart.Compare(rtIn.IncrementStart) != 0)) ||
(rI.Rate == nil && rtIn.Rate != nil ||
rI.Rate != nil && rtIn.Rate == nil ||
(rI.Rate != nil && rtIn.Rate != nil &&
!rI.Rate.Equals(rtIn.Rate))) ||
rI.CompressFactor != rtIn.CompressFactor ||
rI.IntervalRateIndex != rtIn.IntervalRateIndex {
return
@@ -534,10 +517,132 @@ type RateProfileCost struct {
MaxCost float64
MaxCostStrategy string
RateSIntervals []*RateSInterval
Rates map[string]*IntervalRate
Rates map[string]*Rate
Altered []string
}
// RateProfileCost is the cost returned by RateS at cost queries
type ExtRateProfileCost struct {
ID string // RateProfileID
Cost float64
MinCost float64
MaxCost float64
MaxCostStrategy string
RateSIntervals []*ExtRateSInterval
Rates map[string]*ExtRate
Altered []string
}
// AsExtRateProfileCost converts RateProfileCost to ExtRateProfileCost
func (rpC *RateProfileCost) AsExtRateProfileCost() (exRt *ExtRateProfileCost, err error) {
exRt = &ExtRateProfileCost{
ID: rpC.ID,
Cost: rpC.Cost,
MinCost: rpC.Cost,
MaxCost: rpC.MaxCost,
MaxCostStrategy: rpC.MaxCostStrategy,
}
if rpC.RateSIntervals != nil {
exRt.RateSIntervals = make([]*ExtRateSInterval, len(rpC.RateSIntervals))
for idx, val := range rpC.RateSIntervals {
if rcvRtIntv, err := val.AsExtRateSInterval(); err != nil {
return nil, err
} else {
exRt.RateSIntervals[idx] = rcvRtIntv
}
}
}
if rpC.Rates != nil {
exRt.Rates = make(map[string]*ExtRate, len(rpC.Rates))
for key, val := range rpC.Rates {
if rcvRts, err := val.AsExtRate(); err != nil {
return nil, err
} else {
exRt.Rates[key] = rcvRts
}
}
}
if rpC.Altered != nil {
exRt.Altered = make([]string, len(rpC.Altered))
for idx, val := range rpC.Altered {
exRt.Altered[idx] = val
}
}
return
}
// Equals returns the equality between two RateProfileCost
func (rpC *RateProfileCost) Equals(nRpCt *RateProfileCost) (eq bool) {
if rpC.ID != nRpCt.ID ||
rpC.Cost != nRpCt.Cost ||
rpC.MinCost != nRpCt.MinCost ||
rpC.MaxCost != nRpCt.MaxCost ||
rpC.MaxCostStrategy != nRpCt.MaxCostStrategy ||
(rpC.RateSIntervals != nil && nRpCt.RateSIntervals == nil ||
rpC.RateSIntervals == nil && nRpCt.RateSIntervals != nil ||
len(rpC.RateSIntervals) != len(nRpCt.RateSIntervals)) ||
(rpC.Rates != nil && nRpCt.Rates == nil ||
rpC.Rates == nil && nRpCt.Rates != nil ||
len(rpC.Rates) != len(nRpCt.Rates)) ||
(rpC.Altered != nil && nRpCt.Altered == nil ||
rpC.Altered == nil && nRpCt.Altered != nil ||
len(rpC.Altered) != len(nRpCt.Altered)) {
return
}
for idx, val := range rpC.RateSIntervals {
if ok := val.Equals(nRpCt.RateSIntervals[idx]); !ok {
return
}
}
for key, val := range rpC.Rates {
if ok := val.Equals(nRpCt.Rates[key]); !ok {
return
}
}
for idx, val := range rpC.Altered {
if val != nRpCt.Altered[idx] {
return
}
}
return true
}
// Equals returns the equality between two ExtRateProfileCost
func (rpC *ExtRateProfileCost) Equals(nRpCt *ExtRateProfileCost) (eq bool) {
if rpC.ID != nRpCt.ID ||
rpC.Cost != nRpCt.Cost ||
rpC.MinCost != nRpCt.MinCost ||
rpC.MaxCost != nRpCt.MaxCost ||
rpC.MaxCostStrategy != nRpCt.MaxCostStrategy ||
(rpC.RateSIntervals != nil && nRpCt.RateSIntervals == nil ||
rpC.RateSIntervals == nil && nRpCt.RateSIntervals != nil ||
len(rpC.RateSIntervals) != len(nRpCt.RateSIntervals)) ||
(rpC.Rates != nil && nRpCt.Rates == nil ||
rpC.Rates == nil && nRpCt.Rates != nil ||
len(rpC.Rates) != len(nRpCt.Rates)) ||
(rpC.Altered != nil && nRpCt.Altered == nil ||
rpC.Altered == nil && nRpCt.Altered != nil ||
len(rpC.Altered) != len(nRpCt.Altered)) {
return
}
for idx, val := range rpC.RateSIntervals {
if ok := val.Equals(nRpCt.RateSIntervals[idx]); !ok {
return
}
}
for key, val := range rpC.Rates {
if ok := val.Equals(nRpCt.Rates[key]); !ok {
return
}
}
for idx, val := range rpC.Altered {
if val != nRpCt.Altered[idx] {
return
}
}
return true
}
// CorrectCost should be called in final phase of cost calculation
// in order to apply further correction like Min/MaxCost or rounding
func (rPc *RateProfileCost) CorrectCost(rndDec *int, rndMtd string) {
@@ -577,27 +682,35 @@ func (rIv *RateSInterval) CompressEquals(rIv2 *RateSInterval) (eq bool) {
return true
}
func (rIv *RateSInterval) Cost() *decimal.Big {
func (rIv *RateSInterval) Cost(rts map[string]*Rate) (cost *decimal.Big, err error) {
if rIv.cost == nil {
rIv.cost = new(decimal.Big)
for _, incrm := range rIv.Increments {
rIv.cost = SumBig(rIv.cost, incrm.Cost())
cstIncr, err := incrm.Cost(rts)
if err != nil {
return nil, err
}
rIv.cost = SumBig(rIv.cost, cstIncr)
}
}
return rIv.cost
return rIv.cost, nil
}
// CompressEquals compares two RateSIncrement for Compress function
func (rIcr *RateSIncrement) CompressEquals(rIcr2 *RateSIncrement) (eq bool) {
return rIcr.Rate.UID() == rIcr2.Rate.UID() &&
return rIcr.RateID == rIcr2.RateID &&
rIcr.IntervalRateIndex == rIcr2.IntervalRateIndex &&
rIcr.Usage.Big.Cmp(rIcr2.Usage.Big) == 0
}
// Cost computes the Cost on RateSIncrement
func (rIcr *RateSIncrement) Cost() *decimal.Big {
func (rIcr *RateSIncrement) Cost(rts map[string]*Rate) (cost *decimal.Big, err error) {
if rIcr.cost == nil {
icrRt := rIcr.Rate.IntervalRates[rIcr.IntervalRateIndex]
rt, has := rts[rIcr.RateID]
if !has {
return nil, fmt.Errorf("Cannot get the IntervalRate with this RateID: %s", rIcr.RateID)
}
icrRt := rt.IntervalRates[rIcr.IntervalRateIndex]
if rIcr.Usage.Compare(NewDecimal(-1, 0)) == 0 { // FixedFee
rIcr.cost = icrRt.FixedFee.Big
} else {
@@ -614,14 +727,18 @@ func (rIcr *RateSIncrement) Cost() *decimal.Big {
}
}
}
return rIcr.cost
return rIcr.cost, nil
}
// CostForIntervals sums the costs for all intervals
func CostForIntervals(rtIvls []*RateSInterval) (cost *decimal.Big) {
func CostForIntervals(rtIvls []*RateSInterval, rts map[string]*Rate) (cost *decimal.Big, err error) {
cost = new(decimal.Big)
for _, rtIvl := range rtIvls {
cost = SumBig(cost, rtIvl.Cost())
rtCst, err := rtIvl.Cost(rts)
if err != nil {
return nil, err
}
cost = SumBig(cost, rtCst)
}
return
}

View File

@@ -823,6 +823,11 @@ func TestCostForIntervals(t *testing.T) {
},
}
rt1.Compile()
rtsMap := map[string]*Rate{
"RATE0": rt0,
"RATE1": rt1,
}
rtIvls := []*RateSInterval{
{
IntervalStart: NewDecimal(0, 0),
@@ -830,15 +835,15 @@ func TestCostForIntervals(t *testing.T) {
{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rt0,
IntervalRateIndex: 0,
RateID: "RATE0",
CompressFactor: 1,
},
{
IncrementStart: NewDecimal(int64(time.Minute), 0),
Usage: NewDecimal(int64(30*time.Second), 0),
Rate: rt0,
IntervalRateIndex: 1,
RateID: "RATE0",
CompressFactor: 30,
},
},
@@ -850,15 +855,15 @@ func TestCostForIntervals(t *testing.T) {
{
IncrementStart: NewDecimal(int64(90*time.Second), 0),
Usage: NewDecimal(int64(30*time.Second), 0),
Rate: rt1,
IntervalRateIndex: 0,
RateID: "RATE1",
CompressFactor: 30,
},
{
IncrementStart: NewDecimal(int64(2*time.Minute), 0),
Usage: NewDecimal(int64(10*time.Minute), 0),
Rate: rt1,
IntervalRateIndex: 1,
RateID: "RATE1",
CompressFactor: 10,
},
},
@@ -866,11 +871,16 @@ func TestCostForIntervals(t *testing.T) {
},
}
eDcml, _ := new(decimal.Big).SetFloat64(4.3).Float64()
if cost, _ := CostForIntervals(rtIvls).Float64(); cost != eDcml {
cost, err := CostForIntervals(rtIvls, rtsMap)
if err != nil {
t.Error(err)
}
if costFlt, _ := cost.Float64(); costFlt != eDcml {
t.Errorf("eDcml: %f, received: %+v", eDcml, cost)
}
}
/*
func TestCostForIntervalsWIthFixedFee(t *testing.T) {
minDecimal, err := NewDecimalFromUsage("1m")
if err != nil {
@@ -902,8 +912,8 @@ func TestCostForIntervalsWIthFixedFee(t *testing.T) {
rt1 := &Rate{
ID: "RATE1",
IntervalRates: []*IntervalRate{
{},
{
IntervalStart: NewDecimal(0, 0),
FixedFee: NewDecimal(2, 1),
RecurrentFee: NewDecimal(12, 1),
@@ -919,28 +929,33 @@ func TestCostForIntervalsWIthFixedFee(t *testing.T) {
},
}
rt1.Compile()
rtsMap := map[string]*Rate{
"RATE0": rt0,
"RATE1": rt1,
}
rtIvls := []*RateSInterval{
{
IntervalStart: NewDecimal(0, 0),
Increments: []*RateSIncrement{
{ // cost 0,4
IncrementStart: NewDecimal(0, 0),
Rate: rt0,
IntervalRateIndex: 0,
RateID: "RATE0",
CompressFactor: 1,
Usage: NewDecimal(-1, 0),
},
{ // cost 2,4
IncrementStart: NewDecimal(0, 0),
Rate: rt0,
IntervalRateIndex: 0,
RateID: "RATE0",
CompressFactor: 1,
Usage: NewDecimal(int64(time.Minute), 0),
},
{ // cost 1,2
IncrementStart: NewDecimal(int64(time.Minute), 0),
Rate: rt0,
IntervalRateIndex: 1,
RateID: "RATE0",
CompressFactor: 30,
Usage: NewDecimal(int64(30*time.Second), 0),
},
@@ -952,22 +967,22 @@ func TestCostForIntervalsWIthFixedFee(t *testing.T) {
Increments: []*RateSIncrement{
{ // cost 0,2
IncrementStart: NewDecimal(int64(90*time.Second), 0),
Rate: rt1,
IntervalRateIndex: 0,
RateID: "RATE1",
CompressFactor: 1,
Usage: NewDecimal(-1, 0),
},
{ // cost 0,6
IncrementStart: NewDecimal(int64(90*time.Second), 0),
Rate: rt1,
IntervalRateIndex: 0,
RateID: "RATE1",
CompressFactor: 30,
Usage: NewDecimal(int64(30*time.Second), 0),
},
{ // cost 0,1
IncrementStart: NewDecimal(int64(2*time.Minute), 0),
Rate: rt1,
IntervalRateIndex: 1,
RateID: "RATE1",
CompressFactor: 10,
Usage: NewDecimal(int64(10*time.Second), 0),
},
@@ -976,11 +991,17 @@ func TestCostForIntervalsWIthFixedFee(t *testing.T) {
},
}
eDcml, _ := new(decimal.Big).SetFloat64(4.9).Float64()
if cost, _ := CostForIntervals(rtIvls).Float64(); cost != eDcml {
cost, err := CostForIntervals(rtIvls, rtsMap)
if err != nil {
t.Error(err)
}
if costFlt, _ := cost.Float64(); costFlt != eDcml {
t.Errorf("eDcml: %f, received: %+v", eDcml, cost)
}
}
*/
func TestRateProfileCostCorrectCost(t *testing.T) {
rPrfCost := &RateProfileCost{
ID: "Test1",
@@ -1019,49 +1040,15 @@ func TestRateProfileCostCorrectCostMaxCost(t *testing.T) {
}
func TestRateSIncrementCompressEquals(t *testing.T) {
minDecimal, err := NewDecimalFromUsage("1m")
if err != nil {
t.Error(err)
}
secDecimal, err := NewDecimalFromUsage("1s")
if err != nil {
t.Error(err)
}
rate1 := &Rate{
ID: "RATE1",
Weights: DynamicWeights{
{
Weight: 0,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(12, 2),
Unit: minDecimal,
Increment: minDecimal,
},
{
IntervalStart: NewDecimal(int64(time.Minute), 0),
RecurrentFee: NewDecimal(6, 3),
Unit: minDecimal,
Increment: secDecimal,
},
},
uID: "testUID",
}
inCr1 := &RateSIncrement{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate1,
IntervalRateIndex: 0,
CompressFactor: 1,
}
inCr2 := &RateSIncrement{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate1,
IntervalRateIndex: 0,
CompressFactor: 1,
}
@@ -1072,57 +1059,15 @@ func TestRateSIncrementCompressEquals(t *testing.T) {
}
func TestRateSIncrementCompressEqualsCase1(t *testing.T) {
minDecimal, err := NewDecimalFromUsage("1m")
if err != nil {
t.Error(err)
}
rate1 := &Rate{
ID: "RATE1",
Weights: DynamicWeights{
{
Weight: 0,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(12, 2),
Unit: minDecimal,
Increment: minDecimal,
},
},
uID: "ID",
}
rate2 := &Rate{
ID: "RATE1",
Weights: DynamicWeights{
{
Weight: 0,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(12, 2),
Unit: minDecimal,
Increment: minDecimal,
},
},
uID: "ID2",
}
inCr1 := &RateSIncrement{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate1,
IntervalRateIndex: 0,
IntervalRateIndex: 1,
CompressFactor: 1,
}
inCr2 := &RateSIncrement{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate2,
IntervalRateIndex: 0,
CompressFactor: 1,
}
@@ -1131,39 +1076,17 @@ func TestRateSIncrementCompressEqualsCase1(t *testing.T) {
t.Errorf("\nExpecting: <false>,\n Received: <%+v>", result)
}
}
func TestRateSIncrementCompressEqualsCase2(t *testing.T) {
minDecimal, err := NewDecimalFromUsage("1m")
if err != nil {
t.Error(err)
}
rate1 := &Rate{
ID: "RATE1",
Weights: DynamicWeights{
{
Weight: 0,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(12, 2),
Unit: minDecimal,
Increment: minDecimal,
},
},
}
inCr1 := &RateSIncrement{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate1,
IntervalRateIndex: 0,
CompressFactor: 1,
}
inCr2 := &RateSIncrement{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate1,
IntervalRateIndex: 1,
CompressFactor: 1,
}
@@ -1174,38 +1097,15 @@ func TestRateSIncrementCompressEqualsCase2(t *testing.T) {
}
func TestRateSIncrementCompressEqualsCase3(t *testing.T) {
minDecimal, err := NewDecimalFromUsage("1m")
if err != nil {
t.Error(err)
}
rate1 := &Rate{
ID: "RATE1",
Weights: DynamicWeights{
{
Weight: 0,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(12, 2),
Unit: minDecimal,
Increment: minDecimal,
},
},
}
inCr1 := &RateSIncrement{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Second), 0),
Rate: rate1,
IntervalRateIndex: 1,
CompressFactor: 1,
}
inCr2 := &RateSIncrement{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate1,
IntervalRateIndex: 1,
CompressFactor: 1,
}
@@ -1216,51 +1116,18 @@ func TestRateSIncrementCompressEqualsCase3(t *testing.T) {
}
func TestRateSIntervalCompressEqualsCase1(t *testing.T) {
minDecimal, err := NewDecimalFromUsage("1m")
if err != nil {
t.Error(err)
}
secDecimal, err := NewDecimalFromUsage("1s")
if err != nil {
t.Error(err)
}
rate1 := &Rate{
ID: "RATE1",
Weights: DynamicWeights{
{
Weight: 0,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(12, 2),
Unit: minDecimal,
Increment: minDecimal,
},
{
IntervalStart: NewDecimal(int64(time.Minute), 0),
RecurrentFee: NewDecimal(6, 3),
Unit: minDecimal,
Increment: secDecimal,
},
},
}
rateSintrv1 := &RateSInterval{
IntervalStart: NewDecimal(0, 0),
Increments: []*RateSIncrement{
{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate1,
IntervalRateIndex: 0,
CompressFactor: 1,
},
{
IncrementStart: NewDecimal(int64(time.Minute), 0),
Usage: NewDecimal(int64(time.Minute+10*time.Second), 0),
Rate: rate1,
IntervalRateIndex: 1,
CompressFactor: 70,
},
@@ -1274,7 +1141,6 @@ func TestRateSIntervalCompressEqualsCase1(t *testing.T) {
{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate1,
IntervalRateIndex: 0,
CompressFactor: 1,
},
@@ -1288,74 +1154,18 @@ func TestRateSIntervalCompressEqualsCase1(t *testing.T) {
}
func TestRateSIntervalCompressEqualsCase2(t *testing.T) {
minDecimal, err := NewDecimalFromUsage("1m")
if err != nil {
t.Error(err)
}
secDecimal, err := NewDecimalFromUsage("1s")
if err != nil {
t.Error(err)
}
rate1 := &Rate{
ID: "RATE1",
Weights: DynamicWeights{
{
Weight: 0,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(12, 2),
Unit: minDecimal,
Increment: minDecimal,
},
{
IntervalStart: NewDecimal(int64(time.Minute), 0),
RecurrentFee: NewDecimal(6, 3),
Unit: minDecimal,
Increment: secDecimal,
},
},
}
rate2 := &Rate{
ID: "RATE1",
Weights: DynamicWeights{
{
Weight: 0,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(12, 2),
Unit: minDecimal,
Increment: minDecimal,
},
{
IntervalStart: NewDecimal(int64(time.Minute), 0),
RecurrentFee: NewDecimal(6, 3),
Unit: minDecimal,
Increment: secDecimal,
},
},
}
rateSintrv1 := &RateSInterval{
IntervalStart: NewDecimal(0, 0),
Increments: []*RateSIncrement{
{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate1,
IntervalRateIndex: 0,
CompressFactor: 1,
},
{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Second), 0),
Rate: rate1,
IntervalRateIndex: 0,
CompressFactor: 1,
},
@@ -1369,14 +1179,12 @@ func TestRateSIntervalCompressEqualsCase2(t *testing.T) {
{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate2,
IntervalRateIndex: 0,
CompressFactor: 1,
},
{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate2,
IntervalRateIndex: 2,
CompressFactor: 1,
},
@@ -1390,35 +1198,12 @@ func TestRateSIntervalCompressEqualsCase2(t *testing.T) {
}
func TestRateSIntervalCompressEqualsCase3(t *testing.T) {
minDecimal, err := NewDecimalFromUsage("1m")
if err != nil {
t.Error(err)
}
rate1 := &Rate{
ID: "RATE1",
Weights: DynamicWeights{
{
Weight: 0,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(12, 2),
Unit: minDecimal,
Increment: minDecimal,
},
},
}
rateSintrv1 := &RateSInterval{
IntervalStart: NewDecimal(0, 0),
Increments: []*RateSIncrement{
{
IncrementStart: NewDecimal(0, 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: rate1,
IntervalRateIndex: 0,
CompressFactor: 1,
},
@@ -1431,7 +1216,6 @@ func TestRateSIntervalCompressEqualsCase3(t *testing.T) {
Increments: []*RateSIncrement{
{
IncrementStart: NewDecimal(0, 0),
Rate: rate1,
Usage: NewDecimal(int64(time.Minute), 0),
IntervalRateIndex: 0,
CompressFactor: 1,
@@ -1552,11 +1336,8 @@ func TestRatesIntervalEquals(t *testing.T) {
{
IncrementStart: NewDecimal(int64(time.Second), 0),
IntervalRateIndex: 1,
Rate: &Rate{
uID: "newID",
},
CompressFactor: 2,
Usage: NewDecimal(int64(5*time.Second), 0),
CompressFactor: 2,
Usage: NewDecimal(int64(5*time.Second), 0),
},
},
CompressFactor: 2,
@@ -1567,11 +1348,8 @@ func TestRatesIntervalEquals(t *testing.T) {
{
IncrementStart: NewDecimal(int64(time.Second), 0),
IntervalRateIndex: 1,
Rate: &Rate{
uID: "newID",
},
CompressFactor: 2,
Usage: NewDecimal(int64(5*time.Second), 0),
CompressFactor: 2,
Usage: NewDecimal(int64(5*time.Second), 0),
},
},
CompressFactor: 2,
@@ -1627,11 +1405,8 @@ func TestRatesIntervalEquals(t *testing.T) {
{
IncrementStart: NewDecimal(int64(time.Second), 0),
IntervalRateIndex: 1,
Rate: &Rate{
uID: "newID",
},
CompressFactor: 2,
Usage: NewDecimal(int64(5*time.Second), 0),
CompressFactor: 2,
Usage: NewDecimal(int64(5*time.Second), 0),
},
{
IncrementStart: NewDecimal(int64(time.Second), 0),
@@ -1649,20 +1424,14 @@ func TestRatesIncrementEquals(t *testing.T) {
incr1 := &RateSIncrement{
IncrementStart: NewDecimal(int64(time.Second), 0),
IntervalRateIndex: 1,
Rate: &Rate{
uID: "newID",
},
CompressFactor: 2,
Usage: NewDecimal(int64(5*time.Second), 0),
CompressFactor: 2,
Usage: NewDecimal(int64(5*time.Second), 0),
}
incr2 := &RateSIncrement{
IncrementStart: NewDecimal(int64(time.Second), 0),
IntervalRateIndex: 1,
Rate: &Rate{
uID: "newID",
},
CompressFactor: 2,
Usage: NewDecimal(int64(5*time.Second), 0),
CompressFactor: 2,
Usage: NewDecimal(int64(5*time.Second), 0),
}
// equals is not looking for compress factor
@@ -1711,74 +1480,19 @@ func TestRatesIncrementEquals(t *testing.T) {
}
func TestAsExtRateSInterval(t *testing.T) {
minDecimal, err := NewDecimalFromUsage("1m")
if err != nil {
t.Error(err)
}
secDecimal, err := NewDecimalFromUsage("1s")
if err != nil {
t.Error(err)
}
rI := &RateSInterval{
IntervalStart: NewDecimal(int64(time.Second), 0),
Increments: []*RateSIncrement{
{
IncrementStart: NewDecimal(int64(time.Nanosecond), 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: &Rate{
ID: "RATE1",
FilterIDs: []string{"fltr1"},
Weights: DynamicWeights{
{
Weight: 10,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(12, 2),
FixedFee: NewDecimal(4, 1),
Unit: minDecimal,
Increment: minDecimal,
},
{
IntervalStart: NewDecimal(int64(time.Minute), 0),
Unit: minDecimal,
Increment: secDecimal,
},
},
},
IncrementStart: NewDecimal(int64(time.Nanosecond), 0),
Usage: NewDecimal(int64(time.Minute), 0),
IntervalRateIndex: 0,
CompressFactor: 1,
cost: NewDecimal(1000, 0).Big,
},
{
IncrementStart: NewDecimal(int64(time.Minute), 0),
Usage: NewDecimal(int64(2*time.Minute), 0),
Rate: &Rate{
ID: "RATE2",
FilterIDs: []string{},
Weights: DynamicWeights{
{
Weight: 0,
},
},
ActivationTimes: "* * * * 5",
IntervalRates: []*IntervalRate{
{
RecurrentFee: NewDecimal(12, 2),
FixedFee: NewDecimal(4, 1),
Unit: minDecimal,
Increment: minDecimal,
},
{
IntervalStart: NewDecimal(int64(30*time.Second), 0),
Unit: minDecimal,
Increment: secDecimal,
},
},
},
IncrementStart: NewDecimal(int64(time.Minute), 0),
Usage: NewDecimal(int64(2*time.Minute), 0),
IntervalRateIndex: 2,
CompressFactor: 5,
},
@@ -1791,62 +1505,15 @@ func TestAsExtRateSInterval(t *testing.T) {
IntervalStart: Float64Pointer(float64(time.Second)),
Increments: []*ExtRateSIncrement{
{
IncrementStart: Float64Pointer(float64(time.Nanosecond)),
Usage: Float64Pointer(float64(time.Minute)),
Rate: &ExtRate{
ID: "RATE1",
FilterIDs: []string{"fltr1"},
Weights: DynamicWeights{
{
Weight: 10,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*ExtIntervalRate{
{
IntervalStart: Float64Pointer(0),
RecurrentFee: Float64Pointer(0.12),
FixedFee: Float64Pointer(0.4),
Unit: Float64Pointer(float64(time.Minute)),
Increment: Float64Pointer(float64(time.Minute)),
},
{
IntervalStart: Float64Pointer(float64(time.Minute)),
Unit: Float64Pointer(float64(time.Minute)),
Increment: Float64Pointer(float64(time.Second)),
},
},
},
IncrementStart: Float64Pointer(float64(time.Nanosecond)),
Usage: Float64Pointer(float64(time.Minute)),
IntervalRateIndex: 0,
CompressFactor: 1,
cost: Float64Pointer(1000),
},
{
IncrementStart: Float64Pointer(float64(time.Minute)),
Usage: Float64Pointer(float64(2 * time.Minute)),
Rate: &ExtRate{
ID: "RATE2",
FilterIDs: []string{},
Weights: DynamicWeights{
{
Weight: 0,
},
},
ActivationTimes: "* * * * 5",
IntervalRates: []*ExtIntervalRate{
{
RecurrentFee: Float64Pointer(0.12),
FixedFee: Float64Pointer(0.4),
Unit: Float64Pointer(float64(time.Minute)),
Increment: Float64Pointer(float64(time.Minute)),
},
{
IntervalStart: Float64Pointer(float64(30 * time.Second)),
Unit: Float64Pointer(float64(time.Minute)),
Increment: Float64Pointer(float64(time.Second)),
},
},
},
IncrementStart: Float64Pointer(float64(time.Minute)),
Usage: Float64Pointer(float64(2 * time.Minute)),
IntervalRateIndex: 2,
CompressFactor: 5,
},
@@ -1867,27 +1534,8 @@ func TestAsExtRateSIntervalErrorsCheck(t *testing.T) {
IntervalStart: NewDecimal(int64(math.Inf(1))-1, 0),
Increments: []*RateSIncrement{
{
IncrementStart: NewDecimal(int64(time.Nanosecond), 0),
Usage: NewDecimal(int64(time.Minute), 0),
Rate: &Rate{
ID: "RATE1",
FilterIDs: []string{"fltr1"},
Weights: DynamicWeights{
{
Weight: 10,
},
},
ActivationTimes: "* * * * *",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(12, 2),
FixedFee: NewDecimal(4, 1),
//Unit: minDecimal,
//Increment: minDecimal,
},
},
},
IncrementStart: NewDecimal(int64(time.Nanosecond), 0),
Usage: NewDecimal(int64(time.Minute), 0),
IntervalRateIndex: 0,
CompressFactor: 1,
cost: NewDecimal(1000, 0).Big,
@@ -1930,69 +1578,38 @@ func TestAsExtRateSIntervalErrorsCheck(t *testing.T) {
t.Errorf("Expected %+q, received %+q", expErr, err)
}
rI.Increments[0].cost = NewDecimal(0, 0).Big
rI.Increments[0].Rate.IntervalRates[0].IntervalStart = NewDecimal(int64(math.Inf(1))-1, 0)
expErr = "cannot convert decimal IntervalStart to float64"
if _, err := rI.AsExtRateSInterval(); err == nil || err.Error() != expErr {
t.Errorf("Expected %+q, received %+q", expErr, err)
}
rI.Increments[0].Rate.IntervalRates[0].IntervalStart = NewDecimal(0, 0)
rI.Increments[0].Rate.IntervalRates[0].FixedFee = NewDecimal(int64(math.Inf(1))-1, 0)
expErr = "cannot convert decimal FixedFee to float64"
if _, err := rI.AsExtRateSInterval(); err == nil || err.Error() != expErr {
t.Errorf("Expected %+q, received %+q", expErr, err)
}
rI.Increments[0].Rate.IntervalRates[0].FixedFee = NewDecimal(0, 0)
rI.Increments[0].Rate.IntervalRates[0].RecurrentFee = NewDecimal(int64(math.Inf(1))-1, 0)
expErr = "cannot convert decimal RecurrentFee to float64"
if _, err := rI.AsExtRateSInterval(); err == nil || err.Error() != expErr {
t.Errorf("Expected %+q, received %+q", expErr, err)
}
rI.Increments[0].Rate.IntervalRates[0].RecurrentFee = NewDecimal(0, 0)
rI.Increments[0].Rate.IntervalRates[0].Unit = NewDecimal(int64(math.Inf(1))-1, 0)
expErr = "cannot convert decimal Unit to float64"
if _, err := rI.AsExtRateSInterval(); err == nil || err.Error() != expErr {
t.Errorf("Expected %+q, received %+q", expErr, err)
}
rI.Increments[0].Rate.IntervalRates[0].Unit = NewDecimal(0, 0)
rI.Increments[0].Rate.IntervalRates[0].Increment = NewDecimal(int64(math.Inf(1))-1, 0)
expErr = "cannot convert decimal Increment to float64"
if _, err := rI.AsExtRateSInterval(); err == nil || err.Error() != expErr {
t.Errorf("Expected %+q, received %+q", expErr, err)
}
rI.Increments[0].Rate.IntervalRates[0].Increment = NewDecimal(0, 0)
}
func TestCostForIntervalsWithPartialIntervals(t *testing.T) {
rt0 := &Rate{
ID: "RT_2",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(2, 2),
Unit: NewDecimal(int64(time.Second), 0),
Increment: NewDecimal(int64(time.Second), 0),
},
{
IntervalStart: NewDecimal(int64(time.Minute), 0),
FixedFee: NewDecimal(2, 2),
Unit: NewDecimal(int64(time.Second), 0),
Increment: NewDecimal(int64(time.Second), 0),
},
},
}
rtsMap := map[string]*Rate{
"RT_2": rt0,
}
rtIvls := []*RateSInterval{
{
IntervalStart: NewDecimal(int64(2*time.Minute), 0),
CompressFactor: 1,
Increments: []*RateSIncrement{
{
IncrementStart: NewDecimal(int64(2*time.Minute), 0),
Rate: &Rate{
ID: "RT_2",
IntervalRates: []*IntervalRate{
{
IntervalStart: NewDecimal(0, 0),
RecurrentFee: NewDecimal(2, 2),
Unit: NewDecimal(int64(time.Second), 0),
Increment: NewDecimal(int64(time.Second), 0),
},
{
IntervalStart: NewDecimal(int64(time.Minute), 0),
FixedFee: NewDecimal(2, 2),
Unit: NewDecimal(int64(time.Second), 0),
Increment: NewDecimal(int64(time.Second), 0),
},
},
},
IncrementStart: NewDecimal(int64(2*time.Minute), 0),
IntervalRateIndex: 1,
RateID: "RT_2",
CompressFactor: 1,
Usage: NewDecimal(-1, 0),
},
@@ -2000,7 +1617,9 @@ func TestCostForIntervalsWithPartialIntervals(t *testing.T) {
},
}
if cost := CostForIntervals(rtIvls); cost.Cmp(decimal.New(2, 2)) != 0 {
if cost, err := CostForIntervals(rtIvls, rtsMap); err != nil {
t.Error(err)
} else if cost.Cmp(decimal.New(2, 2)) != 0 {
t.Errorf("received cost: %s", cost)
}
}