alert.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 error = fmt.Errorf("Cannot change state on pause alert")
  30. ErrRequiresNewState error = 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 int
  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. Timestamp time.Time
  127. }
  128. type DeleteAlertCommand struct {
  129. AlertId int64
  130. }
  131. //Queries
  132. type GetAlertsQuery struct {
  133. OrgId int64
  134. State []string
  135. DashboardId int64
  136. PanelId int64
  137. Limit int64
  138. Result []*Alert
  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 AlertStateInfoDTO struct {
  153. Id int64 `json:"id"`
  154. DashboardId int64 `json:"dashboardId"`
  155. PanelId int64 `json:"panelId"`
  156. State AlertStateType `json:"state"`
  157. NewStateDate time.Time `json:"newStateDate"`
  158. }