|
|
@@ -5,14 +5,14 @@ import (
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
- "github.com/grafana/grafana/pkg/middleware"
|
|
|
- "github.com/grafana/grafana/pkg/models"
|
|
|
+ m "github.com/grafana/grafana/pkg/models"
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
|
|
+ "github.com/grafana/grafana/pkg/services/guardian"
|
|
|
)
|
|
|
|
|
|
-func ValidateOrgAlert(c *middleware.Context) {
|
|
|
+func ValidateOrgAlert(c *m.ReqContext) {
|
|
|
id := c.ParamsInt64(":alertId")
|
|
|
- query := models.GetAlertByIdQuery{Id: id}
|
|
|
+ query := m.GetAlertByIdQuery{Id: id}
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
c.JsonApiErr(404, "Alert not found", nil)
|
|
|
@@ -25,14 +25,14 @@ func ValidateOrgAlert(c *middleware.Context) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func GetAlertStatesForDashboard(c *middleware.Context) Response {
|
|
|
+func GetAlertStatesForDashboard(c *m.ReqContext) Response {
|
|
|
dashboardId := c.QueryInt64("dashboardId")
|
|
|
|
|
|
if dashboardId == 0 {
|
|
|
return ApiError(400, "Missing query parameter dashboardId", nil)
|
|
|
}
|
|
|
|
|
|
- query := models.GetAlertStatesForDashboardQuery{
|
|
|
+ query := m.GetAlertStatesForDashboardQuery{
|
|
|
OrgId: c.OrgId,
|
|
|
DashboardId: c.QueryInt64("dashboardId"),
|
|
|
}
|
|
|
@@ -45,12 +45,13 @@ func GetAlertStatesForDashboard(c *middleware.Context) Response {
|
|
|
}
|
|
|
|
|
|
// GET /api/alerts
|
|
|
-func GetAlerts(c *middleware.Context) Response {
|
|
|
- query := models.GetAlertsQuery{
|
|
|
+func GetAlerts(c *m.ReqContext) Response {
|
|
|
+ query := m.GetAlertsQuery{
|
|
|
OrgId: c.OrgId,
|
|
|
DashboardId: c.QueryInt64("dashboardId"),
|
|
|
PanelId: c.QueryInt64("panelId"),
|
|
|
Limit: c.QueryInt64("limit"),
|
|
|
+ User: c.SignedInUser,
|
|
|
}
|
|
|
|
|
|
states := c.QueryStrings("state")
|
|
|
@@ -62,47 +63,15 @@ func GetAlerts(c *middleware.Context) Response {
|
|
|
return ApiError(500, "List alerts failed", err)
|
|
|
}
|
|
|
|
|
|
- dashboardIds := make([]int64, 0)
|
|
|
- alertDTOs := make([]*dtos.AlertRule, 0)
|
|
|
for _, alert := range query.Result {
|
|
|
- dashboardIds = append(dashboardIds, alert.DashboardId)
|
|
|
- alertDTOs = append(alertDTOs, &dtos.AlertRule{
|
|
|
- Id: alert.Id,
|
|
|
- DashboardId: alert.DashboardId,
|
|
|
- PanelId: alert.PanelId,
|
|
|
- Name: alert.Name,
|
|
|
- Message: alert.Message,
|
|
|
- State: alert.State,
|
|
|
- NewStateDate: alert.NewStateDate,
|
|
|
- ExecutionError: alert.ExecutionError,
|
|
|
- EvalData: alert.EvalData,
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- dashboardsQuery := models.GetDashboardsQuery{
|
|
|
- DashboardIds: dashboardIds,
|
|
|
- }
|
|
|
-
|
|
|
- if len(alertDTOs) > 0 {
|
|
|
- if err := bus.Dispatch(&dashboardsQuery); err != nil {
|
|
|
- return ApiError(500, "List alerts failed", err)
|
|
|
- }
|
|
|
+ alert.Url = m.GetDashboardUrl(alert.DashboardUid, alert.DashboardSlug)
|
|
|
}
|
|
|
|
|
|
- //TODO: should be possible to speed this up with lookup table
|
|
|
- for _, alert := range alertDTOs {
|
|
|
- for _, dash := range dashboardsQuery.Result {
|
|
|
- if alert.DashboardId == dash.Id {
|
|
|
- alert.DashbboardUri = "db/" + dash.Slug
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return Json(200, alertDTOs)
|
|
|
+ return Json(200, query.Result)
|
|
|
}
|
|
|
|
|
|
// POST /api/alerts/test
|
|
|
-func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response {
|
|
|
+func AlertTest(c *m.ReqContext, dto dtos.AlertTestCommand) Response {
|
|
|
if _, idErr := dto.Dashboard.Get("id").Int64(); idErr != nil {
|
|
|
return ApiError(400, "The dashboard needs to be saved at least once before you can test an alert rule", nil)
|
|
|
}
|
|
|
@@ -144,9 +113,9 @@ func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response {
|
|
|
}
|
|
|
|
|
|
// GET /api/alerts/:id
|
|
|
-func GetAlert(c *middleware.Context) Response {
|
|
|
+func GetAlert(c *m.ReqContext) Response {
|
|
|
id := c.ParamsInt64(":alertId")
|
|
|
- query := models.GetAlertByIdQuery{Id: id}
|
|
|
+ query := m.GetAlertByIdQuery{Id: id}
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
return ApiError(500, "List alerts failed", err)
|
|
|
@@ -155,30 +124,12 @@ func GetAlert(c *middleware.Context) Response {
|
|
|
return Json(200, &query.Result)
|
|
|
}
|
|
|
|
|
|
-// DEL /api/alerts/:id
|
|
|
-func DelAlert(c *middleware.Context) Response {
|
|
|
- alertId := c.ParamsInt64(":alertId")
|
|
|
-
|
|
|
- if alertId == 0 {
|
|
|
- return ApiError(401, "Failed to parse alertid", nil)
|
|
|
- }
|
|
|
-
|
|
|
- cmd := models.DeleteAlertCommand{AlertId: alertId}
|
|
|
-
|
|
|
- if err := bus.Dispatch(&cmd); err != nil {
|
|
|
- return ApiError(500, "Failed to delete alert", err)
|
|
|
- }
|
|
|
-
|
|
|
- var resp = map[string]interface{}{"alertId": alertId}
|
|
|
- return Json(200, resp)
|
|
|
-}
|
|
|
-
|
|
|
-func GetAlertNotifiers(c *middleware.Context) Response {
|
|
|
+func GetAlertNotifiers(c *m.ReqContext) Response {
|
|
|
return Json(200, alerting.GetNotifiers())
|
|
|
}
|
|
|
|
|
|
-func GetAlertNotifications(c *middleware.Context) Response {
|
|
|
- query := &models.GetAllAlertNotificationsQuery{OrgId: c.OrgId}
|
|
|
+func GetAlertNotifications(c *m.ReqContext) Response {
|
|
|
+ query := &m.GetAllAlertNotificationsQuery{OrgId: c.OrgId}
|
|
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
|
|
return ApiError(500, "Failed to get alert notifications", err)
|
|
|
@@ -200,8 +151,8 @@ func GetAlertNotifications(c *middleware.Context) Response {
|
|
|
return Json(200, result)
|
|
|
}
|
|
|
|
|
|
-func GetAlertNotificationById(c *middleware.Context) Response {
|
|
|
- query := &models.GetAlertNotificationsQuery{
|
|
|
+func GetAlertNotificationById(c *m.ReqContext) Response {
|
|
|
+ query := &m.GetAlertNotificationsQuery{
|
|
|
OrgId: c.OrgId,
|
|
|
Id: c.ParamsInt64("notificationId"),
|
|
|
}
|
|
|
@@ -213,7 +164,7 @@ func GetAlertNotificationById(c *middleware.Context) Response {
|
|
|
return Json(200, query.Result)
|
|
|
}
|
|
|
|
|
|
-func CreateAlertNotification(c *middleware.Context, cmd models.CreateAlertNotificationCommand) Response {
|
|
|
+func CreateAlertNotification(c *m.ReqContext, cmd m.CreateAlertNotificationCommand) Response {
|
|
|
cmd.OrgId = c.OrgId
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
|
@@ -223,7 +174,7 @@ func CreateAlertNotification(c *middleware.Context, cmd models.CreateAlertNotifi
|
|
|
return Json(200, cmd.Result)
|
|
|
}
|
|
|
|
|
|
-func UpdateAlertNotification(c *middleware.Context, cmd models.UpdateAlertNotificationCommand) Response {
|
|
|
+func UpdateAlertNotification(c *m.ReqContext, cmd m.UpdateAlertNotificationCommand) Response {
|
|
|
cmd.OrgId = c.OrgId
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
|
@@ -233,8 +184,8 @@ func UpdateAlertNotification(c *middleware.Context, cmd models.UpdateAlertNotifi
|
|
|
return Json(200, cmd.Result)
|
|
|
}
|
|
|
|
|
|
-func DeleteAlertNotification(c *middleware.Context) Response {
|
|
|
- cmd := models.DeleteAlertNotificationCommand{
|
|
|
+func DeleteAlertNotification(c *m.ReqContext) Response {
|
|
|
+ cmd := m.DeleteAlertNotificationCommand{
|
|
|
OrgId: c.OrgId,
|
|
|
Id: c.ParamsInt64("notificationId"),
|
|
|
}
|
|
|
@@ -247,7 +198,7 @@ func DeleteAlertNotification(c *middleware.Context) Response {
|
|
|
}
|
|
|
|
|
|
//POST /api/alert-notifications/test
|
|
|
-func NotificationTest(c *middleware.Context, dto dtos.NotificationTestCommand) Response {
|
|
|
+func NotificationTest(c *m.ReqContext, dto dtos.NotificationTestCommand) Response {
|
|
|
cmd := &alerting.NotificationTestCommand{
|
|
|
Name: dto.Name,
|
|
|
Type: dto.Type,
|
|
|
@@ -255,7 +206,7 @@ func NotificationTest(c *middleware.Context, dto dtos.NotificationTestCommand) R
|
|
|
}
|
|
|
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
|
|
- if err == models.ErrSmtpNotEnabled {
|
|
|
+ if err == m.ErrSmtpNotEnabled {
|
|
|
return ApiError(412, err.Error(), err)
|
|
|
}
|
|
|
return ApiError(500, "Failed to send alert notifications", err)
|
|
|
@@ -265,9 +216,25 @@ func NotificationTest(c *middleware.Context, dto dtos.NotificationTestCommand) R
|
|
|
}
|
|
|
|
|
|
//POST /api/alerts/:alertId/pause
|
|
|
-func PauseAlert(c *middleware.Context, dto dtos.PauseAlertCommand) Response {
|
|
|
+func PauseAlert(c *m.ReqContext, dto dtos.PauseAlertCommand) Response {
|
|
|
alertId := c.ParamsInt64("alertId")
|
|
|
- cmd := models.PauseAlertCommand{
|
|
|
+
|
|
|
+ query := m.GetAlertByIdQuery{Id: alertId}
|
|
|
+
|
|
|
+ if err := bus.Dispatch(&query); err != nil {
|
|
|
+ return ApiError(500, "Get Alert failed", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ guardian := guardian.New(query.Result.DashboardId, c.OrgId, c.SignedInUser)
|
|
|
+ if canEdit, err := guardian.CanEdit(); err != nil || !canEdit {
|
|
|
+ if err != nil {
|
|
|
+ return ApiError(500, "Error while checking permissions for Alert", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return ApiError(403, "Access denied to this dashboard and alert", nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ cmd := m.PauseAlertCommand{
|
|
|
OrgId: c.OrgId,
|
|
|
AlertIds: []int64{alertId},
|
|
|
Paused: dto.Paused,
|
|
|
@@ -277,25 +244,25 @@ func PauseAlert(c *middleware.Context, dto dtos.PauseAlertCommand) Response {
|
|
|
return ApiError(500, "", err)
|
|
|
}
|
|
|
|
|
|
- var response models.AlertStateType = models.AlertStatePending
|
|
|
- pausedState := "un paused"
|
|
|
+ var response m.AlertStateType = m.AlertStatePending
|
|
|
+ pausedState := "un-paused"
|
|
|
if cmd.Paused {
|
|
|
- response = models.AlertStatePaused
|
|
|
+ response = m.AlertStatePaused
|
|
|
pausedState = "paused"
|
|
|
}
|
|
|
|
|
|
result := map[string]interface{}{
|
|
|
"alertId": alertId,
|
|
|
"state": response,
|
|
|
- "message": "alert " + pausedState,
|
|
|
+ "message": "Alert " + pausedState,
|
|
|
}
|
|
|
|
|
|
return Json(200, result)
|
|
|
}
|
|
|
|
|
|
//POST /api/admin/pause-all-alerts
|
|
|
-func PauseAllAlerts(c *middleware.Context, dto dtos.PauseAllAlertsCommand) Response {
|
|
|
- updateCmd := models.PauseAllAlertCommand{
|
|
|
+func PauseAllAlerts(c *m.ReqContext, dto dtos.PauseAllAlertsCommand) Response {
|
|
|
+ updateCmd := m.PauseAllAlertCommand{
|
|
|
Paused: dto.Paused,
|
|
|
}
|
|
|
|
|
|
@@ -303,10 +270,10 @@ func PauseAllAlerts(c *middleware.Context, dto dtos.PauseAllAlertsCommand) Respo
|
|
|
return ApiError(500, "Failed to pause alerts", err)
|
|
|
}
|
|
|
|
|
|
- var response models.AlertStateType = models.AlertStatePending
|
|
|
+ var response m.AlertStateType = m.AlertStatePending
|
|
|
pausedState := "un paused"
|
|
|
if updateCmd.Paused {
|
|
|
- response = models.AlertStatePaused
|
|
|
+ response = m.AlertStatePaused
|
|
|
pausedState = "paused"
|
|
|
}
|
|
|
|