2016-04-26 10:36:50 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-07-21 03:29:11 -05:00
|
|
|
"fmt"
|
|
|
|
|
2016-04-27 06:02:28 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2016-04-26 10:36:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-07-20 09:13:36 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
2016-04-26 10:36:50 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func ValidateOrgAlert(c *middleware.Context) {
|
2016-05-02 09:07:19 -05:00
|
|
|
id := c.ParamsInt64(":alertId")
|
2016-04-28 01:23:50 -05:00
|
|
|
query := models.GetAlertByIdQuery{Id: id}
|
2016-04-26 10:36:50 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
c.JsonApiErr(404, "Alert not found", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.OrgId != query.Result.OrgId {
|
|
|
|
c.JsonApiErr(403, "You are not allowed to edit/view alert", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-11 03:26:48 -05:00
|
|
|
// GET /api/alerts/rules/
|
2016-04-26 10:36:50 -05:00
|
|
|
func GetAlerts(c *middleware.Context) Response {
|
|
|
|
query := models.GetAlertsQuery{
|
2016-05-10 02:45:56 -05:00
|
|
|
OrgId: c.OrgId,
|
|
|
|
State: c.QueryStrings("state"),
|
|
|
|
DashboardId: c.QueryInt64("dashboardId"),
|
|
|
|
PanelId: c.QueryInt64("panelId"),
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
return ApiError(500, "List alerts failed", err)
|
|
|
|
}
|
|
|
|
|
2016-04-27 06:02:28 -05:00
|
|
|
dashboardIds := make([]int64, 0)
|
2016-07-22 09:45:17 -05:00
|
|
|
alertDTOs := make([]*dtos.AlertRule, 0)
|
2016-04-27 06:02:28 -05:00
|
|
|
for _, alert := range query.Result {
|
|
|
|
dashboardIds = append(dashboardIds, alert.DashboardId)
|
2016-07-22 09:45:17 -05:00
|
|
|
alertDTOs = append(alertDTOs, &dtos.AlertRule{
|
2016-04-27 06:02:28 -05:00
|
|
|
Id: alert.Id,
|
|
|
|
DashboardId: alert.DashboardId,
|
|
|
|
PanelId: alert.PanelId,
|
2016-06-06 10:11:46 -05:00
|
|
|
Name: alert.Name,
|
2016-04-27 06:02:28 -05:00
|
|
|
Description: alert.Description,
|
2016-04-28 01:53:34 -05:00
|
|
|
State: alert.State,
|
2016-07-22 06:14:09 -05:00
|
|
|
Severity: alert.Severity,
|
2016-04-27 06:02:28 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
dashboardsQuery := models.GetDashboardsQuery{
|
|
|
|
DashboardIds: dashboardIds,
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:30:28 -05:00
|
|
|
if len(alertDTOs) > 0 {
|
|
|
|
if err := bus.Dispatch(&dashboardsQuery); err != nil {
|
|
|
|
return ApiError(500, "List alerts failed", err)
|
|
|
|
}
|
2016-04-27 06:02:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: should be possible to speed this up with lookup table
|
|
|
|
for _, alert := range alertDTOs {
|
2016-06-23 09:30:12 -05:00
|
|
|
for _, dash := range dashboardsQuery.Result {
|
2016-04-27 06:02:28 -05:00
|
|
|
if alert.DashboardId == dash.Id {
|
|
|
|
alert.DashbboardUri = "db/" + dash.Slug
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, alertDTOs)
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
|
|
|
|
2016-07-20 09:13:36 -05:00
|
|
|
// POST /api/alerts/test
|
2016-07-21 03:29:11 -05:00
|
|
|
func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response {
|
|
|
|
backendCmd := alerting.AlertTestCommand{
|
2016-07-20 09:13:36 -05:00
|
|
|
OrgId: c.OrgId,
|
|
|
|
Dashboard: dto.Dashboard,
|
|
|
|
PanelId: dto.PanelId,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&backendCmd); err != nil {
|
2016-07-21 06:09:12 -05:00
|
|
|
if validationErr, ok := err.(alerting.AlertValidationError); ok {
|
|
|
|
return ApiError(422, validationErr.Error(), nil)
|
|
|
|
}
|
2016-07-20 09:13:36 -05:00
|
|
|
return ApiError(500, "Failed to test rule", err)
|
|
|
|
}
|
|
|
|
|
2016-07-21 03:29:11 -05:00
|
|
|
res := backendCmd.Result
|
|
|
|
|
|
|
|
dtoRes := &dtos.AlertTestResult{
|
2016-07-22 06:14:09 -05:00
|
|
|
Firing: res.Firing,
|
2016-07-21 03:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
dtoRes.Error = res.Error.Error()
|
|
|
|
}
|
|
|
|
|
2016-07-21 06:09:12 -05:00
|
|
|
for _, log := range res.Logs {
|
|
|
|
dtoRes.Logs = append(dtoRes.Logs, &dtos.AlertTestResultLog{Message: log.Message, Data: log.Data})
|
|
|
|
}
|
|
|
|
|
2016-07-21 03:29:11 -05:00
|
|
|
dtoRes.Timing = fmt.Sprintf("%1.3fs", res.GetDurationSeconds())
|
|
|
|
|
|
|
|
return Json(200, dtoRes)
|
2016-07-20 09:13:36 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 01:23:50 -05:00
|
|
|
// GET /api/alerts/:id
|
2016-04-26 10:36:50 -05:00
|
|
|
func GetAlert(c *middleware.Context) Response {
|
2016-05-02 11:00:10 -05:00
|
|
|
id := c.ParamsInt64(":alertId")
|
2016-04-28 01:23:50 -05:00
|
|
|
query := models.GetAlertByIdQuery{Id: id}
|
2016-04-26 10:36:50 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
return ApiError(500, "List alerts failed", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, &query.Result)
|
|
|
|
}
|
2016-04-28 01:23:50 -05:00
|
|
|
|
2016-05-02 09:07:19 -05:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2016-07-22 06:14:09 -05:00
|
|
|
// // GET /api/alerts/events/:id
|
|
|
|
// func GetAlertStates(c *middleware.Context) Response {
|
|
|
|
// alertId := c.ParamsInt64(":alertId")
|
|
|
|
//
|
|
|
|
// query := models.GetAlertsStateQuery{
|
|
|
|
// AlertId: alertId,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if err := bus.Dispatch(&query); err != nil {
|
|
|
|
// return ApiError(500, "Failed get alert state log", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return Json(200, query.Result)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // PUT /api/alerts/events/:id
|
|
|
|
// func PutAlertState(c *middleware.Context, cmd models.UpdateAlertStateCommand) Response {
|
|
|
|
// cmd.AlertId = c.ParamsInt64(":alertId")
|
|
|
|
// cmd.OrgId = c.OrgId
|
|
|
|
//
|
|
|
|
// query := models.GetAlertByIdQuery{Id: cmd.AlertId}
|
|
|
|
// if err := bus.Dispatch(&query); err != nil {
|
|
|
|
// return ApiError(500, "Failed to get alertstate", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if query.Result.OrgId != 0 && query.Result.OrgId != c.OrgId {
|
|
|
|
// return ApiError(500, "Alert not found", nil)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if err := bus.Dispatch(&cmd); err != nil {
|
|
|
|
// return ApiError(500, "Failed to set new state", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return Json(200, cmd.Result)
|
|
|
|
// }
|
2016-06-15 03:48:04 -05:00
|
|
|
|
|
|
|
func GetAlertNotifications(c *middleware.Context) Response {
|
2016-07-22 09:45:17 -05:00
|
|
|
query := &models.GetAlertNotificationsQuery{OrgId: c.OrgId}
|
2016-06-15 03:48:04 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
|
|
|
return ApiError(500, "Failed to get alert notifications", err)
|
|
|
|
}
|
|
|
|
|
2016-07-22 09:45:17 -05:00
|
|
|
var result []dtos.AlertNotification
|
2016-06-20 04:31:20 -05:00
|
|
|
|
|
|
|
for _, notification := range query.Result {
|
2016-07-22 09:45:17 -05:00
|
|
|
result = append(result, dtos.AlertNotification{
|
2016-06-20 04:31:20 -05:00
|
|
|
Id: notification.Id,
|
|
|
|
Name: notification.Name,
|
|
|
|
Type: notification.Type,
|
|
|
|
Created: notification.Created,
|
|
|
|
Updated: notification.Updated,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, result)
|
2016-06-15 03:48:04 -05:00
|
|
|
}
|
|
|
|
|
2016-06-16 07:29:20 -05:00
|
|
|
func GetAlertNotificationById(c *middleware.Context) Response {
|
2016-07-22 09:45:17 -05:00
|
|
|
query := &models.GetAlertNotificationsQuery{
|
|
|
|
OrgId: c.OrgId,
|
2016-06-16 07:29:20 -05:00
|
|
|
Id: c.ParamsInt64("notificationId"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
|
|
|
return ApiError(500, "Failed to get alert notifications", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, query.Result[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateAlertNotification(c *middleware.Context, cmd models.CreateAlertNotificationCommand) Response {
|
2016-07-22 09:45:17 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
2016-06-15 03:48:04 -05:00
|
|
|
|
2016-06-16 07:29:20 -05:00
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2016-06-15 03:48:04 -05:00
|
|
|
return ApiError(500, "Failed to create alert notification", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, cmd.Result)
|
|
|
|
}
|
|
|
|
|
2016-06-16 07:29:20 -05:00
|
|
|
func UpdateAlertNotification(c *middleware.Context, cmd models.UpdateAlertNotificationCommand) Response {
|
2016-07-22 09:45:17 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
2016-06-15 03:48:04 -05:00
|
|
|
|
2016-06-16 07:29:20 -05:00
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2016-06-15 03:48:04 -05:00
|
|
|
return ApiError(500, "Failed to update alert notification", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, cmd.Result)
|
|
|
|
}
|
2016-06-16 08:21:44 -05:00
|
|
|
|
|
|
|
func DeleteAlertNotification(c *middleware.Context) Response {
|
|
|
|
cmd := models.DeleteAlertNotificationCommand{
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
Id: c.ParamsInt64("notificationId"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
|
|
return ApiError(500, "Failed to delete alert notification", err)
|
|
|
|
}
|
|
|
|
|
2016-07-22 09:45:17 -05:00
|
|
|
return ApiSuccess("Notification deleted")
|
2016-06-16 08:21:44 -05:00
|
|
|
}
|