Chore: Expression engine to support relative time range (#57474)

* make TimeRange interface and add relative range
* make Execute methods support the current time
* update resample to support relative time range
* update DSNode to support relative time range
* update query service to create queries with absolute time
* make alerting evaluator create relative time ranges
This commit is contained in:
Yuriy Tseretyan
2022-10-26 16:13:58 -04:00
committed by GitHub
parent 596b4fecb0
commit 2d20c8db7b
15 changed files with 79 additions and 42 deletions
+2 -5
View File
@@ -215,10 +215,7 @@ func getExprRequest(ctx EvaluationContext, data []models.AlertQuery, dsCacheServ
}
req.Queries = append(req.Queries, expr.Query{
TimeRange: expr.TimeRange{
From: q.RelativeTimeRange.ToTimeRange(ctx.At).From,
To: q.RelativeTimeRange.ToTimeRange(ctx.At).To,
},
TimeRange: q.RelativeTimeRange.ToTimeRange(),
DataSource: ds,
JSON: model,
Interval: interval,
@@ -334,7 +331,7 @@ func executeQueriesAndExpressions(ctx EvaluationContext, data []models.AlertQuer
return nil, err
}
return exprService.TransformData(ctx.Ctx, queryDataReq)
return exprService.TransformData(ctx.Ctx, ctx.At, queryDataReq)
}
// datasourceUIDsToRefIDs returns a sorted slice of Ref IDs for each Datasource UID.
+4 -5
View File
@@ -6,7 +6,6 @@ import (
"fmt"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/expr"
)
@@ -70,10 +69,10 @@ func (rtr *RelativeTimeRange) isValid() bool {
return rtr.From > rtr.To
}
func (rtr *RelativeTimeRange) ToTimeRange(now time.Time) backend.TimeRange {
return backend.TimeRange{
From: now.Add(-time.Duration(rtr.From)),
To: now.Add(-time.Duration(rtr.To)),
func (rtr *RelativeTimeRange) ToTimeRange() expr.TimeRange {
return expr.RelativeTimeRange{
From: -time.Duration(rtr.From),
To: -time.Duration(rtr.To),
}
}