From f466fcbb6a822efc4c120cdfed704c85e29a0fe8 Mon Sep 17 00:00:00 2001 From: DanB Date: Tue, 6 May 2014 20:26:33 +0200 Subject: [PATCH] Proper ratingprofile order --- data/storage/mysql/create_tariffplan_tables.sql | 6 +++--- .../prepaid1centpsec/RatingProfiles.csv | 4 ++-- engine/loader_db.go | 2 +- engine/loader_helpers.go | 4 ++-- engine/loader_helpers_test.go | 4 ++-- engine/loader_local_test.go | 4 +--- engine/storage_sql.go | 15 ++++++++------- engine/storage_sql_local_test.go | 4 ++-- engine/tpimporter_csv.go | 2 +- utils/consts.go | 2 +- 10 files changed, 23 insertions(+), 24 deletions(-) diff --git a/data/storage/mysql/create_tariffplan_tables.sql b/data/storage/mysql/create_tariffplan_tables.sql index f1d04b29c..546daf172 100644 --- a/data/storage/mysql/create_tariffplan_tables.sql +++ b/data/storage/mysql/create_tariffplan_tables.sql @@ -99,9 +99,9 @@ CREATE TABLE `tp_rating_profiles` ( `tbid` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, `loadid` varchar(64) NOT NULL, + `direction` varchar(8) NOT NULL, `tenant` varchar(64) NOT NULL, `category` varchar(16) NOT NULL, - `direction` varchar(8) NOT NULL, `subject` varchar(64) NOT NULL, `activation_time` varchar(24) NOT NULL, `rating_plan_id` varchar(64) NOT NULL, @@ -212,11 +212,11 @@ CREATE TABLE `tp_account_actions` ( ); -- --- Table structure for table `tp_lcrs` +-- Table structure for table `tp_lcr_rules` -- DROP TABLE IF EXISTS tp_lcr_rules; -CREATE TABLE tp_lcr ( +CREATE TABLE tp_lcr_rules ( `tbid` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, `direction` varchar(8) NOT NULL, diff --git a/data/tariffplans/prepaid1centpsec/RatingProfiles.csv b/data/tariffplans/prepaid1centpsec/RatingProfiles.csv index 475666ddd..1b6f3aafa 100644 --- a/data/tariffplans/prepaid1centpsec/RatingProfiles.csv +++ b/data/tariffplans/prepaid1centpsec/RatingProfiles.csv @@ -1,2 +1,2 @@ -#Tenant,TOR,Direction,Subject,ActivationTime,RatingPlanTag,FallbackSubject -cgrates.org,call,*out,*any,2012-01-01T00:00:00Z,RP_RETAIL, +#Direction,Tenant,Category,Subject,ActivationTime,RatingPlanId,RatesFallbackSubject +*out,cgrates.org,call,*any,2012-01-01T00:00:00Z,RP_RETAIL, diff --git a/engine/loader_db.go b/engine/loader_db.go index e618f198b..bdabf56e7 100644 --- a/engine/loader_db.go +++ b/engine/loader_db.go @@ -558,10 +558,10 @@ func (dbr *DbReader) LoadActionTriggers() (err error) { Direction: apiAtr.Direction, ThresholdType: apiAtr.ThresholdType, ThresholdValue: apiAtr.ThresholdValue, + Recurrent: apiAtr.Recurrent, DestinationId: apiAtr.DestinationId, Weight: apiAtr.Weight, ActionsId: apiAtr.ActionsId, - Recurrent: apiAtr.Recurrent, } } dbr.actionsTriggers[key] = atrs diff --git a/engine/loader_helpers.go b/engine/loader_helpers.go index b463a110d..0be1eef8f 100644 --- a/engine/loader_helpers.go +++ b/engine/loader_helpers.go @@ -198,8 +198,8 @@ var FileValidators = map[string]*FileLineRegexValidator{ 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.])"}, utils.RATING_PROFILES_CSV: &FileLineRegexValidator{utils.RATE_PROFILES_NRCOLS, - regexp.MustCompile(`(?:\w+\s*,\s*){2}(?:\*out\s*,\s*){1}(?:\*any\s*,\s*|(\w+;?)+\s*,\s*){1}(?:\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z){1}(?:\w*\s*,?\s*){2}$`), - "Tenant([0-9A-Za-z_]),TOR([0-9A-Za-z_]),Direction(*out),Subject([0-9A-Za-z_]|*all),RatesFallbackSubject([0-9A-Za-z_]|),RatesTimingTag([0-9A-Za-z_]),ActivationTime([0-9T:X])"}, + regexp.MustCompile(`^(?:\*out\s*),(?:[0-9A-Za-z_\.]+\s*),(?:\w+\s*),(?:\*any\s*|(\w+;?)+\s*),(?:\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z),(?:\w+\s*),(?:\w+\s*)?$`), + "Direction(*out),Tenant([0-9A-Za-z_]),Category([0-9A-Za-z_]),Subject([0-9A-Za-z_]|*any),ActivationTime([0-9T:X]),RatingPlanId([0-9A-Za-z_]),RatesFallbackSubject([0-9A-Za-z_]|)"}, utils.SHARED_GROUPS_CSV: &FileLineRegexValidator{utils.SHARED_GROUPS_NRCOLS, regexp.MustCompile(`(?:\w+\s*),(?:\*?\w+\s*),(?:\*\w+\s*),(?:\*?\w]+\s*)?`), "Id([0-9A-Za-z_]),Account(*?[0-9A-Za-z_]),Strategy(*[0-9A-Za-z_]),RatingSubject(*?[0-9A-Za-z_])"}, diff --git a/engine/loader_helpers_test.go b/engine/loader_helpers_test.go index 41bc0e580..ea5db6a7f 100644 --- a/engine/loader_helpers_test.go +++ b/engine/loader_helpers_test.go @@ -53,9 +53,9 @@ DUMMY,INVALID;DATA ` var ratingProfilesSample = `#Tenant,TOR,Direction,Subject,ActivationTime,RatingPlanTag,FallbackSubject -cgrates.org,call,*out,*any,2012-01-01T00:00:00Z,RP_RETAIL, +*out,cgrates.org,call,*any,2012-01-01T00:00:00Z,RP_RETAIL, DUMMY,INVALID;DATA -cgrates.org,call,*out,subj1;alias1,2012-01-01T00:00:00Z,RP_RETAIL, +*out,cgrates.org,call,subj1;alias1,2012-01-01T00:00:00Z,RP_RETAIL, ` var actionsSample = `#ActionsTag,Action,BalanceType,Direction,Units,ExpiryTime,DestinationTag,RatingSubject,BalanceWeight,SharedGroup,ExtraParameters,Weight diff --git a/engine/loader_local_test.go b/engine/loader_local_test.go index 220754699..8f325698b 100644 --- a/engine/loader_local_test.go +++ b/engine/loader_local_test.go @@ -20,7 +20,6 @@ package engine import ( "flag" - "fmt" "path" "testing" @@ -224,7 +223,6 @@ func TestLoadFromStorDb(t *testing.T) { t.Error("Failed loading action triggers: ", err.Error()) } if err := loader.LoadAccountActions(); err != nil { - fmt.Printf("Have actionTriggers loaded :%v\n", loader.actionsTriggers) t.Error("Failed loading account actions: ", err.Error()) } if err := loader.WriteToDatabase(true, false); err != nil { @@ -298,7 +296,7 @@ func TestMatchLoadCsvWithStor(t *testing.T) { for idx, rs := range []*RedisStorage{rsCsv, rsStor, rsApier} { qVal, err := rs.db.Get(key) if err != nil { - t.Fatalf("Could not retrieve key %s, error: %s", key, err.Error()) + t.Fatalf("Run: %d, could not retrieve key %s, error: %s", idx, key, err.Error()) } if idx == 0 { // Only compare at second iteration, first one is to set reference value refVal = qVal diff --git a/engine/storage_sql.go b/engine/storage_sql.go index 5cdade5b3..019673a49 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -261,7 +261,7 @@ func (self *SQLStorage) SetTPRatingProfiles(tpid string, rps map[string]*utils.T return nil //Nothing to set } var buffer bytes.Buffer - buffer.WriteString(fmt.Sprintf("INSERT INTO %s (tpid,loadid,tenant,category,direction,subject,activation_time,rating_plan_id,fallback_subjects) VALUES ", + buffer.WriteString(fmt.Sprintf("INSERT INTO %s (tpid,loadid,direction,tenant,category,subject,activation_time,rating_plan_id,fallback_subjects) VALUES ", utils.TBL_TP_RATE_PROFILES)) i := 0 for _, rp := range rps { @@ -269,7 +269,7 @@ func (self *SQLStorage) SetTPRatingProfiles(tpid string, rps map[string]*utils.T if i != 0 { //Consecutive values after the first will be prefixed with "," as separator buffer.WriteRune(',') } - buffer.WriteString(fmt.Sprintf("('%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s')", tpid, rp.LoadId, rp.Tenant, rp.Category, rp.Direction, + buffer.WriteString(fmt.Sprintf("('%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s')", tpid, rp.LoadId, rp.Direction, rp.Tenant, rp.Category, rp.Subject, rpa.ActivationTime, rpa.RatingPlanId, rpa.FallbackSubjects)) i++ } @@ -1031,15 +1031,16 @@ func (self *SQLStorage) GetTpRatingProfiles(qryRpf *utils.TPRatingProfile) (map[ if len(qryRpf.LoadId) != 0 { q += fmt.Sprintf(" AND loadid='%s'", qryRpf.LoadId) } + if len(qryRpf.Direction) != 0 { + q += fmt.Sprintf(" AND direction='%s'", qryRpf.Direction) + } if len(qryRpf.Tenant) != 0 { q += fmt.Sprintf(" AND tenant='%s'", qryRpf.Tenant) } if len(qryRpf.Category) != 0 { q += fmt.Sprintf(" AND category='%s'", qryRpf.Category) } - if len(qryRpf.Direction) != 0 { - q += fmt.Sprintf(" AND direction='%s'", qryRpf.Direction) - } + if len(qryRpf.Subject) != 0 { q += fmt.Sprintf(" AND subject='%s'", qryRpf.Subject) } @@ -1051,10 +1052,10 @@ func (self *SQLStorage) GetTpRatingProfiles(qryRpf *utils.TPRatingProfile) (map[ rpfs := make(map[string]*utils.TPRatingProfile) for rows.Next() { var rcvLoadId, tenant, category, direction, subject, fallback_subjects, rating_plan_tag, activation_time string - if err := rows.Scan(&rcvLoadId, &tenant, &category, &direction, &subject, &activation_time, &rating_plan_tag, &fallback_subjects); err != nil { + if err := rows.Scan(&rcvLoadId, &direction, &tenant, &category, &subject, &activation_time, &rating_plan_tag, &fallback_subjects); err != nil { return nil, err } - rp := &utils.TPRatingProfile{TPid: qryRpf.TPid, LoadId: rcvLoadId, Tenant: tenant, Category: category, Direction: direction, Subject: subject} + rp := &utils.TPRatingProfile{TPid: qryRpf.TPid, LoadId: rcvLoadId, Direction: direction, Tenant: tenant, Category: category, Subject: subject} if existingRp, has := rpfs[rp.KeyId()]; !has { rp.RatingPlanActivations = []*utils.TPRatingActivation{ &utils.TPRatingActivation{ActivationTime: activation_time, RatingPlanId: rating_plan_tag, FallbackSubjects: fallback_subjects}} diff --git a/engine/storage_sql_local_test.go b/engine/storage_sql_local_test.go index 829227d2b..d14144e2f 100644 --- a/engine/storage_sql_local_test.go +++ b/engine/storage_sql_local_test.go @@ -486,10 +486,10 @@ func TestSetGetTPActionTriggers(t *testing.T) { ActionsId: "LOG_BALANCE", } mpAtrgs := map[string][]*utils.TPActionTrigger{TEST_SQL: []*utils.TPActionTrigger{atrg}} - if err := mysql.SetTPActionTriggers(TEST_SQL, mpAtrgs); err != nil { + if err := mysql.SetTPActionTriggers(TEST_SQL+"1", mpAtrgs); err != nil { t.Error("Unexpected error: ", err.Error()) } - if rcvMpAtrgs, err := mysql.GetTpActionTriggers(TEST_SQL, TEST_SQL); err != nil { + if rcvMpAtrgs, err := mysql.GetTpActionTriggers(TEST_SQL+"1", TEST_SQL); err != nil { t.Error("Unexpected error: ", err.Error()) } else if !reflect.DeepEqual(mpAtrgs, rcvMpAtrgs) { t.Errorf("Expecting: %v, received: %v", mpAtrgs, rcvMpAtrgs) diff --git a/engine/tpimporter_csv.go b/engine/tpimporter_csv.go index d96d6a8aa..93019fbaf 100644 --- a/engine/tpimporter_csv.go +++ b/engine/tpimporter_csv.go @@ -258,7 +258,7 @@ func (self *TPCSVImporter) importRatingProfiles(fn string) error { } continue } - tenant, tor, direction, subject, ratingPlanTag, fallbacksubject := record[0], record[1], record[2], record[3], record[5], record[6] + direction, tenant, tor, subject, ratingPlanTag, fallbacksubject := record[0], record[1], record[2], record[3], record[5], record[6] _, err = utils.ParseDate(record[4]) if err != nil { if self.Verbose { diff --git a/utils/consts.go b/utils/consts.go index 291b49be4..ec9503162 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -27,7 +27,7 @@ const ( TBL_TP_RATING_PLANS = "tp_rating_plans" TBL_TP_RATE_PROFILES = "tp_rating_profiles" TBL_TP_SHARED_GROUPS = "tp_shared_groups" - TBL_TP_LCRS = "tp_lcrs" + TBL_TP_LCRS = "tp_lcr_rules" TBL_TP_ACTIONS = "tp_actions" TBL_TP_ACTION_PLANS = "tp_action_plans" TBL_TP_ACTION_TRIGGERS = "tp_action_triggers"