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:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user