diff --git a/cmd/cgr-loader/rates.go b/cmd/cgr-loader/rates.go index fb26584d4..ce587f20d 100644 --- a/cmd/cgr-loader/rates.go +++ b/cmd/cgr-loader/rates.go @@ -182,10 +182,9 @@ func loadRatingProfiles() { ActivationTime: at, } for _, r := range rs { //rates + ap.AddIntervalIfNotPresent(rt.GetInterval(r)) for _, d := range destinations { if d.Id == r.DestinationsTag { - log.Print("dId: ", d.Id) - ap.AddInterval(rt.GetInterval(r)) for _, p := range d.Prefixes { //destinations // Search for a CallDescriptor with the same key var cd *timespans.CallDescriptor @@ -209,7 +208,7 @@ func loadRatingProfiles() { foundAp := false for _, actPer := range cd.ActivationPeriods { if actPer.ActivationTime == ap.ActivationTime { - actPer.AddInterval(ap.Intervals...) + actPer.AddIntervalIfNotPresent(ap.Intervals...) foundAp = true break } diff --git a/data/Timings.csv b/data/Timings.csv index 7e5cd24d7..888cbe5df 100644 --- a/data/Timings.csv +++ b/data/Timings.csv @@ -1,6 +1,6 @@ TimingTag,Months,MonthDays,WeekDays,StartTime WORKDAYS_00,*all,*all,1;2;3;4;5,00:00:00 -WORKDAYS_18,*all,*all,6;7,18:00:00 +WORKDAYS_18,*all,*all,1;2;3;4;5,18:00:00 WEEKENDS,*all,*all,6;7,00:00:00 WEEKLY_SAME_TIME,*all,*all,1,*now FIRST_DAY_OF_MONTH,*all,1,*all,00:00:00 diff --git a/timespans/activationperiod.go b/timespans/activationperiod.go index cb33a10a9..684742973 100644 --- a/timespans/activationperiod.go +++ b/timespans/activationperiod.go @@ -35,7 +35,23 @@ type ActivationPeriod struct { Adds one ore more intervals to the internal interval list. */ func (ap *ActivationPeriod) AddInterval(is ...*Interval) { + ap.Intervals = append(ap.Intervals, is...) +} + +/* +Adds one ore more intervals to the internal interval list only if it is not allready in the list. +*/ +func (ap *ActivationPeriod) AddIntervalIfNotPresent(is ...*Interval) { for _, i := range is { - ap.Intervals = append(ap.Intervals, i) + found := false + for _, ei := range ap.Intervals { + if i.Equals(ei) { + found = true + break + } + } + if !found { + ap.Intervals = append(ap.Intervals, i) + } } } diff --git a/timespans/activationperiod_test.go b/timespans/activationperiod_test.go index 4a179dec1..6479c445a 100644 --- a/timespans/activationperiod_test.go +++ b/timespans/activationperiod_test.go @@ -114,6 +114,33 @@ func TestFallbackNoInfiniteLoop(t *testing.T) { } } +func TestApAddIntervalIfNotPresent(t *testing.T) { + i1 := &Interval{Months: Months{time.February}, + MonthDays: MonthDays{1}, + WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, + StartTime: "14:30:00", + EndTime: "15:00:00"} + i2 := &Interval{Months: Months{time.February}, + MonthDays: MonthDays{1}, + WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, + StartTime: "14:30:00", + EndTime: "15:00:00"} + i3 := &Interval{Months: Months{time.February}, + MonthDays: MonthDays{1}, + WeekDays: []time.Weekday{time.Wednesday}, + StartTime: "14:30:00", + EndTime: "15:00:00"} + ap := &ActivationPeriod{Intervals: []*Interval{i1}} + ap.AddIntervalIfNotPresent(i2) + if len(ap.Intervals) != 1 { + t.Error("Wronfully appended interval ;)") + } + ap.AddIntervalIfNotPresent(i3) + if len(ap.Intervals) != 2 { + t.Error("Wronfully not appended interval ;)") + } +} + /**************************** Benchmarks *************************************/ func BenchmarkActivationPeriodRestore(b *testing.B) { diff --git a/timespans/callcost_test.go b/timespans/callcost_test.go index 5a6b8b7a4..b7f5b443f 100644 --- a/timespans/callcost_test.go +++ b/timespans/callcost_test.go @@ -70,9 +70,6 @@ func TestMultipleResultMerge(t *testing.T) { cc2, _ := cd.GetCost() if cc2.Cost != 30 { t.Errorf("expected 30 was %v", cc2.Cost) - for _, i := range cd.ActivationPeriods[1].Intervals { - t.Log(i) - } for _, ts := range cc1.Timespans { t.Log(ts.Interval) } @@ -91,22 +88,22 @@ func TestMultipleInputLeftMerge(t *testing.T) { t2 := time.Date(2012, time.February, 2, 18, 01, 0, 0, time.UTC) cd := &CallDescriptor{Direction: "OUT", TOR: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} cc1, _ := cd.GetCost() - if cc1.Cost != 18 { - t.Errorf("expected 12 was %v", cc1.Cost) + if cc1.Cost != 90 { + t.Errorf("expected 90 was %v", cc1.Cost) } t1 = time.Date(2012, time.February, 2, 18, 01, 0, 0, time.UTC) t2 = time.Date(2012, time.February, 2, 18, 02, 0, 0, time.UTC) cd = &CallDescriptor{Direction: "OUT", TOR: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} cc2, _ := cd.GetCost() - if cc2.Cost != 6 { - t.Errorf("expected 6 was %v", cc2.Cost) + if cc2.Cost != 30 { + t.Errorf("expected 30 was %v", cc2.Cost) } cc1.Merge(cc2) if len(cc1.Timespans) != 2 || cc1.Timespans[1].GetDuration().Seconds() != 120 { t.Error("wrong resulted timespan: ", len(cc1.Timespans), cc1.Timespans[0].GetDuration().Seconds()) } - if cc1.Cost != 24 { - t.Errorf("Exdpected 24 was %v", cc1.Cost) + if cc1.Cost != 120 { + t.Errorf("Exdpected 120 was %v", cc1.Cost) } } @@ -115,22 +112,22 @@ func TestMultipleInputRightMerge(t *testing.T) { t2 := time.Date(2012, time.February, 2, 17, 59, 0, 0, time.UTC) cd := &CallDescriptor{Direction: "OUT", TOR: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} cc1, _ := cd.GetCost() - if cc1.Cost != 12 { - t.Errorf("expected 12 was %v", cc1.Cost) + if cc1.Cost != 60 { + t.Errorf("expected 60 was %v", cc1.Cost) } t1 = time.Date(2012, time.February, 2, 17, 59, 0, 0, time.UTC) t2 = time.Date(2012, time.February, 2, 18, 01, 0, 0, time.UTC) cd = &CallDescriptor{Direction: "OUT", TOR: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} cc2, _ := cd.GetCost() - if cc2.Cost != 18 { - t.Errorf("expected 18 was %v", cc2.Cost) + if cc2.Cost != 90 { + t.Errorf("expected 90 was %v", cc2.Cost) } cc1.Merge(cc2) if len(cc1.Timespans) != 2 || cc1.Timespans[0].GetDuration().Seconds() != 120 { t.Error("wrong resulted timespan: ", len(cc1.Timespans), cc1.Timespans[0].GetDuration().Seconds()) t.Log(cc1.Timespans[0].GetDuration()) } - if cc1.Cost != 30 { - t.Errorf("Exdpected 30 was %v", cc1.Cost) + if cc1.Cost != 150 { + t.Errorf("Exdpected 150 was %v", cc1.Cost) } } diff --git a/timespans/interval.go b/timespans/interval.go index 90a2e1449..3f627fef0 100644 --- a/timespans/interval.go +++ b/timespans/interval.go @@ -20,6 +20,7 @@ package timespans import ( "fmt" + "reflect" "strconv" "strings" "time" @@ -89,12 +90,6 @@ func (i *Interval) getRightMargin(t time.Time) (rigthtTime time.Time) { year, month, day := t.Year(), t.Month(), t.Day() hour, min, sec, nsec := 23, 59, 59, 0 loc := t.Location() - if len(i.Months) > 0 { - month = i.Months[len(i.Months)-1] - } - if len(i.MonthDays) > 0 { - day = i.MonthDays[len(i.MonthDays)-1] - } if i.EndTime != "" { split := strings.Split(i.EndTime, ":") hour, _ = strconv.Atoi(split[0]) @@ -111,12 +106,6 @@ func (i *Interval) getLeftMargin(t time.Time) (rigthtTime time.Time) { year, month, day := t.Year(), t.Month(), t.Day() hour, min, sec, nsec := 0, 0, 0, 0 loc := t.Location() - if len(i.Months) > 0 { - month = i.Months[0] - } - if len(i.MonthDays) > 0 { - day = i.MonthDays[0] - } if i.StartTime != "" { split := strings.Split(i.StartTime, ":") hour, _ = strconv.Atoi(split[0]) @@ -129,3 +118,11 @@ func (i *Interval) getLeftMargin(t time.Time) (rigthtTime time.Time) { func (i *Interval) String() string { return fmt.Sprintf("%v %v %v %v %v", i.Months, i.MonthDays, i.WeekDays, i.StartTime, i.EndTime) } + +func (i *Interval) Equals(o *Interval) bool { + return reflect.DeepEqual(i.Months, o.Months) && + reflect.DeepEqual(i.MonthDays, o.MonthDays) && + reflect.DeepEqual(i.WeekDays, o.WeekDays) && + i.StartTime == o.StartTime && + i.EndTime == o.EndTime +} diff --git a/timespans/interval_test.go b/timespans/interval_test.go index 9e3a3d38a..244ad66b4 100644 --- a/timespans/interval_test.go +++ b/timespans/interval_test.go @@ -121,7 +121,7 @@ func TestHours(t *testing.T) { } } -func TestEverything(t *testing.T) { +func TestIntervalEverything(t *testing.T) { i := &Interval{Months: Months{time.February}, MonthDays: MonthDays{1}, WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, @@ -145,6 +145,38 @@ func TestEverything(t *testing.T) { } } +func TestIntervalEquals(t *testing.T) { + i1 := &Interval{Months: Months{time.February}, + MonthDays: MonthDays{1}, + WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, + StartTime: "14:30:00", + EndTime: "15:00:00"} + i2 := &Interval{Months: Months{time.February}, + MonthDays: MonthDays{1}, + WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, + StartTime: "14:30:00", + EndTime: "15:00:00"} + if !i1.Equals(i2) || !i2.Equals(i1) { + t.Errorf("%v and %v are not equal", i1, i2) + } +} + +func TestIntervalNotEquals(t *testing.T) { + i1 := &Interval{Months: Months{time.February}, + MonthDays: MonthDays{1}, + WeekDays: []time.Weekday{time.Wednesday}, + StartTime: "14:30:00", + EndTime: "15:00:00"} + i2 := &Interval{Months: Months{time.February}, + MonthDays: MonthDays{1}, + WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, + StartTime: "14:30:00", + EndTime: "15:00:00"} + if i1.Equals(i2) || i2.Equals(i1) { + t.Errorf("%v and %v not equal", i1, i2) + } +} + func BenchmarkIntervalContainsDate(b *testing.B) { i := &Interval{Months: Months{time.February}, MonthDays: MonthDays{1}, WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, StartTime: "14:30:00", EndTime: "15:00:00"} d := time.Date(2012, time.February, 1, 14, 30, 0, 0, time.UTC) diff --git a/timespans/timespans.go b/timespans/timespans.go index 4127d21b6..16034a3d4 100644 --- a/timespans/timespans.go +++ b/timespans/timespans.go @@ -20,7 +20,7 @@ package timespans import ( "fmt" - // "log" + //"log" "time" ) @@ -102,7 +102,7 @@ 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) SplitByInterval(i *Interval) (nts *TimeSpan) { - //log.Print("here: ", i) + //log.Print("here: ", ts, " +++ ", i) // if the span is not in interval return nil if !(i.Contains(ts.TimeStart) || i.Contains(ts.TimeEnd)) { //log.Print("Not in interval")