alert.go 2.4 KB

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