alert.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package models
  2. import (
  3. "time"
  4. "fmt"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. )
  7. type AlertStateType string
  8. type AlertSeverityType string
  9. type NoDataOption string
  10. type ExecutionErrorOption string
  11. const (
  12. AlertStateNoData AlertStateType = "no_data"
  13. AlertStatePaused AlertStateType = "paused"
  14. AlertStateAlerting AlertStateType = "alerting"
  15. AlertStateOK AlertStateType = "ok"
  16. AlertStatePending AlertStateType = "pending"
  17. )
  18. const (
  19. NoDataSetOK NoDataOption = "ok"
  20. NoDataSetNoData NoDataOption = "no_data"
  21. NoDataKeepState NoDataOption = "keep_state"
  22. NoDataSetAlerting NoDataOption = "alerting"
  23. )
  24. const (
  25. ExecutionErrorSetAlerting ExecutionErrorOption = "alerting"
  26. ExecutionErrorKeepState ExecutionErrorOption = "keep_state"
  27. )
  28. var (
  29. ErrCannotChangeStateOnPausedAlert = fmt.Errorf("Cannot change state on pause alert")
  30. ErrRequiresNewState = fmt.Errorf("update alert state requires a new state.")
  31. )
  32. func (s AlertStateType) IsValid() bool {
  33. return s == AlertStateOK || s == AlertStateNoData || s == AlertStatePaused || s == AlertStatePending
  34. }
  35. func (s NoDataOption) IsValid() bool {
  36. return s == NoDataSetNoData || s == NoDataSetAlerting || s == NoDataKeepState || s == NoDataSetOK
  37. }
  38. func (s NoDataOption) ToAlertState() AlertStateType {
  39. return AlertStateType(s)
  40. }
  41. func (s ExecutionErrorOption) IsValid() bool {
  42. return s == ExecutionErrorSetAlerting || s == ExecutionErrorKeepState
  43. }
  44. func (s ExecutionErrorOption) ToAlertState() AlertStateType {
  45. return AlertStateType(s)
  46. }
  47. type Alert struct {
  48. Id int64
  49. Version int64
  50. OrgId int64
  51. DashboardId int64
  52. PanelId int64
  53. Name string
  54. Message string
  55. Severity string
  56. State AlertStateType
  57. Handler int64
  58. Silenced bool
  59. ExecutionError string
  60. Frequency int64
  61. EvalData *simplejson.Json
  62. NewStateDate time.Time
  63. StateChanges int64
  64. Created time.Time
  65. Updated time.Time
  66. Settings *simplejson.Json
  67. }
  68. func (alert *Alert) ValidToSave() bool {
  69. return alert.DashboardId != 0 && alert.OrgId != 0 && alert.PanelId != 0
  70. }
  71. func (alert *Alert) ShouldUpdateState(newState AlertStateType) bool {
  72. return alert.State != newState
  73. }
  74. func (this *Alert) ContainsUpdates(other *Alert) bool {
  75. result := false
  76. result = result || this.Name != other.Name
  77. result = result || this.Message != other.Message
  78. if this.Settings != nil && other.Settings != nil {
  79. json1, err1 := this.Settings.Encode()
  80. json2, err2 := other.Settings.Encode()
  81. if err1 != nil || err2 != nil {
  82. return false
  83. }
  84. result = result || string(json1) != string(json2)
  85. }
  86. //don't compare .State! That would be insane.
  87. return result
  88. }
  89. type AlertingClusterInfo struct {
  90. ServerId string
  91. ClusterSize int
  92. UptimePosition int
  93. }
  94. type HeartBeat struct {
  95. Id int64
  96. ServerId string
  97. Updated time.Time
  98. Created time.Time
  99. }
  100. type HeartBeatCommand struct {
  101. ServerId string
  102. Result AlertingClusterInfo
  103. }
  104. type SaveAlertsCommand struct {
  105. DashboardId int64
  106. UserId int64
  107. OrgId int64
  108. Alerts []*Alert
  109. }
  110. type PauseAlertCommand struct {
  111. OrgId int64
  112. AlertIds []int64
  113. ResultCount int64
  114. Paused bool
  115. }
  116. type PauseAllAlertCommand struct {
  117. ResultCount int64
  118. Paused bool
  119. }
  120. type SetAlertStateCommand struct {
  121. AlertId int64
  122. OrgId int64
  123. State AlertStateType
  124. Error string
  125. EvalData *simplejson.Json
  126. Result Alert
  127. }
  128. //Queries
  129. type GetAlertsQuery struct {
  130. OrgId int64
  131. State []string
  132. DashboardIDs []int64
  133. PanelId int64
  134. Limit int64
  135. Query string
  136. User *SignedInUser
  137. Result []*AlertListItemDTO
  138. }
  139. type GetAllAlertsQuery struct {
  140. Result []*Alert
  141. }
  142. type GetAlertByIdQuery struct {
  143. Id int64
  144. Result *Alert
  145. }
  146. type GetAlertStatesForDashboardQuery struct {
  147. OrgId int64
  148. DashboardId int64
  149. Result []*AlertStateInfoDTO
  150. }
  151. type AlertListItemDTO struct {
  152. Id int64 `json:"id"`
  153. DashboardId int64 `json:"dashboardId"`
  154. DashboardUid string `json:"dashboardUid"`
  155. DashboardSlug string `json:"dashboardSlug"`
  156. PanelId int64 `json:"panelId"`
  157. Name string `json:"name"`
  158. State AlertStateType `json:"state"`
  159. NewStateDate time.Time `json:"newStateDate"`
  160. EvalDate time.Time `json:"evalDate"`
  161. EvalData *simplejson.Json `json:"evalData"`
  162. ExecutionError string `json:"executionError"`
  163. Url string `json:"url"`
  164. }
  165. type AlertStateInfoDTO struct {
  166. Id int64 `json:"id"`
  167. DashboardId int64 `json:"dashboardId"`
  168. PanelId int64 `json:"panelId"`
  169. State AlertStateType `json:"state"`
  170. NewStateDate time.Time `json:"newStateDate"`
  171. }
  172. // "Internal" commands
  173. type UpdateDashboardAlertsCommand struct {
  174. OrgId int64
  175. Dashboard *Dashboard
  176. User *SignedInUser
  177. }
  178. type ValidateDashboardAlertsCommand struct {
  179. UserId int64
  180. OrgId int64
  181. Dashboard *Dashboard
  182. User *SignedInUser
  183. }