From 77b7cdfadba4fb9ab2bc85e2b61d34f4fd9307e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 16 May 2016 15:39:09 +0200 Subject: [PATCH] feat(alerting): added few fields to alert rule --- pkg/api/alerting/alerting.go | 34 +++++++++++++++++++++++++++++ pkg/models/alerts.go | 20 +++++++++++++---- pkg/services/sqlstore/alert_rule.go | 15 ++++++++----- 3 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 pkg/api/alerting/alerting.go diff --git a/pkg/api/alerting/alerting.go b/pkg/api/alerting/alerting.go new file mode 100644 index 00000000000..5a6dabf828b --- /dev/null +++ b/pkg/api/alerting/alerting.go @@ -0,0 +1,34 @@ +package alerting + +import ( + "time" + + m "github.com/grafana/grafana/pkg/models" +) + +func Init() { + go dispatcher() +} + +func dispatcher() { + + ticker := time.NewTicker(time.Second) + + for { + select { + case <-ticker.C: + scheduleJobs() + } + } +} + +func scheduleJobs() { + +} + +type Scheduler interface { +} + +type Executor interface { + Execute(rule *m.AlertRule) +} diff --git a/pkg/models/alerts.go b/pkg/models/alerts.go index 94bf430659c..8b723151c6b 100644 --- a/pkg/models/alerts.go +++ b/pkg/models/alerts.go @@ -1,13 +1,15 @@ package models import ( - "github.com/grafana/grafana/pkg/components/simplejson" "time" + + "github.com/grafana/grafana/pkg/components/simplejson" ) type AlertRule struct { Id int64 `json:"id"` OrgId int64 `json:"-"` + DataSourceId int64 `json:"datasourceId"` DashboardId int64 `json:"dashboardId"` PanelId int64 `json:"panelId"` Query string `json:"query"` @@ -17,11 +19,15 @@ type AlertRule struct { WarnOperator string `json:"warnOperator"` CritOperator string `json:"critOperator"` Interval string `json:"interval"` + Frequency int64 `json:"frequency"` Title string `json:"title"` Description string `json:"description"` QueryRange string `json:"queryRange"` Aggregator string `json:"aggregator"` State string `json:"state"` + + Created time.Time `json:"created"` + Updated time.Time `json:"updated"` } type AlertRuleChange struct { @@ -32,7 +38,7 @@ type AlertRuleChange struct { Created time.Time `json:"created"` } -func (cmd *SaveDashboardCommand) GetAlertModels() *[]AlertRule { +func (cmd *SaveDashboardCommand) GetAlertModels() []AlertRule { alerts := make([]AlertRule, 0) for _, rowObj := range cmd.Dashboard.Get("rows").MustArray() { @@ -77,7 +83,7 @@ func (cmd *SaveDashboardCommand) GetAlertModels() *[]AlertRule { } } - return &alerts + return alerts } // Commands @@ -86,7 +92,7 @@ type SaveAlertsCommand struct { UserId int64 OrgId int64 - Alerts *[]AlertRule + Alerts []AlertRule } type DeleteAlertCommand struct { @@ -103,6 +109,12 @@ type GetAlertsQuery struct { Result []AlertRule } +type GetAlertsForExecutionQuery struct { + Timestamp int64 + + Result []AlertRule +} + type GetAlertByIdQuery struct { Id int64 diff --git a/pkg/services/sqlstore/alert_rule.go b/pkg/services/sqlstore/alert_rule.go index c3157a7f617..3c2f7dc1fd2 100644 --- a/pkg/services/sqlstore/alert_rule.go +++ b/pkg/services/sqlstore/alert_rule.go @@ -3,10 +3,12 @@ package sqlstore import ( "bytes" "fmt" + "strings" + "time" + "github.com/go-xorm/xorm" "github.com/grafana/grafana/pkg/bus" m "github.com/grafana/grafana/pkg/models" - "strings" ) func init() { @@ -134,8 +136,8 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error { }) } -func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Session) error { - for _, alert := range *posted { +func upsertAlerts(alerts []m.AlertRule, posted []m.AlertRule, sess *xorm.Session) error { + for _, alert := range posted { update := false var alertToUpdate m.AlertRule @@ -149,6 +151,7 @@ func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Sessio if update { if alertIsDifferent(alertToUpdate, alert) { + alert.Updated = time.Now() alert.State = alertToUpdate.State _, err := sess.Id(alert.Id).Update(&alert) if err != nil { @@ -159,6 +162,8 @@ func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Sessio } } else { + alert.Updated = time.Now() + alert.Created = time.Now() alert.State = "OK" _, err := sess.Insert(&alert) if err != nil { @@ -171,11 +176,11 @@ func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Sessio return nil } -func deleteMissingAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Session) error { +func deleteMissingAlerts(alerts []m.AlertRule, posted []m.AlertRule, sess *xorm.Session) error { for _, missingAlert := range alerts { missing := true - for _, k := range *posted { + for _, k := range posted { if missingAlert.PanelId == k.PanelId { missing = false }