mirror of https://github.com/grafana/grafana
Alerting: Add endpoint for querying state history (#62166)
* Define endpoint and generate * Wire up and register endpoint * Cleanup, define authorization * Forgot the leading slash * Wire up query and SignedInUser * Wire up timerange query params * Add todo for label queries * Drop comment * Update path to rules subtreepull/62815/head
parent
2f218ab928
commit
6ad1cfef38
@ -0,0 +1,40 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"context" |
||||
"net/http" |
||||
"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 { |
||||
QueryStates(ctx context.Context, query models.HistoryQuery) (*data.Frame, error) |
||||
} |
||||
|
||||
type HistorySrv struct { |
||||
logger log.Logger |
||||
hist Historian |
||||
} |
||||
|
||||
func (srv *HistorySrv) RouteQueryStateHistory(c *contextmodel.ReqContext) response.Response { |
||||
from := c.QueryInt64("from") |
||||
to := c.QueryInt64("to") |
||||
query := models.HistoryQuery{ |
||||
RuleUID: c.Query("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.
|
||||
} |
||||
frame, err := srv.hist.QueryStates(c.Req.Context(), query) |
||||
if err != nil { |
||||
return ErrResp(http.StatusInternalServerError, err, "") |
||||
} |
||||
return response.JSON(http.StatusOK, frame) |
||||
} |
@ -0,0 +1,40 @@ |
||||
/*Package api contains base API implementation of unified alerting |
||||
* |
||||
*Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
|
||||
* |
||||
*Do not manually edit these files, please find ngalert/api/swagger-codegen/ for commands on how to generate them. |
||||
*/ |
||||
package api |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
"github.com/grafana/grafana/pkg/api/response" |
||||
"github.com/grafana/grafana/pkg/api/routing" |
||||
"github.com/grafana/grafana/pkg/middleware" |
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" |
||||
"github.com/grafana/grafana/pkg/services/ngalert/metrics" |
||||
) |
||||
|
||||
type HistoryApi interface { |
||||
RouteGetStateHistory(*contextmodel.ReqContext) response.Response |
||||
} |
||||
|
||||
func (f *HistoryApiHandler) RouteGetStateHistory(ctx *contextmodel.ReqContext) response.Response { |
||||
return f.handleRouteGetStateHistory(ctx) |
||||
} |
||||
|
||||
func (api *API) RegisterHistoryApiEndpoints(srv HistoryApi, m *metrics.API) { |
||||
api.RouteRegister.Group("", func(group routing.RouteRegister) { |
||||
group.Get( |
||||
toMacaronPath("/api/v1/rules/history"), |
||||
api.authorize(http.MethodGet, "/api/v1/rules/history"), |
||||
metrics.Instrument( |
||||
http.MethodGet, |
||||
"/api/v1/rules/history", |
||||
srv.RouteGetStateHistory, |
||||
m, |
||||
), |
||||
) |
||||
}, middleware.ReqSignedIn) |
||||
} |
@ -0,0 +1,20 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"github.com/grafana/grafana/pkg/api/response" |
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" |
||||
) |
||||
|
||||
type HistoryApiHandler struct { |
||||
svc *HistorySrv |
||||
} |
||||
|
||||
func NewStateHistoryApi(svc *HistorySrv) *HistoryApiHandler { |
||||
return &HistoryApiHandler{ |
||||
svc: svc, |
||||
} |
||||
} |
||||
|
||||
func (f *HistoryApiHandler) handleRouteGetStateHistory(ctx *contextmodel.ReqContext) response.Response { |
||||
return f.svc.RouteQueryStateHistory(ctx) |
||||
} |
@ -0,0 +1,17 @@ |
||||
package definitions |
||||
|
||||
import "github.com/grafana/grafana-plugin-sdk-go/data" |
||||
|
||||
// swagger:route GET /api/v1/rules/history history RouteGetStateHistory
|
||||
//
|
||||
// Query state history.
|
||||
//
|
||||
// Produces:
|
||||
// - application/json
|
||||
//
|
||||
// Responses:
|
||||
// 200: StateHistory
|
||||
|
||||
type StateHistory struct { |
||||
Results *data.Frame `json:"results"` |
||||
} |
@ -1,12 +1,17 @@ |
||||
package models |
||||
|
||||
import "time" |
||||
import ( |
||||
"time" |
||||
|
||||
"github.com/grafana/grafana/pkg/services/user" |
||||
) |
||||
|
||||
// HistoryQuery represents a query for alert state history.
|
||||
type HistoryQuery struct { |
||||
RuleUID string |
||||
OrgID int64 |
||||
Labels map[string]string |
||||
From time.Time |
||||
To time.Time |
||||
RuleUID string |
||||
OrgID int64 |
||||
Labels map[string]string |
||||
From time.Time |
||||
To time.Time |
||||
SignedInUser *user.SignedInUser |
||||
} |
||||
|
Loading…
Reference in new issue