alert.go 2.5 KB

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