diff --git a/engine/filterhelpers.go b/engine/filterhelpers.go index 62f4d0654..6870f77ac 100644 --- a/engine/filterhelpers.go +++ b/engine/filterhelpers.go @@ -101,3 +101,17 @@ func MatchingItemIDsForEvent(ev utils.MapStorage, stringFldIDs, prefixFldIDs, su } return } + +// Weight returns weight of the first matching DynamicWeight +func WeightFromDynamics(dWs []*utils.DynamicWeight, + fltrS FilterS, tnt string, ev utils.DataProvider) (wg float64, err error) { + for _, dW := range dWs { + var pass bool + if pass, err = fltrS.Pass(tnt, dW.FilterIDs, ev); err != nil { + return + } else if pass { + return dW.Weight, nil + } + } + return 0.0, nil +} diff --git a/utils/dynamicweight.go b/utils/dynamicweight.go index a9b8f79ea..6020a201f 100644 --- a/utils/dynamicweight.go +++ b/utils/dynamicweight.go @@ -32,13 +32,13 @@ func NewDynamicWeightsFromString(s, dWSep, fltrSep string) (dWs []*DynamicWeight if lnDwStrs%nrFlds != 0 { // need to have multiples of number of fields in one DynamicWeight return nil, fmt.Errorf("invalid DynamicWeight format for string <%s>", s) } - dWs = make([]*DynamicWeight, lnDwStrs/2) + dWs = make([]*DynamicWeight, lnDwStrs/nrFlds) for i := 0; i < lnDwStrs; i += nrFlds { dw := &DynamicWeight{FilterIDs: strings.Split(dwStrs[i], fltrSep)} if dw.Weight, err = strconv.ParseFloat(dwStrs[i+1], 64); err != nil { return nil, fmt.Errorf("invalid Weight <%s> in string: <%s>", dwStrs[i+1], s) } - dWs[i/nrFlds] = dw + dWs = append(dWs, dw) } return }