alert.go 2.4 KB

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