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 //Unused
  56. State AlertStateType
  57. Handler int64 //Unused
  58. Silenced bool
  59. ExecutionError string
  60. Frequency int64
  61. DebounceDuration time.Duration
  62. EvalData *simplejson.Json
  63. NewStateDate time.Time
  64. StateChanges int64
  65. Created time.Time
  66. Updated time.Time
  67. Settings *simplejson.Json
  68. }
  69. func (alert *Alert) ValidToSave() bool {
  70. return alert.DashboardId != 0 && alert.OrgId != 0 && alert.PanelId != 0
  71. }
  72. func (alert *Alert) ShouldUpdateState(newState AlertStateType) bool {
  73. return alert.State != newState
  74. }
  75. func (this *Alert) ContainsUpdates(other *Alert) bool {
  76. result := false
  77. result = result || this.Name != other.Name
  78. result = result || this.Message != other.Message
  79. if this.Settings != nil && other.Settings != nil {
  80. json1, err1 := this.Settings.Encode()
  81. json2, err2 := other.Settings.Encode()
  82. if err1 != nil || err2 != nil {
  83. return false
  84. }
  85. result = result || string(json1) != string(json2)
  86. }
  87. //don't compare .State! That would be insane.
  88. return result
  89. }
  90. type AlertingClusterInfo struct {
  91. ServerId string
  92. ClusterSize int
  93. UptimePosition int
  94. }
  95. type HeartBeat struct {
  96. Id int64
  97. ServerId string
  98. Updated time.Time
  99. Created time.Time
  100. }
  101. type HeartBeatCommand struct {
  102. ServerId string
  103. Result AlertingClusterInfo
  104. }
  105. type SaveAlertsCommand struct {
  106. DashboardId int64
  107. UserId int64
  108. OrgId int64
  109. Alerts []*Alert
  110. }
  111. type PauseAlertCommand struct {
  112. OrgId int64
  113. AlertIds []int64
  114. ResultCount int64
  115. Paused bool
  116. }
  117. type PauseAllAlertCommand struct {
  118. ResultCount int64
  119. Paused bool
  120. }
  121. type SetAlertStateCommand struct {
  122. AlertId int64
  123. OrgId int64
  124. State AlertStateType
  125. Error string
  126. EvalData *simplejson.Json
  127. Result Alert
  128. }
  129. //Queries
  130. type GetAlertsQuery struct {
  131. OrgId int64
  132. State []string
  133. DashboardIDs []int64
  134. PanelId int64
  135. Limit int64
  136. Query string
  137. User *SignedInUser
  138. Result []*AlertListItemDTO
  139. }
  140. type GetAllAlertsQuery struct {
  141. Result []*Alert
  142. }
  143. type GetAlertByIdQuery struct {
  144. Id int64
  145. Result *Alert
  146. }
  147. type GetAlertStatesForDashboardQuery struct {
  148. OrgId int64
  149. DashboardId int64
  150. Result []*AlertStateInfoDTO
  151. }
  152. type AlertListItemDTO struct {
  153. Id int64 `json:"id"`
  154. DashboardId int64 `json:"dashboardId"`
  155. DashboardUid string `json:"dashboardUid"`
  156. DashboardSlug string `json:"dashboardSlug"`
  157. PanelId int64 `json:"panelId"`
  158. Name string `json:"name"`
  159. State AlertStateType `json:"state"`
  160. NewStateDate time.Time `json:"newStateDate"`
  161. EvalDate time.Time `json:"evalDate"`
  162. EvalData *simplejson.Json `json:"evalData"`
  163. ExecutionError string `json:"executionError"`
  164. Url string `json:"url"`
  165. }
  166. type AlertStateInfoDTO struct {
  167. Id int64 `json:"id"`
  168. DashboardId int64 `json:"dashboardId"`
  169. PanelId int64 `json:"panelId"`
  170. State AlertStateType `json:"state"`
  171. NewStateDate time.Time `json:"newStateDate"`
  172. }
  173. // "Internal" commands
  174. type UpdateDashboardAlertsCommand struct {
  175. UserId int64
  176. OrgId int64
  177. Dashboard *Dashboard
  178. }
  179. type ValidateDashboardAlertsCommand struct {
  180. UserId int64
  181. OrgId int64
  182. Dashboard *Dashboard
  183. }