alerting.go 5.1 KB

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