[v9.4.x] Alerting: Add label query parameters to state history endpoint (#62835)

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

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

* Pre-initialize map

(cherry picked from commit 9eeea8f5ea)

Co-authored-by: Alexander Weaver <weaver.alex.d@gmail.com>
This commit is contained in:
Grot (@grafanabot)
2023-02-02 23:14:16 +00:00
committed by GitHub
co-authored by Alexander Weaver
parent 21f204d35a
commit 8fd1547edb
+14 -2
View File
@@ -3,6 +3,7 @@ package api
import (
"context"
"net/http"
"strings"
"time"
"github.com/grafana/grafana-plugin-sdk-go/data"
@@ -21,16 +22,27 @@ type HistorySrv struct {
hist Historian
}
const labelQueryPrefix = "labels_"
func (srv *HistorySrv) RouteQueryStateHistory(c *contextmodel.ReqContext) response.Response {
from := c.QueryInt64("from")
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{
RuleUID: c.Query("ruleUID"),
RuleUID: ruleUID,
OrgID: c.OrgID,
SignedInUser: c.SignedInUser,
From: time.Unix(from, 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)
if err != nil {