From 4e0ccbfa19dd4ad055de4e37138a4f6a32765736 Mon Sep 17 00:00:00 2001 From: DanB Date: Mon, 18 Nov 2013 10:28:27 +0100 Subject: [PATCH] Unifying fallback subjects key between csv and db, first match between loaded data from csv and from stordb --- engine/calldesc.go | 1 - engine/loader_csv.go | 10 +--------- engine/loader_db.go | 6 +++--- utils/apitpdata.go | 16 ++++++++++++++++ utils/consts.go | 2 ++ 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/engine/calldesc.go b/engine/calldesc.go index 020a19123..8e6f1049f 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -50,7 +50,6 @@ func init() { const ( RECURSION_MAX_DEPTH = 3 FALLBACK_SUBJECT = "*any" - FALLBACK_SEP = ";" ) var ( diff --git a/engine/loader_csv.go b/engine/loader_csv.go index 7b932979e..a2f8a53fb 100644 --- a/engine/loader_csv.go +++ b/engine/loader_csv.go @@ -372,15 +372,7 @@ func (csvr *CSVReader) LoadRatingProfiles() (err error) { rpa := &RatingPlanActivation{ ActivationTime: at, RatingPlanId: record[5], - } - if fallbacksubject != "" { - var sslice utils.StringSlice = rpa.FallbackKeys - for _, fbs := range strings.Split(fallbacksubject, FALLBACK_SEP) { - newKey := fmt.Sprintf("%s:%s:%s:%s", direction, tenant, tor, fbs) - if !sslice.Contains(newKey) { - rpa.FallbackKeys = append(rpa.FallbackKeys, newKey) - } - } + FallbackKeys: utils.FallbackSubjKeys(direction, tenant, tor, fallbacksubject), } rp.RatingPlanActivations = append(rp.RatingPlanActivations, rpa) csvr.ratingProfiles[rp.Id] = rp diff --git a/engine/loader_db.go b/engine/loader_db.go index 6ac24aa2e..25b138022 100644 --- a/engine/loader_db.go +++ b/engine/loader_db.go @@ -23,7 +23,6 @@ import ( "fmt" "github.com/cgrates/cgrates/utils" "log" - "strings" ) type DbReader struct { @@ -233,7 +232,7 @@ func (dbr *DbReader) LoadRatingProfiles() error { &RatingPlanActivation{ ActivationTime: at, RatingPlanId: tpRa.RatingPlanId, - FallbackKeys: strings.Split(tpRa.FallbackSubjects,FALLBACK_SEP), + FallbackKeys: utils.FallbackSubjKeys(tpRpf.Direction, tpRpf.Tenant, tpRpf.TOR, tpRa.FallbackSubjects), }) } dbr.ratingProfiles[tpRpf.KeyId()] = rpf @@ -318,7 +317,8 @@ func (dbr *DbReader) LoadRatingProfileByTag(tag string) error { } } resultRatingProfile.RatingPlanActivations = append(resultRatingProfile.RatingPlanActivations, - &RatingPlanActivation{at, tpRa.RatingPlanId, strings.Split(tpRa.FallbackSubjects,FALLBACK_SEP)}) + &RatingPlanActivation{at, tpRa.RatingPlanId, + utils.FallbackSubjKeys(tpRpf.Direction, tpRpf.Tenant, tpRpf.TOR, tpRa.FallbackSubjects)}) } } return dbr.dataDb.SetRatingProfile(resultRatingProfile) diff --git a/utils/apitpdata.go b/utils/apitpdata.go index b4f5dfee8..b032e9b2e 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -21,6 +21,7 @@ package utils import ( "time" "fmt" + "strings" ) // This file deals with tp_* data definition @@ -142,6 +143,21 @@ type TPRatingActivation struct { FallbackSubjects string // So we follow the api } +// Helper to return the subject fallback keys we need in dataDb +func FallbackSubjKeys(direction, tenant, tor, fallbackSubjects string) []string { + var subjKeys []string + if len(fallbackSubjects) != 0 { + var sslice StringSlice + for _, fbs := range strings.Split(fallbackSubjects, string(FALLBACK_SEP)) { + newKey := fmt.Sprintf("%s:%s:%s:%s", direction, tenant, tor, fbs) + if !sslice.Contains(newKey) { + subjKeys = append(subjKeys, newKey) + } + } + } + return subjKeys +} + type AttrTPRatingProfileIds struct { TPid string // Tariff plan id Tenant string // Tenant's Id diff --git a/utils/consts.go b/utils/consts.go index e4a023d2a..6bf81f977 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -57,6 +57,8 @@ const ( ROUNDING_MIDDLE = "*middle" ROUNDING_DOWN = "*down" COMMENT_CHAR = '#' + FALLBACK_SEP = ';' JSON = "json" MSGPACK = "msgpack" + )