working at rates

This commit is contained in:
Radu Ioan Fericean
2012-05-30 18:32:11 +03:00
parent 3d6d7289d1
commit c1005b6170
17 changed files with 255 additions and 65 deletions

View File

@@ -19,33 +19,213 @@ package main
import (
"flag"
// "github.com/rif/cgrates/timespans"
"github.com/rif/cgrates/timespans"
"log"
"os"
"strconv"
"encoding/csv"
"time"
)
var (
separator = flag.String("separator", ";", "Default field separator")
redisserver = flag.String("redisserver", "tcp:127.0.0.1:6379", "redis server address (tcp:127.0.0.1:6379)")
redisdb = flag.Int("rdb", 10, "redis database number (10)")
redispass = flag.String("pass", "", "redis database password")
months = flag.String("month", "Months.csv", "Months file")
separator = flag.String("separator", ",", "Default field separator")
redisserver = flag.String("redisserver", "tcp:127.0.0.1:6379", "redis server address (tcp:127.0.0.1:6379)")
redisdb = flag.Int("rdb", 10, "redis database number (10)")
redispass = flag.String("pass", "", "redis database password")
monthsFn = flag.String("month", "Months.csv", "Months file")
monthdaysFn = flag.String("monthdays", "MonthDays.csv", "Month days file")
weekdaysFn = flag.String("weekdays", "WeekDays.csv", "Week days file")
destinationsFn = flag.String("destinations", "Destinations.csv", "Destinations file")
ratesFn = flag.String("rates", "Rates.csv", "Rates file")
ratingprofilesFn = flag.String("ratingprofiles", "RatingProfiles.csv", "Rating profiles file")
)
func main() {
flag.Parse()
fp, err := os.Open(*months)
var (
months = make(map[string][]time.Month)
monthdays = make(map[string][]int)
weekdays = make(map[string][]time.Weekday)
destinations = make(map[string][]string)
rates = make(map[string][]*timespans.Interval)
ratingProfiles = make(map[string][]*timespans.ActivationPeriod)
)
func loadDataSeries() {
// MONTHS
fp, err := os.Open(*monthsFn)
if err != nil {
log.Printf("Could not open months file: %v", err)
}
csv := csv.NewReader(fp)
csv.Comma = rune(*separator)
for record, err := csv.Read(); err == nil; record, err = csv.Read() {
if record[0] == "Tag" {
csvReader := csv.NewReader(fp)
sep := []rune(*separator)[0]
csvReader.Comma = sep
for record, err := csvReader.Read(); err == nil; record, err = csvReader.Read() {
tag := record[0]
if tag == "Tag" {
// skip header line
continue
}
log.Print(record)
for i, m := range record[1:] {
if m == "1" {
months[tag] = append(months[tag], time.Month(i+1))
}
}
log.Print(tag, months[tag])
}
fp.Close()
// MONTH DAYS
fp, err = os.Open(*monthdaysFn)
if err != nil {
log.Printf("Could not open month days file: %v", err)
}
csvReader = csv.NewReader(fp)
csvReader.Comma = sep
for record, err := csvReader.Read(); err == nil; record, err = csvReader.Read() {
tag := record[0]
if tag == "Tag" {
// skip header line
continue
}
for i, m := range record[1:] {
if m == "1" {
monthdays[tag] = append(monthdays[tag], i+1)
}
}
log.Print(tag, monthdays[tag])
}
fp.Close()
// WEEK DAYS
fp, err = os.Open(*weekdaysFn)
if err != nil {
log.Printf("Could not open week days file: %v", err)
}
csvReader = csv.NewReader(fp)
csvReader.Comma = sep
for record, err := csvReader.Read(); err == nil; record, err = csvReader.Read() {
tag := record[0]
if tag == "Tag" {
// skip header line
continue
}
for i, m := range record[1:] {
if m == "1" {
weekdays[tag] = append(weekdays[tag], time.Weekday(((i + 1) % 7)))
}
}
log.Print(tag, weekdays[tag])
}
fp.Close()
}
func loadDestinations() {
fp, err := os.Open(*destinationsFn)
defer fp.Close()
if err != nil {
log.Printf("Could not open destinations file: %v", err)
return
}
csvReader := csv.NewReader(fp)
sep := []rune(*separator)[0]
csvReader.Comma = sep
for record, err := csvReader.Read(); err == nil; record, err = csvReader.Read() {
tag := record[0]
if tag == "Tag" {
// skip header line
continue
}
for _, m := range record[1:] {
destinations[tag] = append(destinations[tag], m)
}
log.Print(tag, destinations[tag])
}
}
func loadRates() {
fp, err := os.Open(*ratesFn)
defer fp.Close()
if err != nil {
log.Printf("Could not open rates file: %v", err)
return
}
csvReader := csv.NewReader(fp)
sep := []rune(*separator)[0]
csvReader.Comma = sep
for record, err := csvReader.Read(); err == nil; record, err = csvReader.Read() {
tag := record[0]
if tag == "Tag" {
// skip header line
continue
}
if len(record) < 9 {
log.Printf("Malformed rates record: %v", record)
continue
}
cf, err := strconv.ParseFloat(record[5], 64)
if err != nil {
log.Printf("Error parsing connect fee from: %v", record)
continue
}
p, err := strconv.ParseFloat(record[6], 64)
if err != nil {
log.Printf("Error parsing price from: %v", record)
continue
}
bu, err := strconv.ParseFloat(record[7], 64)
if err != nil {
log.Printf("Error parsing billing unit from: %v", record)
continue
}
w, err := strconv.ParseFloat(record[8], 64)
if err != nil {
log.Printf("Error parsing weight from: %v", record)
continue
}
i := &timespans.Interval{
Months: timespans.Months(months[record[1]]),
MonthDays: timespans.MonthDays(monthdays[record[2]]),
WeekDays: timespans.WeekDays(weekdays[record[3]]),
StartTime: record[4],
ConnectFee: cf,
Price: p,
BillingUnit: bu,
Weight: w,
}
rates[tag] = append(rates[tag], i)
log.Print(tag)
for _, i := range rates[tag] {
log.Print(i)
}
}
}
func loadRatingProfiles() {
fp, err := os.Open(*destinationsFn)
defer fp.Close()
if err != nil {
log.Printf("Could not open destinations file: %v", err)
return
}
csvReader := csv.NewReader(fp)
sep := []rune(*separator)[0]
csvReader.Comma = sep
for record, err := csvReader.Read(); err == nil; record, err = csvReader.Read() {
tag := record[0]
if tag == "Tag" {
// skip header line
continue
}
for _, m := range record[1:] {
destinations[tag] = append(destinations[tag], m)
}
log.Print(tag, destinations[tag])
}
}
func main() {
flag.Parse()
loadDataSeries()
loadDestinations()
loadRates()
}

View File

@@ -1,3 +1,3 @@
Account;AccountType;MonetaryBalance;SMSBalance;TrafficBalance;MonthsTag;MonthDaysTag;WeekDaysTag;StartTime;RecurrentTopupTag;RecurrentClears
rif;postpaid;10;50;100;ALL;20th;ALL;00:00:00;STARTER_PACK;1
danb;prepaid;10;50;500;ALL;20th;ALL;00:00:00;STARTER_PACK;1
Account,AccountType,MonetaryBalance,SMSBalance,TrafficBalance,RecurrentTopupsTag,RecurrentMonthsTag,RecurrentMonthDaysTag,RecurrentWeekDaysTag,RecurrentStartTime,RecurrentClears
rif,postpaid,10,50,100,STARTER_PACK,ALL,20th,ALL,00:00:00,1
danb,prepaid,10,50,500,STARTER_PACK,ALL,20th,ALL,00:00:00,1
1 Account AccountType MonetaryBalance SMSBalance TrafficBalance MonthsTag RecurrentTopupsTag MonthDaysTag RecurrentMonthsTag WeekDaysTag RecurrentMonthDaysTag StartTime RecurrentWeekDaysTag RecurrentTopupTag RecurrentStartTime RecurrentClears
2 rif postpaid 10 50 100 ALL STARTER_PACK 20th ALL ALL 20th 00:00:00 ALL STARTER_PACK 00:00:00 1
3 danb prepaid 10 50 500 ALL STARTER_PACK 20th ALL ALL 20th 00:00:00 ALL STARTER_PACK 00:00:00 1

View File

@@ -1,3 +1,6 @@
Tag;Prefix
GERMANY;49
GERMANY_MOBILE_O2;49176
Tag,Prefix
GERMANY,49
GERMANY_MOBILE_O2,49176
ALL,49
ALL,41
ALL,43
1 Tag Prefix
2 GERMANY 49
3 GERMANY_MOBILE_O2 49176
4 ALL 49
5 ALL 41
6 ALL 43

View File

@@ -1,3 +1,3 @@
Tag;TOR;InboundUnits;MonetaryUnits;SMSUnits;TrafficUnits
STARTER_BONUSES;0;60;0.01;0;0
STARTER_BONUSES;100;1;0.01;0;0
Tag,TOR,InboundUnits,MonetaryUnits,SMSUnits,TrafficUnits
STARTER_BONUSES,0,60,0.01,0,0
STARTER_BONUSES,100,1,0.01,0,0
1 Tag TOR InboundUnits MonetaryUnits SMSUnits TrafficUnits
2 STARTER_BONUSES 0 60 0.01 0 0
3 STARTER_BONUSES 100 1 0.01 0 0

View File

@@ -1,6 +1,6 @@
Tag;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31
ALL;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1
CHRISTMAS_DAY;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;1;0;0;0;0;0;0
VALENTINES_DAY;0;0;0;0;0;0;0;0;0;0;0;0;0;1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
20th;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;1;0;0;0;0;0;0;0;0;0;0;0
FIRST;1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
Tag,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
ALL,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
CHRISTMAS_DAY,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0
VALENTINES_DAY,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
20th,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0
FIRST,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1 Tag 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
2 ALL 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3 CHRISTMAS_DAY 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
4 VALENTINES_DAY 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 20th 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
6 FIRST 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

View File

@@ -1,7 +1,7 @@
Tag;January;February;March;April;May;June;Jully;August;September;October;November;December
ALL;1;1;1;1;1;1;1;1;1;1;1;1
WINTER;1;1;0;0;0;0;0;0;0;0;0;1
SPRING;0;0;1;1;1;0;0;0;0;0;0;0
SUMMER;0;0;0;0;0;1;1;1;0;0;0;0
AUTUMN;0;0;0;0;0;0;0;0;1;1;1;0
JANUARY;1;0;0;0;0;0;0;0;0;0;0;0
Tag,January,February,March,April,May,June,Jully,August,September,October,November,December
ALL,1,1,1,1,1,1,1,1,1,1,1,1
WINTER,1,1,0,0,0,0,0,0,0,0,0,1
SPRING,0,0,1,1,1,0,0,0,0,0,0,0
SUMMER,0,0,0,0,0,1,1,1,0,0,0,0
AUTUMN,0,0,0,0,0,0,0,0,1,1,1,0
JANUARY,1,0,0,0,0,0,0,0,0,0,0,0
1 Tag January February March April May June Jully August September October November December
2 ALL 1 1 1 1 1 1 1 1 1 1 1 1
3 WINTER 1 1 0 0 0 0 0 0 0 0 0 1
4 SPRING 0 0 1 1 1 0 0 0 0 0 0 0
5 SUMMER 0 0 0 0 0 1 1 1 0 0 0 0
6 AUTUMN 0 0 0 0 0 0 0 0 1 1 1 0
7 JANUARY 1 0 0 0 0 0 0 0 0 0 0 0

View File

@@ -1,3 +1,3 @@
Tag;TOR;OutboundUnits;DestinationsTag;MonetaryUnits;SMSUnits;TrafficUnits
STARTER_BONUSES;0;60;GERMANY;0.01;0;0
STARTER_BONUSES;100;1;GERMANY;0.01;0;0
Tag,TOR,OutboundUnits,DestinationsTag,MonetaryUnits,SMSUnits,TrafficUnits
STARTER_BONUSES,0,60,GERMANY,0.01,0,0
STARTER_BONUSES,100,1,GERMANY,0.01,0,0
1 Tag TOR OutboundUnits DestinationsTag MonetaryUnits SMSUnits TrafficUnits
2 STARTER_BONUSES 0 60 GERMANY 0.01 0 0
3 STARTER_BONUSES 100 1 GERMANY 0.01 0 0

View File

@@ -1,4 +1,5 @@
Tag;MonthsTag;MonthDaysTag;WeekDaysTag;StartTime;ConnectFee;Price;BillingUnit;Weight
STANDARD;ALL;ALL;WORKDAYS;00:00:00;0;0.2;1;10
STANDARD;ALL;ALL;WORKDAYS;18:00:00;0;0.1;1;10
STANDARD;ALL;ALL;WEEKENDS;00:00:00;0;0.1;1;10
Tag,DestinationsTag,MonthsTag,MonthDaysTag,WeekDaysTag,StartTime,ConnectFee,Price,BillingUnit,Weight
STANDARD,GERMANY,ALL,ALL,WORKDAYS,00:00:00,0,0.2,1,10
STANDARD,GERMANY_O2,ALL,ALL,WORKDAYS,18:00:00,0,0.1,1,10
STANDARD,GERMANY_PREMIUM,ALL,ALL,WEEKENDS,00:00:00,0,0.1,1,10
DEFAULT,ALL,ALL,ALL,ALL,00:00:00,0,0.1,1,10
1 Tag DestinationsTag MonthsTag MonthDaysTag WeekDaysTag StartTime ConnectFee Price BillingUnit Weight
2 STANDARD GERMANY ALL ALL WORKDAYS 00:00:00 0 0.2 1 10
3 STANDARD GERMANY_O2 ALL ALL WORKDAYS 18:00:00 0 0.1 1 10
4 STANDARD GERMANY_PREMIUM ALL ALL WEEKENDS 00:00:00 0 0.1 1 10
5 DEFAULT ALL ALL ALL ALL 00:00:00 0 0.1 1 10

View File

@@ -1,3 +1,4 @@
Tenant;Subject;TOR;DestinationsTag;RatesTag;ActivationTime
CUSTOMER_1;rif;0;GERMANY;STANDARD;2012-01-01T00:00:00:00.00000
CUSTOMER_2;danb;0;GERMANY_O2;STANDARD;2012-01-01T00:00:00:00.00000
Tenant,Subject,TOR,RatesTag,FallbackRatesTag,ActivationTime
CUSTOMER_1,rif,0,STANDARD,DEFAULT,2012-01-01T00:00:00:00.00000
CUSTOMER_2,danb,0,STANDARD,DEFAULT,2012-01-01T00:00:00:00.00000
CUSTOMER_1,danb,0,PREMIUM,,2012-01-01T00:00:00:00.00000
1 Tenant Subject TOR DestinationsTag RatesTag FallbackRatesTag ActivationTime
2 CUSTOMER_1 rif 0 GERMANY STANDARD DEFAULT 2012-01-01T00:00:00:00.00000
3 CUSTOMER_2 danb 0 GERMANY_O2 STANDARD DEFAULT 2012-01-01T00:00:00:00.00000
4 CUSTOMER_1 danb 0 PREMIUM 2012-01-01T00:00:00:00.00000

View File

@@ -1,2 +1,2 @@
Tag;MonetaryUnits;SMSUnits;TrafficUnits
DEBIT_5;5;0;0
Tag,MonetaryUnits,SMSUnits,TrafficUnits
DEBIT_5,5,0,0
1 Tag MonetaryUnits SMSUnits TrafficUnits
2 DEBIT_5 5 0 0

View File

@@ -1,2 +1,2 @@
Tag;RecurrentDebitsTag;MonetaryBalance;SMSBalance;TrafficBalance;VolumeRatesTag;VolumeDiscountsTag;InboundBonusesTag;OutboundBonusesTag
STARTER_PACK;DEBIT_5;2;50;100;GERMANY_100_LANDLINE_MINUTES;UPTO_2000_GERMANY_10PERC;STARTER_BONUSES;STARTER_BONUSES
Tag,RecurrentDebitsTag,MonetaryBalance,SMSBalance,TrafficBalance,VolumeRatesTag,VolumeDiscountsTag,InboundBonusesTag,OutboundBonusesTag
STARTER_PACK,DEBIT_5,2,50,100,GERMANY_100_LANDLINE_MINUTES,UPTO_2000_GERMANY_10PERC,STARTER_BONUSES,STARTER_BONUSES
1 Tag RecurrentDebitsTag MonetaryBalance SMSBalance TrafficBalance VolumeRatesTag VolumeDiscountsTag InboundBonusesTag OutboundBonusesTag
2 STARTER_PACK DEBIT_5 2 50 100 GERMANY_100_LANDLINE_MINUTES UPTO_2000_GERMANY_10PERC STARTER_BONUSES STARTER_BONUSES

View File

@@ -1,2 +1,2 @@
Tag;TOR;DestinationsTag;VolumeUnits;Discount;Weight
UPTO_2000_GERMANY_10PERC;0;GERMANY;2000;10;10
Tag,TOR,DestinationsTag,VolumeUnits,Discount,Weight
UPTO_2000_GERMANY_10PERC,0,GERMANY,2000,10,10
1 Tag TOR DestinationsTag VolumeUnits Discount Weight
2 UPTO_2000_GERMANY_10PERC 0 GERMANY 2000 10 10

View File

@@ -1,2 +1,2 @@
Tag;TOR;DestinationsTag;VolumeUnits;Price;Weight
GERMANY_100_LANDLINE_MINUTES;0;GERMANY;100;0;10
Tag,TOR,DestinationsTag,VolumeUnits,Price,Weight
GERMANY_100_LANDLINE_MINUTES,0,GERMANY,100,0,10
1 Tag TOR DestinationsTag VolumeUnits Price Weight
2 GERMANY_100_LANDLINE_MINUTES 0 GERMANY 100 0 10

View File

@@ -1,4 +1,4 @@
Tag;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday;Sunday
ALL;1;1;1;1;1;1;1
WORKDAYS;1;1;1;1;1;0;0
WEEKENDS;0;0;0;0;0;1;1
Tag,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
ALL,1,1,1,1,1,1,1
WORKDAYS,1,1,1,1,1,0,0
WEEKENDS,0,0,0,0,0,1,1
1 Tag Monday Tuesday Wednesday Thursday Friday Saturday Sunday
2 ALL 1 1 1 1 1 1 1
3 WORKDAYS 1 1 1 1 1 0 0
4 WEEKENDS 0 0 0 0 0 1 1

View File

@@ -54,7 +54,7 @@ func (ap *ActivationPeriod) store() (result string) {
is += i.WeekDays.store() + "|"
is += i.StartTime + "|"
is += i.EndTime + "|"
is += strconv.FormatFloat(i.Ponder, 'f', -1, 64) + "|"
is += strconv.FormatFloat(i.Weight, 'f', -1, 64) + "|"
is += strconv.FormatFloat(i.ConnectFee, 'f', -1, 64) + "|"
is += strconv.FormatFloat(i.Price, 'f', -1, 64) + "|"
is += strconv.FormatFloat(i.BillingUnit, 'f', -1, 64)
@@ -79,7 +79,7 @@ func (ap *ActivationPeriod) restore(input string) {
i.WeekDays.restore(ise[2])
i.StartTime = ise[3]
i.EndTime = ise[4]
i.Ponder, _ = strconv.ParseFloat(ise[5], 64)
i.Weight, _ = strconv.ParseFloat(ise[5], 64)
i.ConnectFee, _ = strconv.ParseFloat(ise[6], 64)
i.Price, _ = strconv.ParseFloat(ise[7], 64)
i.BillingUnit, _ = strconv.ParseFloat(ise[8], 64)

View File

@@ -22,6 +22,7 @@ import (
"strconv"
"strings"
"time"
"fmt"
//"log"
)
@@ -33,7 +34,7 @@ type Interval struct {
MonthDays MonthDays
WeekDays WeekDays
StartTime, EndTime string // ##:##:## format
Ponder, ConnectFee, Price, BillingUnit float64
Weight, ConnectFee, Price, BillingUnit float64
}
/*
@@ -124,3 +125,7 @@ func (i *Interval) getLeftMargin(t time.Time) (rigthtTime time.Time) {
}
return time.Date(year, month, day, hour, min, sec, nsec, loc)
}
func (i *Interval) String() string {
return fmt.Sprintf("%v %v %v %v %v", i.Months, i.MonthDays, i.WeekDays, i.StartTime, i.EndTime)
}

View File

@@ -83,14 +83,14 @@ func (ts *TimeSpan) Contains(t time.Time) bool {
}
/*
Will set the interval as spans's interval if new ponder is greater then span's interval ponder
or if the ponders are equal and new price is lower then spans's interval price
Will set the interval as spans's interval if new Weight is greater then span's interval Weight
or if the Weights are equal and new price is lower then spans's interval price
*/
func (ts *TimeSpan) SetInterval(i *Interval) {
if ts.Interval == nil || ts.Interval.Ponder < i.Ponder {
if ts.Interval == nil || ts.Interval.Weight < i.Weight {
ts.Interval = i
}
if ts.Interval.Ponder == i.Ponder && i.Price < ts.Interval.Price {
if ts.Interval.Weight == i.Weight && i.Price < ts.Interval.Price {
ts.Interval = i
}
}