mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-12-26 07:29:04 +01:00
Add caching for condition evaluation
This commit is contained in:
parent
248c815a1c
commit
b44e226496
@ -66,6 +66,7 @@ var language gval.Language = gval.NewLanguage(
|
|||||||
gval.Full(),
|
gval.Full(),
|
||||||
metricCacheLanguage,
|
metricCacheLanguage,
|
||||||
)
|
)
|
||||||
|
var evaluables map[string]gval.Evaluable = make(map[string]gval.Evaluable)
|
||||||
|
|
||||||
func (c *metricAggregator) Init(output chan lp.CCMetric) error {
|
func (c *metricAggregator) Init(output chan lp.CCMetric) error {
|
||||||
c.output = output
|
c.output = output
|
||||||
@ -288,66 +289,41 @@ func (c *metricAggregator) AddFunction(name string, function func(args ...interf
|
|||||||
}
|
}
|
||||||
|
|
||||||
func EvalBoolCondition(condition string, params map[string]interface{}) (bool, error) {
|
func EvalBoolCondition(condition string, params map[string]interface{}) (bool, error) {
|
||||||
newcond :=
|
var evaluable gval.Evaluable
|
||||||
strings.ReplaceAll(
|
var ok bool
|
||||||
|
if evaluable, ok = evaluables[condition]; !ok {
|
||||||
|
newcond :=
|
||||||
strings.ReplaceAll(
|
strings.ReplaceAll(
|
||||||
condition, "'", "\""), "%", "\\")
|
strings.ReplaceAll(
|
||||||
value, err := gval.Evaluate(newcond, params, language)
|
condition, "'", "\""), "%", "\\")
|
||||||
if err != nil {
|
var err error
|
||||||
return false, err
|
evaluable, err = language.NewEvaluable(newcond)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
evaluables[condition] = evaluable
|
||||||
}
|
}
|
||||||
endResult := false
|
value, err := evaluable.EvalBool(context.Background(), params)
|
||||||
err = nil
|
return value, err
|
||||||
switch r := value.(type) {
|
|
||||||
case bool:
|
|
||||||
endResult = r
|
|
||||||
case float64:
|
|
||||||
endResult = r != 0.0
|
|
||||||
case float32:
|
|
||||||
endResult = r != 0.0
|
|
||||||
case int:
|
|
||||||
endResult = r != 0
|
|
||||||
case int64:
|
|
||||||
endResult = r != 0
|
|
||||||
case int32:
|
|
||||||
endResult = r != 0
|
|
||||||
default:
|
|
||||||
err = fmt.Errorf("cannot evaluate '%s' to bool", newcond)
|
|
||||||
}
|
|
||||||
return endResult, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func EvalFloat64Condition(condition string, params map[string]interface{}) (float64, error) {
|
func EvalFloat64Condition(condition string, params map[string]interface{}) (float64, error) {
|
||||||
var endResult float64 = math.NaN()
|
var evaluable gval.Evaluable
|
||||||
newcond := strings.ReplaceAll(condition, "'", "\"")
|
var ok bool
|
||||||
newcond = strings.ReplaceAll(newcond, "%", "\\")
|
if evaluable, ok = evaluables[condition]; !ok {
|
||||||
value, err := gval.Evaluate(newcond, params, language)
|
newcond :=
|
||||||
if err != nil {
|
strings.ReplaceAll(
|
||||||
cclog.ComponentDebug("MetricRouter", condition, " = ", err.Error())
|
strings.ReplaceAll(
|
||||||
return endResult, err
|
condition, "'", "\""), "%", "\\")
|
||||||
}
|
var err error
|
||||||
err = nil
|
evaluable, err = language.NewEvaluable(newcond)
|
||||||
switch r := value.(type) {
|
if err != nil {
|
||||||
case bool:
|
return math.NaN(), err
|
||||||
if r {
|
|
||||||
endResult = 1.0
|
|
||||||
} else {
|
|
||||||
endResult = 0.0
|
|
||||||
}
|
}
|
||||||
case float64:
|
evaluables[condition] = evaluable
|
||||||
endResult = r
|
|
||||||
case float32:
|
|
||||||
endResult = float64(r)
|
|
||||||
case int:
|
|
||||||
endResult = float64(r)
|
|
||||||
case int64:
|
|
||||||
endResult = float64(r)
|
|
||||||
case int32:
|
|
||||||
endResult = float64(r)
|
|
||||||
default:
|
|
||||||
err = fmt.Errorf("cannot evaluate '%s' to float64", newcond)
|
|
||||||
}
|
}
|
||||||
return endResult, err
|
value, err := evaluable.EvalFloat64(context.Background(), params)
|
||||||
|
return value, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAggregator(output chan lp.CCMetric) (MetricAggregator, error) {
|
func NewAggregator(output chan lp.CCMetric) (MetricAggregator, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user