mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
chore: move alert-related models (#61716)
* chore: move alert notification models into the alerting service (alerting/models)
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/alerting"
|
||||
alertmodels "github.com/grafana/grafana/pkg/services/alerting/models"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/guardian"
|
||||
@@ -28,7 +29,7 @@ func (hs *HTTPServer) ValidateOrgAlert(c *models.ReqContext) {
|
||||
c.JsonApiErr(http.StatusBadRequest, "alertId is invalid", nil)
|
||||
return
|
||||
}
|
||||
query := models.GetAlertByIdQuery{Id: id}
|
||||
query := alertmodels.GetAlertByIdQuery{Id: id}
|
||||
|
||||
if err := hs.AlertEngine.AlertStore.GetAlertById(c.Req.Context(), &query); err != nil {
|
||||
c.JsonApiErr(404, "Alert not found", nil)
|
||||
@@ -57,7 +58,7 @@ func (hs *HTTPServer) GetAlertStatesForDashboard(c *models.ReqContext) response.
|
||||
return response.Error(400, "Missing query parameter dashboardId", nil)
|
||||
}
|
||||
|
||||
query := models.GetAlertStatesForDashboardQuery{
|
||||
query := alertmodels.GetAlertStatesForDashboardQuery{
|
||||
OrgId: c.OrgID,
|
||||
DashboardId: c.QueryInt64("dashboardId"),
|
||||
}
|
||||
@@ -125,11 +126,11 @@ func (hs *HTTPServer) GetAlerts(c *models.ReqContext) response.Response {
|
||||
|
||||
// if we didn't find any dashboards, return empty result
|
||||
if len(dashboardIDs) == 0 {
|
||||
return response.JSON(http.StatusOK, []*models.AlertListItemDTO{})
|
||||
return response.JSON(http.StatusOK, []*alertmodels.AlertListItemDTO{})
|
||||
}
|
||||
}
|
||||
|
||||
query := models.GetAlertsQuery{
|
||||
query := alertmodels.GetAlertsQuery{
|
||||
OrgId: c.OrgID,
|
||||
DashboardIDs: dashboardIDs,
|
||||
PanelId: c.QueryInt64("panelId"),
|
||||
@@ -223,7 +224,7 @@ func (hs *HTTPServer) GetAlert(c *models.ReqContext) response.Response {
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "alertId is invalid", err)
|
||||
}
|
||||
query := models.GetAlertByIdQuery{Id: id}
|
||||
query := alertmodels.GetAlertByIdQuery{Id: id}
|
||||
|
||||
if err := hs.AlertEngine.AlertStore.GetAlertById(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, "List alerts failed", err)
|
||||
@@ -295,8 +296,8 @@ func (hs *HTTPServer) GetAlertNotifications(c *models.ReqContext) response.Respo
|
||||
return response.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) getAlertNotificationsInternal(c *models.ReqContext) ([]*models.AlertNotification, error) {
|
||||
query := &models.GetAllAlertNotificationsQuery{OrgId: c.OrgID}
|
||||
func (hs *HTTPServer) getAlertNotificationsInternal(c *models.ReqContext) ([]*alertmodels.AlertNotification, error) {
|
||||
query := &alertmodels.GetAllAlertNotificationsQuery{OrgId: c.OrgID}
|
||||
|
||||
if err := hs.AlertNotificationService.GetAllAlertNotifications(c.Req.Context(), query); err != nil {
|
||||
return nil, err
|
||||
@@ -322,7 +323,7 @@ func (hs *HTTPServer) GetAlertNotificationByID(c *models.ReqContext) response.Re
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "notificationId is invalid", err)
|
||||
}
|
||||
query := &models.GetAlertNotificationsQuery{
|
||||
query := &alertmodels.GetAlertNotificationsQuery{
|
||||
OrgId: c.OrgID,
|
||||
Id: notificationId,
|
||||
}
|
||||
@@ -355,7 +356,7 @@ func (hs *HTTPServer) GetAlertNotificationByID(c *models.ReqContext) response.Re
|
||||
// 404: notFoundError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) GetAlertNotificationByUID(c *models.ReqContext) response.Response {
|
||||
query := &models.GetAlertNotificationsWithUidQuery{
|
||||
query := &alertmodels.GetAlertNotificationsWithUidQuery{
|
||||
OrgId: c.OrgID,
|
||||
Uid: web.Params(c.Req)[":uid"],
|
||||
}
|
||||
@@ -388,14 +389,14 @@ func (hs *HTTPServer) GetAlertNotificationByUID(c *models.ReqContext) response.R
|
||||
// 409: conflictError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) CreateAlertNotification(c *models.ReqContext) response.Response {
|
||||
cmd := models.CreateAlertNotificationCommand{}
|
||||
cmd := alertmodels.CreateAlertNotificationCommand{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
cmd.OrgId = c.OrgID
|
||||
|
||||
if err := hs.AlertNotificationService.CreateAlertNotificationCommand(c.Req.Context(), &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrAlertNotificationWithSameNameExists) || errors.Is(err, models.ErrAlertNotificationWithSameUIDExists) {
|
||||
if errors.Is(err, alertmodels.ErrAlertNotificationWithSameNameExists) || errors.Is(err, alertmodels.ErrAlertNotificationWithSameUIDExists) {
|
||||
return response.Error(409, "Failed to create alert notification", err)
|
||||
}
|
||||
var alertingErr alerting.ValidationError
|
||||
@@ -421,7 +422,7 @@ func (hs *HTTPServer) CreateAlertNotification(c *models.ReqContext) response.Res
|
||||
// 404: notFoundError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) UpdateAlertNotification(c *models.ReqContext) response.Response {
|
||||
cmd := models.UpdateAlertNotificationCommand{}
|
||||
cmd := alertmodels.UpdateAlertNotificationCommand{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
@@ -433,7 +434,7 @@ func (hs *HTTPServer) UpdateAlertNotification(c *models.ReqContext) response.Res
|
||||
}
|
||||
|
||||
if err := hs.AlertNotificationService.UpdateAlertNotification(c.Req.Context(), &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||
if errors.Is(err, alertmodels.ErrAlertNotificationNotFound) {
|
||||
return response.Error(404, err.Error(), err)
|
||||
}
|
||||
var alertingErr alerting.ValidationError
|
||||
@@ -443,7 +444,7 @@ func (hs *HTTPServer) UpdateAlertNotification(c *models.ReqContext) response.Res
|
||||
return response.Error(500, "Failed to update alert notification", err)
|
||||
}
|
||||
|
||||
query := models.GetAlertNotificationsQuery{
|
||||
query := alertmodels.GetAlertNotificationsQuery{
|
||||
OrgId: c.OrgID,
|
||||
Id: cmd.Id,
|
||||
}
|
||||
@@ -468,7 +469,7 @@ func (hs *HTTPServer) UpdateAlertNotification(c *models.ReqContext) response.Res
|
||||
// 404: notFoundError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) UpdateAlertNotificationByUID(c *models.ReqContext) response.Response {
|
||||
cmd := models.UpdateAlertNotificationWithUidCommand{}
|
||||
cmd := alertmodels.UpdateAlertNotificationWithUidCommand{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
@@ -481,13 +482,13 @@ func (hs *HTTPServer) UpdateAlertNotificationByUID(c *models.ReqContext) respons
|
||||
}
|
||||
|
||||
if err := hs.AlertNotificationService.UpdateAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||
if errors.Is(err, alertmodels.ErrAlertNotificationNotFound) {
|
||||
return response.Error(404, err.Error(), nil)
|
||||
}
|
||||
return response.Error(500, "Failed to update alert notification", err)
|
||||
}
|
||||
|
||||
query := models.GetAlertNotificationsWithUidQuery{
|
||||
query := alertmodels.GetAlertNotificationsWithUidQuery{
|
||||
OrgId: cmd.OrgId,
|
||||
Uid: cmd.Uid,
|
||||
}
|
||||
@@ -499,12 +500,12 @@ func (hs *HTTPServer) UpdateAlertNotificationByUID(c *models.ReqContext) respons
|
||||
return response.JSON(http.StatusOK, dtos.NewAlertNotification(query.Result))
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) fillWithSecureSettingsData(ctx context.Context, cmd *models.UpdateAlertNotificationCommand) error {
|
||||
func (hs *HTTPServer) fillWithSecureSettingsData(ctx context.Context, cmd *alertmodels.UpdateAlertNotificationCommand) error {
|
||||
if len(cmd.SecureSettings) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
query := &models.GetAlertNotificationsQuery{
|
||||
query := &alertmodels.GetAlertNotificationsQuery{
|
||||
OrgId: cmd.OrgId,
|
||||
Id: cmd.Id,
|
||||
}
|
||||
@@ -527,12 +528,12 @@ func (hs *HTTPServer) fillWithSecureSettingsData(ctx context.Context, cmd *model
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) fillWithSecureSettingsDataByUID(ctx context.Context, cmd *models.UpdateAlertNotificationWithUidCommand) error {
|
||||
func (hs *HTTPServer) fillWithSecureSettingsDataByUID(ctx context.Context, cmd *alertmodels.UpdateAlertNotificationWithUidCommand) error {
|
||||
if len(cmd.SecureSettings) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
query := &models.GetAlertNotificationsWithUidQuery{
|
||||
query := &alertmodels.GetAlertNotificationsWithUidQuery{
|
||||
OrgId: cmd.OrgId,
|
||||
Uid: cmd.Uid,
|
||||
}
|
||||
@@ -573,13 +574,13 @@ func (hs *HTTPServer) DeleteAlertNotification(c *models.ReqContext) response.Res
|
||||
return response.Error(http.StatusBadRequest, "notificationId is invalid", err)
|
||||
}
|
||||
|
||||
cmd := models.DeleteAlertNotificationCommand{
|
||||
cmd := alertmodels.DeleteAlertNotificationCommand{
|
||||
OrgId: c.OrgID,
|
||||
Id: notificationId,
|
||||
}
|
||||
|
||||
if err := hs.AlertNotificationService.DeleteAlertNotification(c.Req.Context(), &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||
if errors.Is(err, alertmodels.ErrAlertNotificationNotFound) {
|
||||
return response.Error(404, err.Error(), nil)
|
||||
}
|
||||
return response.Error(500, "Failed to delete alert notification", err)
|
||||
@@ -601,13 +602,13 @@ func (hs *HTTPServer) DeleteAlertNotification(c *models.ReqContext) response.Res
|
||||
// 404: notFoundError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) DeleteAlertNotificationByUID(c *models.ReqContext) response.Response {
|
||||
cmd := models.DeleteAlertNotificationWithUidCommand{
|
||||
cmd := alertmodels.DeleteAlertNotificationWithUidCommand{
|
||||
OrgId: c.OrgID,
|
||||
Uid: web.Params(c.Req)[":uid"],
|
||||
}
|
||||
|
||||
if err := hs.AlertNotificationService.DeleteAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||
if errors.Is(err, alertmodels.ErrAlertNotificationNotFound) {
|
||||
return response.Error(404, err.Error(), nil)
|
||||
}
|
||||
return response.Error(500, "Failed to delete alert notification", err)
|
||||
@@ -690,7 +691,7 @@ func (hs *HTTPServer) PauseAlert(legacyAlertingEnabled *bool) func(c *models.Req
|
||||
result := make(map[string]interface{})
|
||||
result["alertId"] = alertID
|
||||
|
||||
query := models.GetAlertByIdQuery{Id: alertID}
|
||||
query := alertmodels.GetAlertByIdQuery{Id: alertID}
|
||||
if err := hs.AlertEngine.AlertStore.GetAlertById(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, "Get Alert failed", err)
|
||||
}
|
||||
@@ -708,17 +709,17 @@ func (hs *HTTPServer) PauseAlert(legacyAlertingEnabled *bool) func(c *models.Req
|
||||
}
|
||||
|
||||
// Alert state validation
|
||||
if query.Result.State != models.AlertStatePaused && !dto.Paused {
|
||||
if query.Result.State != alertmodels.AlertStatePaused && !dto.Paused {
|
||||
result["state"] = "un-paused"
|
||||
result["message"] = "Alert is already un-paused"
|
||||
return response.JSON(http.StatusOK, result)
|
||||
} else if query.Result.State == models.AlertStatePaused && dto.Paused {
|
||||
result["state"] = models.AlertStatePaused
|
||||
} else if query.Result.State == alertmodels.AlertStatePaused && dto.Paused {
|
||||
result["state"] = alertmodels.AlertStatePaused
|
||||
result["message"] = "Alert is already paused"
|
||||
return response.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
cmd := models.PauseAlertCommand{
|
||||
cmd := alertmodels.PauseAlertCommand{
|
||||
OrgId: c.OrgID,
|
||||
AlertIds: []int64{alertID},
|
||||
Paused: dto.Paused,
|
||||
@@ -728,10 +729,10 @@ func (hs *HTTPServer) PauseAlert(legacyAlertingEnabled *bool) func(c *models.Req
|
||||
return response.Error(500, "", err)
|
||||
}
|
||||
|
||||
resp := models.AlertStateUnknown
|
||||
resp := alertmodels.AlertStateUnknown
|
||||
pausedState := "un-paused"
|
||||
if cmd.Paused {
|
||||
resp = models.AlertStatePaused
|
||||
resp = alertmodels.AlertStatePaused
|
||||
pausedState = "paused"
|
||||
}
|
||||
|
||||
@@ -765,7 +766,7 @@ func (hs *HTTPServer) PauseAllAlerts(legacyAlertingEnabled *bool) func(c *models
|
||||
if err := web.Bind(c.Req, &dto); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
updateCmd := models.PauseAllAlertCommand{
|
||||
updateCmd := alertmodels.PauseAllAlertCommand{
|
||||
Paused: dto.Paused,
|
||||
}
|
||||
|
||||
@@ -773,10 +774,10 @@ func (hs *HTTPServer) PauseAllAlerts(legacyAlertingEnabled *bool) func(c *models
|
||||
return response.Error(500, "Failed to pause alerts", err)
|
||||
}
|
||||
|
||||
resp := models.AlertStatePending
|
||||
resp := alertmodels.AlertStatePending
|
||||
pausedState := "un paused"
|
||||
if updateCmd.Paused {
|
||||
resp = models.AlertStatePaused
|
||||
resp = alertmodels.AlertStatePaused
|
||||
pausedState = "paused"
|
||||
}
|
||||
|
||||
@@ -836,14 +837,14 @@ type NotificationChannelTestParams struct {
|
||||
type CreateAlertNotificationChannelParams struct {
|
||||
// in:body
|
||||
// required:true
|
||||
Body models.CreateAlertNotificationCommand `json:"body"`
|
||||
Body alertmodels.CreateAlertNotificationCommand `json:"body"`
|
||||
}
|
||||
|
||||
// swagger:parameters updateAlertNotificationChannel
|
||||
type UpdateAlertNotificationChannelParams struct {
|
||||
// in:body
|
||||
// required:true
|
||||
Body models.UpdateAlertNotificationCommand `json:"body"`
|
||||
Body alertmodels.UpdateAlertNotificationCommand `json:"body"`
|
||||
// in:path
|
||||
// required:true
|
||||
NotificationID int64 `json:"notification_channel_id"`
|
||||
@@ -853,7 +854,7 @@ type UpdateAlertNotificationChannelParams struct {
|
||||
type UpdateAlertNotificationChannelByUIDParams struct {
|
||||
// in:body
|
||||
// required:true
|
||||
Body models.UpdateAlertNotificationWithUidCommand `json:"body"`
|
||||
Body alertmodels.UpdateAlertNotificationWithUidCommand `json:"body"`
|
||||
// in:path
|
||||
// required:true
|
||||
NotificationUID string `json:"notification_channel_uid"`
|
||||
@@ -997,14 +998,14 @@ type SMTPNotEnabledError PreconditionFailedError
|
||||
type GetAlertsResponse struct {
|
||||
// The response message
|
||||
// in: body
|
||||
Body []*models.AlertListItemDTO `json:"body"`
|
||||
Body []*alertmodels.AlertListItemDTO `json:"body"`
|
||||
}
|
||||
|
||||
// swagger:response getAlertResponse
|
||||
type GetAlertResponse struct {
|
||||
// The response message
|
||||
// in: body
|
||||
Body *models.Alert `json:"body"`
|
||||
Body *alertmodels.Alert `json:"body"`
|
||||
}
|
||||
|
||||
// swagger:response pauseAlertResponse
|
||||
@@ -1032,5 +1033,5 @@ type TestAlertResponse struct {
|
||||
type GetDashboardStatesResponse struct {
|
||||
// The response message
|
||||
// in: body
|
||||
Body []*models.AlertStateInfoDTO `json:"body"`
|
||||
Body []*alertmodels.AlertStateInfoDTO `json:"body"`
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/null"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/alerting/models"
|
||||
)
|
||||
|
||||
func formatShort(interval time.Duration) string {
|
||||
|
||||
Reference in New Issue
Block a user