alert.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. AlertId int64
  109. Paused bool
  110. }
  111. type SetAlertStateCommand struct {
  112. AlertId int64
  113. OrgId int64
  114. State AlertStateType
  115. Error string
  116. EvalData *simplejson.Json
  117. Timestamp time.Time
  118. }
  119. type DeleteAlertCommand struct {
  120. AlertId int64
  121. }
  122. //Queries
  123. type GetAlertsQuery struct {
  124. OrgId int64
  125. State []string
  126. DashboardId int64
  127. PanelId int64
  128. Limit int64
  129. Result []*Alert
  130. }
  131. type GetAllAlertsQuery struct {
  132. Result []*Alert
  133. }
  134. type GetAlertByIdQuery struct {
  135. Id int64
  136. Result *Alert
  137. }
  138. type GetAlertStatesForDashboardQuery struct {
  139. OrgId int64
  140. DashboardId int64
  141. Result []*AlertStateInfoDTO
  142. }
  143. type AlertStateInfoDTO struct {
  144. Id int64 `json:"id"`
  145. DashboardId int64 `json:"dashboardId"`
  146. PanelId int64 `json:"panelId"`
  147. State AlertStateType `json:"state"`
  148. NewStateDate time.Time `json:"newStateDate"`
  149. }