alert.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 SetAlertStateCommand struct {
  83. AlertId int64
  84. OrgId int64
  85. State AlertStateType
  86. Error string
  87. EvalData *simplejson.Json
  88. Timestamp time.Time
  89. }
  90. type DeleteAlertCommand struct {
  91. AlertId int64
  92. }
  93. //Queries
  94. type GetAlertsQuery struct {
  95. OrgId int64
  96. State []string
  97. DashboardId int64
  98. PanelId int64
  99. Limit int64
  100. Result []*Alert
  101. }
  102. type GetAllAlertsQuery struct {
  103. Result []*Alert
  104. }
  105. type GetAlertByIdQuery struct {
  106. Id int64
  107. Result *Alert
  108. }
  109. type GetAlertStatesForDashboardQuery struct {
  110. OrgId int64
  111. DashboardId int64
  112. Result []*AlertStateInfoDTO
  113. }
  114. type AlertStateInfoDTO struct {
  115. Id int64 `json:"id"`
  116. DashboardId int64 `json:"dashboardId"`
  117. PanelId int64 `json:"panelId"`
  118. State AlertStateType `json:"state"`
  119. NewStateDate time.Time `json:"newStateDate"`
  120. }