alert.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package models
  2. import (
  3. "time"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. )
  6. type Alert struct {
  7. Id int64
  8. OrgId int64
  9. DashboardId int64
  10. PanelId int64
  11. Name string
  12. Description string
  13. Severity string
  14. State string
  15. Handler int64
  16. Enabled bool
  17. Frequency int64
  18. CreatedBy int64
  19. UpdatedBy int64
  20. Created time.Time
  21. Updated time.Time
  22. Settings *simplejson.Json
  23. }
  24. func (alert *Alert) ValidToSave() bool {
  25. return alert.DashboardId != 0 && alert.OrgId != 0 && alert.PanelId != 0
  26. }
  27. func (alert *Alert) ShouldUpdateState(newState string) bool {
  28. return alert.State != newState
  29. }
  30. func (this *Alert) ContainsUpdates(other *Alert) bool {
  31. result := false
  32. result = result || this.Name != other.Name
  33. result = result || this.Description != other.Description
  34. if this.Settings != nil && other.Settings != nil {
  35. json1, err1 := this.Settings.Encode()
  36. json2, err2 := other.Settings.Encode()
  37. if err1 != nil || err2 != nil {
  38. return false
  39. }
  40. result = result || string(json1) != string(json2)
  41. }
  42. //don't compare .State! That would be insane.
  43. return result
  44. }
  45. type AlertingClusterInfo struct {
  46. ServerId string
  47. ClusterSize int
  48. UptimePosition int
  49. }
  50. type HeartBeat struct {
  51. Id int64
  52. ServerId string
  53. Updated time.Time
  54. Created time.Time
  55. }
  56. type HeartBeatCommand struct {
  57. ServerId string
  58. Result AlertingClusterInfo
  59. }
  60. type AlertChange struct {
  61. Id int64 `json:"id"`
  62. OrgId int64 `json:"-"`
  63. AlertId int64 `json:"alertId"`
  64. UpdatedBy int64 `json:"updatedBy"`
  65. NewAlertSettings *simplejson.Json `json:"newAlertSettings"`
  66. Type string `json:"type"`
  67. Created time.Time `json:"created"`
  68. }
  69. // Commands
  70. type CreateAlertChangeCommand struct {
  71. OrgId int64
  72. AlertId int64
  73. UpdatedBy int64
  74. NewAlertSettings *simplejson.Json
  75. Type string
  76. }
  77. type SaveAlertsCommand struct {
  78. DashboardId int64
  79. UserId int64
  80. OrgId int64
  81. Alerts []*Alert
  82. }
  83. type DeleteAlertCommand struct {
  84. AlertId int64
  85. }
  86. //Queries
  87. type GetAlertsQuery struct {
  88. OrgId int64
  89. State []string
  90. DashboardId int64
  91. PanelId int64
  92. Result []*Alert
  93. }
  94. type GetAllAlertsQuery struct {
  95. Result []*Alert
  96. }
  97. type GetAlertByIdQuery struct {
  98. Id int64
  99. Result *Alert
  100. }
  101. type GetAlertChangesQuery struct {
  102. OrgId int64
  103. Limit int64
  104. SinceId int64
  105. Result []*AlertChange
  106. }