From bb14e68b0cd68ead992d2515cd42ee4920033e7f Mon Sep 17 00:00:00 2001 From: Trial97 Date: Thu, 14 Feb 2019 13:16:57 +0200 Subject: [PATCH] Modified matching functions for attributes and supliers to return only the best found --- engine/attributes.go | 28 +++++++--------------------- engine/attributes_test.go | 24 ------------------------ engine/model_helpers_test.go | 2 +- engine/suppliers.go | 23 ++++++++--------------- engine/suppliers_test.go | 24 ++++++++++++------------ 5 files changed, 28 insertions(+), 73 deletions(-) diff --git a/engine/attributes.go b/engine/attributes.go index ff07e3837..09d9f9771 100644 --- a/engine/attributes.go +++ b/engine/attributes.go @@ -58,15 +58,13 @@ func (alS *AttributeService) Shutdown() (err error) { } // matchingAttributeProfilesForEvent returns ordered list of matching resources which are active by the time of the call -func (alS *AttributeService) matchingAttributeProfilesForEvent(args *AttrArgsProcessEvent) (aPrfls AttributeProfiles, err error) { - var attrIdxKey string +func (alS *AttributeService) attributeProfileForEvent(args *AttrArgsProcessEvent) (matchAttrPrfl *AttributeProfile, err error) { var attrIDs []string contextVal := utils.META_DEFAULT if args.Context != nil && *args.Context != "" { contextVal = *args.Context } - attrIdxKey = utils.ConcatenatedKey(args.Tenant, contextVal) - matchingAPs := make(map[string]*AttributeProfile) + attrIdxKey := utils.ConcatenatedKey(args.Tenant, contextVal) if len(args.AttributeIDs) != 0 { attrIDs = args.AttributeIDs } else { @@ -102,27 +100,15 @@ func (alS *AttributeService) matchingAttributeProfilesForEvent(args *AttrArgsPro } else if !pass { continue } - matchingAPs[apID] = aPrfl + if matchAttrPrfl == nil || matchAttrPrfl.Weight < aPrfl.Weight { + matchAttrPrfl = aPrfl + } } // All good, convert from Map to Slice so we can sort - aPrfls = make(AttributeProfiles, len(matchingAPs)) - i := 0 - for _, aPrfl := range matchingAPs { - aPrfls[i] = aPrfl - i++ - } - aPrfls.Sort() - return -} - -func (alS *AttributeService) attributeProfileForEvent(args *AttrArgsProcessEvent) (attrPrfl *AttributeProfile, err error) { - var attrPrfls AttributeProfiles - if attrPrfls, err = alS.matchingAttributeProfilesForEvent(args); err != nil { - return - } else if len(attrPrfls) == 0 { + if matchAttrPrfl == nil { return nil, utils.ErrNotFound } - return attrPrfls[0], nil + return } // AttrSFldNameValue is a helper struct for AttrSDigest deserialization diff --git a/engine/attributes_test.go b/engine/attributes_test.go index e6c3de865..ec478ee36 100644 --- a/engine/attributes_test.go +++ b/engine/attributes_test.go @@ -265,30 +265,6 @@ func TestAttributeCache(t *testing.T) { } } -func TestAttributeMatchingAttributeProfilesForEvent(t *testing.T) { - atrp, err := attrService.matchingAttributeProfilesForEvent(attrEvs[0]) - if err != nil { - t.Errorf("Error: %+v", err) - } - if !reflect.DeepEqual(atrPs[0], atrp[0]) { - t.Errorf("Expecting: %+v, received: %+v ", atrPs[0], atrp[0]) - } - atrp, err = attrService.matchingAttributeProfilesForEvent(attrEvs[1]) - if err != nil { - t.Errorf("Error: %+v", err) - } - if !reflect.DeepEqual(atrPs[1], atrp[0]) { - t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(atrPs), utils.ToJSON(atrp)) - } - atrp, err = attrService.matchingAttributeProfilesForEvent(attrEvs[2]) - if err != nil { - t.Errorf("Error: %+v", err) - } - if !reflect.DeepEqual(atrPs[2], atrp[0]) { - t.Errorf("Expecting: %+v, received: %+v ", atrPs[2], atrp[0]) - } -} - func TestAttributeProfileForEvent(t *testing.T) { atrp, err := attrService.attributeProfileForEvent(attrEvs[0]) if err != nil { diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index 4c8d26d7f..bf09b7138 100644 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -1802,7 +1802,7 @@ func TestAPItoDispatcherProfile(t *testing.T) { ID: "C1", FilterIDs: []string{}, Weight: 10, - Params: map[string]interface{}{"\u0000": "192.168.54.203"}, + Params: map[string]interface{}{"0": "192.168.54.203"}, Blocker: false, }, }, diff --git a/engine/suppliers.go b/engine/suppliers.go index 0c280c9a9..530e0dcb7 100644 --- a/engine/suppliers.go +++ b/engine/suppliers.go @@ -126,8 +126,7 @@ func (spS *SupplierService) Shutdown() error { } // matchingSupplierProfilesForEvent returns ordered list of matching resources which are active by the time of the call -func (spS *SupplierService) matchingSupplierProfilesForEvent(ev *utils.CGREvent) (sPrfls SupplierProfiles, err error) { - matchingLPs := make(map[string]*SupplierProfile) +func (spS *SupplierService) matchingSupplierProfilesForEvent(ev *utils.CGREvent) (matchingLP *SupplierProfile, err error) { sPrflIDs, err := MatchingItemIDsForEvent(ev.Event, spS.stringIndexedFields, spS.prefixIndexedFields, spS.dm, utils.CacheSupplierFilterIndexes, ev.Tenant, spS.filterS.cfg.FilterSCfg().IndexedSelects) if err != nil { @@ -151,16 +150,13 @@ func (spS *SupplierService) matchingSupplierProfilesForEvent(ev *utils.CGREvent) } else if !pass { continue } - matchingLPs[lpID] = splPrfl + if matchingLP == nil || matchingLP.Weight < splPrfl.Weight { + matchingLP = splPrfl + } } - // All good, convert from Map to Slice so we can sort - sPrfls = make(SupplierProfiles, len(matchingLPs)) - i := 0 - for _, sPrfl := range matchingLPs { - sPrfls[i] = sPrfl - i++ + if matchingLP == nil { + return nil, utils.ErrNotFound } - sPrfls.Sort() return } @@ -386,13 +382,10 @@ func (spS *SupplierService) sortedSuppliersForEvent(args *ArgsGetSuppliers) (sor if _, has := args.CGREvent.Event[utils.Usage]; !has { args.CGREvent.Event[utils.Usage] = time.Duration(time.Minute) // make sure we have default set for Usage } - var suppPrfls SupplierProfiles - if suppPrfls, err = spS.matchingSupplierProfilesForEvent(&args.CGREvent); err != nil { + var splPrfl *SupplierProfile + if splPrfl, err = spS.matchingSupplierProfilesForEvent(&args.CGREvent); err != nil { return - } else if len(suppPrfls) == 0 { - return nil, utils.ErrNotFound } - splPrfl := suppPrfls[0] // pick up the first lcr profile as winner extraOpts, err := args.asOptsGetSuppliers() // convert suppliers arguments into internal options used to limit data if err != nil { return nil, err diff --git a/engine/suppliers_test.go b/engine/suppliers_test.go index 099c19297..433aeeef1 100644 --- a/engine/suppliers_test.go +++ b/engine/suppliers_test.go @@ -390,24 +390,24 @@ func TestSuppliersmatchingSupplierProfilesForEvent(t *testing.T) { if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(sppTest[0], sprf[0]) { - t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf[0]) + if !reflect.DeepEqual(sppTest[0], sprf) { + t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf) } sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[1].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(sppTest[1], sprf[0]) { - t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf[0]) + if !reflect.DeepEqual(sppTest[1], sprf) { + t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf) } sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[2].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(sppTest[2], sprf[0]) { - t.Errorf("Expecting: %+v, received: %+v", sppTest[2], sprf[0]) + if !reflect.DeepEqual(sppTest[2], sprf) { + t.Errorf("Expecting: %+v, received: %+v", sppTest[2], sprf) } } @@ -634,23 +634,23 @@ func TestSuppliersMatchWithIndexFalse(t *testing.T) { if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(sppTest[0], sprf[0]) { - t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf[0]) + if !reflect.DeepEqual(sppTest[0], sprf) { + t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf) } sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[1].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(sppTest[1], sprf[0]) { - t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf[0]) + if !reflect.DeepEqual(sppTest[1], sprf) { + t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf) } sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[2].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } - if !reflect.DeepEqual(sppTest[2], sprf[0]) { - t.Errorf("Expecting: %+v, received: %+v", sppTest[2], sprf[0]) + if !reflect.DeepEqual(sppTest[2], sprf) { + t.Errorf("Expecting: %+v, received: %+v", sppTest[2], sprf) } }