alert.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. Type string `json:"type"`
  62. Created time.Time `json:"created"`
  63. }
  64. // Commands
  65. type SaveAlertsCommand struct {
  66. DashboardId int64
  67. UserId int64
  68. OrgId int64
  69. Alerts []*Alert
  70. }
  71. type DeleteAlertCommand struct {
  72. AlertId int64
  73. }
  74. //Queries
  75. type GetAlertsQuery struct {
  76. OrgId int64
  77. State []string
  78. DashboardId int64
  79. PanelId int64
  80. Result []*Alert
  81. }
  82. type GetAllAlertsQuery struct {
  83. Result []*Alert
  84. }
  85. type GetAlertByIdQuery struct {
  86. Id int64
  87. Result *Alert
  88. }
  89. type GetAlertChangesQuery struct {
  90. OrgId int64
  91. Limit int64
  92. SinceId int64
  93. Result []*AlertChange
  94. }