alert.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. )
  31. func (s AlertStateType) IsValid() bool {
  32. return s == AlertStateOK || s == AlertStateNoData || s == AlertStatePaused || s == AlertStatePending
  33. }
  34. func (s NoDataOption) IsValid() bool {
  35. return s == NoDataSetNoData || s == NoDataSetAlerting || s == NoDataKeepState || s == NoDataSetOK
  36. }
  37. func (s NoDataOption) ToAlertState() AlertStateType {
  38. return AlertStateType(s)
  39. }
  40. func (s ExecutionErrorOption) IsValid() bool {
  41. return s == ExecutionErrorSetAlerting || s == ExecutionErrorKeepState
  42. }
  43. func (s ExecutionErrorOption) ToAlertState() AlertStateType {
  44. return AlertStateType(s)
  45. }
  46. type Alert struct {
  47. Id int64
  48. Version int64
  49. OrgId int64
  50. DashboardId int64
  51. PanelId int64
  52. Name string
  53. Message string
  54. Severity string
  55. State AlertStateType
  56. Handler int64
  57. Silenced bool
  58. ExecutionError string
  59. Frequency int64
  60. EvalData *simplejson.Json
  61. NewStateDate time.Time
  62. StateChanges int
  63. Created time.Time
  64. Updated time.Time
  65. Settings *simplejson.Json
  66. }
  67. func (alert *Alert) ValidToSave() bool {
  68. return alert.DashboardId != 0 && alert.OrgId != 0 && alert.PanelId != 0
  69. }
  70. func (alert *Alert) ShouldUpdateState(newState AlertStateType) bool {
  71. return alert.State != newState
  72. }
  73. func (this *Alert) ContainsUpdates(other *Alert) bool {
  74. result := false
  75. result = result || this.Name != other.Name
  76. result = result || this.Message != other.Message
  77. if this.Settings != nil && other.Settings != nil {
  78. json1, err1 := this.Settings.Encode()
  79. json2, err2 := other.Settings.Encode()
  80. if err1 != nil || err2 != nil {
  81. return false
  82. }
  83. result = result || string(json1) != string(json2)
  84. }
  85. //don't compare .State! That would be insane.
  86. return result
  87. }
  88. type AlertingClusterInfo struct {
  89. ServerId string
  90. ClusterSize int
  91. UptimePosition int
  92. }
  93. type HeartBeat struct {
  94. Id int64
  95. ServerId string
  96. Updated time.Time
  97. Created time.Time
  98. }
  99. type HeartBeatCommand struct {
  100. ServerId string
  101. Result AlertingClusterInfo
  102. }
  103. type SaveAlertsCommand struct {
  104. DashboardId int64
  105. UserId int64
  106. OrgId int64
  107. Alerts []*Alert
  108. }
  109. type PauseAlertCommand struct {
  110. OrgId int64
  111. AlertIds []int64
  112. ResultCount int64
  113. Paused bool
  114. }
  115. type PauseAllAlertCommand struct {
  116. ResultCount int64
  117. Paused bool
  118. }
  119. type SetAlertStateCommand struct {
  120. AlertId int64
  121. OrgId int64
  122. State AlertStateType
  123. Error string
  124. EvalData *simplejson.Json
  125. Timestamp time.Time
  126. }
  127. type DeleteAlertCommand struct {
  128. AlertId int64
  129. }
  130. //Queries
  131. type GetAlertsQuery struct {
  132. OrgId int64
  133. State []string
  134. DashboardId int64
  135. PanelId int64
  136. Limit int64
  137. Result []*Alert
  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 AlertStateInfoDTO struct {
  152. Id int64 `json:"id"`
  153. DashboardId int64 `json:"dashboardId"`
  154. PanelId int64 `json:"panelId"`
  155. State AlertStateType `json:"state"`
  156. NewStateDate time.Time `json:"newStateDate"`
  157. }