2021-04-13 12:58:34 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
2022-04-01 19:00:23 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
|
2021-04-13 12:58:34 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-04-01 19:00:23 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2021-04-13 12:58:34 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2021-04-19 13:26:04 -05:00
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
2021-04-21 14:44:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/eval"
|
2022-04-01 19:00:23 -05:00
|
|
|
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-04-13 12:58:34 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestingApiSrv struct {
|
|
|
|
*AlertingProxy
|
2022-06-27 16:40:44 -05:00
|
|
|
DatasourceCache datasources.CacheService
|
|
|
|
log log.Logger
|
|
|
|
accessControl accesscontrol.AccessControl
|
|
|
|
evaluator eval.Evaluator
|
2021-04-13 12:58:34 -05:00
|
|
|
}
|
|
|
|
|
2022-02-04 11:42:04 -06:00
|
|
|
func (srv TestingApiSrv) RouteTestGrafanaRuleConfig(c *models.ReqContext, body apimodels.TestRulePayload) response.Response {
|
|
|
|
if body.Type() != apimodels.GrafanaBackend || body.GrafanaManagedCondition == nil {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(backendTypeDoesNotMatchPayloadTypeError(apimodels.GrafanaBackend, body.Type().String()))
|
2021-04-13 12:58:34 -05:00
|
|
|
}
|
2022-04-01 19:00:23 -05:00
|
|
|
|
|
|
|
if !authorizeDatasourceAccessForRule(&ngmodels.AlertRule{Data: body.GrafanaManagedCondition.Data}, func(evaluator accesscontrol.Evaluator) bool {
|
|
|
|
return accesscontrol.HasAccess(srv.accessControl, c)(accesscontrol.ReqSignedIn, evaluator)
|
|
|
|
}) {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(fmt.Errorf("%w to query one or many data sources used by the rule", ErrAuthorization))
|
2022-04-01 19:00:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
evalCond := ngmodels.Condition{
|
|
|
|
Condition: body.GrafanaManagedCondition.Condition,
|
|
|
|
OrgID: c.SignedInUser.OrgId,
|
|
|
|
Data: body.GrafanaManagedCondition.Data,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateCondition(c.Req.Context(), evalCond, c.SignedInUser, c.SkipCache, srv.DatasourceCache); err != nil {
|
|
|
|
return ErrResp(http.StatusBadRequest, err, "invalid condition")
|
|
|
|
}
|
|
|
|
|
|
|
|
now := body.GrafanaManagedCondition.Now
|
|
|
|
if now.IsZero() {
|
|
|
|
now = timeNow()
|
|
|
|
}
|
|
|
|
|
2022-07-13 09:21:11 -05:00
|
|
|
evalResults := srv.evaluator.ConditionEval(c.Req.Context(), evalCond, now)
|
2022-04-01 19:00:23 -05:00
|
|
|
|
|
|
|
frame := evalResults.AsDataFrame()
|
|
|
|
return response.JSONStreaming(http.StatusOK, util.DynMap{
|
|
|
|
"instances": []*data.Frame{&frame},
|
|
|
|
})
|
2022-02-04 11:42:04 -06:00
|
|
|
}
|
2021-04-13 12:58:34 -05:00
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (srv TestingApiSrv) RouteTestRuleConfig(c *models.ReqContext, body apimodels.TestRulePayload, datasourceUID string) response.Response {
|
2021-04-13 12:58:34 -05:00
|
|
|
if body.Type() != apimodels.LoTexRulerBackend {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(backendTypeDoesNotMatchPayloadTypeError(apimodels.LoTexRulerBackend, body.Type().String()))
|
2021-04-13 12:58:34 -05:00
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
ds, err := getDatasourceByUID(c, srv.DatasourceCache, apimodels.LoTexRulerBackend)
|
2022-05-17 06:10:20 -05:00
|
|
|
if err != nil {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2022-05-17 06:10:20 -05:00
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
var path string
|
2022-05-17 06:10:20 -05:00
|
|
|
switch ds.Type {
|
|
|
|
case "loki":
|
|
|
|
path = "loki/api/v1/query"
|
|
|
|
case "prometheus":
|
|
|
|
path = "api/v1/query"
|
|
|
|
default:
|
2022-08-02 08:33:59 -05:00
|
|
|
// this should not happen because getDatasourceByUID would not return the data source
|
|
|
|
return errorToResponse(unexpectedDatasourceTypeError(ds.Type, "loki, prometheus"))
|
2021-04-13 12:58:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
t := timeNow()
|
|
|
|
queryURL, err := url.Parse(path)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(http.StatusInternalServerError, err, "failed to parse url")
|
2021-04-13 12:58:34 -05:00
|
|
|
}
|
|
|
|
params := queryURL.Query()
|
|
|
|
params.Set("query", body.Expr)
|
|
|
|
params.Set("time", strconv.FormatInt(t.Unix(), 10))
|
|
|
|
queryURL.RawQuery = params.Encode()
|
|
|
|
return srv.withReq(
|
|
|
|
c,
|
|
|
|
http.MethodGet,
|
|
|
|
queryURL,
|
|
|
|
nil,
|
2021-05-25 10:54:50 -05:00
|
|
|
instantQueryResultsExtractor,
|
2021-04-13 12:58:34 -05:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
}
|
2021-04-21 14:44:50 -05:00
|
|
|
|
|
|
|
func (srv TestingApiSrv) RouteEvalQueries(c *models.ReqContext, cmd apimodels.EvalQueriesPayload) response.Response {
|
|
|
|
now := cmd.Now
|
|
|
|
if now.IsZero() {
|
|
|
|
now = timeNow()
|
|
|
|
}
|
2021-04-28 03:31:51 -05:00
|
|
|
|
2022-04-01 19:00:23 -05:00
|
|
|
if !authorizeDatasourceAccessForRule(&ngmodels.AlertRule{Data: cmd.Data}, func(evaluator accesscontrol.Evaluator) bool {
|
|
|
|
return accesscontrol.HasAccess(srv.accessControl, c)(accesscontrol.ReqSignedIn, evaluator)
|
|
|
|
}) {
|
|
|
|
return ErrResp(http.StatusUnauthorized, fmt.Errorf("%w to query one or many data sources used by the rule", ErrAuthorization), "")
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:05:33 -06:00
|
|
|
if _, err := validateQueriesAndExpressions(c.Req.Context(), cmd.Data, c.SignedInUser, c.SkipCache, srv.DatasourceCache); err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(http.StatusBadRequest, err, "invalid queries or expressions")
|
2021-04-21 14:44:50 -05:00
|
|
|
}
|
|
|
|
|
2022-07-13 09:21:11 -05:00
|
|
|
evalResults, err := srv.evaluator.QueriesAndExpressionsEval(c.Req.Context(), c.SignedInUser.OrgId, cmd.Data, now)
|
2021-04-21 14:44:50 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(http.StatusBadRequest, err, "Failed to evaluate queries and expressions")
|
2021-04-21 14:44:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSONStreaming(http.StatusOK, evalResults)
|
|
|
|
}
|