alert.go 2.7 KB

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