first rating profile import

This commit is contained in:
Radu Ioan Fericean
2012-06-12 15:10:06 +03:00
parent b153362f61
commit 73942ba9dd
7 changed files with 85 additions and 35 deletions

View File

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

View File

@@ -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],

View File

@@ -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 = &timespans.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 := &timespans.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 := &timespans.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 = &timespans.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)
}
}
}

View File

@@ -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
1 Tag RatesTag MonthsTag MonthDaysTag WeekDaysTag StartTime
2 STANDARD RT_STANDARD ALL ALL WORKDAYS 00:00:00
3 STANDARD RT_STD_WEEKEND ALL ALL WORKDAYS 18:00:00
4 STANDARD RT_STD_WEEKEND ALL ALL WEEKENDS 00:00:00
5 PREMIUM RT_STD_WEEKEND ALL ALL WEEKENDS 00:00:00
6 DEFAULT RT_DEFAULT ALL ALL ALL 00:00:00

6
data/RatesTimings.csv Normal file
View File

@@ -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
1 Tag RatesTag TimingProfile
2 STANDARD RT_STANDARD WORKDAYS_00
3 STANDARD RT_STD_WEEKEND WORKDAYS_18
4 STANDARD RT_STD_WEEKEND WEEKENDS
5 PREMIUM RT_STD_WEEKEND WEEKENDS
6 DEFAULT RT_DEFAULT ALL

9
data/Timings.csv Normal file
View File

@@ -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
1 TimingTag MonthsTag MonthDaysTag WeekDaysTag StartTime
2 WORKDAYS_00 ALL ALL WORKDAYS 00:00:00
3 WORKDAYS_18 ALL ALL WORKDAYS 18:00:00
4 WEEKENDS ALL ALL WEEKENDS 00:00:00
5 WEEKLY_SAME_TIME ALL ALL MONDAY *now
6 FIRST_DAY_OF_MONTH ALL FIRST ALL 00:00:00
7 DAILY_SAME_TIME ALL ALL ALL *now
8 ONE_TIME_RUN NONE NONE NONE *now
9 TRIGGERED NONE NONE NONE *trigerred

View File

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