From 47fee04b2e6a1ea76ceab88f1b922e4b03dab966 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Fri, 10 Jan 2020 14:56:04 +0200 Subject: [PATCH] Added in-line Attributes --- data/tariffplans/testit/Attributes.csv | 1 - data/tariffplans/testit/Chargers.csv | 2 +- data/tariffplans/tutorial/Attributes.csv | 1 - data/tariffplans/tutorial/Chargers.csv | 2 +- dispatchers/caches_it_test.go | 2 +- dispatchers/chargers_it_test.go | 6 ++--- engine/datamanager.go | 9 +++++++- engine/libattributes.go | 28 ++++++++++++++++++++++++ 8 files changed, 42 insertions(+), 9 deletions(-) diff --git a/data/tariffplans/testit/Attributes.csv b/data/tariffplans/testit/Attributes.csv index 674065be3..5be22cc2f 100644 --- a/data/tariffplans/testit/Attributes.csv +++ b/data/tariffplans/testit/Attributes.csv @@ -2,4 +2,3 @@ cgrates.org,ATTR_ACNT_1001,*sessions,FLTR_ACCOUNT_1001,,,OfficeGroup,*constant,Marketing,false,10 cgrates.org,ATTR_SUPPLIER1,*chargers,,,,Subject,*constant,SUPPLIER1,false,10 cgrates.org,ATTR_PAYPAL,*cdrs,*string:~*req.Subject:ANY2CNT,,,PayPalAccount,*constant,paypal@cgrates.org,false,10 -cgrates.org,ATTR_RAW_REQ,*any,*string:~*req.RunID:*raw,,,RequestType,*constant,*none,false,10 diff --git a/data/tariffplans/testit/Chargers.csv b/data/tariffplans/testit/Chargers.csv index 5226f378f..fc5456b60 100644 --- a/data/tariffplans/testit/Chargers.csv +++ b/data/tariffplans/testit/Chargers.csv @@ -1,4 +1,4 @@ #Tenant,ID,FilterIDs,ActivationInterval,RunID,AttributeIDs,Weight -cgrates.org,Raw,,,*raw,ATTR_RAW_REQ,20 +cgrates.org,Raw,,,*raw,*constant:RequestType:*none,20 cgrates.org,CustomerCharges,,,CustomerCharges,*none,20 cgrates.org,SupplierCharges,,,SupplierCharges,ATTR_SUPPLIER1,10 \ No newline at end of file diff --git a/data/tariffplans/tutorial/Attributes.csv b/data/tariffplans/tutorial/Attributes.csv index 2d40f5f98..a52b9d86a 100644 --- a/data/tariffplans/tutorial/Attributes.csv +++ b/data/tariffplans/tutorial/Attributes.csv @@ -22,4 +22,3 @@ cgrates.com,ATTR_TNT_ALIAS,*any,,,,RequestType,*constant,*prepaid,, cgrates.com,ATTR_TNT_ALIAS,*any,,,,*tenant,*constant,cgrates.org,, cgrates.com,ATTR_TNT_1001,*any,*string:~*req.Account:1001,,,*tenant,*constant,cgrates.org,, cgrates.com,ATTR_TNT_DISC,*any,*string:~*req.Account:testDiamInitWithSessionDisconnect,,,*tenant,*constant,cgrates.org,, -cgrates.org,ATTR_RAW_REQ,*any,*string:~*req.RunID:*raw,,,RequestType,*constant,*none,false,10 diff --git a/data/tariffplans/tutorial/Chargers.csv b/data/tariffplans/tutorial/Chargers.csv index d6acebd62..c58b582cb 100644 --- a/data/tariffplans/tutorial/Chargers.csv +++ b/data/tariffplans/tutorial/Chargers.csv @@ -1,3 +1,3 @@ #Tenant,ID,FilterIDs,ActivationInterval,RunID,AttributeIDs,Weight cgrates.org,DEFAULT,,,*default,*none,0 -cgrates.org,Raw,,,*raw,ATTR_RAW_REQ,0 \ No newline at end of file +cgrates.org,Raw,,,*raw,*constant:RequestType:*none,0 \ No newline at end of file diff --git a/dispatchers/caches_it_test.go b/dispatchers/caches_it_test.go index dac491ac2..c0d2e9696 100644 --- a/dispatchers/caches_it_test.go +++ b/dispatchers/caches_it_test.go @@ -122,7 +122,7 @@ func testDspChcLoadAfterFolder(t *testing.T) { t.Error(reply) } expStats[utils.CacheActions].Items = 2 - expStats[utils.CacheAttributeProfiles].Items = 11 + expStats[utils.CacheAttributeProfiles].Items = 10 expStats[utils.CacheChargerProfiles].Items = 2 expStats[utils.CacheFilters].Items = 7 expStats[utils.CacheRatingPlans].Items = 5 diff --git a/dispatchers/chargers_it_test.go b/dispatchers/chargers_it_test.go index 4987bd5db..27a9e9f26 100755 --- a/dispatchers/chargers_it_test.go +++ b/dispatchers/chargers_it_test.go @@ -133,7 +133,7 @@ func testDspCppGetChtgFailover(t *testing.T) { ID: "Raw", FilterIDs: []string{}, RunID: utils.MetaRaw, - AttributeIDs: []string{"ATTR_RAW_REQ"}, + AttributeIDs: []string{"*constant:RequestType:*none"}, Weight: 0, }, ) @@ -222,7 +222,7 @@ func testDspCppTestAuthKey2(t *testing.T) { ID: "Raw", FilterIDs: []string{}, RunID: utils.MetaRaw, - AttributeIDs: []string{"ATTR_RAW_REQ"}, + AttributeIDs: []string{"*constant:RequestType:*none"}, Weight: 0, }, } @@ -285,7 +285,7 @@ func testDspCppGetChtgRoundRobin(t *testing.T) { ID: "Raw", FilterIDs: []string{}, RunID: utils.MetaRaw, - AttributeIDs: []string{"ATTR_RAW_REQ"}, + AttributeIDs: []string{"*constant:RequestType:*none"}, Weight: 0, }, ) diff --git a/engine/datamanager.go b/engine/datamanager.go index baa1477e7..42d6f3387 100644 --- a/engine/datamanager.go +++ b/engine/datamanager.go @@ -1781,7 +1781,14 @@ func (dm *DataManager) GetAttributeProfile(tenant, id string, cacheRead, cacheWr return x.(*AttributeProfile), nil } } - attrPrfl, err = dm.dataDB.GetAttributeProfileDrv(tenant, id) + if strings.HasPrefix(id, utils.Meta) { + attrPrfl, err = NewAttributeFromInline(tenant, id) + } else if dm == nil { // in case we want the filter from dataDB but the connection to dataDB a optional (e.g. SessionS) + err = utils.ErrNoDatabaseConn + return + } else { + attrPrfl, err = dm.dataDB.GetAttributeProfileDrv(tenant, id) + } if err != nil { if err == utils.ErrNotFound && config.CgrConfig().DataDbCfg().Items[utils.MetaAttributeProfiles].Remote { diff --git a/engine/libattributes.go b/engine/libattributes.go index 71f493f85..3d2a9409c 100644 --- a/engine/libattributes.go +++ b/engine/libattributes.go @@ -19,7 +19,9 @@ along with this program. If not, see package engine import ( + "fmt" "sort" + "strings" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" @@ -114,3 +116,29 @@ func (ext *ExternalAttributeProfile) AsAttributeProfile() (attr *AttributeProfil attr.Weight = ext.Weight return } + +// NewAttributeFromInline parses an inline rule into a compiled AttributeProfile +func NewAttributeFromInline(tenant, inlnRule string) (attr *AttributeProfile, err error) { + ruleSplt := strings.Split(inlnRule, utils.InInFieldSep) + if len(ruleSplt) < 3 { + return nil, fmt.Errorf("inline parse error for string: <%s>", inlnRule) + } + var vals config.RSRParsers + if vals, err = config.NewRSRParsers(strings.Join(ruleSplt[2:], utils.InInFieldSep), true, utils.INFIELD_SEP); err != nil { + return nil, err + } + attr = &AttributeProfile{ + Tenant: tenant, + ID: inlnRule, + Contexts: []string{utils.META_ANY}, + Attributes: []*Attribute{&Attribute{ + FieldName: ruleSplt[1], + Type: ruleSplt[0], + Value: vals, + }}, + } + if err = attr.Compile(); err != nil { + return nil, err + } + return +}