Modified matching functions for attributes and supliers to return only the best found

This commit is contained in:
Trial97
2019-02-14 13:16:57 +02:00
committed by Dan Christian Bogos
parent bb2d874319
commit bb14e68b0c
5 changed files with 28 additions and 73 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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,
},
},

View File

@@ -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

View File

@@ -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)
}
}