mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
WeightFromDynamics function
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user