chore(alerting): move aggregations into alerting package

pull/5622/head
bergquist 9 years ago
parent 7224ea5229
commit 7c3dbe2a38
  1. 36
      pkg/models/timeseries.go
  2. 36
      pkg/models/timeseries_test.go
  3. 64
      pkg/services/alerting/executor.go

@ -1,49 +1,15 @@
package models
import "math"
type TimeSeries struct {
Name string `json:"name"`
Points [][2]float64 `json:"points"`
Avg float64
Sum float64
Min float64
Max float64
Mean float64
}
type TimeSeriesSlice []*TimeSeries
func NewTimeSeries(name string, points [][2]float64) *TimeSeries {
//Todo: This should be made safer :)
ts := &TimeSeries{
return &TimeSeries{
Name: name,
Points: points,
}
ts.Min = points[0][0]
ts.Max = points[0][0]
for _, v := range points {
value := v[0]
if value > ts.Max {
ts.Max = value
}
if value < ts.Min {
ts.Min = value
}
ts.Sum += value
}
ts.Avg = ts.Sum / float64(len(points))
midPosition := int64(math.Floor(float64(len(points)) / float64(2)))
ts.Mean = points[midPosition][0]
return ts
}

@ -1,36 +0,0 @@
package models
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestTimeSeries(t *testing.T) {
Convey("timeseries aggregation tests", t, func() {
ts := NewTimeSeries("test", [][2]float64{
{1, 0},
{2, 0},
{3, 0},
})
Convey("sum", func() {
So(ts.Sum, ShouldEqual, 6)
})
Convey("avg", func() {
So(ts.Avg, ShouldEqual, 2)
})
Convey("min", func() {
So(ts.Min, ShouldEqual, 1)
})
Convey("max", func() {
So(ts.Max, ShouldEqual, 3)
})
Convey("mean", func() {
So(ts.Mean, ShouldEqual, 2)
})
})
}

@ -4,6 +4,7 @@ import (
"fmt"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting/graphite"
"math"
)
type Executor interface {
@ -24,11 +25,50 @@ var operators map[string]compareFn = map[string]compareFn{
}
var aggregator map[string]aggregationFn = map[string]aggregationFn{
"avg": func(series *m.TimeSeries) float64 { return series.Avg },
"sum": func(series *m.TimeSeries) float64 { return series.Sum },
"min": func(series *m.TimeSeries) float64 { return series.Min },
"max": func(series *m.TimeSeries) float64 { return series.Max },
"mean": func(series *m.TimeSeries) float64 { return series.Mean },
"avg": func(series *m.TimeSeries) float64 {
sum := float64(0)
for _, v := range series.Points {
sum += v[0]
}
return sum / float64(len(series.Points))
},
"sum": func(series *m.TimeSeries) float64 {
sum := float64(0)
for _, v := range series.Points {
sum += v[0]
}
return sum
},
"min": func(series *m.TimeSeries) float64 {
min := series.Points[0][0]
for _, v := range series.Points {
if v[0] < min {
min = v[0]
}
}
return min
},
"max": func(series *m.TimeSeries) float64 {
max := series.Points[0][0]
for _, v := range series.Points {
if v[0] > max {
max = v[0]
}
}
return max
},
"mean": func(series *m.TimeSeries) float64 {
midPosition := int64(math.Floor(float64(len(series.Points)) / float64(2)))
return series.Points[midPosition][0]
},
}
func (this *ExecutorImpl) GetSeries(job *m.AlertJob) (m.TimeSeriesSlice, error) {
@ -58,11 +98,21 @@ func (this *ExecutorImpl) ValidateRule(rule m.AlertRule, series m.TimeSeriesSlic
var aggValue = aggregator[rule.Aggregator](serie)
if operators[rule.CritOperator](aggValue, rule.CritLevel) {
return &m.AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: aggValue, Rule: rule}
return &m.AlertResult{
State: m.AlertStateCritical,
Id: rule.Id,
ActualValue: aggValue,
Rule: rule,
}
}
if operators[rule.WarnOperator](aggValue, rule.WarnLevel) {
return &m.AlertResult{State: m.AlertStateWarn, Id: rule.Id, ActualValue: aggValue, Rule: rule}
return &m.AlertResult{
State: m.AlertStateWarn,
Id: rule.Id,
ActualValue: aggValue,
Rule: rule,
}
}
}

Loading…
Cancel
Save