alerts.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 AlertingClusterInfo struct {
  29. ServerId string
  30. ClusterSize int
  31. UptimePosition int
  32. }
  33. type HeartBeatCommand struct {
  34. ServerId string
  35. Result AlertingClusterInfo
  36. }
  37. type AlertRuleChange struct {
  38. Id int64 `json:"id"`
  39. OrgId int64 `json:"-"`
  40. AlertId int64 `json:"alertId"`
  41. Type string `json:"type"`
  42. Created time.Time `json:"created"`
  43. }
  44. func (cmd *SaveDashboardCommand) GetAlertModels() []AlertRule {
  45. alerts := make([]AlertRule, 0)
  46. for _, rowObj := range cmd.Dashboard.Get("rows").MustArray() {
  47. row := simplejson.NewFromAny(rowObj)
  48. for _, panelObj := range row.Get("panels").MustArray() {
  49. panel := simplejson.NewFromAny(panelObj)
  50. alerting := panel.Get("alerting")
  51. alert := AlertRule{
  52. DashboardId: cmd.Result.Id,
  53. OrgId: cmd.Result.OrgId,
  54. PanelId: panel.Get("id").MustInt64(),
  55. Id: alerting.Get("id").MustInt64(),
  56. QueryRefId: alerting.Get("queryRef").MustString(),
  57. WarnLevel: alerting.Get("warnLevel").MustInt64(),
  58. CritLevel: alerting.Get("critLevel").MustInt64(),
  59. WarnOperator: alerting.Get("warnOperator").MustString(),
  60. CritOperator: alerting.Get("critOperator").MustString(),
  61. Interval: alerting.Get("interval").MustString(),
  62. Title: alerting.Get("title").MustString(),
  63. Description: alerting.Get("description").MustString(),
  64. QueryRange: alerting.Get("queryRange").MustString(),
  65. Aggregator: alerting.Get("aggregator").MustString(),
  66. }
  67. for _, targetsObj := range panel.Get("targets").MustArray() {
  68. target := simplejson.NewFromAny(targetsObj)
  69. if target.Get("refId").MustString() == alert.QueryRefId {
  70. targetJson, err := target.MarshalJSON()
  71. if err == nil {
  72. alert.Query = string(targetJson)
  73. }
  74. continue
  75. }
  76. }
  77. if alert.Query != "" {
  78. alerts = append(alerts, alert)
  79. }
  80. }
  81. }
  82. return alerts
  83. }
  84. // Commands
  85. type SaveAlertsCommand struct {
  86. DashboardId int64
  87. UserId int64
  88. OrgId int64
  89. Alerts []AlertRule
  90. }
  91. type DeleteAlertCommand struct {
  92. AlertId int64
  93. }
  94. //Queries
  95. type GetAlertsQuery struct {
  96. OrgId int64
  97. State []string
  98. DashboardId int64
  99. PanelId int64
  100. Result []AlertRule
  101. }
  102. type GetAlertsForExecutionQuery struct {
  103. Timestamp int64
  104. Result []AlertRule
  105. }
  106. type GetAlertByIdQuery struct {
  107. Id int64
  108. Result AlertRule
  109. }
  110. type GetAlertChangesQuery struct {
  111. OrgId int64
  112. Limit int64
  113. SinceId int64
  114. Result []AlertRuleChange
  115. }