correcting rates loading

This commit is contained in:
Radu Ioan Fericean
2012-06-30 16:10:34 +03:00
parent e1827017a0
commit b151d0dc82
12 changed files with 78 additions and 47 deletions

View File

@@ -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
}
}
}
}
}*/

View File

@@ -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)
}

View File

@@ -4,4 +4,7 @@ GERMANY_O2,41
GERMANY_PREMIUM,43
ALL,49
ALL,41
ALL,43
ALL,43
NAT,0256
NAT,0257
NAT,0723
1 Tag Prefix
4 GERMANY_PREMIUM 43
5 ALL 49
6 ALL 41
7 ALL 43
8 NAT 0256
9 NAT 0257
10 NAT 0723

View File

@@ -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
RT_STD_WEEKEND,GERMANY_O2,0,0.05,1,10
P1,NAT,0,1,1,10
P2,NAT,0,0.5,1,10
1 Tag DestinationsTag ConnectFee Price BillingUnit Weight
4 RT_STANDARD GERMANY_PREMIUM 0 0.1 1 10
5 RT_DEFAULT ALL 0 0.1 1 10
6 RT_STD_WEEKEND GERMANY 0 0.1 1 10
7 RT_STD_WEEKEND GERMANY_O2 0 0.05 1 10
8 P1 NAT 0 1 1 10
9 P2 NAT 0 0.5 1 10

View File

@@ -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
DEFAULT,RT_DEFAULT,WORKDAYS_00
EVENING,P1,WORKDAYS_00
EVENING,P2,WORKDAYS_18
EVENING,P2,WEEKENDS
1 Tag RatesTag TimingProfile
3 STANDARD RT_STD_WEEKEND WORKDAYS_18
4 STANDARD RT_STD_WEEKEND WEEKENDS
5 PREMIUM RT_STD_WEEKEND WEEKENDS
6 DEFAULT RT_DEFAULT WORKDAYS_00
7 EVENING P1 WORKDAYS_00
8 EVENING P2 WORKDAYS_18
9 EVENING P2 WEEKENDS

View File

@@ -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
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
1 Tenant TOR Direction Subject RatesFallbackSubject RatesTimingTag ActivationTime
2 CUSTOMER_1 0 OUT rif:from:tm danb PREMIUM 2012-01-01T00:00:00Z
3 CUSTOMER_1 0 OUT rif:from:tm danb STANDARD 2012-02-28T00:00:00Z
4 CUSTOMER_2 0 OUT danb:87.139.12.167 danb STANDARD 2012-01-01T00:00:00Z
5 CUSTOMER_1 0 OUT danb PREMIUM 2012-01-01T00:00:00Z
6 vdf 0 OUT rif EVENING 2012-01-01T00:00:00Z
7 vdf 0 OUT rif EVENING 2012-02-28T00:00:00Z

View File

@@ -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
1 TimingTag Months MonthDays WeekDays StartTime
2 WORKDAYS_00 *all *all *all 1;2;3;4;5 00:00:00
3 WORKDAYS_18 *all *all *all 6;7 18:00:00
4 WEEKENDS *all *all 6;7 00:00:00
5 WEEKLY_SAME_TIME *all *all 1 *now
6 FIRST_DAY_OF_MONTH *all 1 *all 00:00:00

View File

@@ -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)
}

View File

@@ -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))
}
}

View File

@@ -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 {

View File

@@ -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()

View File

@@ -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()