mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Partial TPImporter implementation
This commit is contained in:
@@ -50,7 +50,8 @@ var (
|
||||
tpid = flag.String("tpid", "", "The tariff plan id from the database")
|
||||
dataPath = flag.String("path", ".", "The path containing the data files")
|
||||
version = flag.Bool("version", false, "Prints the application version.")
|
||||
importer = flag.Bool("import", false, "Import to storDb instead of directly loading to dataDb")
|
||||
fromStorDb = flag.Bool("from_stordb", false, "Load the tariff plan from storDb to dataDb")
|
||||
toStorDb = flag.Bool("to_stordb", false, "Import the tariff plan from files to storDb")
|
||||
|
||||
sep rune
|
||||
)
|
||||
@@ -67,23 +68,29 @@ func main() {
|
||||
fmt.Println("CGRateS " + utils.VERSION)
|
||||
return
|
||||
}
|
||||
var err error
|
||||
var db rater.DataStorage
|
||||
if *importer { // Loader has importer function, we need connection to storDb
|
||||
db, err = rater.ConfigureDatabase(*stor_db_type, *stor_db_host, *stor_db_port, *stor_db_name, *stor_db_user, *stor_db_pass)
|
||||
} else { // Loader function, need connection directly to dataDb
|
||||
db, err = rater.ConfigureDatabase(*data_db_type, *data_db_host, *data_db_port, *data_db_name, *data_db_user, *data_db_pass)
|
||||
var errDataDb, errStorDb, err error
|
||||
var dataDb, storDb rater.DataStorage
|
||||
// Init necessary db connections
|
||||
if *fromStorDb {
|
||||
dataDb, errDataDb = rater.ConfigureDatabase(*stor_db_type, *stor_db_host, *stor_db_port, *stor_db_name, *stor_db_user, *stor_db_pass)
|
||||
storDb, errStorDb = rater.ConfigureDatabase(*data_db_type, *data_db_host, *data_db_port, *data_db_name, *data_db_user, *data_db_pass)
|
||||
} else if *toStorDb { // Import from csv files to storDb
|
||||
storDb, errStorDb = rater.ConfigureDatabase(*data_db_type, *data_db_host, *data_db_port, *data_db_name, *data_db_user, *data_db_pass)
|
||||
} else { // Default load from csv files to dataDb
|
||||
dataDb, errDataDb = rater.ConfigureDatabase(*stor_db_type, *stor_db_host, *stor_db_port, *stor_db_name, *stor_db_user, *stor_db_pass)
|
||||
}
|
||||
defer db.Close()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not open database connection: %v", err)
|
||||
defer dataDb.Close()
|
||||
defer storDb.Close()
|
||||
for _,err = range []error{errDataDb, errStorDb} {
|
||||
if err != nil {
|
||||
log.Fatalf("Could not open database connection: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if *tpid != "" && *dataPath != "" {
|
||||
log.Fatal("You can read either from db or from files, not both.")
|
||||
}
|
||||
var loader rater.TPLoader
|
||||
if *dataPath != "" {
|
||||
if *fromStorDb {
|
||||
loader = rater.NewDbReader(storDb, dataDb, *tpid)
|
||||
} else { // Default load from csv files to dataDb
|
||||
dataFilesValidators := []*validator{
|
||||
&validator{utils.DESTINATIONS_CSV,
|
||||
regexp.MustCompile(`(?:\w+\s*,\s*){1}(?:\d+.?\d*){1}$`),
|
||||
@@ -123,11 +130,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
//sep = []rune(*separator)[0]
|
||||
loader = rater.NewFileCSVReader(db, ',', utils.DESTINATIONS_CSV, utils.TIMINGS_CSV, utils.RATES_CSV, utils.DESTINATION_RATES_CSV, utils.DESTRATE_TIMINGS_CSV, utils.RATE_PROFILES_CSV, utils.ACTIONS_CSV, utils.ACTION_TIMINGS_CSV, utils.ACTION_TRIGGERS_CSV, utils.ACCOUNT_ACTIONS_CSV)
|
||||
}
|
||||
|
||||
if *tpid != "" {
|
||||
loader = rater.NewDbReader(db, db, *tpid)
|
||||
loader = rater.NewFileCSVReader(dataDb, ',', utils.DESTINATIONS_CSV, utils.TIMINGS_CSV, utils.RATES_CSV, utils.DESTINATION_RATES_CSV, utils.DESTRATE_TIMINGS_CSV, utils.RATE_PROFILES_CSV, utils.ACTIONS_CSV, utils.ACTION_TIMINGS_CSV, utils.ACTION_TRIGGERS_CSV, utils.ACCOUNT_ACTIONS_CSV)
|
||||
}
|
||||
|
||||
err = loader.LoadDestinations()
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
type TPLoader interface {
|
||||
@@ -197,3 +198,42 @@ func ValidateCSVData(fn string, re *regexp.Regexp) (err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type TPCSVRowValidator struct {
|
||||
FileName string // File name
|
||||
Rule *regexp.Regexp // Regexp rule
|
||||
ErrMessage string // Error message
|
||||
}
|
||||
|
||||
var TPCSVRowValidators = []*TPCSVRowValidator{
|
||||
&TPCSVRowValidator{utils.DESTINATIONS_CSV,
|
||||
regexp.MustCompile(`(?:\w+\s*,\s*){1}(?:\d+.?\d*){1}$`),
|
||||
"Tag[0-9A-Za-z_],Prefix[0-9]"},
|
||||
&TPCSVRowValidator{utils.TIMINGS_CSV,
|
||||
regexp.MustCompile(`(?:\w+\s*,\s*){1}(?:\*all\s*,\s*|(?:\d{1,4};?)+\s*,\s*|\s*,\s*){4}(?:\d{2}:\d{2}:\d{2}|\*asap){1}$`),
|
||||
"Tag[0-9A-Za-z_],Years[0-9;]|*all|<empty>,Months[0-9;]|*all|<empty>,MonthDays[0-9;]|*all|<empty>,WeekDays[0-9;]|*all|<empty>,Time[0-9:]|*asap(00:00:00)"},
|
||||
&TPCSVRowValidator{utils.RATES_CSV,
|
||||
regexp.MustCompile(`(?:\w+\s*,\s*){2}(?:\d+.?\d*,?){4}$`),
|
||||
"Tag[0-9A-Za-z_],ConnectFee[0-9.],Price[0-9.],PricedUnits[0-9.],RateIncrement[0-9.]"},
|
||||
&TPCSVRowValidator{utils.DESTINATION_RATES_CSV,
|
||||
regexp.MustCompile(`(?:\w+\s*,\s*){2}(?:\d+.?\d*,?){4}$`),
|
||||
"Tag[0-9A-Za-z_],DestinationsTag[0-9A-Za-z_],RateTag[0-9A-Za-z_]"},
|
||||
&TPCSVRowValidator{utils.DESTRATE_TIMINGS_CSV,
|
||||
regexp.MustCompile(`(?:\w+\s*,\s*){3}(?:\d+.?\d*){1}$`),
|
||||
"Tag[0-9A-Za-z_],DestinationRatesTag[0-9A-Za-z_],TimingProfile[0-9A-Za-z_],Weight[0-9.]"},
|
||||
&TPCSVRowValidator{utils.RATE_PROFILES_CSV,
|
||||
regexp.MustCompile(`(?:\w+\s*,\s*){1}(?:\d+\s*,\s*){1}(?:OUT\s*,\s*|IN\s*,\s*){1}(?:\*all\s*,\s*|[\w:\.]+\s*,\s*){1}(?:\w*\s*,\s*){1}(?:\w+\s*,\s*){1}(?:\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z){1}$`),
|
||||
"Tenant[0-9A-Za-z_],TOR[0-9],Direction OUT|IN,Subject[0-9A-Za-z_:.]|*all,RatesFallbackSubject[0-9A-Za-z_]|<empty>,RatesTimingTag[0-9A-Za-z_],ActivationTime[[0-9T:X]] (2012-01-01T00:00:00Z)"},
|
||||
&TPCSVRowValidator{utils.ACTIONS_CSV,
|
||||
regexp.MustCompile(`(?:\w+\s*,\s*){3}(?:OUT\s*,\s*|IN\s*,\s*){1}(?:\d+\s*,\s*){1}(?:\w+\s*,\s*|\*all\s*,\s*){1}(?:ABSOLUTE\s*,\s*|PERCENT\s*,\s*|\s*,\s*){1}(?:\d*\.?\d*\s*,?\s*){3}$`),
|
||||
"Tag[0-9A-Za-z_],Action[0-9A-Za-z_],BalanceTag[0-9A-Za-z_],Direction OUT|IN,Units[0-9],DestinationTag[0-9A-Za-z_]|*all,PriceType ABSOLUT|PERCENT,PriceValue[0-9.],MinutesWeight[0-9.],Weight[0-9.]"},
|
||||
&TPCSVRowValidator{utils.ACTION_TIMINGS_CSV,
|
||||
regexp.MustCompile(`(?:\w+\s*,\s*){3}(?:\d+\.?\d*){1}`),
|
||||
"Tag[0-9A-Za-z_],ActionsTag[0-9A-Za-z_],TimingTag[0-9A-Za-z_],Weight[0-9.]"},
|
||||
&TPCSVRowValidator{utils.ACTION_TRIGGERS_CSV,
|
||||
regexp.MustCompile(`(?:\w+\s*,\s*){1}(?:MONETARY\s*,\s*|SMS\s*,\s*|MINUTES\s*,\s*|INTERNET\s*,\s*|INTERNET_TIME\s*,\s*){1}(?:OUT\s*,\s*|IN\s*,\s*){1}(?:\d+\.?\d*\s*,\s*){1}(?:\w+\s*,\s*|\*all\s*,\s*){1}(?:\w+\s*,\s*){1}(?:\d+\.?\d*){1}$`),
|
||||
"Tag[0-9A-Za-z_],BalanceTag MONETARY|SMS|MINUTES|INTERNET|INTERNET_TIME,Direction OUT|IN,ThresholdValue[0-9.],DestinationTag[0-9A-Za-z_]|*all,ActionsTag[0-9A-Za-z_],Weight[0-9.]"},
|
||||
&TPCSVRowValidator{utils.ACCOUNT_ACTIONS_CSV,
|
||||
regexp.MustCompile(`(?:\w+\s*,\s*){1}(?:[\w:.]+\s*,\s*){1}(?:OUT\s*,\s*|IN\s*,\s*){1}(?:\w+\s*,?\s*){2}$`),
|
||||
"Tenant[0-9A-Za-z_],Account[0-9A-Za-z_:.],Direction OUT|IN,ActionTimingsTag[0-9A-Za-z_],ActionTriggersTag[0-9A-Za-z_]"},
|
||||
}
|
||||
|
||||
30
rater/tpimporter_csv.go
Normal file
30
rater/tpimporter_csv.go
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
Rating system designed to be used in VoIP Carriers World
|
||||
Copyright (C) 2013 ITsysCOM
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package rater
|
||||
|
||||
// Import tariff plan from csv into storDb
|
||||
|
||||
type TPImporterCSV struct {
|
||||
sep rune
|
||||
storDb DataStorage
|
||||
}
|
||||
|
||||
func (self *TPImporterCSV) ProcessFolder (fPath string) (err error) {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user