2023-02-02 11:34:00 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2023-02-02 16:52:08 -06:00
|
|
|
"strings"
|
2023-02-02 11:34:00 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Historian interface {
|
2023-03-17 12:41:18 -05:00
|
|
|
Query(ctx context.Context, query models.HistoryQuery) (*data.Frame, error)
|
2023-02-02 11:34:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type HistorySrv struct {
|
|
|
|
logger log.Logger
|
|
|
|
hist Historian
|
|
|
|
}
|
|
|
|
|
2023-02-02 16:52:08 -06:00
|
|
|
const labelQueryPrefix = "labels_"
|
|
|
|
|
2023-02-02 11:34:00 -06:00
|
|
|
func (srv *HistorySrv) RouteQueryStateHistory(c *contextmodel.ReqContext) response.Response {
|
|
|
|
from := c.QueryInt64("from")
|
|
|
|
to := c.QueryInt64("to")
|
2023-06-28 13:32:28 -05:00
|
|
|
limit := c.QueryInt("limit")
|
2023-02-02 16:52:08 -06:00
|
|
|
ruleUID := c.Query("ruleUID")
|
2023-07-24 23:46:46 -05:00
|
|
|
dashUID := c.Query("dashboardUID")
|
|
|
|
panelID := c.QueryInt64("panelID")
|
2023-02-02 16:52:08 -06:00
|
|
|
|
|
|
|
labels := make(map[string]string)
|
|
|
|
for k, v := range c.Req.URL.Query() {
|
|
|
|
if strings.HasPrefix(k, labelQueryPrefix) {
|
|
|
|
labels[k[len(labelQueryPrefix):]] = v[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 11:34:00 -06:00
|
|
|
query := models.HistoryQuery{
|
2023-02-02 16:52:08 -06:00
|
|
|
RuleUID: ruleUID,
|
2023-10-09 03:40:19 -05:00
|
|
|
OrgID: c.SignedInUser.GetOrgID(),
|
2023-07-24 23:46:46 -05:00
|
|
|
DashboardUID: dashUID,
|
|
|
|
PanelID: panelID,
|
2023-02-02 11:34:00 -06:00
|
|
|
SignedInUser: c.SignedInUser,
|
|
|
|
From: time.Unix(from, 0),
|
|
|
|
To: time.Unix(to, 0),
|
2023-06-28 13:32:28 -05:00
|
|
|
Limit: limit,
|
2023-02-02 16:52:08 -06:00
|
|
|
Labels: labels,
|
2023-02-02 11:34:00 -06:00
|
|
|
}
|
2023-03-17 12:41:18 -05:00
|
|
|
frame, err := srv.hist.Query(c.Req.Context(), query)
|
2023-02-02 11:34:00 -06:00
|
|
|
if err != nil {
|
|
|
|
return ErrResp(http.StatusInternalServerError, err, "")
|
|
|
|
}
|
|
|
|
return response.JSON(http.StatusOK, frame)
|
|
|
|
}
|