alert.go 3.7 KB

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