alerting.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package api
  2. import (
  3. "fmt"
  4. "github.com/grafana/grafana/pkg/api/dtos"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/middleware"
  7. "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/alerting"
  9. )
  10. func ValidateOrgAlert(c *middleware.Context) {
  11. id := c.ParamsInt64(":alertId")
  12. query := models.GetAlertByIdQuery{Id: id}
  13. if err := bus.Dispatch(&query); err != nil {
  14. c.JsonApiErr(404, "Alert not found", nil)
  15. return
  16. }
  17. if c.OrgId != query.Result.OrgId {
  18. c.JsonApiErr(403, "You are not allowed to edit/view alert", nil)
  19. return
  20. }
  21. }
  22. // GET /api/alerts/rules/
  23. func GetAlerts(c *middleware.Context) Response {
  24. query := models.GetAlertsQuery{
  25. OrgId: c.OrgId,
  26. State: c.QueryStrings("state"),
  27. DashboardId: c.QueryInt64("dashboardId"),
  28. PanelId: c.QueryInt64("panelId"),
  29. }
  30. if err := bus.Dispatch(&query); err != nil {
  31. return ApiError(500, "List alerts failed", err)
  32. }
  33. dashboardIds := make([]int64, 0)
  34. alertDTOs := make([]*dtos.AlertRule, 0)
  35. for _, alert := range query.Result {
  36. dashboardIds = append(dashboardIds, alert.DashboardId)
  37. alertDTOs = append(alertDTOs, &dtos.AlertRule{
  38. Id: alert.Id,
  39. DashboardId: alert.DashboardId,
  40. PanelId: alert.PanelId,
  41. Name: alert.Name,
  42. Message: alert.Message,
  43. State: alert.State,
  44. Severity: alert.Severity,
  45. EvalDate: alert.EvalDate,
  46. NewStateDate: alert.NewStateDate,
  47. ExecutionError: alert.ExecutionError,
  48. })
  49. }
  50. dashboardsQuery := models.GetDashboardsQuery{
  51. DashboardIds: dashboardIds,
  52. }
  53. if len(alertDTOs) > 0 {
  54. if err := bus.Dispatch(&dashboardsQuery); err != nil {
  55. return ApiError(500, "List alerts failed", err)
  56. }
  57. }
  58. //TODO: should be possible to speed this up with lookup table
  59. for _, alert := range alertDTOs {
  60. for _, dash := range dashboardsQuery.Result {
  61. if alert.DashboardId == dash.Id {
  62. alert.DashbboardUri = "db/" + dash.Slug
  63. }
  64. }
  65. }
  66. return Json(200, alertDTOs)
  67. }
  68. // POST /api/alerts/test
  69. func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response {
  70. backendCmd := alerting.AlertTestCommand{
  71. OrgId: c.OrgId,
  72. Dashboard: dto.Dashboard,
  73. PanelId: dto.PanelId,
  74. }
  75. if err := bus.Dispatch(&backendCmd); err != nil {
  76. if validationErr, ok := err.(alerting.ValidationError); ok {
  77. return ApiError(422, validationErr.Error(), nil)
  78. }
  79. return ApiError(500, "Failed to test rule", err)
  80. }
  81. res := backendCmd.Result
  82. dtoRes := &dtos.AlertTestResult{
  83. Firing: res.Firing,
  84. }
  85. if res.Error != nil {
  86. dtoRes.Error = res.Error.Error()
  87. }
  88. for _, log := range res.Logs {
  89. dtoRes.Logs = append(dtoRes.Logs, &dtos.AlertTestResultLog{Message: log.Message, Data: log.Data})
  90. }
  91. dtoRes.TimeMs = fmt.Sprintf("%1.3fms", res.GetDurationMs())
  92. return Json(200, dtoRes)
  93. }
  94. // GET /api/alerts/:id
  95. func GetAlert(c *middleware.Context) Response {
  96. id := c.ParamsInt64(":alertId")
  97. query := models.GetAlertByIdQuery{Id: id}
  98. if err := bus.Dispatch(&query); err != nil {
  99. return ApiError(500, "List alerts failed", err)
  100. }
  101. return Json(200, &query.Result)
  102. }
  103. // DEL /api/alerts/:id
  104. func DelAlert(c *middleware.Context) Response {
  105. alertId := c.ParamsInt64(":alertId")
  106. if alertId == 0 {
  107. return ApiError(401, "Failed to parse alertid", nil)
  108. }
  109. cmd := models.DeleteAlertCommand{AlertId: alertId}
  110. if err := bus.Dispatch(&cmd); err != nil {
  111. return ApiError(500, "Failed to delete alert", err)
  112. }
  113. var resp = map[string]interface{}{"alertId": alertId}
  114. return Json(200, resp)
  115. }
  116. func GetAlertNotifications(c *middleware.Context) Response {
  117. query := &models.GetAlertNotificationsQuery{OrgId: c.OrgId}
  118. if err := bus.Dispatch(query); err != nil {
  119. return ApiError(500, "Failed to get alert notifications", err)
  120. }
  121. var result []dtos.AlertNotification
  122. for _, notification := range query.Result {
  123. result = append(result, dtos.AlertNotification{
  124. Id: notification.Id,
  125. Name: notification.Name,
  126. Type: notification.Type,
  127. Created: notification.Created,
  128. Updated: notification.Updated,
  129. })
  130. }
  131. return Json(200, result)
  132. }
  133. func GetAlertNotificationById(c *middleware.Context) Response {
  134. query := &models.GetAlertNotificationsQuery{
  135. OrgId: c.OrgId,
  136. Id: c.ParamsInt64("notificationId"),
  137. }
  138. if err := bus.Dispatch(query); err != nil {
  139. return ApiError(500, "Failed to get alert notifications", err)
  140. }
  141. return Json(200, query.Result[0])
  142. }
  143. func CreateAlertNotification(c *middleware.Context, cmd models.CreateAlertNotificationCommand) Response {
  144. cmd.OrgId = c.OrgId
  145. if err := bus.Dispatch(&cmd); err != nil {
  146. return ApiError(500, "Failed to create alert notification", err)
  147. }
  148. return Json(200, cmd.Result)
  149. }
  150. func UpdateAlertNotification(c *middleware.Context, cmd models.UpdateAlertNotificationCommand) Response {
  151. cmd.OrgId = c.OrgId
  152. if err := bus.Dispatch(&cmd); err != nil {
  153. return ApiError(500, "Failed to update alert notification", err)
  154. }
  155. return Json(200, cmd.Result)
  156. }
  157. func DeleteAlertNotification(c *middleware.Context) Response {
  158. cmd := models.DeleteAlertNotificationCommand{
  159. OrgId: c.OrgId,
  160. Id: c.ParamsInt64("notificationId"),
  161. }
  162. if err := bus.Dispatch(&cmd); err != nil {
  163. return ApiError(500, "Failed to delete alert notification", err)
  164. }
  165. return ApiSuccess("Notification deleted")
  166. }