From 73942ba9ddeb09ea5f30e89970619ed10c44c368 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 12 Jun 2012 15:10:06 +0300 Subject: [PATCH] first rating profile import --- cmd/cgr-loader/cgr-loader.go | 20 ++++++++++ cmd/cgr-loader/helpers.go | 2 +- cmd/cgr-loader/rates.go | 76 +++++++++++++++++++++++------------- data/RatesTiming.csv | 6 --- data/RatesTimings.csv | 6 +++ data/Timings.csv | 9 +++++ timespans/calldesc.go | 1 + 7 files changed, 85 insertions(+), 35 deletions(-) delete mode 100644 data/RatesTiming.csv create mode 100644 data/RatesTimings.csv create mode 100644 data/Timings.csv diff --git a/cmd/cgr-loader/cgr-loader.go b/cmd/cgr-loader/cgr-loader.go index b788ebf48..f369260db 100644 --- a/cmd/cgr-loader/cgr-loader.go +++ b/cmd/cgr-loader/cgr-loader.go @@ -19,6 +19,8 @@ package main import ( "flag" + "github.com/cgrates/cgrates/timespans" + "log" ) var ( @@ -44,6 +46,23 @@ var ( sep rune ) +func writeToDatabase() { + storage, err := timespans.NewRedisStorage(*redisserver, *redisdb) + if err != nil { + log.Fatalf("Could not open database connection: %v", err) + } + for _, d := range destinations { + storage.SetDestination(d) + } + for k, cds := range ratingProfiles { + log.Print(k) + for _, cd := range cds { + storage.SetActivationPeriodsOrFallback(cd.GetKey(), cd.ActivationPeriods, "") + log.Print(cd.GetKey()) + } + } +} + func main() { flag.Parse() sep = []rune(*separator)[0] @@ -60,4 +79,5 @@ func main() { loadRecurrentDebits() loadRecurrentTopups() loadBalanceProfiles() + writeToDatabase() } diff --git a/cmd/cgr-loader/helpers.go b/cmd/cgr-loader/helpers.go index eb509b7f4..2493fbce6 100644 --- a/cmd/cgr-loader/helpers.go +++ b/cmd/cgr-loader/helpers.go @@ -65,7 +65,7 @@ type Timing struct { } func NewTiming(timeingInfo ...string) (rt *Timing) { - rt = &RateTiming{ + rt = &Timing{ MonthsTag: timeingInfo[0], MonthDaysTag: timeingInfo[1], WeekDaysTag: timeingInfo[2], diff --git a/cmd/cgr-loader/rates.go b/cmd/cgr-loader/rates.go index ac41efbd8..75f57d1a7 100644 --- a/cmd/cgr-loader/rates.go +++ b/cmd/cgr-loader/rates.go @@ -21,6 +21,7 @@ import ( "encoding/csv" "github.com/cgrates/cgrates/timespans" "log" + "fmt" "os" "time" ) @@ -29,7 +30,7 @@ var ( months = make(map[string][]time.Month) monthdays = make(map[string][]int) weekdays = make(map[string][]time.Weekday) - destinations = make(map[string][]string) + destinations []*timespans.Destination rates = make(map[string][]*Rate) timings = make(map[string][]*Timing) ratesTimings = make(map[string][]*RateTiming) @@ -120,9 +121,20 @@ func loadDestinations() { // skip header line continue } - destinations[tag] = record[1:] - log.Print(tag, destinations[tag]) + var dest *timespans.Destination + for _, d := range destinations { + if d.Id == tag { + dest = d + break + } + } + if dest == nil { + dest = ×pans.Destination{Id: tag} + destinations = append(destinations, dest) + } + dest.Prefixes = append(dest.Prefixes, record[1:]...) } + log.Print(destinations) } func loadRates() { @@ -149,7 +161,7 @@ func loadRates() { } } -func loadTiming() { +func loadTimings() { fp, err := os.Open(*timingsFn) if err != nil { log.Printf("Could not open timings file: %v", err) @@ -165,17 +177,17 @@ func loadTiming() { continue } - t := NewTiming(rates[1:]...) + t := NewTiming(record[1:]...) timings[tag] = append(timings[tag], t) log.Print(tag) - for _, i := range ratesTiming[tag] { + for _, i := range timings[tag] { log.Print(i) } } } -func loadRatesTiming() { +func loadRatesTimings() { fp, err := os.Open(*ratestimingsFn) if err != nil { log.Printf("Could not open rates timings file: %v", err) @@ -199,10 +211,10 @@ func loadRatesTiming() { for _, t := range ts { rt := NewRateTiming(record[1], t) - ratesTiming[tag] = append(ratesTiming[tag], rt) + ratesTimings[tag] = append(ratesTimings[tag], rt) } log.Print(tag) - for _, i := range ratesTiming[tag] { + for _, i := range ratesTimings[tag] { log.Print(i) } } @@ -229,7 +241,7 @@ func loadRatingProfiles() { log.Printf("Cannot parse activation time from %v", record[5]) continue } - rts, exists := ratesTiming[record[4]] + rts, exists := ratesTimings[record[4]] if !exists { log.Printf("Could not get rate timing for tag %v", record[4]) continue @@ -243,25 +255,33 @@ func loadRatingProfiles() { ap := ×pans.ActivationPeriod{ ActivationTime: at, } - for _, r := range rs { //rates - ds, exists := destinations[r.DestinationsTag] - if !exists { - log.Printf("Could not get destinations for tag %v", r.DestinationsTag) - continue - } - ap.AddInterval(rt.GetInterval(r)) - for _, d := range ds { //destinations - cd := ×pans.CallDescriptor{ - Tenant: tenant, - TOR: tor, - Subject: subject, - DestinationPrefix: d, + for _, r := range rs { //rates + for _, d := range destinations { + if d.Id == r.DestinationsTag { + ap.AddInterval(rt.GetInterval(r)) + for _, p := range d.Prefixes { //destinations + // Search for a CallDescriptor with the same key + var cd *timespans.CallDescriptor + for _, c := range ratingProfiles[p] { + if c.GetKey() == fmt.Sprintf("%s:%s:%s", tenant, subject, p) { + cd = c + } + } + if cd == nil { + cd = ×pans.CallDescriptor{ + Tenant: tenant, + TOR: tor, + Subject: subject, + Destination: p, + } + ratingProfiles[p] = append(ratingProfiles[p], cd) + } + if fallbacksubject != "" { + // construct a new cd!!!! + } + cd.ActivationPeriods = append(cd.ActivationPeriods, ap) + } } - if fallbacksubject != "" { - // construct a new cd!!!! - } - cd.ActivationPeriods = append(cd.ActivationPeriods, ap) - ratingProfiles[d] = append(ratingProfiles[d], cd) } } } diff --git a/data/RatesTiming.csv b/data/RatesTiming.csv deleted file mode 100644 index 570027e77..000000000 --- a/data/RatesTiming.csv +++ /dev/null @@ -1,6 +0,0 @@ -Tag,RatesTag,MonthsTag,MonthDaysTag,WeekDaysTag,StartTime -STANDARD,RT_STANDARD,ALL,ALL,WORKDAYS,00:00:00 -STANDARD,RT_STD_WEEKEND,ALL,ALL,WORKDAYS,18:00:00 -STANDARD,RT_STD_WEEKEND,ALL,ALL,WEEKENDS,00:00:00 -PREMIUM,RT_STD_WEEKEND,ALL,ALL,WEEKENDS,00:00:00 -DEFAULT,RT_DEFAULT,ALL,ALL,ALL,00:00:00 \ No newline at end of file diff --git a/data/RatesTimings.csv b/data/RatesTimings.csv new file mode 100644 index 000000000..c58fa68a4 --- /dev/null +++ b/data/RatesTimings.csv @@ -0,0 +1,6 @@ +Tag,RatesTag,TimingProfile +STANDARD,RT_STANDARD,WORKDAYS_00 +STANDARD,RT_STD_WEEKEND,WORKDAYS_18 +STANDARD,RT_STD_WEEKEND,WEEKENDS +PREMIUM,RT_STD_WEEKEND,WEEKENDS +DEFAULT,RT_DEFAULT,ALL \ No newline at end of file diff --git a/data/Timings.csv b/data/Timings.csv new file mode 100644 index 000000000..6fe72181e --- /dev/null +++ b/data/Timings.csv @@ -0,0 +1,9 @@ +TimingTag,MonthsTag,MonthDaysTag,WeekDaysTag,StartTime +WORKDAYS_00,ALL,ALL,WORKDAYS,00:00:00 +WORKDAYS_18,ALL,ALL,WORKDAYS,18:00:00 +WEEKENDS,ALL,ALL,WEEKENDS,00:00:00 +WEEKLY_SAME_TIME,ALL,ALL,MONDAY,*now +FIRST_DAY_OF_MONTH,ALL,FIRST,ALL,00:00:00 +DAILY_SAME_TIME,ALL,ALL,ALL,*now +ONE_TIME_RUN,NONE,NONE,NONE,*now +TRIGGERED,NONE,NONE,NONE,*trigerred \ No newline at end of file diff --git a/timespans/calldesc.go b/timespans/calldesc.go index 3e1e8fc92..02f4c40ae 100644 --- a/timespans/calldesc.go +++ b/timespans/calldesc.go @@ -53,6 +53,7 @@ func round(val float64, prec int) float64 { The input stucture that contains call information. */ type CallDescriptor struct { + Direction string TOR string Tenant, Subject, Destination string TimeStart, TimeEnd time.Time