alert.go 3.0 KB

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