Alerting: rule backtesting API (#57318)

* Implement backtesting engine that can process regular rule specification (with queries to datasource) as well as special kind of rules that have data frame instead of query.
* declare a new API endpoint and model
* add feature toggle `alertingBacktesting`
This commit is contained in:
Yuri Tseretyan
2022-12-14 09:44:14 -05:00
committed by GitHub
parent 258696409d
commit ad09feed83
24 changed files with 1871 additions and 1 deletions
@@ -0,0 +1,27 @@
package backtesting
import (
"context"
"time"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
)
// QueryEvaluator is evaluator of regular alert rule queries
type queryEvaluator struct {
eval eval.ConditionEvaluator
}
func (d *queryEvaluator) Eval(ctx context.Context, from, to time.Time, interval time.Duration, callback callbackFunc) error {
for now := from; now.Before(to); now = now.Add(interval) {
results, err := d.eval.Evaluate(ctx, now)
if err != nil {
return err
}
err = callback(now, results)
if err != nil {
return err
}
}
return nil
}