alerts.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package models
  2. import (
  3. "github.com/grafana/grafana/pkg/components/simplejson"
  4. )
  5. type Alert struct {
  6. Id int64
  7. DashboardId int64
  8. PanelId int64
  9. Query string
  10. QueryRefId string
  11. WarnLevel int64
  12. ErrorLevel int64
  13. Interval int64
  14. Title string
  15. Description string
  16. QueryRange string
  17. Aggregator string
  18. }
  19. func (cmd *SaveDashboardCommand) GetAlertModels() *[]Alert {
  20. dash := NewDashboardFromJson(cmd.Dashboard)
  21. alerts := make([]Alert, 0)
  22. for _, rowObj := range cmd.Dashboard.Get("rows").MustArray() {
  23. row := simplejson.NewFromAny(rowObj)
  24. for _, panelObj := range row.Get("panels").MustArray() {
  25. panel := simplejson.NewFromAny(panelObj)
  26. alerting := panel.Get("alerting")
  27. alert := Alert{
  28. DashboardId: dash.Id,
  29. PanelId: panel.Get("id").MustInt64(),
  30. Id: alerting.Get("id").MustInt64(),
  31. QueryRefId: alerting.Get("query_ref").MustString(),
  32. WarnLevel: alerting.Get("warn_level").MustInt64(),
  33. ErrorLevel: alerting.Get("error_level").MustInt64(),
  34. Interval: alerting.Get("interval").MustInt64(),
  35. Title: alerting.Get("title").MustString(),
  36. Description: alerting.Get("description").MustString(),
  37. QueryRange: alerting.Get("query_range").MustString(),
  38. Aggregator: alerting.Get("aggregator").MustString(),
  39. }
  40. for _, targetsObj := range panel.Get("targets").MustArray() {
  41. target := simplejson.NewFromAny(targetsObj)
  42. if target.Get("refId").MustString() == alert.QueryRefId {
  43. alert.Query = target.Get("target").MustString()
  44. continue
  45. }
  46. }
  47. if alert.Query != "" {
  48. alerts = append(alerts, alert)
  49. }
  50. }
  51. }
  52. return &alerts
  53. }
  54. // Commands
  55. type SaveAlertsCommand struct {
  56. DashboardId int64
  57. UserId int64
  58. OrgId int64
  59. Alerts *[]Alert
  60. }