feat(alerting): add copy of AlertRule to AlertResult

This commit is contained in:
bergquist
2016-05-31 13:27:12 +02:00
parent 1ded0b30c2
commit 338fdcb576
4 changed files with 35 additions and 23 deletions
+18 -9
View File
@@ -1,6 +1,7 @@
package alerting
import (
"fmt"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting/graphite"
)
@@ -30,14 +31,22 @@ var aggregator map[string]aggregationFn = map[string]aggregationFn{
"mean": func(series *m.TimeSeries) float64 { return series.Mean },
}
func (this *ExecutorImpl) Execute(rule *m.AlertJob, responseQueue chan *m.AlertResult) {
response, err := graphite.GraphiteClient{}.GetSeries(rule)
if err != nil {
responseQueue <- &m.AlertResult{State: "PENDING", Id: rule.Rule.Id}
func (this *ExecutorImpl) GetSeries(job *m.AlertJob) (m.TimeSeriesSlice, error) {
if job.Datasource.Type == m.DS_GRAPHITE {
return graphite.GraphiteClient{}.GetSeries(job)
}
responseQueue <- this.ValidateRule(rule.Rule, response)
return nil, fmt.Errorf("Grafana does not support alerts for %s", job.Datasource.Type)
}
func (this *ExecutorImpl) Execute(job *m.AlertJob, responseQueue chan *m.AlertResult) {
response, err := this.GetSeries(job)
if err != nil {
responseQueue <- &m.AlertResult{State: "PENDING", Id: job.Rule.Id, Rule: job.Rule}
}
responseQueue <- this.ValidateRule(job.Rule, response)
}
func (this *ExecutorImpl) ValidateRule(rule m.AlertRule, series m.TimeSeriesSlice) *m.AlertResult {
@@ -49,13 +58,13 @@ func (this *ExecutorImpl) ValidateRule(rule m.AlertRule, series m.TimeSeriesSlic
var aggValue = aggregator[rule.Aggregator](serie)
if operators[rule.CritOperator](aggValue, float64(rule.CritLevel)) {
return &m.AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: aggValue}
return &m.AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: aggValue, Rule: rule}
}
if operators[rule.WarnOperator](aggValue, float64(rule.WarnLevel)) {
return &m.AlertResult{State: m.AlertStateWarn, Id: rule.Id, ActualValue: aggValue}
return &m.AlertResult{State: m.AlertStateWarn, Id: rule.Id, ActualValue: aggValue, Rule: rule}
}
}
return &m.AlertResult{State: m.AlertStateOk, Id: rule.Id}
return &m.AlertResult{State: m.AlertStateOk, Id: rule.Id, Rule: rule}
}