mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-21 15:18:44 +05:00
fix for data interval splitting (thanks DanB)
This commit is contained in:
@@ -340,8 +340,8 @@ func (cd *CallDescriptor) splitInTimeSpans() (timespans []*TimeSpan) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// Logger.Debug(fmt.Sprintf("After SplitByRatingPlan: %+v", timespans))
|
||||
// split on price intervals
|
||||
for i := 0; i < len(timespans); i++ {
|
||||
@@ -356,7 +356,7 @@ func (cd *CallDescriptor) splitInTimeSpans() (timespans []*TimeSpan) {
|
||||
if timespans[i].RateInterval != nil && timespans[i].RateInterval.Weight < interval.Weight {
|
||||
continue // if the timespan has an interval than it already has a heigher weight
|
||||
}
|
||||
newTs := timespans[i].SplitByRateInterval(interval)
|
||||
newTs := timespans[i].SplitByRateInterval(interval, cd.TOR != MINUTES)
|
||||
if newTs != nil {
|
||||
newTs.ratingInfo = rp
|
||||
// insert the new timespan
|
||||
@@ -368,6 +368,7 @@ func (cd *CallDescriptor) splitInTimeSpans() (timespans []*TimeSpan) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Logger.Debug(fmt.Sprintf("After SplitByRateInterval: %+v", timespans))
|
||||
//log.Printf("After SplitByRateInterval: %+v", timespans)
|
||||
timespans = cd.roundTimeSpansToIncrement(timespans)
|
||||
|
||||
@@ -98,7 +98,7 @@ func populateDB() {
|
||||
func TestSplitSpans(t *testing.T) {
|
||||
t1 := time.Date(2012, time.February, 2, 17, 30, 0, 0, time.UTC)
|
||||
t2 := time.Date(2012, time.February, 2, 18, 30, 0, 0, time.UTC)
|
||||
cd := &CallDescriptor{Direction: "*out", Category: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2}
|
||||
cd := &CallDescriptor{Direction: "*out", Category: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2, TOR: MINUTES}
|
||||
|
||||
cd.LoadRatingPlans()
|
||||
timespans := cd.splitInTimeSpans()
|
||||
|
||||
@@ -317,7 +317,7 @@ It will modify the endtime of the received timespan and it will return
|
||||
a new timespan starting from the end of the received one.
|
||||
The interval will attach itself to the timespan that overlaps the interval.
|
||||
*/
|
||||
func (ts *TimeSpan) SplitByRateInterval(i *RateInterval) (nts *TimeSpan) {
|
||||
func (ts *TimeSpan) SplitByRateInterval(i *RateInterval, data bool) (nts *TimeSpan) {
|
||||
// if the span is not in interval return nil
|
||||
if !(i.Contains(ts.TimeStart, false) || i.Contains(ts.TimeEnd, true)) {
|
||||
//Logger.Debug("Not in interval")
|
||||
@@ -347,6 +347,12 @@ func (ts *TimeSpan) SplitByRateInterval(i *RateInterval) (nts *TimeSpan) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if data {
|
||||
if i.Contains(ts.TimeStart, false) {
|
||||
ts.SetRateInterval(i)
|
||||
}
|
||||
return
|
||||
}
|
||||
// if the span is enclosed in the interval try to set as new interval and return nil
|
||||
if i.Contains(ts.TimeStart, false) && i.Contains(ts.TimeEnd, true) {
|
||||
//Logger.Debug("All in interval")
|
||||
|
||||
@@ -32,7 +32,7 @@ func TestRightMargin(t *testing.T) {
|
||||
t2 := time.Date(2012, time.February, 4, 0, 10, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2, ratingInfo: &RatingInfo{}}
|
||||
oldDuration := ts.GetDuration()
|
||||
nts := ts.SplitByRateInterval(i)
|
||||
nts := ts.SplitByRateInterval(i, false)
|
||||
if ts.TimeStart != t1 || ts.TimeEnd != time.Date(2012, time.February, 3, 24, 0, 0, 0, time.UTC) {
|
||||
t.Error("Incorrect first half", ts)
|
||||
}
|
||||
@@ -69,7 +69,7 @@ func TestSplitMiddle(t *testing.T) {
|
||||
t.Errorf("%+v should contain %+v", i, ts.TimeEnd)
|
||||
}
|
||||
|
||||
newTs := ts.SplitByRateInterval(i)
|
||||
newTs := ts.SplitByRateInterval(i, false)
|
||||
if newTs == nil {
|
||||
t.Errorf("Error spliting interval %+v", newTs)
|
||||
}
|
||||
@@ -81,7 +81,7 @@ func TestRightHourMargin(t *testing.T) {
|
||||
t2 := time.Date(2012, time.February, 3, 18, 00, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2, ratingInfo: &RatingInfo{}}
|
||||
oldDuration := ts.GetDuration()
|
||||
nts := ts.SplitByRateInterval(i)
|
||||
nts := ts.SplitByRateInterval(i, false)
|
||||
if ts.TimeStart != t1 || ts.TimeEnd != time.Date(2012, time.February, 3, 17, 59, 0, 0, time.UTC) {
|
||||
t.Error("Incorrect first half", ts)
|
||||
}
|
||||
@@ -106,7 +106,7 @@ func TestLeftMargin(t *testing.T) {
|
||||
t2 := time.Date(2012, time.February, 6, 0, 10, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2, ratingInfo: &RatingInfo{}}
|
||||
oldDuration := ts.GetDuration()
|
||||
nts := ts.SplitByRateInterval(i)
|
||||
nts := ts.SplitByRateInterval(i, false)
|
||||
if ts.TimeStart != t1 || ts.TimeEnd != time.Date(2012, time.February, 6, 0, 0, 0, 0, time.UTC) {
|
||||
t.Error("Incorrect first half", ts)
|
||||
}
|
||||
@@ -130,7 +130,7 @@ func TestLeftHourMargin(t *testing.T) {
|
||||
t2 := time.Date(2012, time.December, 1, 9, 20, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2, ratingInfo: &RatingInfo{}}
|
||||
oldDuration := ts.GetDuration()
|
||||
nts := ts.SplitByRateInterval(i)
|
||||
nts := ts.SplitByRateInterval(i, false)
|
||||
if ts.TimeStart != t1 || ts.TimeEnd != time.Date(2012, time.December, 1, 9, 0, 0, 0, time.UTC) {
|
||||
t.Error("Incorrect first half", ts)
|
||||
}
|
||||
@@ -153,7 +153,7 @@ func TestEnclosingMargin(t *testing.T) {
|
||||
t1 := time.Date(2012, time.February, 5, 17, 45, 0, 0, time.UTC)
|
||||
t2 := time.Date(2012, time.February, 5, 18, 10, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2}
|
||||
nts := ts.SplitByRateInterval(i)
|
||||
nts := ts.SplitByRateInterval(i, false)
|
||||
if ts.TimeStart != t1 || ts.TimeEnd != t2 || nts != nil {
|
||||
t.Error("Incorrect enclosing", ts)
|
||||
}
|
||||
@@ -167,7 +167,7 @@ func TestOutsideMargin(t *testing.T) {
|
||||
t1 := time.Date(2012, time.February, 5, 17, 45, 0, 0, time.UTC)
|
||||
t2 := time.Date(2012, time.February, 5, 18, 10, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2}
|
||||
result := ts.SplitByRateInterval(i)
|
||||
result := ts.SplitByRateInterval(i, false)
|
||||
if result != nil {
|
||||
t.Error("RateInterval not split correctly")
|
||||
}
|
||||
@@ -267,7 +267,7 @@ func TestTimespanSplitGroupedRates(t *testing.T) {
|
||||
t2 := time.Date(2012, time.February, 3, 18, 00, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2, DurationIndex: 1800 * time.Second, ratingInfo: &RatingInfo{}}
|
||||
oldDuration := ts.GetDuration()
|
||||
nts := ts.SplitByRateInterval(i)
|
||||
nts := ts.SplitByRateInterval(i, false)
|
||||
splitTime := time.Date(2012, time.February, 3, 17, 45, 00, 0, time.UTC)
|
||||
if ts.TimeStart != t1 || ts.TimeEnd != splitTime {
|
||||
t.Error("Incorrect first half", ts.TimeStart, ts.TimeEnd)
|
||||
@@ -315,7 +315,7 @@ func TestTimespanSplitGroupedRatesIncrements(t *testing.T) {
|
||||
t2 := time.Date(2012, time.February, 3, 17, 31, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2, DurationIndex: 60 * time.Second, ratingInfo: &RatingInfo{}}
|
||||
oldDuration := ts.GetDuration()
|
||||
nts := ts.SplitByRateInterval(i)
|
||||
nts := ts.SplitByRateInterval(i, false)
|
||||
cd := &CallDescriptor{}
|
||||
timespans := cd.roundTimeSpansToIncrement([]*TimeSpan{ts, nts})
|
||||
if len(timespans) != 2 {
|
||||
@@ -361,7 +361,7 @@ func TestTimespanSplitRightHourMarginBeforeGroup(t *testing.T) {
|
||||
t2 := time.Date(2012, time.February, 3, 17, 01, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2, ratingInfo: &RatingInfo{}}
|
||||
oldDuration := ts.GetDuration()
|
||||
nts := ts.SplitByRateInterval(i)
|
||||
nts := ts.SplitByRateInterval(i, false)
|
||||
splitTime := time.Date(2012, time.February, 3, 17, 00, 30, 0, time.UTC)
|
||||
if ts.TimeStart != t1 || ts.TimeEnd != splitTime {
|
||||
t.Error("Incorrect first half", ts)
|
||||
@@ -379,7 +379,7 @@ func TestTimespanSplitRightHourMarginBeforeGroup(t *testing.T) {
|
||||
if ts.GetDuration().Seconds()+nts.GetDuration().Seconds() != oldDuration.Seconds() {
|
||||
t.Errorf("The duration has changed: %v + %v != %v", ts.GetDuration().Seconds(), nts.GetDuration().Seconds(), oldDuration.Seconds())
|
||||
}
|
||||
nnts := nts.SplitByRateInterval(i)
|
||||
nnts := nts.SplitByRateInterval(i, false)
|
||||
if nnts != nil {
|
||||
t.Error("Bad new split", nnts)
|
||||
}
|
||||
@@ -397,7 +397,7 @@ func TestTimespanSplitGroupSecondSplit(t *testing.T) {
|
||||
t2 := time.Date(2012, time.February, 3, 17, 04, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2, DurationIndex: 240 * time.Second, ratingInfo: &RatingInfo{}}
|
||||
oldDuration := ts.GetDuration()
|
||||
nts := ts.SplitByRateInterval(i)
|
||||
nts := ts.SplitByRateInterval(i, false)
|
||||
splitTime := time.Date(2012, time.February, 3, 17, 01, 00, 0, time.UTC)
|
||||
if ts.TimeStart != t1 || ts.TimeEnd != splitTime {
|
||||
t.Error("Incorrect first half", nts)
|
||||
@@ -415,7 +415,7 @@ func TestTimespanSplitGroupSecondSplit(t *testing.T) {
|
||||
if ts.GetDuration().Seconds()+nts.GetDuration().Seconds() != oldDuration.Seconds() {
|
||||
t.Errorf("The duration has changed: %v + %v != %v", ts.GetDuration().Seconds(), nts.GetDuration().Seconds(), oldDuration.Seconds())
|
||||
}
|
||||
nnts := nts.SplitByRateInterval(i)
|
||||
nnts := nts.SplitByRateInterval(i, false)
|
||||
nsplitTime := time.Date(2012, time.February, 3, 17, 03, 30, 0, time.UTC)
|
||||
if nts.TimeStart != splitTime || nts.TimeEnd != nsplitTime {
|
||||
t.Error("Incorrect first half", nts)
|
||||
@@ -442,7 +442,7 @@ func TestTimespanSplitLong(t *testing.T) {
|
||||
t2 := time.Date(2013, time.October, 10, 20, 0, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2, DurationIndex: t2.Sub(t1), ratingInfo: &RatingInfo{}}
|
||||
oldDuration := ts.GetDuration()
|
||||
nts := ts.SplitByRateInterval(i)
|
||||
nts := ts.SplitByRateInterval(i, false)
|
||||
splitTime := time.Date(2013, time.October, 9, 18, 0, 0, 0, time.UTC)
|
||||
if ts.TimeStart != t1 || ts.TimeEnd != splitTime {
|
||||
t.Error("Incorrect first half", nts)
|
||||
@@ -474,7 +474,7 @@ func TestTimespanSplitMultipleGroup(t *testing.T) {
|
||||
t2 := time.Date(2012, time.February, 3, 17, 04, 0, 0, time.UTC)
|
||||
ts := &TimeSpan{TimeStart: t1, TimeEnd: t2, DurationIndex: 240 * time.Second, ratingInfo: &RatingInfo{}}
|
||||
oldDuration := ts.GetDuration()
|
||||
nts := ts.SplitByRateInterval(i)
|
||||
nts := ts.SplitByRateInterval(i, false)
|
||||
splitTime := time.Date(2012, time.February, 3, 17, 01, 00, 0, time.UTC)
|
||||
if ts.TimeStart != t1 || ts.TimeEnd != splitTime {
|
||||
t.Error("Incorrect first half", nts)
|
||||
@@ -492,7 +492,7 @@ func TestTimespanSplitMultipleGroup(t *testing.T) {
|
||||
if ts.GetDuration().Seconds()+nts.GetDuration().Seconds() != oldDuration.Seconds() {
|
||||
t.Errorf("The duration has changed: %v + %v != %v", ts.GetDuration().Seconds(), nts.GetDuration().Seconds(), oldDuration.Seconds())
|
||||
}
|
||||
nnts := nts.SplitByRateInterval(i)
|
||||
nnts := nts.SplitByRateInterval(i, false)
|
||||
nsplitTime := time.Date(2012, time.February, 3, 17, 03, 00, 0, time.UTC)
|
||||
if nts.TimeStart != splitTime || nts.TimeEnd != nsplitTime {
|
||||
t.Error("Incorrect first half", nts)
|
||||
|
||||
Reference in New Issue
Block a user