From bbe64e3e303bed3e4f0b3388a3debfac3e52bd9a Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 19 May 2014 23:53:22 +0300 Subject: [PATCH] fix for data interval splitting (thanks DanB) --- engine/calldesc.go | 5 +-- engine/calldesc_test.go | 2 +- engine/timespans.go | 8 ++++- engine/timespans_test.go | 32 +++++++++---------- ...atachrg1_test_tmp.go => datachrg1_test.go} | 3 +- 5 files changed, 29 insertions(+), 21 deletions(-) rename general_tests/{datachrg1_test_tmp.go => datachrg1_test.go} (97%) diff --git a/engine/calldesc.go b/engine/calldesc.go index f99e32b29..58266fdad 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -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) diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index f3887f8f5..a46cdf3f9 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -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() diff --git a/engine/timespans.go b/engine/timespans.go index 9b710452b..47fd62cb5 100644 --- a/engine/timespans.go +++ b/engine/timespans.go @@ -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") diff --git a/engine/timespans_test.go b/engine/timespans_test.go index 976a6b02c..b66665dbf 100644 --- a/engine/timespans_test.go +++ b/engine/timespans_test.go @@ -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) diff --git a/general_tests/datachrg1_test_tmp.go b/general_tests/datachrg1_test.go similarity index 97% rename from general_tests/datachrg1_test_tmp.go rename to general_tests/datachrg1_test.go index 8e292a299..3cb36395b 100644 --- a/general_tests/datachrg1_test_tmp.go +++ b/general_tests/datachrg1_test.go @@ -116,7 +116,7 @@ func TestGetDataCostSecondIntDtChrg1(t *testing.T) { } } -func TestGetBetweenCostDtChrg1(t *testing.T) { +func TestGetDataBetweenCostDtChrg1(t *testing.T) { usedData := 20 usageDur := time.Duration(usedData) * time.Second timeStart := time.Date(2014, 3, 4, 0, 59, 50, 0, time.Local) @@ -134,6 +134,7 @@ func TestGetBetweenCostDtChrg1(t *testing.T) { if cc, err := cd.GetCost(); err != nil { t.Error(err) } else if cc.Cost != 0.004 { + t.Logf("%+v", cc.Timespans[1].RateInterval.Timing) t.Error("Wrong cost returned: ", cc.Cost) } }