From d01f8b58dc1bc3e4de00a62a0b5c6683a34c9d86 Mon Sep 17 00:00:00 2001 From: edwardro22 Date: Wed, 13 Sep 2017 09:08:09 +0000 Subject: [PATCH] Added Tenant to resources --- data/storage/migrator/rc7_to_rc8.sh | 6 ++++- .../mysql/create_tariffplan_tables.sql | 1 + .../postgres/create_tariffplan_tables.sql | 1 + data/tariffplans/testtp/Resources.csv | 12 ++++----- data/tariffplans/tutorial/Resources.csv | 12 ++++----- engine/loader_csv_test.go | 12 +++++---- engine/model_helpers.go | 3 +++ engine/model_helpers_test.go | 5 ++++ engine/models.go | 25 ++++++++++--------- engine/resources.go | 3 +++ engine/storage_sql.go | 5 +--- utils/apitpdata.go | 1 + 12 files changed, 52 insertions(+), 34 deletions(-) diff --git a/data/storage/migrator/rc7_to_rc8.sh b/data/storage/migrator/rc7_to_rc8.sh index 3f4b3b809..5f6d7143e 100755 --- a/data/storage/migrator/rc7_to_rc8.sh +++ b/data/storage/migrator/rc7_to_rc8.sh @@ -66,11 +66,15 @@ DIR="$(dirname "$(readlink -f "$0")")" case $datadb in "redis") -./dbsmerge_redis.py +echo "executing dbsmerge_redis.py" +./dbsmerge_redis.py +echo "done!" ;; "mongo") +echo "executing dbsmerge_mongo.py" ./dbsmerge_mongo.py +echo "done!" ;; esac diff --git a/data/storage/mysql/create_tariffplan_tables.sql b/data/storage/mysql/create_tariffplan_tables.sql index 5f74cd89e..e62bd1146 100644 --- a/data/storage/mysql/create_tariffplan_tables.sql +++ b/data/storage/mysql/create_tariffplan_tables.sql @@ -397,6 +397,7 @@ CREATE TABLE tp_aliases ( DROP TABLE IF EXISTS tp_resources; CREATE TABLE tp_resources ( + `tenant` varchar(64) NOT NULL, `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, `tag` varchar(64) NOT NULL, diff --git a/data/storage/postgres/create_tariffplan_tables.sql b/data/storage/postgres/create_tariffplan_tables.sql index 5bb88688c..c0772103c 100644 --- a/data/storage/postgres/create_tariffplan_tables.sql +++ b/data/storage/postgres/create_tariffplan_tables.sql @@ -393,6 +393,7 @@ CREATE INDEX tpaliases_idx ON tp_aliases (tpid,direction,tenant,category,account DROP TABLE IF EXISTS tp_resources; CREATE TABLE tp_resources ( + "tenant"varchar(64) NOT NULL, "id" SERIAL PRIMARY KEY, "tpid" varchar(64) NOT NULL, "tag" varchar(64) NOT NULL, diff --git a/data/tariffplans/testtp/Resources.csv b/data/tariffplans/testtp/Resources.csv index 06204d7c7..6597b961d 100755 --- a/data/tariffplans/testtp/Resources.csv +++ b/data/tariffplans/testtp/Resources.csv @@ -1,6 +1,6 @@ -#Id[0],FilterType[1],FilterFieldName[2],FilterFieldValues[3],ActivationInterval[4],TTL[5],Limit[6],AllocationMessage[7],Blocker[8],Stored[9],Weight[10],Thresholds[11] -ResGroup1,*string,Account,1001;1002,2014-07-29T15:00:00Z,1s,7,,true,true,20, -ResGroup1,*string_prefix,Destination,10;20,,,,,,,, -ResGroup1,*rsr_fields,,Subject(~^1.*1$);Destination(1002),,,,,,,, -ResGroup2,*destinations,Destination,DST_FS,2014-07-29T15:00:00Z,3600s,8,SPECIAL_1002,true,true,10, -ResGroup3,*cdr_stats,,CDRST1:*min_ASR:34;CDRST_1001:*min_ASR:20,,,,,,,, +#Tenant[0],Id[1],FilterType[2],FilterFieldName[3],FilterFieldValues[4],ActivationInterval[5],TTL[6],Limit[7],AllocationMessage[8],Blocker[9],Stored[10],Weight[11],Thresholds[12] +Tester,ResGroup1,*string,Account,1001;1002,2014-07-29T15:00:00Z,1s,7,,true,true,20, +Tester,ResGroup1,*string_prefix,Destination,10;20,,,,,,,, +Tester,ResGroup1,*rsr_fields,,Subject(~^1.*1$);Destination(1002),,,,,,,, +Tester,ResGroup2,*destinations,Destination,DST_FS,2014-07-29T15:00:00Z,3600s,8,SPECIAL_1002,true,true,10, +Tester,ResGroup3,*cdr_stats,,CDRST1:*min_ASR:34;CDRST_1001:*min_ASR:20,,,,,,,, diff --git a/data/tariffplans/tutorial/Resources.csv b/data/tariffplans/tutorial/Resources.csv index 980438b6a..30f655dcc 100755 --- a/data/tariffplans/tutorial/Resources.csv +++ b/data/tariffplans/tutorial/Resources.csv @@ -1,8 +1,8 @@ -#Id[0],FilterType[1],FilterFieldName[2],FilterFieldValues[3],ActivationInterval[4],TTL[5],Limit[6],AllocationMessage[7],Blocker[8],Stored[9],Weight[10],Thresholds[11] -ResGroup1,*string,Account,1001;1002,2014-07-29T15:00:00Z,1s,7,,true,true,20, -ResGroup1,*string_prefix,Destination,10;20,,,,,,,, -ResGroup1,*rsr_fields,,Subject(~^1.*1$);Destination(1002),,,,,,,, -ResGroup2,*destinations,Destination,DST_FS,2014-07-29T15:00:00Z,3600s,8,SPECIAL_1002,true,true,10, -ResGroup3,*string,Account,3001,2014-07-29T15:00:00Z,1s,3,,true,true,20, +#Tenant[0],Id[1],FilterType[2],FilterFieldName[3],FilterFieldValues[4],ActivationInterval[5],TTL[6],Limit[7],AllocationMessage[8],Blocker[9],Stored[10],Weight[11],Thresholds[12] +Tester,ResGroup1,*string,Account,1001;1002,2014-07-29T15:00:00Z,1s,7,,true,true,20, +Tester,ResGroup1,*string_prefix,Destination,10;20,,,,,,,, +Tester,ResGroup1,*rsr_fields,,Subject(~^1.*1$);Destination(1002),,,,,,,, +Tester,ResGroup2,*destinations,Destination,DST_FS,2014-07-29T15:00:00Z,3600s,8,SPECIAL_1002,true,true,10, +Tester,ResGroup3,*string,Account,3001,2014-07-29T15:00:00Z,1s,3,,true,true,20, #ResGroup3,*timings,SetupTime,PEAK,,,,,,,, #ResGroup3,*cdr_stats,,CDRST1:*min_ASR:34;CDRST_1001:*min_ASR:20,,,,,,,, diff --git a/engine/loader_csv_test.go b/engine/loader_csv_test.go index d472eac1d..798aa0a4a 100755 --- a/engine/loader_csv_test.go +++ b/engine/loader_csv_test.go @@ -266,11 +266,11 @@ cgrates.org,mas,true,another,value,10 *out,cgrates.org,call,remo,remo,*any,*rating,Account,remo,minu,10 ` resProfiles = ` -#Id[0],FilterType[1],FilterFieldName[2],FilterFieldValues[3],ActivationInterval[4],TTL[5],Limit[6],AllocationMessage[7],Weight[8],Thresholds[9] -ResGroup21,*string,HdrAccount,1001;1002,2014-07-29T15:00:00Z,1s,2,call,true,true,10, -ResGroup21,*string_prefix,HdrDestination,10;20,,,,,,,, -ResGroup21,*rsr_fields,,HdrSubject(~^1.*1$);HdrDestination(1002),,,,,,,, -ResGroup22,*destinations,HdrDestination,DST_FS,2014-07-29T15:00:00Z,3600s,2,premium_call,true,true,10, +#Tenant[0],Id[1],FilterType[2],FilterFieldName[3],FilterFieldValues[4],ActivationInterval[5],TTL[6],Limit[7],AllocationMessage[8],Blocker[9],Stored[10],Weight[11],Thresholds[12] +Tester,ResGroup21,*string,HdrAccount,1001;1002,2014-07-29T15:00:00Z,1s,2,call,true,true,10, +Tester,ResGroup21,*string_prefix,HdrDestination,10;20,,,,,,,, +Tester,ResGroup21,*rsr_fields,,HdrSubject(~^1.*1$);HdrDestination(1002),,,,,,,, +Tester,ResGroup22,*destinations,HdrDestination,DST_FS,2014-07-29T15:00:00Z,3600s,2,premium_call,true,true,10, ` stats = ` #Tenant[0],Id[1],FilterType[2],FilterFieldName[3],FilterFieldValues[4],ActivationInterval[5],QueueLength[6],TTL[7],Metrics[8],Blocker[9],Stored[10],Weight[11],Thresholds[12] @@ -1393,6 +1393,7 @@ func TestLoadResourceProfiles(t *testing.T) { eResProfiles := map[string]*utils.TPResource{ "ResGroup21": &utils.TPResource{ TPid: testTPID, + Tenant:"Tester", ID: "ResGroup21", Filters: []*utils.TPRequestFilter{ &utils.TPRequestFilter{Type: MetaString, FieldName: "HdrAccount", Values: []string{"1001", "1002"}}, @@ -1409,6 +1410,7 @@ func TestLoadResourceProfiles(t *testing.T) { }, "ResGroup22": &utils.TPResource{ TPid: testTPID, + Tenant:"Tester", ID: "ResGroup22", Filters: []*utils.TPRequestFilter{ &utils.TPRequestFilter{Type: MetaDestinations, FieldName: "HdrDestination", Values: []string{"DST_FS"}}, diff --git a/engine/model_helpers.go b/engine/model_helpers.go index 11b6d8500..b06b5cce8 100755 --- a/engine/model_helpers.go +++ b/engine/model_helpers.go @@ -1821,6 +1821,7 @@ func (tps TpResources) AsTPResources() (result []*utils.TPResource) { if !found { rl = &utils.TPResource{ TPid: tp.Tpid, + Tenant: tp.Tenant, ID: tp.Tag, Blocker: tp.Blocker, Stored: tp.Stored, @@ -1880,6 +1881,7 @@ func APItoModelResource(rl *utils.TPResource) (mdls TpResources) { for i, fltr := range rl.Filters { mdl := &TpResource{ Tpid: rl.TPid, + Tenant: rl.Tenant, Tag: rl.ID, } if i == 0 { @@ -1921,6 +1923,7 @@ func APItoModelResource(rl *utils.TPResource) (mdls TpResources) { func APItoResource(tpRL *utils.TPResource, timezone string) (rp *ResourceProfile, err error) { rp = &ResourceProfile{ + Tenant: tpRL.Tenant, ID: tpRL.ID, Weight: tpRL.Weight, Blocker: tpRL.Blocker, diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index ee9fba139..bc57d7df3 100755 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -713,6 +713,7 @@ func TestTpResourcesAsTpResources(t *testing.T) { tps := []*TpResource{ &TpResource{ Tpid: "TEST_TPID", + Tenant: "Tester", Tag: "ResGroup1", FilterType: MetaStringPrefix, FilterFieldName: "Destination", @@ -726,12 +727,14 @@ func TestTpResourcesAsTpResources(t *testing.T) { &TpResource{ Tpid: "TEST_TPID", Tag: "ResGroup1", + Tenant: "Tester", FilterType: MetaStringPrefix, FilterFieldName: "Category", FilterFieldValues: "call;inbound_call", Thresholds: "WARN3"}, &TpResource{ Tpid: "TEST_TPID", + Tenant: "Tester", Tag: "ResGroup2", FilterType: MetaStringPrefix, FilterFieldName: "Destination", @@ -745,6 +748,7 @@ func TestTpResourcesAsTpResources(t *testing.T) { eTPs := []*utils.TPResource{ &utils.TPResource{ TPid: tps[0].Tpid, + Tenant: tps[0].Tenant, ID: tps[0].Tag, Filters: []*utils.TPRequestFilter{ &utils.TPRequestFilter{ @@ -769,6 +773,7 @@ func TestTpResourcesAsTpResources(t *testing.T) { }, &utils.TPResource{ TPid: tps[2].Tpid, + Tenant: tps[2].Tenant, ID: tps[2].Tag, Filters: []*utils.TPRequestFilter{ &utils.TPRequestFilter{ diff --git a/engine/models.go b/engine/models.go index bc03cd5b6..8b7fd037a 100755 --- a/engine/models.go +++ b/engine/models.go @@ -454,18 +454,19 @@ func (t TBLSMCosts) TableName() string { type TpResource struct { ID int64 Tpid string - Tag string `index:"0" re:""` - FilterType string `index:"1" re:"^\*[A-Za-z].*"` - FilterFieldName string `index:"2" re:""` - FilterFieldValues string `index:"3" re:""` - ActivationInterval string `index:"4" re:""` - UsageTTL string `index:"5" re:""` - Limit string `index:"6" re:""` - AllocationMessage string `index:"7" re:""` - Blocker bool `index:"8" re:""` - Stored bool `index:"9" re:""` - Weight float64 `index:"10" re:"\d+\.?\d*"` - Thresholds string `index:"11" re:""` + Tenant string `index:"0" re:""` + Tag string `index:"1" re:""` + FilterType string `index:"2" re:"^\*[A-Za-z].*"` + FilterFieldName string `index:"3" re:""` + FilterFieldValues string `index:"4" re:""` + ActivationInterval string `index:"5" re:""` + UsageTTL string `index:"6" re:""` + Limit string `index:"7" re:""` + AllocationMessage string `index:"8" re:""` + Blocker bool `index:"9" re:""` + Stored bool `index:"10" re:""` + Weight float64 `index:"11" re:"\d+\.?\d*"` + Thresholds string `index:"12" re:""` CreatedAt time.Time } diff --git a/engine/resources.go b/engine/resources.go index 78f7d2dc2..fceea912e 100755 --- a/engine/resources.go +++ b/engine/resources.go @@ -39,6 +39,7 @@ func init() { // ResourceProfile represents the user configuration for the resource type ResourceProfile struct { + Tenant string ID string // identifier of this resource Filters []*RequestFilter // filters for the request ActivationInterval *utils.ActivationInterval // time when this resource becomes active and expires @@ -53,6 +54,7 @@ type ResourceProfile struct { // ResourceUsage represents an usage counted type ResourceUsage struct { + Tenant string ID string // Unique identifier of this ResourceUsage, Eg: FreeSWITCH UUID ExpiryTime time.Time Units float64 // Number of units used @@ -66,6 +68,7 @@ func (ru *ResourceUsage) isActive(atTime time.Time) bool { // Resource represents a resource in the system // not thread safe, needs locking at process level type Resource struct { + Tenant string ID string Usages map[string]*ResourceUsage TTLIdx []string // holds ordered list of ResourceIDs based on their TTL, empty if feature is disabled diff --git a/engine/storage_sql.go b/engine/storage_sql.go index 7cd83c7aa..f5fd41d6e 100755 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -588,7 +588,7 @@ func (self *SQLStorage) SetTPStats(sts []*utils.TPStats) error { tx := self.db.Begin() for _, stq := range sts { // Remove previous - if err := tx.Where(&TpStats{Tpid: stq.TPid,Tenant: stq.Tenant,Tag: stq.ID}).Delete(TpStats{}).Error; err != nil { + if err := tx.Where(&TpStats{Tpid: stq.TPid,Tag: stq.ID}).Delete(TpStats{}).Error; err != nil { tx.Rollback() return err } @@ -1171,8 +1171,6 @@ func (self *SQLStorage) GetTPDestinationRates(tpid, id string, pagination *utils func (self *SQLStorage) GetTPTimings(tpid, id string) ([]*utils.ApierTPTiming, error) { var tpTimings TpTimings q := self.db.Where("tpid = ?", tpid) - utils.Logger.Debug(fmt.Sprintf("#1 Id care trimite %s", id)) - utils.Logger.Debug(fmt.Sprintf("#1 TPId care trimite %s", tpid)) if len(id) != 0 { q = q.Where("tag = ?", id) } @@ -1180,7 +1178,6 @@ func (self *SQLStorage) GetTPTimings(tpid, id string) ([]*utils.ApierTPTiming, e return nil, err } ts := tpTimings.AsTPTimings() - utils.Logger.Debug(fmt.Sprintf("#2 ce gaseste : %s", ts)) if len(ts) == 0 { return ts, utils.ErrNotFound } diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 754088e49..db55e86b7 100755 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -1270,6 +1270,7 @@ type AttrSetBalance struct { type TPResource struct { TPid string + Tenant string ID string // Identifier of this limit Filters []*TPRequestFilter // Filters for the request ActivationInterval *TPActivationInterval // Time when this limit becomes active/expires