alert.go 3.4 KB

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