alerts.go 2.9 KB

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