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. OrgId int64
  28. DashboardId int64
  29. PanelId int64
  30. Name string
  31. Message string
  32. Severity AlertSeverityType
  33. State AlertStateType
  34. Handler int64
  35. Paused bool
  36. Silenced bool
  37. ExecutionError string
  38. Frequency int64
  39. LastEvalData *simplejson.Json
  40. LastEvalTime time.Time
  41. CreatedBy int64
  42. UpdatedBy int64
  43. Created time.Time
  44. Updated time.Time
  45. Settings *simplejson.Json
  46. }
  47. func (alert *Alert) ValidToSave() bool {
  48. return alert.DashboardId != 0 && alert.OrgId != 0 && alert.PanelId != 0
  49. }
  50. func (alert *Alert) ShouldUpdateState(newState AlertStateType) bool {
  51. return alert.State != newState
  52. }
  53. func (this *Alert) ContainsUpdates(other *Alert) bool {
  54. result := false
  55. result = result || this.Name != other.Name
  56. result = result || this.Message != other.Message
  57. if this.Settings != nil && other.Settings != nil {
  58. json1, err1 := this.Settings.Encode()
  59. json2, err2 := other.Settings.Encode()
  60. if err1 != nil || err2 != nil {
  61. return false
  62. }
  63. result = result || string(json1) != string(json2)
  64. }
  65. //don't compare .State! That would be insane.
  66. return result
  67. }
  68. type AlertingClusterInfo struct {
  69. ServerId string
  70. ClusterSize int
  71. UptimePosition int
  72. }
  73. type HeartBeat struct {
  74. Id int64
  75. ServerId string
  76. Updated time.Time
  77. Created time.Time
  78. }
  79. type HeartBeatCommand struct {
  80. ServerId string
  81. Result AlertingClusterInfo
  82. }
  83. type SaveAlertsCommand struct {
  84. DashboardId int64
  85. UserId int64
  86. OrgId int64
  87. Alerts []*Alert
  88. }
  89. type SetAlertStateCommand struct {
  90. AlertId int64
  91. OrgId int64
  92. State AlertStateType
  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. Result []*Alert
  105. }
  106. type GetAllAlertsQuery struct {
  107. Result []*Alert
  108. }
  109. type GetAlertByIdQuery struct {
  110. Id int64
  111. Result *Alert
  112. }