alert.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package models
  2. import (
  3. "fmt"
  4. "time"
  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. func (alert *Alert) GetTagsFromSettings() []*Tag {
  97. tags := []*Tag{}
  98. if alert.Settings != nil {
  99. if data, ok := alert.Settings.CheckGet("alertRuleTags"); ok {
  100. for tagNameString, tagValue := range data.MustMap() {
  101. // MustMap() already guarantees the return of a `map[string]interface{}`.
  102. // Therefore we only need to verify that tagValue is a String.
  103. tagValueString := simplejson.NewFromAny(tagValue).MustString()
  104. tags = append(tags, &Tag{Key: tagNameString, Value: tagValueString})
  105. }
  106. }
  107. }
  108. return tags
  109. }
  110. type AlertingClusterInfo struct {
  111. ServerId string
  112. ClusterSize int
  113. UptimePosition int
  114. }
  115. type HeartBeat struct {
  116. Id int64
  117. ServerId string
  118. Updated time.Time
  119. Created time.Time
  120. }
  121. type HeartBeatCommand struct {
  122. ServerId string
  123. Result AlertingClusterInfo
  124. }
  125. type SaveAlertsCommand struct {
  126. DashboardId int64
  127. UserId int64
  128. OrgId int64
  129. Alerts []*Alert
  130. }
  131. type PauseAlertCommand struct {
  132. OrgId int64
  133. AlertIds []int64
  134. ResultCount int64
  135. Paused bool
  136. }
  137. type PauseAllAlertCommand struct {
  138. ResultCount int64
  139. Paused bool
  140. }
  141. type SetAlertStateCommand struct {
  142. AlertId int64
  143. OrgId int64
  144. State AlertStateType
  145. Error string
  146. EvalData *simplejson.Json
  147. Result Alert
  148. }
  149. //Queries
  150. type GetAlertsQuery struct {
  151. OrgId int64
  152. State []string
  153. DashboardIDs []int64
  154. PanelId int64
  155. Limit int64
  156. Query string
  157. User *SignedInUser
  158. Result []*AlertListItemDTO
  159. }
  160. type GetAllAlertsQuery struct {
  161. Result []*Alert
  162. }
  163. type GetAlertByIdQuery struct {
  164. Id int64
  165. Result *Alert
  166. }
  167. type GetAlertStatesForDashboardQuery struct {
  168. OrgId int64
  169. DashboardId int64
  170. Result []*AlertStateInfoDTO
  171. }
  172. type AlertListItemDTO struct {
  173. Id int64 `json:"id"`
  174. DashboardId int64 `json:"dashboardId"`
  175. DashboardUid string `json:"dashboardUid"`
  176. DashboardSlug string `json:"dashboardSlug"`
  177. PanelId int64 `json:"panelId"`
  178. Name string `json:"name"`
  179. State AlertStateType `json:"state"`
  180. NewStateDate time.Time `json:"newStateDate"`
  181. EvalDate time.Time `json:"evalDate"`
  182. EvalData *simplejson.Json `json:"evalData"`
  183. ExecutionError string `json:"executionError"`
  184. Url string `json:"url"`
  185. }
  186. type AlertStateInfoDTO struct {
  187. Id int64 `json:"id"`
  188. DashboardId int64 `json:"dashboardId"`
  189. PanelId int64 `json:"panelId"`
  190. State AlertStateType `json:"state"`
  191. NewStateDate time.Time `json:"newStateDate"`
  192. }
  193. // "Internal" commands
  194. type UpdateDashboardAlertsCommand struct {
  195. OrgId int64
  196. Dashboard *Dashboard
  197. User *SignedInUser
  198. }
  199. type ValidateDashboardAlertsCommand struct {
  200. UserId int64
  201. OrgId int64
  202. Dashboard *Dashboard
  203. User *SignedInUser
  204. }