alert.go 5.1 KB

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