From 6121d15ba7b8541d53e602bf3b33d1a10145b7f0 Mon Sep 17 00:00:00 2001 From: bergquist Date: Thu, 23 Jun 2016 16:07:23 +0200 Subject: [PATCH] feat(alerting): more aggressive requirements for parsing alertrules --- pkg/services/alerting/alert_rule.go | 17 +++++++++++++++-- pkg/services/alerting/alert_rule_test.go | 2 +- pkg/services/alerting/engine.go | 2 -- pkg/services/alerting/extractor_test.go | 6 +++--- pkg/services/alerting/models.go | 1 - 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pkg/services/alerting/alert_rule.go b/pkg/services/alerting/alert_rule.go index 77f21255156..cc5968eeb70 100644 --- a/pkg/services/alerting/alert_rule.go +++ b/pkg/services/alerting/alert_rule.go @@ -4,6 +4,7 @@ import ( "fmt" "regexp" "strconv" + "strings" "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/services/alerting/transformers" @@ -63,7 +64,16 @@ func NewAlertRuleFromDBModel(ruleDef *m.Alert) (*AlertRule, error) { model.State = ruleDef.State model.Frequency = ruleDef.Frequency - model.NotificationGroups = []int64{1, 2} + ngs := ruleDef.Settings.Get("notificationGroups").MustString() + var ids []int64 + for _, v := range strings.Split(ngs, ",") { + id, err := strconv.Atoi(v) + if err == nil { + ids = append(ids, int64(id)) + } + } + + model.NotificationGroups = ids critical := ruleDef.Settings.Get("crit") model.Critical = Level{ @@ -78,6 +88,10 @@ func NewAlertRuleFromDBModel(ruleDef *m.Alert) (*AlertRule, error) { } model.Transform = ruleDef.Settings.Get("transform").Get("type").MustString() + if model.Transform == "" { + return nil, fmt.Errorf("missing transform") + } + model.TransformParams = *ruleDef.Settings.Get("transform") if model.Transform == "aggregation" { @@ -91,7 +105,6 @@ func NewAlertRuleFromDBModel(ruleDef *m.Alert) (*AlertRule, error) { DatasourceId: query.Get("datasourceId").MustInt64(), From: query.Get("from").MustString(), To: query.Get("to").MustString(), - Aggregator: query.Get("agg").MustString(), } if model.Query.Query == "" { diff --git a/pkg/services/alerting/alert_rule_test.go b/pkg/services/alerting/alert_rule_test.go index f02ce9e40c5..8050dd46aa9 100644 --- a/pkg/services/alerting/alert_rule_test.go +++ b/pkg/services/alerting/alert_rule_test.go @@ -55,7 +55,7 @@ func TestAlertRuleModel(t *testing.T) { "datasourceId": 1 }, "transform": { - "method": "avg", + "type": "avg", "name": "aggregation" } } diff --git a/pkg/services/alerting/engine.go b/pkg/services/alerting/engine.go index 6061cf42d03..4e002e9eb0d 100644 --- a/pkg/services/alerting/engine.go +++ b/pkg/services/alerting/engine.go @@ -132,12 +132,10 @@ func (e *Engine) resultHandler() { result.State = alertstates.Critical result.Description = fmt.Sprintf("Failed to run check after %d retires, Error: %v", maxAlertExecutionRetries, result.Error) - //e.reactToState(result) e.responseHandler.Handle(result) } } else { result.AlertJob.ResetRetry() - //e.reactToState(result) e.responseHandler.Handle(result) } } diff --git a/pkg/services/alerting/extractor_test.go b/pkg/services/alerting/extractor_test.go index 7e032d1a3ac..88fab4bf70d 100644 --- a/pkg/services/alerting/extractor_test.go +++ b/pkg/services/alerting/extractor_test.go @@ -52,8 +52,8 @@ func TestAlertRuleExtraction(t *testing.T) { "to": "now" }, "transform": { - "method": "avg", - "type": "aggregation" + "type": "avg", + "name": "aggregation" }, "warn": { "value": 10, @@ -87,7 +87,7 @@ func TestAlertRuleExtraction(t *testing.T) { "to": "now" }, "transform": { - "method": "avg", + "type": "avg", "name": "aggregation" }, "warn": { diff --git a/pkg/services/alerting/models.go b/pkg/services/alerting/models.go index aff42603412..e3b1722a689 100644 --- a/pkg/services/alerting/models.go +++ b/pkg/services/alerting/models.go @@ -47,7 +47,6 @@ type Level struct { type AlertQuery struct { Query string DatasourceId int64 - Aggregator string From string To string }