diff --git a/cmd/cgr-loader/cgr-loader.go b/cmd/cgr-loader/cgr-loader.go index e7d8b2026..e7db8a2a0 100644 --- a/cmd/cgr-loader/cgr-loader.go +++ b/cmd/cgr-loader/cgr-loader.go @@ -20,9 +20,9 @@ package main import ( "flag" - "fmt" "github.com/cgrates/cgrates/timespans" - "os" + "log" + "path" ) var ( @@ -31,15 +31,16 @@ var ( redisdb = flag.Int("redisdb", 10, "redis database number (10)") redispass = flag.String("pass", "", "redis database password") flush = flag.Bool("flush", false, "Flush the database before importing") - destinationsFn = flag.String("destinations", "", "Destinations file") - ratesFn = flag.String("rates", "", "Rates file") - timingsFn = flag.String("timings", "", "Timings file") - ratetimingsFn = flag.String("ratetimings", "", "Rates timings file") - ratingprofilesFn = flag.String("ratingprofiles", "", "Rating profiles file") - actionsFn = flag.String("actions", "", "Actions file") - actiontimingsFn = flag.String("actiontimings", "", "Actions timings file") - actiontriggersFn = flag.String("actiontriggers", "", "Actions triggers file") - accountactionsFn = flag.String("accountactions", "", "Account actions file") + dataPath = flag.String("path", ".", "The path containing the data files") + destinationsFn = "Destinations.csv" + ratesFn = "Rates.csv" + timingsFn = "Timings.csv" + ratetimingsFn = "RateTimings.csv" + ratingprofilesFn = "RatingProfiles.csv" + actionsFn = "Actions.csv" + actiontimingsFn = "ActionTimings.csv" + actiontriggersFn = "ActionTriggers.csv" + accountactionsFn = "AccountActions.csv" sep rune ) @@ -47,19 +48,18 @@ func main() { flag.Parse() sep = []rune(*separator)[0] csvr := timespans.NewFileCSVReader() - csvr.LoadDestinations(*destinationsFn, sep) - csvr.LoadRates(*ratesFn, sep) - csvr.LoadTimings(*timingsFn, sep) - csvr.LoadRateTimings(*ratetimingsFn, sep) - csvr.LoadRatingProfiles(*ratingprofilesFn, sep) - csvr.LoadActions(*actionsFn, sep) - csvr.LoadActionTimings(*actiontimingsFn, sep) - csvr.LoadActionTriggers(*actiontriggersFn, sep) - csvr.LoadAccountActions(*accountactionsFn, sep) + csvr.LoadDestinations(path.Join(*dataPath, destinationsFn), sep) + csvr.LoadRates(path.Join(*dataPath, ratesFn), sep) + csvr.LoadTimings(path.Join(*dataPath, timingsFn), sep) + csvr.LoadRateTimings(path.Join(*dataPath, ratetimingsFn), sep) + csvr.LoadRatingProfiles(path.Join(*dataPath, ratingprofilesFn), sep) + csvr.LoadActions(path.Join(*dataPath, actionsFn), sep) + csvr.LoadActionTimings(path.Join(*dataPath, actiontimingsFn), sep) + csvr.LoadActionTriggers(path.Join(*dataPath, actiontriggersFn), sep) + csvr.LoadAccountActions(path.Join(*dataPath, accountactionsFn), sep) storage, err := timespans.NewRedisStorage(*redissrv, *redisdb, *redispass) if err != nil { - timespans.Logger.Crit(fmt.Sprintf("Could not open database connection: %v", err)) - os.Exit(1) + log.Fatal("Could not open database connection: %v", err) } csvr.WriteToDatabase(storage, *flush, true) } diff --git a/timespans/csvreader.go b/timespans/csvreader.go index ac45a712f..91dc906b0 100644 --- a/timespans/csvreader.go +++ b/timespans/csvreader.go @@ -21,6 +21,7 @@ package timespans import ( "encoding/csv" "fmt" + "log" "os" "strconv" "strings" @@ -82,50 +83,50 @@ func (csvr *CSVReader) WriteToDatabase(storage StorageGetter, flush, verbose boo storage.Flush() } if verbose { - Logger.Info("Destinations") + log.Print("Destinations") } for _, d := range csvr.destinations { storage.SetDestination(d) if verbose { - Logger.Info(fmt.Sprintf("%v : %v", d.Id, d.Prefixes)) + log.Print(d.Id, " : ", d.Prefixes) } } if verbose { - Logger.Info("Rating profiles") + log.Print("Rating profiles") } for _, cds := range csvr.ratingProfiles { for _, cd := range cds { storage.SetActivationPeriodsOrFallback(cd.GetKey(), cd.ActivationPeriods, cd.FallbackKey) if verbose { - Logger.Debug(cd.GetKey()) + log.Print(cd.GetKey()) } } } if verbose { - Logger.Info("Action timings") + log.Print("Action timings") } for k, ats := range csvr.actionsTimings { storage.SetActionTimings(ACTION_TIMING_PREFIX+":"+k, ats) if verbose { - Logger.Debug(k) + log.Println(k) } } if verbose { - Logger.Info("Actions") + log.Print("Actions") } for k, as := range csvr.actions { storage.SetActions(k, as) if verbose { - Logger.Debug(k) + log.Println(k) } } if verbose { - Logger.Info("Account actions") + log.Print("Account actions") } for _, ub := range csvr.accountActions { storage.SetUserBalance(ub) if verbose { - Logger.Debug(ub.Id) + log.Println(ub.Id) } } } @@ -219,14 +220,14 @@ func (csvr *CSVReader) LoadRateTimings(fn string, comma rune) { ts, exists := csvr.timings[record[2]] if !exists { - Logger.Warning(fmt.Sprintf("Could not get timing for tag %v", record[2])) + log.Printf("Could not get timing for tag %v", record[2]) continue } for _, t := range ts { rt := NewRateTiming(record[1], t, record[3]) rs, exists := csvr.rates[record[1]] if !exists { - Logger.Warning(fmt.Sprintf("Could not rate for tag %v", record[2])) + log.Printf("Could not rate for tag %v", record[2]) continue } for _, r := range rs { @@ -255,13 +256,13 @@ func (csvr *CSVReader) LoadRatingProfiles(fn string, comma rune) { continue } if len(record) != 7 { - Logger.Warning(fmt.Sprintf("Malformed rating profile: %v", record)) + log.Printf("Malformed rating profile: %v", record) continue } tenant, tor, direction, subject, fallbacksubject := record[0], record[1], record[2], record[3], record[4] at, err := time.Parse(time.RFC3339, record[6]) if err != nil { - Logger.Warning(fmt.Sprintf("Cannot parse activation time from %v", record[6])) + log.Printf("Cannot parse activation time from %v", record[6]) continue } for _, d := range csvr.destinations { @@ -286,7 +287,7 @@ func (csvr *CSVReader) LoadRatingProfiles(fn string, comma rune) { } ap, exists := csvr.activationPeriods[record[5]] if !exists { - Logger.Warning(fmt.Sprintf("Could not load ratinTiming for tag: ", record[5])) + log.Print("Could not load ratinTiming for tag: ", record[5]) continue } newAP := &ActivationPeriod{} @@ -327,7 +328,7 @@ func (csvr *CSVReader) LoadActions(fn string, comma rune) { } units, err := strconv.ParseFloat(record[4], 64) if err != nil { - Logger.Warning(fmt.Sprintf("Could not parse action units: %v", err)) + log.Printf("Could not parse action units: %v", err) continue } var a *Action @@ -342,7 +343,7 @@ func (csvr *CSVReader) LoadActions(fn string, comma rune) { price, percent := 0.0, 0.0 value, err := strconv.ParseFloat(record[7], 64) if err != nil { - Logger.Warning(fmt.Sprintf("Could not parse action price: %v", err)) + log.Printf("Could not parse action price: %v", err) continue } if record[6] == PERCENT { @@ -353,12 +354,12 @@ func (csvr *CSVReader) LoadActions(fn string, comma rune) { } minutesWeight, err := strconv.ParseFloat(record[8], 64) if err != nil { - Logger.Warning(fmt.Sprintf("Could not parse action minutes weight: %v", err)) + log.Printf("Could not parse action minutes weight: %v", err) continue } weight, err := strconv.ParseFloat(record[9], 64) if err != nil { - Logger.Warning(fmt.Sprintf("Could not parse action weight: %v", err)) + log.Printf("Could not parse action weight: %v", err) continue } a = &Action{ @@ -396,12 +397,12 @@ func (csvr *CSVReader) LoadActionTimings(fn string, comma rune) { ts, exists := csvr.timings[record[2]] if !exists { - Logger.Warning(fmt.Sprintf("Could not load the timing for tag: %v", record[2])) + log.Printf("Could not load the timing for tag: %v", record[2]) continue } weight, err := strconv.ParseFloat(record[3], 64) if err != nil { - Logger.Warning(fmt.Sprintf("Could not parse action timing weight: %v", err)) + log.Printf("Could not parse action timing weight: %v", err) continue } for _, t := range ts { @@ -438,12 +439,12 @@ func (csvr *CSVReader) LoadActionTriggers(fn string, comma rune) { } value, err := strconv.ParseFloat(record[3], 64) if err != nil { - Logger.Warning(fmt.Sprintf("Could not parse action trigger value: %v", err)) + log.Printf("Could not parse action trigger value: %v", err) continue } weight, err := strconv.ParseFloat(record[6], 64) if err != nil { - Logger.Warning(fmt.Sprintf("Could not parse action trigger weight: %v", err)) + log.Printf("Could not parse action trigger weight: %v", err) continue } at := &ActionTrigger{ @@ -473,7 +474,7 @@ func (csvr *CSVReader) LoadAccountActions(fn string, comma rune) { tag := fmt.Sprintf("%s:%s:%s", record[2], record[0], record[1]) aTriggers, exists := csvr.actionsTriggers[record[4]] if !exists { - Logger.Warning(fmt.Sprintf("Could not get action triggers for tag %v", record[4])) + log.Printf("Could not get action triggers for tag %v", record[4]) continue } aTimingsTag := record[3] @@ -486,7 +487,7 @@ func (csvr *CSVReader) LoadAccountActions(fn string, comma rune) { aTimings, exists := csvr.actionsTimings[aTimingsTag] if !exists { - Logger.Warning(fmt.Sprintf("Could not get action triggers for tag %v", aTimingsTag)) + log.Printf("Could not get action triggers for tag %v", aTimingsTag) // must not continue here } for _, at := range aTimings { diff --git a/timespans/csvreader_helpers.go b/timespans/csvreader_helpers.go index fd7ee38b0..27b6aede7 100644 --- a/timespans/csvreader_helpers.go +++ b/timespans/csvreader_helpers.go @@ -19,7 +19,7 @@ along with this program. If not, see package timespans import ( - "fmt" + "log" "strconv" ) @@ -31,22 +31,22 @@ type Rate struct { func NewRate(destinationsTag, connectFee, price, pricedUnits, rateIncrements string) (r *Rate, err error) { cf, err := strconv.ParseFloat(connectFee, 64) if err != nil { - Logger.Err(fmt.Sprintf("Error parsing connect fee from: %v", connectFee)) + log.Printf("Error parsing connect fee from: %v", connectFee) return } p, err := strconv.ParseFloat(price, 64) if err != nil { - Logger.Err(fmt.Sprintf("Error parsing price from: %v", price)) + log.Printf("Error parsing price from: %v", price) return } pu, err := strconv.ParseFloat(pricedUnits, 64) if err != nil { - Logger.Err(fmt.Sprintf("Error parsing priced units from: %v", pricedUnits)) + log.Printf("Error parsing priced units from: %v", pricedUnits) return } ri, err := strconv.ParseFloat(rateIncrements, 64) if err != nil { - Logger.Err(fmt.Sprintf("Error parsing rates increments from: %v", rateIncrements)) + log.Printf("Error parsing rates increments from: %v", rateIncrements) return } r = &Rate{ @@ -86,7 +86,7 @@ type RateTiming struct { func NewRateTiming(ratesTag string, timing *Timing, weight string) (rt *RateTiming) { w, err := strconv.ParseFloat(weight, 64) if err != nil { - Logger.Err(fmt.Sprintf("Error parsing weight unit from: %v", weight)) + log.Printf("Error parsing weight unit from: %v", weight) return } rt = &RateTiming{ diff --git a/timespans/csvreader_test.go b/timespans/csvreader_test.go index 0eb63dd84..88585dcfb 100644 --- a/timespans/csvreader_test.go +++ b/timespans/csvreader_test.go @@ -38,7 +38,6 @@ RET,0723 RET,0724 ` rts = ` -Tag,DestinationsTag,ConnectFee,Price,PricedUnits,RateIncrements RT_STANDARD,GERMANY,0,0.2,60,1 RT_STANDARD,GERMANY_O2,0,0.1,60,1 RT_STANDARD,GERMANY_PREMIUM,0,0.1,60,1 @@ -49,14 +48,12 @@ P1,NAT,0,1,1,1 P2,NAT,0,0.5,1,1 ` ts = ` -Tag,Years,Months,MonthDays,WeekDays,Time WORKDAYS_00,*all,*all,*all,1;2;3;4;5,00:00:00 WORKDAYS_18,*all,*all,*all,1;2;3;4;5,18:00:00 WEEKENDS,*all,*all,*all,6;7,00:00:00 ONE_TIME_RUN,2012,*none,*none,*none,*asap ` rtts = ` -Tag,RatesTag,TimingProfile,Weight STANDARD,RT_STANDARD,WORKDAYS_00,10 STANDARD,RT_STD_WEEKEND,WORKDAYS_18,10 STANDARD,RT_STD_WEEKEND,WEEKENDS,10 @@ -67,7 +64,6 @@ EVENING,P2,WORKDAYS_18,10 EVENING,P2,WEEKENDS,10 ` rp = ` -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 @@ -78,20 +74,16 @@ vdf,0,OUT,minu,,EVENING,2012-01-01T00:00:00Z vdf,0,OUT,minu,,EVENING,2012-02-28T00:00:00Z ` a = ` -Tag,Action,BalanceTag,Direction,Units,DestinationTag,PriceType,PriceValue,MinutesWeight,Weight MINI,TOPUP,MINUTES,OUT,100,NAT,ABSOLUTE,0,10,10 ` atms = ` -Tag,ActionsTag,TimingTag, Weight MORE_MINUTES,MINI,ONE_TIME_RUN,10 ` atrs = ` -Tag,BalanceTag,ThresholdValue,Direction,DestinationTag,ActionsTag,Weight STANDARD_TRIGGER,MINUTES,OUT,10,GERMANY_O2,SOME_1,10 STANDARD_TRIGGER,MINUTES,OUT,200,GERMANY,SOME_2,10 ` accs = ` -Tenant,Account,Direction,ActionTimingsTag,ActionTriggersTag vdf,minitsboy,OUT,MORE_MINUTES,STANDARD_TRIGGER ` )