alert.go 2.9 KB

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