alert.go 2.6 KB

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