mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Updated compressed timespan price
This commit is contained in:
@@ -176,6 +176,7 @@ func (tss *TimeSpans) Compress() { // must be pointer receiver
|
||||
} else {
|
||||
cTs := cTss[len(cTss)-1]
|
||||
cTs.CompressFactor++
|
||||
cTs.Cost += ts.Cost
|
||||
cTs.TimeEnd = ts.TimeEnd
|
||||
cTs.DurationIndex = ts.DurationIndex
|
||||
}
|
||||
@@ -199,9 +200,12 @@ func (tss *TimeSpans) Decompress() { // must be pointer receiver
|
||||
uTs.TimeEnd = cTs.TimeStart.Add(duration)
|
||||
uTs.DurationIndex = cTs.DurationIndex - time.Duration((i-1)*int(duration))
|
||||
uTs.CompressFactor = 1
|
||||
uTs.Cost = cTs.Cost / float64(cTs.GetCompressFactor())
|
||||
cTs.TimeStart = uTs.TimeEnd
|
||||
cTss = append(cTss, uTs)
|
||||
}
|
||||
cTs.Cost = cTs.GetUnitCost()
|
||||
cTs.CompressFactor = 1
|
||||
cTss = append(cTss, cTs)
|
||||
}
|
||||
*tss = cTss
|
||||
@@ -233,6 +237,10 @@ func (incr *Increment) GetCompressFactor() int {
|
||||
return incr.CompressFactor
|
||||
}
|
||||
|
||||
func (incr *Increment) GetCost() float64 {
|
||||
return float64(incr.GetCompressFactor()) * incr.Cost
|
||||
}
|
||||
|
||||
type Increments []*Increment
|
||||
|
||||
func (incs Increments) Equal(other Increments) bool {
|
||||
@@ -270,7 +278,7 @@ func (incs *Increments) Decompress() { // must be pointer receiver
|
||||
func (incs Increments) GetTotalCost() float64 {
|
||||
cost := 0.0
|
||||
for _, increment := range incs {
|
||||
cost += (float64(increment.GetCompressFactor()) * increment.Cost)
|
||||
cost += increment.GetCost()
|
||||
}
|
||||
return cost
|
||||
}
|
||||
@@ -292,6 +300,10 @@ func (ts *TimeSpan) GetUnitDuration() time.Duration {
|
||||
return time.Duration(int(ts.TimeEnd.Sub(ts.TimeStart)) / ts.GetCompressFactor())
|
||||
}
|
||||
|
||||
func (ts *TimeSpan) GetUnitCost() float64 {
|
||||
return ts.Cost / float64(ts.GetCompressFactor())
|
||||
}
|
||||
|
||||
// Returns true if the given time is inside timespan range.
|
||||
func (ts *TimeSpan) Contains(t time.Time) bool {
|
||||
return t.After(ts.TimeStart) && t.Before(ts.TimeEnd)
|
||||
@@ -632,7 +644,7 @@ func (ts *TimeSpan) hasBetterRateIntervalThan(interval *RateInterval) bool {
|
||||
func (ts *TimeSpan) Equal(other *TimeSpan) bool {
|
||||
return ts.Increments.Equal(other.Increments) &&
|
||||
ts.RateInterval.Equal(other.RateInterval) &&
|
||||
ts.Cost == other.Cost &&
|
||||
ts.GetUnitCost() == other.GetUnitCost() &&
|
||||
ts.GetUnitDuration() == other.GetUnitDuration() &&
|
||||
ts.MatchedSubject == other.MatchedSubject &&
|
||||
ts.MatchedPrefix == other.MatchedPrefix &&
|
||||
|
||||
@@ -1700,21 +1700,25 @@ func TestTSCompressDecompress(t *testing.T) {
|
||||
&TimeSpan{
|
||||
TimeStart: time.Date(2015, 1, 9, 16, 18, 0, 0, time.UTC),
|
||||
TimeEnd: time.Date(2015, 1, 9, 16, 19, 0, 0, time.UTC),
|
||||
Cost: 1.2,
|
||||
DurationIndex: 1 * time.Minute,
|
||||
},
|
||||
&TimeSpan{
|
||||
TimeStart: time.Date(2015, 1, 9, 16, 19, 0, 0, time.UTC),
|
||||
TimeEnd: time.Date(2015, 1, 9, 16, 20, 0, 0, time.UTC),
|
||||
Cost: 1.2,
|
||||
DurationIndex: 2 * time.Minute,
|
||||
},
|
||||
&TimeSpan{
|
||||
TimeStart: time.Date(2015, 1, 9, 16, 20, 0, 0, time.UTC),
|
||||
TimeEnd: time.Date(2015, 1, 9, 16, 21, 0, 0, time.UTC),
|
||||
Cost: 1.2,
|
||||
DurationIndex: 3 * time.Minute,
|
||||
},
|
||||
&TimeSpan{
|
||||
TimeStart: time.Date(2015, 1, 9, 16, 21, 0, 0, time.UTC),
|
||||
TimeEnd: time.Date(2015, 1, 9, 16, 22, 0, 0, time.UTC),
|
||||
Cost: 1.2,
|
||||
DurationIndex: 4 * time.Minute,
|
||||
},
|
||||
}
|
||||
@@ -1722,9 +1726,11 @@ func TestTSCompressDecompress(t *testing.T) {
|
||||
if len(tss) != 1 ||
|
||||
!tss[0].TimeStart.Equal(time.Date(2015, 1, 9, 16, 18, 0, 0, time.UTC)) ||
|
||||
!tss[0].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 22, 0, 0, time.UTC)) ||
|
||||
tss[0].DurationIndex != 4*time.Minute {
|
||||
tss[0].DurationIndex != 4*time.Minute ||
|
||||
tss[0].Cost != 4.8 ||
|
||||
tss[0].CompressFactor != 4 {
|
||||
for _, ts := range tss {
|
||||
t.Logf("TS: %v", ts)
|
||||
t.Logf("TS: %+v", ts)
|
||||
}
|
||||
t.Error("Error compressing timespans: ", tss)
|
||||
}
|
||||
@@ -1733,17 +1739,25 @@ func TestTSCompressDecompress(t *testing.T) {
|
||||
!tss[0].TimeStart.Equal(time.Date(2015, 1, 9, 16, 18, 0, 0, time.UTC)) ||
|
||||
!tss[0].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 19, 0, 0, time.UTC)) ||
|
||||
tss[0].DurationIndex != 1*time.Minute ||
|
||||
tss[0].CompressFactor != 1 ||
|
||||
tss[0].Cost != 1.2 ||
|
||||
!tss[1].TimeStart.Equal(time.Date(2015, 1, 9, 16, 19, 0, 0, time.UTC)) ||
|
||||
!tss[1].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 20, 0, 0, time.UTC)) ||
|
||||
tss[1].DurationIndex != 2*time.Minute ||
|
||||
tss[1].CompressFactor != 1 ||
|
||||
tss[1].Cost != 1.2 ||
|
||||
!tss[2].TimeStart.Equal(time.Date(2015, 1, 9, 16, 20, 0, 0, time.UTC)) ||
|
||||
!tss[2].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 21, 0, 0, time.UTC)) ||
|
||||
tss[2].DurationIndex != 3*time.Minute ||
|
||||
tss[2].CompressFactor != 1 ||
|
||||
tss[2].Cost != 1.2 ||
|
||||
!tss[3].TimeStart.Equal(time.Date(2015, 1, 9, 16, 21, 0, 0, time.UTC)) ||
|
||||
!tss[3].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 22, 0, 0, time.UTC)) ||
|
||||
tss[3].DurationIndex != 4*time.Minute {
|
||||
tss[3].DurationIndex != 4*time.Minute ||
|
||||
tss[3].CompressFactor != 1 ||
|
||||
tss[3].Cost != 1.2 {
|
||||
for i, ts := range tss {
|
||||
t.Logf("TS(%d): %v", i, ts)
|
||||
t.Logf("TS(%d): %+v", i, ts)
|
||||
}
|
||||
t.Error("Error decompressing timespans: ", tss)
|
||||
}
|
||||
@@ -1755,24 +1769,28 @@ func TestTSDifferentCompressDecompress(t *testing.T) {
|
||||
TimeStart: time.Date(2015, 1, 9, 16, 18, 0, 0, time.UTC),
|
||||
TimeEnd: time.Date(2015, 1, 9, 16, 19, 0, 0, time.UTC),
|
||||
RateInterval: &RateInterval{Weight: 1},
|
||||
Cost: 1.2,
|
||||
DurationIndex: 1 * time.Minute,
|
||||
},
|
||||
&TimeSpan{
|
||||
TimeStart: time.Date(2015, 1, 9, 16, 19, 0, 0, time.UTC),
|
||||
TimeEnd: time.Date(2015, 1, 9, 16, 20, 0, 0, time.UTC),
|
||||
RateInterval: &RateInterval{Weight: 2},
|
||||
Cost: 1.2,
|
||||
DurationIndex: 2 * time.Minute,
|
||||
},
|
||||
&TimeSpan{
|
||||
TimeStart: time.Date(2015, 1, 9, 16, 20, 0, 0, time.UTC),
|
||||
TimeEnd: time.Date(2015, 1, 9, 16, 21, 0, 0, time.UTC),
|
||||
RateInterval: &RateInterval{Weight: 1},
|
||||
Cost: 1.2,
|
||||
DurationIndex: 3 * time.Minute,
|
||||
},
|
||||
&TimeSpan{
|
||||
TimeStart: time.Date(2015, 1, 9, 16, 21, 0, 0, time.UTC),
|
||||
TimeEnd: time.Date(2015, 1, 9, 16, 22, 0, 0, time.UTC),
|
||||
RateInterval: &RateInterval{Weight: 1},
|
||||
Cost: 1.2,
|
||||
DurationIndex: 4 * time.Minute,
|
||||
},
|
||||
}
|
||||
@@ -1781,14 +1799,17 @@ func TestTSDifferentCompressDecompress(t *testing.T) {
|
||||
!tss[0].TimeStart.Equal(time.Date(2015, 1, 9, 16, 18, 0, 0, time.UTC)) ||
|
||||
!tss[0].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 19, 0, 0, time.UTC)) ||
|
||||
tss[0].DurationIndex != 1*time.Minute ||
|
||||
tss[0].Cost != 1.2 ||
|
||||
!tss[1].TimeStart.Equal(time.Date(2015, 1, 9, 16, 19, 0, 0, time.UTC)) ||
|
||||
!tss[1].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 20, 0, 0, time.UTC)) ||
|
||||
tss[1].DurationIndex != 2*time.Minute ||
|
||||
tss[1].Cost != 1.2 ||
|
||||
!tss[2].TimeStart.Equal(time.Date(2015, 1, 9, 16, 20, 0, 0, time.UTC)) ||
|
||||
!tss[2].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 22, 0, 0, time.UTC)) ||
|
||||
tss[2].DurationIndex != 4*time.Minute {
|
||||
tss[2].DurationIndex != 4*time.Minute ||
|
||||
tss[2].Cost != 2.4 {
|
||||
for _, ts := range tss {
|
||||
t.Logf("TS: %v", ts)
|
||||
t.Logf("TS: %+v", ts)
|
||||
}
|
||||
t.Error("Error compressing timespans: ", tss)
|
||||
}
|
||||
@@ -1797,17 +1818,25 @@ func TestTSDifferentCompressDecompress(t *testing.T) {
|
||||
!tss[0].TimeStart.Equal(time.Date(2015, 1, 9, 16, 18, 0, 0, time.UTC)) ||
|
||||
!tss[0].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 19, 0, 0, time.UTC)) ||
|
||||
tss[0].DurationIndex != 1*time.Minute ||
|
||||
tss[0].CompressFactor != 1 ||
|
||||
tss[0].Cost != 1.2 ||
|
||||
!tss[1].TimeStart.Equal(time.Date(2015, 1, 9, 16, 19, 0, 0, time.UTC)) ||
|
||||
!tss[1].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 20, 0, 0, time.UTC)) ||
|
||||
tss[1].DurationIndex != 2*time.Minute ||
|
||||
tss[1].CompressFactor != 1 ||
|
||||
tss[1].Cost != 1.2 ||
|
||||
!tss[2].TimeStart.Equal(time.Date(2015, 1, 9, 16, 20, 0, 0, time.UTC)) ||
|
||||
!tss[2].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 21, 0, 0, time.UTC)) ||
|
||||
tss[2].DurationIndex != 3*time.Minute ||
|
||||
tss[2].CompressFactor != 1 ||
|
||||
tss[2].Cost != 1.2 ||
|
||||
!tss[3].TimeStart.Equal(time.Date(2015, 1, 9, 16, 21, 0, 0, time.UTC)) ||
|
||||
!tss[3].TimeEnd.Equal(time.Date(2015, 1, 9, 16, 22, 0, 0, time.UTC)) ||
|
||||
tss[3].DurationIndex != 4*time.Minute {
|
||||
tss[3].DurationIndex != 4*time.Minute ||
|
||||
tss[3].CompressFactor != 1 ||
|
||||
tss[3].Cost != 1.2 {
|
||||
for i, ts := range tss {
|
||||
t.Logf("TS(%d): %v", i, ts)
|
||||
t.Logf("TS(%d): %+v", i, ts)
|
||||
}
|
||||
t.Error("Error decompressing timespans: ", tss)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user