Alerting: Add label query parameters to state history endpoint (#62831)

* Allow equality-only matching of arbitrary labels via query params

* Pre-initialize map
This commit is contained in:
Alexander Weaver 2023-02-02 16:52:08 -06:00 committed by GitHub
parent 70c1850888
commit 9eeea8f5ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ package api
import ( import (
"context" "context"
"net/http" "net/http"
"strings"
"time" "time"
"github.com/grafana/grafana-plugin-sdk-go/data" "github.com/grafana/grafana-plugin-sdk-go/data"
@ -21,16 +22,27 @@ type HistorySrv struct {
hist Historian hist Historian
} }
const labelQueryPrefix = "labels_"
func (srv *HistorySrv) RouteQueryStateHistory(c *contextmodel.ReqContext) response.Response { func (srv *HistorySrv) RouteQueryStateHistory(c *contextmodel.ReqContext) response.Response {
from := c.QueryInt64("from") from := c.QueryInt64("from")
to := c.QueryInt64("to") to := c.QueryInt64("to")
ruleUID := c.Query("ruleUID")
labels := make(map[string]string)
for k, v := range c.Req.URL.Query() {
if strings.HasPrefix(k, labelQueryPrefix) {
labels[k[len(labelQueryPrefix):]] = v[0]
}
}
query := models.HistoryQuery{ query := models.HistoryQuery{
RuleUID: c.Query("ruleUID"), RuleUID: ruleUID,
OrgID: c.OrgID, OrgID: c.OrgID,
SignedInUser: c.SignedInUser, SignedInUser: c.SignedInUser,
From: time.Unix(from, 0), From: time.Unix(from, 0),
To: time.Unix(to, 0), To: time.Unix(to, 0),
Labels: map[string]string{}, // TODO, not supported by all backends yet. Labels: labels,
} }
frame, err := srv.hist.QueryStates(c.Req.Context(), query) frame, err := srv.hist.QueryStates(c.Req.Context(), query)
if err != nil { if err != nil {