alerts.go 2.1 KB

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