alert.go 2.0 KB

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