From b151d0dc821132491fcba2d7ed27c0fc3a536549 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Sat, 30 Jun 2012 16:10:34 +0300 Subject: [PATCH] correcting rates loading --- cmd/cgr-loader/helpers.go | 12 +++++++ cmd/cgr-loader/rates.go | 14 ++++---- data/Destinations.csv | 5 ++- data/Rates.csv | 4 ++- data/RatesTimings.csv | 5 ++- data/RatingProfiles.csv | 4 ++- data/Timings.csv | 4 +-- timespans/actions_test.go | 6 ++-- timespans/activationperiod_test.go | 5 +-- timespans/callcost_test.go | 57 +++++++++++++++++------------- timespans/calldesc_test.go | 2 +- timespans/timespans.go | 7 +++- 12 files changed, 78 insertions(+), 47 deletions(-) diff --git a/cmd/cgr-loader/helpers.go b/cmd/cgr-loader/helpers.go index ea7ede0bd..53125529e 100644 --- a/cmd/cgr-loader/helpers.go +++ b/cmd/cgr-loader/helpers.go @@ -119,3 +119,15 @@ func (cds CallDescriptors) getKey(key string) *timespans.CallDescriptor { } return nil } + +/*func (cds CallDescriptors) setIntervalEndTime() { + for _, cd := range cds { + for _, ap := range cd.ActivationPeriods { + for x, i := range ap.Intervals { + if x < len(ap.Intervals)-1 { + i.EndTime = ap.Intervals[x+1].StartTime + } + } + } + } +}*/ diff --git a/cmd/cgr-loader/rates.go b/cmd/cgr-loader/rates.go index f8383f75a..fb26584d4 100644 --- a/cmd/cgr-loader/rates.go +++ b/cmd/cgr-loader/rates.go @@ -134,7 +134,6 @@ func loadRatesTimings() { log.Printf("Could not get timing for tag %v", record[2]) continue } - for _, t := range ts { rt := NewRateTiming(record[1], t) ratesTimings[tag] = append(ratesTimings[tag], rt) @@ -163,18 +162,17 @@ func loadRatingProfiles() { continue } tenant, tor, direction, subject, fallbacksubject := record[0], record[1], record[2], record[3], record[4] - fmt.Sprintf("%s:%s:%s:%s:%s", direction, tenant, tor, subject) at, err := time.Parse(time.RFC3339, record[6]) if err != nil { - log.Printf("Cannot parse activation time from %v", record[5]) + log.Printf("Cannot parse activation time from %v", record[6]) continue } rts, exists := ratesTimings[record[5]] if !exists { - log.Printf("Could not get rate timing for tag %v", record[4]) + log.Printf("Could not get rate timing for tag %v", record) continue } - for _, rt := range rts { // rates timing + for _, rt := range rts { // rates timing rs, exists := rates[rt.RatesTag] if !exists { log.Printf("Could not get rates for tag %v", rt.RatesTag) @@ -186,10 +184,9 @@ func loadRatingProfiles() { for _, r := range rs { //rates for _, d := range destinations { if d.Id == r.DestinationsTag { + log.Print("dId: ", d.Id) ap.AddInterval(rt.GetInterval(r)) - log.Print(d) - for _, p := range d.Prefixes { //destinations - log.Print("P: ", p) + for _, p := range d.Prefixes { //destinations // Search for a CallDescriptor with the same key var cd *timespans.CallDescriptor key := fmt.Sprintf("%s:%s:%s:%s:%s", direction, tenant, tor, subject, p) @@ -242,6 +239,7 @@ func loadRatingProfiles() { log.Print("Call descriptors:") for dest, cds := range ratingProfiles { log.Print(dest) + //cds.setIntervalEndTime() for _, cd := range cds { log.Print(cd) } diff --git a/data/Destinations.csv b/data/Destinations.csv index 2b46411f2..c3e0822fa 100644 --- a/data/Destinations.csv +++ b/data/Destinations.csv @@ -4,4 +4,7 @@ GERMANY_O2,41 GERMANY_PREMIUM,43 ALL,49 ALL,41 -ALL,43 \ No newline at end of file +ALL,43 +NAT,0256 +NAT,0257 +NAT,0723 \ No newline at end of file diff --git a/data/Rates.csv b/data/Rates.csv index dcc537495..137782ca5 100644 --- a/data/Rates.csv +++ b/data/Rates.csv @@ -4,4 +4,6 @@ RT_STANDARD,GERMANY_O2,0,0.1,1,10 RT_STANDARD,GERMANY_PREMIUM,0,0.1,1,10 RT_DEFAULT,ALL,0,0.1,1,10 RT_STD_WEEKEND,GERMANY,0,0.1,1,10 -RT_STD_WEEKEND,GERMANY_O2,0,0.05,1,10 \ No newline at end of file +RT_STD_WEEKEND,GERMANY_O2,0,0.05,1,10 +P1,NAT,0,1,1,10 +P2,NAT,0,0.5,1,10 \ No newline at end of file diff --git a/data/RatesTimings.csv b/data/RatesTimings.csv index a5be1c997..2d49a5112 100644 --- a/data/RatesTimings.csv +++ b/data/RatesTimings.csv @@ -3,4 +3,7 @@ STANDARD,RT_STANDARD,WORKDAYS_00 STANDARD,RT_STD_WEEKEND,WORKDAYS_18 STANDARD,RT_STD_WEEKEND,WEEKENDS PREMIUM,RT_STD_WEEKEND,WEEKENDS -DEFAULT,RT_DEFAULT,WORKDAYS_00 \ No newline at end of file +DEFAULT,RT_DEFAULT,WORKDAYS_00 +EVENING,P1,WORKDAYS_00 +EVENING,P2,WORKDAYS_18 +EVENING,P2,WEEKENDS \ No newline at end of file diff --git a/data/RatingProfiles.csv b/data/RatingProfiles.csv index 8db032214..b45a7622f 100644 --- a/data/RatingProfiles.csv +++ b/data/RatingProfiles.csv @@ -2,4 +2,6 @@ Tenant,TOR,Direction,Subject,RatesFallbackSubject,RatesTimingTag,ActivationTime CUSTOMER_1,0,OUT,rif:from:tm,danb,PREMIUM,2012-01-01T00:00:00Z CUSTOMER_1,0,OUT,rif:from:tm,danb,STANDARD,2012-02-28T00:00:00Z CUSTOMER_2,0,OUT,danb:87.139.12.167,danb,STANDARD,2012-01-01T00:00:00Z -CUSTOMER_1,0,OUT,danb,,PREMIUM,2012-01-01T00:00:00Z \ No newline at end of file +CUSTOMER_1,0,OUT,danb,,PREMIUM,2012-01-01T00:00:00Z +vdf,0,OUT,rif,,EVENING,2012-01-01T00:00:00Z +vdf,0,OUT,rif,,EVENING,2012-02-28T00:00:00Z \ No newline at end of file diff --git a/data/Timings.csv b/data/Timings.csv index 25445417c..7e5cd24d7 100644 --- a/data/Timings.csv +++ b/data/Timings.csv @@ -1,6 +1,6 @@ TimingTag,Months,MonthDays,WeekDays,StartTime -WORKDAYS_00,*all,*all,*all,00:00:00 -WORKDAYS_18,*all,*all,*all,18:00:00 +WORKDAYS_00,*all,*all,1;2;3;4;5,00:00:00 +WORKDAYS_18,*all,*all,6;7,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/actions_test.go b/timespans/actions_test.go index 5cd52e915..b24bc90b3 100644 --- a/timespans/actions_test.go +++ b/timespans/actions_test.go @@ -146,17 +146,17 @@ func TestActionTimingHourMonthdaysMonths(t *testing.T) { if now.After(testTime) { day = tomorrow.Day() } - /*month := now.Month() + month := now.Month() if now.After(testTime) { month = nextMonth.Month() - }*/ + } at := &ActionTiming{Timing: &Interval{ Months: Months{now.Month(), nextMonth.Month()}, MonthDays: MonthDays{now.Day(), tomorrow.Day()}, StartTime: "10:01:00", }} st := at.GetNextStartTime() - expected := time.Date(y, m, day, 10, 1, 0, 0, time.Local) + expected := time.Date(y, month, day, 10, 1, 0, 0, time.Local) if !st.Equal(expected) { t.Errorf("Expected %v was %v", expected, st) } diff --git a/timespans/activationperiod_test.go b/timespans/activationperiod_test.go index 0dbc3c53a..4a179dec1 100644 --- a/timespans/activationperiod_test.go +++ b/timespans/activationperiod_test.go @@ -39,10 +39,7 @@ func TestApRestoreRedis(t *testing.T) { Subject: "rif:from:tm", Destination: "49"} cd.SearchStorageForPrefix() - if len(cd.ActivationPeriods) != 4 { - for _, ap := range cd.ActivationPeriods { - t.Log(ap) - } + if len(cd.ActivationPeriods) != 2 { t.Error("Error restoring activation periods: ", len(cd.ActivationPeriods)) } } diff --git a/timespans/callcost_test.go b/timespans/callcost_test.go index d951318ae..5a6b8b7a4 100644 --- a/timespans/callcost_test.go +++ b/timespans/callcost_test.go @@ -32,69 +32,78 @@ func init() { func TestSingleResultMerge(t *testing.T) { t1 := time.Date(2012, time.February, 2, 17, 00, 0, 0, time.UTC) t2 := time.Date(2012, time.February, 2, 17, 01, 0, 0, time.UTC) - cd := &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} + 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, 01, 0, 0, time.UTC) t2 = time.Date(2012, time.February, 2, 17, 02, 0, 0, time.UTC) - cd = &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} + cd = &CallDescriptor{Direction: "OUT", TOR: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} cc2, _ := cd.GetCost() - if cc2.Cost != 12 { - t.Errorf("expected 12 was %v", cc2.Cost) + if cc2.Cost != 60 { + t.Errorf("expected 60 was %v", cc2.Cost) } cc1.Merge(cc2) if len(cc1.Timespans) != 1 || cc1.Timespans[0].GetDuration().Seconds() != 120 { - t.Error("wrong resulted timespan") + 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) } } func TestMultipleResultMerge(t *testing.T) { t1 := time.Date(2012, time.February, 2, 17, 59, 0, 0, time.UTC) t2 := time.Date(2012, time.February, 2, 18, 00, 0, 0, time.UTC) - cd := &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} + 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) + for _, ts := range cc1.Timespans { + t.Log(ts.Interval) + } } t1 = time.Date(2012, time.February, 2, 18, 00, 0, 0, time.UTC) t2 = time.Date(2012, time.February, 2, 18, 01, 0, 0, time.UTC) - cd = &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} + 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) + for _, i := range cd.ActivationPeriods[1].Intervals { + t.Log(i) + } + for _, ts := range cc1.Timespans { + t.Log(ts.Interval) + } } cc1.Merge(cc2) if len(cc1.Timespans) != 2 || cc1.Timespans[0].GetDuration().Seconds() != 60 { - t.Error("wrong resulted timespan") + t.Error("wrong resulted timespan: ", len(cc1.Timespans), cc1.Timespans[0].GetDuration().Seconds()) } - if cc1.Cost != 18 { - t.Errorf("Exdpected 18 was %v", cc1.Cost) + if cc1.Cost != 90 { + t.Errorf("Exdpected 90 was %v", cc1.Cost) } } func TestMultipleInputLeftMerge(t *testing.T) { 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{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} + 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) } 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{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} + 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) } cc1.Merge(cc2) if len(cc1.Timespans) != 2 || cc1.Timespans[1].GetDuration().Seconds() != 120 { - t.Error("wrong resulted timespan") + 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) @@ -104,21 +113,21 @@ func TestMultipleInputLeftMerge(t *testing.T) { func TestMultipleInputRightMerge(t *testing.T) { t1 := time.Date(2012, time.February, 2, 17, 58, 0, 0, time.UTC) t2 := time.Date(2012, time.February, 2, 17, 59, 0, 0, time.UTC) - cd := &CallDescriptor{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} + 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) } 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{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} + 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) } cc1.Merge(cc2) if len(cc1.Timespans) != 2 || cc1.Timespans[0].GetDuration().Seconds() != 120 { - t.Error("wrong resulted timespan") + t.Error("wrong resulted timespan: ", len(cc1.Timespans), cc1.Timespans[0].GetDuration().Seconds()) t.Log(cc1.Timespans[0].GetDuration()) } if cc1.Cost != 30 { diff --git a/timespans/calldesc_test.go b/timespans/calldesc_test.go index d5f8abe5e..4b66b0aaa 100644 --- a/timespans/calldesc_test.go +++ b/timespans/calldesc_test.go @@ -36,7 +36,7 @@ func init() { 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{Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} + cd := &CallDescriptor{Direction: "OUT", TOR: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} cd.SearchStorageForPrefix() timespans := cd.splitInTimeSpans() diff --git a/timespans/timespans.go b/timespans/timespans.go index 61d8274c1..4127d21b6 100644 --- a/timespans/timespans.go +++ b/timespans/timespans.go @@ -102,17 +102,21 @@ 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) // if the span is not in interval return nil if !(i.Contains(ts.TimeStart) || i.Contains(ts.TimeEnd)) { + //log.Print("Not in interval") return } // if the span is enclosed in the interval try to set as new interval and return nil if i.Contains(ts.TimeStart) && i.Contains(ts.TimeEnd) { + //log.Print("All in interval") ts.SetInterval(i) return } // if only the start time is in the interval split the interval if i.Contains(ts.TimeStart) { + //log.Print("Start in interval") splitTime := i.getRightMargin(ts.TimeStart) ts.SetInterval(i) if splitTime == ts.TimeStart { @@ -125,6 +129,7 @@ func (ts *TimeSpan) SplitByInterval(i *Interval) (nts *TimeSpan) { } // if only the end time is in the interval split the interval if i.Contains(ts.TimeEnd) { + //log.Print("End in interval") splitTime := i.getLeftMargin(ts.TimeEnd) if splitTime == ts.TimeEnd { return @@ -151,7 +156,7 @@ func (ts *TimeSpan) SplitByActivationPeriod(ap *ActivationPeriod) (newTs *TimeSp } /* -Splits the given timespan on activation period's activation time. +Splits the given timespan on minute bucket's duration. */ func (ts *TimeSpan) SplitByMinuteBucket(mb *MinuteBucket) (newTs *TimeSpan) { s := ts.GetDuration().Seconds()