mirror of https://github.com/grafana/grafana
Alerting: Cleanup and move legacy to a legacy file (#32803)
* Alerting: Cleanup and move legacy to a legacy file A quick cleanup of the ngalert/api directory, optimising for an easy removal of what is will be considered legacy at some point. A quick summary of what's done is: - Add a prefix `generated` prefix to files that are auto-generated by our swagger definitions. - Create a legacy file to place all the legacy API routes implementation and helpers. Deleting files that where no longer needed after this move. - Rename the `lotex` file to `lotex_ruler` - Adding a couple of comments here and there. With this, I hope to organise our code in this directory a bit better given there's a lot going on.pull/32286/head
parent
51e4106d1d
commit
c9e5088e8b
@ -1,118 +0,0 @@ |
||||
/*Package api contains base API implementation of unified alerting |
||||
* |
||||
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
|
||||
* |
||||
* Need to remove unused imports. |
||||
*/ |
||||
package api |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
"github.com/go-macaron/binding" |
||||
apimodels "github.com/grafana/alerting-api/pkg/api" |
||||
"github.com/grafana/grafana/pkg/api/response" |
||||
"github.com/grafana/grafana/pkg/api/routing" |
||||
"github.com/grafana/grafana/pkg/infra/log" |
||||
"github.com/grafana/grafana/pkg/middleware" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
type AlertmanagerApiService interface { |
||||
RouteCreateSilence(*models.ReqContext, apimodels.PostableSilence) response.Response |
||||
RouteDeleteAlertingConfig(*models.ReqContext) response.Response |
||||
RouteDeleteSilence(*models.ReqContext) response.Response |
||||
RouteGetAMAlertGroups(*models.ReqContext) response.Response |
||||
RouteGetAMAlerts(*models.ReqContext) response.Response |
||||
RouteGetAlertingConfig(*models.ReqContext) response.Response |
||||
RouteGetSilence(*models.ReqContext) response.Response |
||||
RouteGetSilences(*models.ReqContext) response.Response |
||||
RoutePostAMAlerts(*models.ReqContext, apimodels.PostableAlerts) response.Response |
||||
RoutePostAlertingConfig(*models.ReqContext, apimodels.PostableUserConfig) response.Response |
||||
} |
||||
|
||||
type AlertmanagerApiBase struct { |
||||
log log.Logger |
||||
} |
||||
|
||||
func (api *API) RegisterAlertmanagerApiEndpoints(srv AlertmanagerApiService) { |
||||
api.RouteRegister.Group("", func(group routing.RouteRegister) { |
||||
group.Post(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/silences"), binding.Bind(apimodels.PostableSilence{}), routing.Wrap(srv.RouteCreateSilence)) |
||||
group.Delete(toMacaronPath("/api/alertmanager/{Recipient}/config/api/v1/alerts"), routing.Wrap(srv.RouteDeleteAlertingConfig)) |
||||
group.Delete(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/silence/{SilenceId}"), routing.Wrap(srv.RouteDeleteSilence)) |
||||
group.Get(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/alerts/groups"), routing.Wrap(srv.RouteGetAMAlertGroups)) |
||||
group.Get(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/alerts"), routing.Wrap(srv.RouteGetAMAlerts)) |
||||
group.Get(toMacaronPath("/api/alertmanager/{Recipient}/config/api/v1/alerts"), routing.Wrap(srv.RouteGetAlertingConfig)) |
||||
group.Get(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/silence/{SilenceId}"), routing.Wrap(srv.RouteGetSilence)) |
||||
group.Get(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/silences"), routing.Wrap(srv.RouteGetSilences)) |
||||
group.Post(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/alerts"), binding.Bind(apimodels.PostableAlerts{}), routing.Wrap(srv.RoutePostAMAlerts)) |
||||
group.Post(toMacaronPath("/api/alertmanager/{Recipient}/config/api/v1/alerts"), binding.Bind(apimodels.PostableUserConfig{}), routing.Wrap(srv.RoutePostAlertingConfig)) |
||||
}, middleware.ReqSignedIn) |
||||
} |
||||
|
||||
func (base AlertmanagerApiBase) RouteCreateSilence(c *models.ReqContext, body apimodels.PostableSilence) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteCreateSilence: ", "Recipient", recipient) |
||||
base.log.Info("RouteCreateSilence: ", "body", body) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base AlertmanagerApiBase) RouteDeleteAlertingConfig(c *models.ReqContext) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteDeleteAlertingConfig: ", "Recipient", recipient) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base AlertmanagerApiBase) RouteDeleteSilence(c *models.ReqContext) response.Response { |
||||
silenceId := c.Params(":SilenceId") |
||||
base.log.Info("RouteDeleteSilence: ", "SilenceId", silenceId) |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteDeleteSilence: ", "Recipient", recipient) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base AlertmanagerApiBase) RouteGetAMAlertGroups(c *models.ReqContext) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteGetAMAlertGroups: ", "Recipient", recipient) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base AlertmanagerApiBase) RouteGetAMAlerts(c *models.ReqContext) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteGetAMAlerts: ", "Recipient", recipient) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base AlertmanagerApiBase) RouteGetAlertingConfig(c *models.ReqContext) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteGetAlertingConfig: ", "Recipient", recipient) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base AlertmanagerApiBase) RouteGetSilence(c *models.ReqContext) response.Response { |
||||
silenceId := c.Params(":SilenceId") |
||||
base.log.Info("RouteGetSilence: ", "SilenceId", silenceId) |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteGetSilence: ", "Recipient", recipient) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base AlertmanagerApiBase) RouteGetSilences(c *models.ReqContext) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteGetSilences: ", "Recipient", recipient) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base AlertmanagerApiBase) RoutePostAMAlerts(c *models.ReqContext, body apimodels.PostableAlerts) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RoutePostAMAlerts: ", "Recipient", recipient) |
||||
base.log.Info("RoutePostAMAlerts: ", "body", body) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base AlertmanagerApiBase) RoutePostAlertingConfig(c *models.ReqContext, body apimodels.PostableUserConfig) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RoutePostAlertingConfig: ", "Recipient", recipient) |
||||
base.log.Info("RoutePostAlertingConfig: ", "body", body) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
@ -1,94 +0,0 @@ |
||||
/*Package api contains base API implementation of unified alerting |
||||
* |
||||
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
|
||||
* |
||||
* Need to remove unused imports. |
||||
*/ |
||||
package api |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
"github.com/go-macaron/binding" |
||||
apimodels "github.com/grafana/alerting-api/pkg/api" |
||||
"github.com/grafana/grafana/pkg/api/response" |
||||
"github.com/grafana/grafana/pkg/api/routing" |
||||
"github.com/grafana/grafana/pkg/infra/log" |
||||
"github.com/grafana/grafana/pkg/middleware" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
type RulerApiService interface { |
||||
RouteDeleteNamespaceRulesConfig(*models.ReqContext) response.Response |
||||
RouteDeleteRuleGroupConfig(*models.ReqContext) response.Response |
||||
RouteGetNamespaceRulesConfig(*models.ReqContext) response.Response |
||||
RouteGetRulegGroupConfig(*models.ReqContext) response.Response |
||||
RouteGetRulesConfig(*models.ReqContext) response.Response |
||||
RoutePostNameRulesConfig(*models.ReqContext, apimodels.PostableRuleGroupConfig) response.Response |
||||
} |
||||
|
||||
type RulerApiBase struct { |
||||
log log.Logger |
||||
} |
||||
|
||||
func (api *API) RegisterRulerApiEndpoints(srv RulerApiService) { |
||||
api.RouteRegister.Group("", func(group routing.RouteRegister) { |
||||
group.Delete(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules/{Namespace}"), routing.Wrap(srv.RouteDeleteNamespaceRulesConfig)) |
||||
group.Delete(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules/{Namespace}/{Groupname}"), routing.Wrap(srv.RouteDeleteRuleGroupConfig)) |
||||
group.Get(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules/{Namespace}"), routing.Wrap(srv.RouteGetNamespaceRulesConfig)) |
||||
group.Get(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules/{Namespace}/{Groupname}"), routing.Wrap(srv.RouteGetRulegGroupConfig)) |
||||
group.Get(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules"), routing.Wrap(srv.RouteGetRulesConfig)) |
||||
group.Post(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules/{Namespace}"), binding.Bind(apimodels.PostableRuleGroupConfig{}), routing.Wrap(srv.RoutePostNameRulesConfig)) |
||||
}, middleware.ReqSignedIn) |
||||
} |
||||
|
||||
func (base RulerApiBase) RouteDeleteNamespaceRulesConfig(c *models.ReqContext) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteDeleteNamespaceRulesConfig: ", "Recipient", recipient) |
||||
namespace := c.Params(":Namespace") |
||||
base.log.Info("RouteDeleteNamespaceRulesConfig: ", "Namespace", namespace) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base RulerApiBase) RouteDeleteRuleGroupConfig(c *models.ReqContext) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteDeleteRuleGroupConfig: ", "Recipient", recipient) |
||||
namespace := c.Params(":Namespace") |
||||
base.log.Info("RouteDeleteRuleGroupConfig: ", "Namespace", namespace) |
||||
groupname := c.Params(":Groupname") |
||||
base.log.Info("RouteDeleteRuleGroupConfig: ", "Groupname", groupname) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base RulerApiBase) RouteGetNamespaceRulesConfig(c *models.ReqContext) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteGetNamespaceRulesConfig: ", "Recipient", recipient) |
||||
namespace := c.Params(":Namespace") |
||||
base.log.Info("RouteGetNamespaceRulesConfig: ", "Namespace", namespace) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base RulerApiBase) RouteGetRulegGroupConfig(c *models.ReqContext) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteGetRulegGroupConfig: ", "Recipient", recipient) |
||||
namespace := c.Params(":Namespace") |
||||
base.log.Info("RouteGetRulegGroupConfig: ", "Namespace", namespace) |
||||
groupname := c.Params(":Groupname") |
||||
base.log.Info("RouteGetRulegGroupConfig: ", "Groupname", groupname) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base RulerApiBase) RouteGetRulesConfig(c *models.ReqContext) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RouteGetRulesConfig: ", "Recipient", recipient) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
||||
|
||||
func (base RulerApiBase) RoutePostNameRulesConfig(c *models.ReqContext, body apimodels.PostableRuleGroupConfig) response.Response { |
||||
recipient := c.Params(":Recipient") |
||||
base.log.Info("RoutePostNameRulesConfig: ", "Recipient", recipient) |
||||
namespace := c.Params(":Namespace") |
||||
base.log.Info("RoutePostNameRulesConfig: ", "Namespace", namespace) |
||||
base.log.Info("RoutePostNameRulesConfig: ", "body", body) |
||||
return response.Error(http.StatusNotImplemented, "", nil) |
||||
} |
@ -0,0 +1,45 @@ |
||||
/*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 ( |
||||
"github.com/go-macaron/binding" |
||||
|
||||
apimodels "github.com/grafana/alerting-api/pkg/api" |
||||
"github.com/grafana/grafana/pkg/api/response" |
||||
"github.com/grafana/grafana/pkg/api/routing" |
||||
"github.com/grafana/grafana/pkg/middleware" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
type AlertmanagerApiService interface { |
||||
RouteCreateSilence(*models.ReqContext, apimodels.PostableSilence) response.Response |
||||
RouteDeleteAlertingConfig(*models.ReqContext) response.Response |
||||
RouteDeleteSilence(*models.ReqContext) response.Response |
||||
RouteGetAMAlertGroups(*models.ReqContext) response.Response |
||||
RouteGetAMAlerts(*models.ReqContext) response.Response |
||||
RouteGetAlertingConfig(*models.ReqContext) response.Response |
||||
RouteGetSilence(*models.ReqContext) response.Response |
||||
RouteGetSilences(*models.ReqContext) response.Response |
||||
RoutePostAMAlerts(*models.ReqContext, apimodels.PostableAlerts) response.Response |
||||
RoutePostAlertingConfig(*models.ReqContext, apimodels.PostableUserConfig) response.Response |
||||
} |
||||
|
||||
func (api *API) RegisterAlertmanagerApiEndpoints(srv AlertmanagerApiService) { |
||||
api.RouteRegister.Group("", func(group routing.RouteRegister) { |
||||
group.Post(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/silences"), binding.Bind(apimodels.PostableSilence{}), routing.Wrap(srv.RouteCreateSilence)) |
||||
group.Delete(toMacaronPath("/api/alertmanager/{Recipient}/config/api/v1/alerts"), routing.Wrap(srv.RouteDeleteAlertingConfig)) |
||||
group.Delete(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/silence/{SilenceId}"), routing.Wrap(srv.RouteDeleteSilence)) |
||||
group.Get(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/alerts/groups"), routing.Wrap(srv.RouteGetAMAlertGroups)) |
||||
group.Get(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/alerts"), routing.Wrap(srv.RouteGetAMAlerts)) |
||||
group.Get(toMacaronPath("/api/alertmanager/{Recipient}/config/api/v1/alerts"), routing.Wrap(srv.RouteGetAlertingConfig)) |
||||
group.Get(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/silence/{SilenceId}"), routing.Wrap(srv.RouteGetSilence)) |
||||
group.Get(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/silences"), routing.Wrap(srv.RouteGetSilences)) |
||||
group.Post(toMacaronPath("/api/alertmanager/{Recipient}/api/v2/alerts"), binding.Bind(apimodels.PostableAlerts{}), routing.Wrap(srv.RoutePostAMAlerts)) |
||||
group.Post(toMacaronPath("/api/alertmanager/{Recipient}/config/api/v1/alerts"), binding.Bind(apimodels.PostableUserConfig{}), routing.Wrap(srv.RoutePostAlertingConfig)) |
||||
}, middleware.ReqSignedIn) |
||||
} |
@ -0,0 +1,37 @@ |
||||
/*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 ( |
||||
"github.com/go-macaron/binding" |
||||
|
||||
apimodels "github.com/grafana/alerting-api/pkg/api" |
||||
"github.com/grafana/grafana/pkg/api/response" |
||||
"github.com/grafana/grafana/pkg/api/routing" |
||||
"github.com/grafana/grafana/pkg/middleware" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
type RulerApiService interface { |
||||
RouteDeleteNamespaceRulesConfig(*models.ReqContext) response.Response |
||||
RouteDeleteRuleGroupConfig(*models.ReqContext) response.Response |
||||
RouteGetNamespaceRulesConfig(*models.ReqContext) response.Response |
||||
RouteGetRulegGroupConfig(*models.ReqContext) response.Response |
||||
RouteGetRulesConfig(*models.ReqContext) response.Response |
||||
RoutePostNameRulesConfig(*models.ReqContext, apimodels.PostableRuleGroupConfig) response.Response |
||||
} |
||||
|
||||
func (api *API) RegisterRulerApiEndpoints(srv RulerApiService) { |
||||
api.RouteRegister.Group("", func(group routing.RouteRegister) { |
||||
group.Delete(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules/{Namespace}"), routing.Wrap(srv.RouteDeleteNamespaceRulesConfig)) |
||||
group.Delete(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules/{Namespace}/{Groupname}"), routing.Wrap(srv.RouteDeleteRuleGroupConfig)) |
||||
group.Get(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules/{Namespace}"), routing.Wrap(srv.RouteGetNamespaceRulesConfig)) |
||||
group.Get(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules/{Namespace}/{Groupname}"), routing.Wrap(srv.RouteGetRulegGroupConfig)) |
||||
group.Get(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules"), routing.Wrap(srv.RouteGetRulesConfig)) |
||||
group.Post(toMacaronPath("/api/ruler/{Recipient}/api/v1/rules/{Namespace}"), binding.Bind(apimodels.PostableRuleGroupConfig{}), routing.Wrap(srv.RoutePostNameRulesConfig)) |
||||
}, middleware.ReqSignedIn) |
||||
} |
@ -1,19 +0,0 @@ |
||||
package api |
||||
|
||||
import ( |
||||
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models" |
||||
|
||||
"github.com/grafana/grafana/pkg/api/response" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
// listAlertInstancesEndpoint handles GET /api/alert-instances.
|
||||
func (api *API) listAlertInstancesEndpoint(c *models.ReqContext) response.Response { |
||||
cmd := ngmodels.ListAlertInstancesQuery{DefinitionOrgID: c.SignedInUser.OrgId} |
||||
|
||||
if err := api.Store.ListAlertInstances(&cmd); err != nil { |
||||
return response.Error(500, "Failed to list alert instances", err) |
||||
} |
||||
|
||||
return response.JSON(200, cmd.Result) |
||||
} |
@ -0,0 +1,274 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data" |
||||
"github.com/grafana/grafana/pkg/api/response" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/services/ngalert/eval" |
||||
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models" |
||||
"github.com/grafana/grafana/pkg/util" |
||||
) |
||||
|
||||
// listAlertInstancesEndpoint handles GET /api/alert-instances.
|
||||
func (api *API) listAlertInstancesEndpoint(c *models.ReqContext) response.Response { |
||||
cmd := ngmodels.ListAlertInstancesQuery{DefinitionOrgID: c.SignedInUser.OrgId} |
||||
|
||||
if err := api.Store.ListAlertInstances(&cmd); err != nil { |
||||
return response.Error(500, "Failed to list alert instances", err) |
||||
} |
||||
|
||||
return response.JSON(200, cmd.Result) |
||||
} |
||||
|
||||
// conditionEvalEndpoint handles POST /api/alert-definitions/eval.
|
||||
func (api *API) conditionEvalEndpoint(c *models.ReqContext, cmd ngmodels.EvalAlertConditionCommand) response.Response { |
||||
evalCond := ngmodels.Condition{ |
||||
Condition: cmd.Condition, |
||||
OrgID: c.SignedInUser.OrgId, |
||||
Data: cmd.Data, |
||||
} |
||||
if err := api.validateCondition(evalCond, c.SignedInUser, c.SkipCache); err != nil { |
||||
return response.Error(400, "invalid condition", err) |
||||
} |
||||
|
||||
now := cmd.Now |
||||
if now.IsZero() { |
||||
now = timeNow() |
||||
} |
||||
|
||||
evaluator := eval.Evaluator{Cfg: api.Cfg} |
||||
evalResults, err := evaluator.ConditionEval(&evalCond, timeNow(), api.DataService) |
||||
if err != nil { |
||||
return response.Error(400, "Failed to evaluate conditions", err) |
||||
} |
||||
|
||||
frame := evalResults.AsDataFrame() |
||||
|
||||
return response.JSONStreaming(200, util.DynMap{ |
||||
"instances": []*data.Frame{&frame}, |
||||
}) |
||||
} |
||||
|
||||
// alertDefinitionEvalEndpoint handles GET /api/alert-definitions/eval/:alertDefinitionUID.
|
||||
func (api *API) alertDefinitionEvalEndpoint(c *models.ReqContext) response.Response { |
||||
alertDefinitionUID := c.Params(":alertDefinitionUID") |
||||
|
||||
condition, err := api.LoadAlertCondition(alertDefinitionUID, c.SignedInUser.OrgId) |
||||
if err != nil { |
||||
return response.Error(400, "Failed to load alert definition conditions", err) |
||||
} |
||||
|
||||
if err := api.validateCondition(*condition, c.SignedInUser, c.SkipCache); err != nil { |
||||
return response.Error(400, "invalid condition", err) |
||||
} |
||||
|
||||
evaluator := eval.Evaluator{Cfg: api.Cfg} |
||||
evalResults, err := evaluator.ConditionEval(condition, timeNow(), api.DataService) |
||||
if err != nil { |
||||
return response.Error(400, "Failed to evaluate alert", err) |
||||
} |
||||
frame := evalResults.AsDataFrame() |
||||
|
||||
return response.JSONStreaming(200, util.DynMap{ |
||||
"instances": []*data.Frame{&frame}, |
||||
}) |
||||
} |
||||
|
||||
// getAlertDefinitionEndpoint handles GET /api/alert-definitions/:alertDefinitionUID.
|
||||
func (api *API) getAlertDefinitionEndpoint(c *models.ReqContext) response.Response { |
||||
alertDefinitionUID := c.Params(":alertDefinitionUID") |
||||
|
||||
query := ngmodels.GetAlertDefinitionByUIDQuery{ |
||||
UID: alertDefinitionUID, |
||||
OrgID: c.SignedInUser.OrgId, |
||||
} |
||||
|
||||
if err := api.Store.GetAlertDefinitionByUID(&query); err != nil { |
||||
return response.Error(500, "Failed to get alert definition", err) |
||||
} |
||||
|
||||
return response.JSON(200, &query.Result) |
||||
} |
||||
|
||||
// deleteAlertDefinitionEndpoint handles DELETE /api/alert-definitions/:alertDefinitionUID.
|
||||
func (api *API) deleteAlertDefinitionEndpoint(c *models.ReqContext) response.Response { |
||||
alertDefinitionUID := c.Params(":alertDefinitionUID") |
||||
|
||||
cmd := ngmodels.DeleteAlertDefinitionByUIDCommand{ |
||||
UID: alertDefinitionUID, |
||||
OrgID: c.SignedInUser.OrgId, |
||||
} |
||||
|
||||
if err := api.Store.DeleteAlertDefinitionByUID(&cmd); err != nil { |
||||
return response.Error(500, "Failed to delete alert definition", err) |
||||
} |
||||
|
||||
return response.Success("Alert definition deleted") |
||||
} |
||||
|
||||
// updateAlertDefinitionEndpoint handles PUT /api/alert-definitions/:alertDefinitionUID.
|
||||
func (api *API) updateAlertDefinitionEndpoint(c *models.ReqContext, cmd ngmodels.UpdateAlertDefinitionCommand) response.Response { |
||||
cmd.UID = c.Params(":alertDefinitionUID") |
||||
cmd.OrgID = c.SignedInUser.OrgId |
||||
|
||||
evalCond := ngmodels.Condition{ |
||||
Condition: cmd.Condition, |
||||
OrgID: c.SignedInUser.OrgId, |
||||
Data: cmd.Data, |
||||
} |
||||
if err := api.validateCondition(evalCond, c.SignedInUser, c.SkipCache); err != nil { |
||||
return response.Error(400, "invalid condition", err) |
||||
} |
||||
|
||||
if err := api.Store.UpdateAlertDefinition(&cmd); err != nil { |
||||
return response.Error(500, "Failed to update alert definition", err) |
||||
} |
||||
|
||||
return response.JSON(200, cmd.Result) |
||||
} |
||||
|
||||
// createAlertDefinitionEndpoint handles POST /api/alert-definitions.
|
||||
func (api *API) createAlertDefinitionEndpoint(c *models.ReqContext, cmd ngmodels.SaveAlertDefinitionCommand) response.Response { |
||||
cmd.OrgID = c.SignedInUser.OrgId |
||||
|
||||
evalCond := ngmodels.Condition{ |
||||
Condition: cmd.Condition, |
||||
OrgID: c.SignedInUser.OrgId, |
||||
Data: cmd.Data, |
||||
} |
||||
if err := api.validateCondition(evalCond, c.SignedInUser, c.SkipCache); err != nil { |
||||
return response.Error(400, "invalid condition", err) |
||||
} |
||||
|
||||
if err := api.Store.SaveAlertDefinition(&cmd); err != nil { |
||||
return response.Error(500, "Failed to create alert definition", err) |
||||
} |
||||
|
||||
return response.JSON(200, cmd.Result) |
||||
} |
||||
|
||||
// listAlertDefinitions handles GET /api/alert-definitions.
|
||||
func (api *API) listAlertDefinitions(c *models.ReqContext) response.Response { |
||||
query := ngmodels.ListAlertDefinitionsQuery{OrgID: c.SignedInUser.OrgId} |
||||
|
||||
if err := api.Store.GetOrgAlertDefinitions(&query); err != nil { |
||||
return response.Error(500, "Failed to list alert definitions", err) |
||||
} |
||||
|
||||
return response.JSON(200, util.DynMap{"results": query.Result}) |
||||
} |
||||
|
||||
func (api *API) pauseScheduler() response.Response { |
||||
err := api.Schedule.Pause() |
||||
if err != nil { |
||||
return response.Error(500, "Failed to pause scheduler", err) |
||||
} |
||||
return response.JSON(200, util.DynMap{"message": "alert definition scheduler paused"}) |
||||
} |
||||
|
||||
func (api *API) unpauseScheduler() response.Response { |
||||
err := api.Schedule.Unpause() |
||||
if err != nil { |
||||
return response.Error(500, "Failed to unpause scheduler", err) |
||||
} |
||||
return response.JSON(200, util.DynMap{"message": "alert definition scheduler unpaused"}) |
||||
} |
||||
|
||||
// alertDefinitionPauseEndpoint handles POST /api/alert-definitions/pause.
|
||||
func (api *API) alertDefinitionPauseEndpoint(c *models.ReqContext, cmd ngmodels.UpdateAlertDefinitionPausedCommand) response.Response { |
||||
cmd.OrgID = c.SignedInUser.OrgId |
||||
cmd.Paused = true |
||||
|
||||
err := api.Store.UpdateAlertDefinitionPaused(&cmd) |
||||
if err != nil { |
||||
return response.Error(500, "Failed to pause alert definition", err) |
||||
} |
||||
return response.JSON(200, util.DynMap{"message": fmt.Sprintf("%d alert definitions paused", cmd.ResultCount)}) |
||||
} |
||||
|
||||
// alertDefinitionUnpauseEndpoint handles POST /api/alert-definitions/unpause.
|
||||
func (api *API) alertDefinitionUnpauseEndpoint(c *models.ReqContext, cmd ngmodels.UpdateAlertDefinitionPausedCommand) response.Response { |
||||
cmd.OrgID = c.SignedInUser.OrgId |
||||
cmd.Paused = false |
||||
|
||||
err := api.Store.UpdateAlertDefinitionPaused(&cmd) |
||||
if err != nil { |
||||
return response.Error(500, "Failed to unpause alert definition", err) |
||||
} |
||||
return response.JSON(200, util.DynMap{"message": fmt.Sprintf("%d alert definitions unpaused", cmd.ResultCount)}) |
||||
} |
||||
|
||||
// LoadAlertCondition returns a Condition object for the given alertDefinitionID.
|
||||
func (api *API) LoadAlertCondition(alertDefinitionUID string, orgID int64) (*ngmodels.Condition, error) { |
||||
q := ngmodels.GetAlertDefinitionByUIDQuery{UID: alertDefinitionUID, OrgID: orgID} |
||||
if err := api.Store.GetAlertDefinitionByUID(&q); err != nil { |
||||
return nil, err |
||||
} |
||||
alertDefinition := q.Result |
||||
|
||||
err := api.Store.ValidateAlertDefinition(alertDefinition, true) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return &ngmodels.Condition{ |
||||
Condition: alertDefinition.Condition, |
||||
OrgID: alertDefinition.OrgID, |
||||
Data: alertDefinition.Data, |
||||
}, nil |
||||
} |
||||
|
||||
func (api *API) validateCondition(c ngmodels.Condition, user *models.SignedInUser, skipCache bool) error { |
||||
var refID string |
||||
|
||||
if len(c.Data) == 0 { |
||||
return nil |
||||
} |
||||
|
||||
for _, query := range c.Data { |
||||
if c.Condition == query.RefID { |
||||
refID = c.Condition |
||||
} |
||||
|
||||
datasourceUID, err := query.GetDatasource() |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
isExpression, err := query.IsExpression() |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if isExpression { |
||||
continue |
||||
} |
||||
|
||||
_, err = api.DatasourceCache.GetDatasourceByUID(datasourceUID, user, skipCache) |
||||
if err != nil { |
||||
return fmt.Errorf("failed to get datasource: %s: %w", datasourceUID, err) |
||||
} |
||||
} |
||||
|
||||
if refID == "" { |
||||
return fmt.Errorf("condition %s not found in any query or expression", c.Condition) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (api *API) validateOrgAlertDefinition(c *models.ReqContext) { |
||||
uid := c.ParamsEscape(":alertDefinitionUID") |
||||
|
||||
if uid == "" { |
||||
c.JsonApiErr(403, "Permission denied", nil) |
||||
return |
||||
} |
||||
|
||||
query := ngmodels.GetAlertDefinitionByUIDQuery{UID: uid, OrgID: c.SignedInUser.OrgId} |
||||
|
||||
if err := api.Store.GetAlertDefinitionByUID(&query); err != nil { |
||||
c.JsonApiErr(404, "Alert definition not found", nil) |
||||
return |
||||
} |
||||
} |
@ -1,23 +0,0 @@ |
||||
package api |
||||
|
||||
import ( |
||||
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models" |
||||
|
||||
"github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
func (api *API) validateOrgAlertDefinition(c *models.ReqContext) { |
||||
uid := c.ParamsEscape(":alertDefinitionUID") |
||||
|
||||
if uid == "" { |
||||
c.JsonApiErr(403, "Permission denied", nil) |
||||
return |
||||
} |
||||
|
||||
query := ngmodels.GetAlertDefinitionByUIDQuery{UID: uid, OrgID: c.SignedInUser.OrgId} |
||||
|
||||
if err := api.Store.GetAlertDefinitionByUID(&query); err != nil { |
||||
c.JsonApiErr(404, "Alert definition not found", nil) |
||||
return |
||||
} |
||||
} |
@ -1,6 +1,6 @@ |
||||
/*Package api contains base API implementation of unified alerting |
||||
* |
||||
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) |
||||
*Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) |
||||
* |
||||
* Need to remove unused imports. |
||||
*Do not manually edit these files, please find ngalert/api/swagger-codegen/ for commands on how to generate them. |
||||
*/ |
||||
|
Loading…
Reference in new issue