alert.go 3.5 KB

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