alert.go 3.0 KB

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