alerts.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package models
  2. import (
  3. "time"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. )
  6. type AlertRule struct {
  7. Id int64 `json:"id"`
  8. OrgId int64 `json:"-"`
  9. //DataSourceId int64 `json:"datasourceId"`
  10. DashboardId int64 `json:"dashboardId"`
  11. PanelId int64 `json:"panelId"`
  12. Query string `json:"query"`
  13. QueryRefId string `json:"queryRefId"`
  14. WarnLevel int64 `json:"warnLevel"`
  15. CritLevel int64 `json:"critLevel"`
  16. WarnOperator string `json:"warnOperator"`
  17. CritOperator string `json:"critOperator"`
  18. Interval string `json:"interval"`
  19. Frequency int64 `json:"frequency"`
  20. Title string `json:"title"`
  21. Description string `json:"description"`
  22. QueryRange string `json:"queryRange"`
  23. Aggregator string `json:"aggregator"`
  24. State string `json:"state"`
  25. Created time.Time `json:"created"`
  26. Updated time.Time `json:"updated"`
  27. }
  28. type HeartBeat struct {
  29. ServerId string
  30. Updated time.Time
  31. Created time.Time
  32. }
  33. type AlertRuleChange struct {
  34. Id int64 `json:"id"`
  35. OrgId int64 `json:"-"`
  36. AlertId int64 `json:"alertId"`
  37. Type string `json:"type"`
  38. Created time.Time `json:"created"`
  39. }
  40. func (cmd *SaveDashboardCommand) GetAlertModels() []AlertRule {
  41. alerts := make([]AlertRule, 0)
  42. for _, rowObj := range cmd.Dashboard.Get("rows").MustArray() {
  43. row := simplejson.NewFromAny(rowObj)
  44. for _, panelObj := range row.Get("panels").MustArray() {
  45. panel := simplejson.NewFromAny(panelObj)
  46. alerting := panel.Get("alerting")
  47. alert := AlertRule{
  48. DashboardId: cmd.Result.Id,
  49. OrgId: cmd.Result.OrgId,
  50. PanelId: panel.Get("id").MustInt64(),
  51. Id: alerting.Get("id").MustInt64(),
  52. QueryRefId: alerting.Get("queryRef").MustString(),
  53. WarnLevel: alerting.Get("warnLevel").MustInt64(),
  54. CritLevel: alerting.Get("critLevel").MustInt64(),
  55. WarnOperator: alerting.Get("warnOperator").MustString(),
  56. CritOperator: alerting.Get("critOperator").MustString(),
  57. Interval: alerting.Get("interval").MustString(),
  58. Title: alerting.Get("title").MustString(),
  59. Description: alerting.Get("description").MustString(),
  60. QueryRange: alerting.Get("queryRange").MustString(),
  61. Aggregator: alerting.Get("aggregator").MustString(),
  62. }
  63. for _, targetsObj := range panel.Get("targets").MustArray() {
  64. target := simplejson.NewFromAny(targetsObj)
  65. if target.Get("refId").MustString() == alert.QueryRefId {
  66. targetJson, err := target.MarshalJSON()
  67. if err == nil {
  68. alert.Query = string(targetJson)
  69. }
  70. continue
  71. }
  72. }
  73. if alert.Query != "" {
  74. alerts = append(alerts, alert)
  75. }
  76. }
  77. }
  78. return alerts
  79. }
  80. // Commands
  81. type SaveAlertsCommand struct {
  82. DashboardId int64
  83. UserId int64
  84. OrgId int64
  85. Alerts []AlertRule
  86. }
  87. type DeleteAlertCommand struct {
  88. AlertId int64
  89. }
  90. //Queries
  91. type GetAlertsQuery struct {
  92. OrgId int64
  93. State []string
  94. DashboardId int64
  95. PanelId int64
  96. Result []AlertRule
  97. }
  98. type GetAlertsForExecutionQuery struct {
  99. Timestamp int64
  100. Result []AlertRule
  101. }
  102. type GetAlertByIdQuery struct {
  103. Id int64
  104. Result AlertRule
  105. }
  106. type GetAlertChangesQuery struct {
  107. OrgId int64
  108. Limit int64
  109. SinceId int64
  110. Result []AlertRuleChange
  111. }