alert.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package models
  2. import (
  3. "time"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. )
  6. type AlertStateType string
  7. type AlertSeverityType string
  8. type NoDataOption string
  9. type ExecutionErrorOption string
  10. const (
  11. AlertStateNoData AlertStateType = "no_data"
  12. AlertStatePaused AlertStateType = "paused"
  13. AlertStateAlerting AlertStateType = "alerting"
  14. AlertStateOK AlertStateType = "ok"
  15. AlertStatePending AlertStateType = "pending"
  16. )
  17. const (
  18. NoDataSetOK NoDataOption = "ok"
  19. NoDataSetNoData NoDataOption = "no_data"
  20. NoDataKeepState NoDataOption = "keep_state"
  21. NoDataSetAlerting NoDataOption = "alerting"
  22. )
  23. const (
  24. ExecutionErrorSetAlerting ExecutionErrorOption = "alerting"
  25. ExecutionErrorKeepState ExecutionErrorOption = "keep_state"
  26. )
  27. func (s AlertStateType) IsValid() bool {
  28. return s == AlertStateOK || s == AlertStateNoData || s == AlertStatePaused || s == AlertStatePending
  29. }
  30. func (s NoDataOption) IsValid() bool {
  31. return s == NoDataSetNoData || s == NoDataSetAlerting || s == NoDataKeepState || s == NoDataSetOK
  32. }
  33. func (s NoDataOption) ToAlertState() AlertStateType {
  34. return AlertStateType(s)
  35. }
  36. func (s ExecutionErrorOption) IsValid() bool {
  37. return s == ExecutionErrorSetAlerting || s == ExecutionErrorKeepState
  38. }
  39. func (s ExecutionErrorOption) ToAlertState() AlertStateType {
  40. return AlertStateType(s)
  41. }
  42. type Alert struct {
  43. Id int64
  44. Version int64
  45. OrgId int64
  46. DashboardId int64
  47. PanelId int64
  48. Name string
  49. Message string
  50. Severity string
  51. State AlertStateType
  52. Handler int64
  53. Silenced bool
  54. ExecutionError string
  55. Frequency int64
  56. EvalData *simplejson.Json
  57. EvalDate time.Time
  58. NewStateDate time.Time
  59. StateChanges int
  60. Created time.Time
  61. Updated time.Time
  62. Settings *simplejson.Json
  63. }
  64. func (alert *Alert) ValidToSave() bool {
  65. return alert.DashboardId != 0 && alert.OrgId != 0 && alert.PanelId != 0
  66. }
  67. func (alert *Alert) ShouldUpdateState(newState AlertStateType) bool {
  68. return alert.State != newState
  69. }
  70. func (this *Alert) ContainsUpdates(other *Alert) bool {
  71. result := false
  72. result = result || this.Name != other.Name
  73. result = result || this.Message != other.Message
  74. if this.Settings != nil && other.Settings != nil {
  75. json1, err1 := this.Settings.Encode()
  76. json2, err2 := other.Settings.Encode()
  77. if err1 != nil || err2 != nil {
  78. return false
  79. }
  80. result = result || string(json1) != string(json2)
  81. }
  82. //don't compare .State! That would be insane.
  83. return result
  84. }
  85. type AlertingClusterInfo struct {
  86. ServerId string
  87. ClusterSize int
  88. UptimePosition int
  89. }
  90. type HeartBeat struct {
  91. Id int64
  92. ServerId string
  93. Updated time.Time
  94. Created time.Time
  95. }
  96. type HeartBeatCommand struct {
  97. ServerId string
  98. Result AlertingClusterInfo
  99. }
  100. type SaveAlertsCommand struct {
  101. DashboardId int64
  102. UserId int64
  103. OrgId int64
  104. Alerts []*Alert
  105. }
  106. type PauseAlertCommand struct {
  107. OrgId int64
  108. AlertIds []int64
  109. ResultCount int64
  110. Paused bool
  111. }
  112. type SetAlertStateCommand struct {
  113. AlertId int64
  114. OrgId int64
  115. State AlertStateType
  116. Error string
  117. EvalData *simplejson.Json
  118. Timestamp time.Time
  119. }
  120. type DeleteAlertCommand struct {
  121. AlertId int64
  122. }
  123. //Queries
  124. type GetAlertsQuery struct {
  125. OrgId int64
  126. State []string
  127. DashboardId int64
  128. PanelId int64
  129. Limit int64
  130. Result []*Alert
  131. }
  132. type GetAllAlertsQuery struct {
  133. Result []*Alert
  134. }
  135. type GetAlertByIdQuery struct {
  136. Id int64
  137. Result *Alert
  138. }
  139. type GetAlertStatesForDashboardQuery struct {
  140. OrgId int64
  141. DashboardId int64
  142. Result []*AlertStateInfoDTO
  143. }
  144. type AlertStateInfoDTO struct {
  145. Id int64 `json:"id"`
  146. DashboardId int64 `json:"dashboardId"`
  147. PanelId int64 `json:"panelId"`
  148. State AlertStateType `json:"state"`
  149. NewStateDate time.Time `json:"newStateDate"`
  150. }