mirror of https://github.com/grafana/grafana
parent
205afd7212
commit
abc1ae3956
@ -1,8 +1,47 @@ |
||||
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 { |
||||
ts := &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 |
||||
} |
||||
|
@ -0,0 +1,36 @@ |
||||
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) |
||||
}) |
||||
}) |
||||
} |
@ -0,0 +1,58 @@ |
||||
package alerting |
||||
|
||||
import ( |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
. "github.com/smartystreets/goconvey/convey" |
||||
"testing" |
||||
) |
||||
|
||||
func TestAlertingExecutor(t *testing.T) { |
||||
Convey("Test alert execution", t, func() { |
||||
executor := &ExecutorImpl{} |
||||
|
||||
Convey("Show return ok since avg is above 2", func() { |
||||
rule := m.AlertRule{CritLevel: 10, CritOperator: "<", Aggregator: "sum"} |
||||
|
||||
timeseries := []*m.TimeSeries{ |
||||
m.NewTimeSeries("test1", [][2]float64{{2, 0}}), |
||||
} |
||||
|
||||
result := executor.ValidateRule(rule, timeseries) |
||||
So(result.State, ShouldEqual, m.AlertStateOk) |
||||
}) |
||||
|
||||
Convey("Show return critical since below 2", func() { |
||||
rule := m.AlertRule{CritLevel: 10, CritOperator: ">", Aggregator: "sum"} |
||||
|
||||
timeseries := []*m.TimeSeries{ |
||||
m.NewTimeSeries("test1", [][2]float64{{2, 0}}), |
||||
} |
||||
|
||||
result := executor.ValidateRule(rule, timeseries) |
||||
So(result.State, ShouldEqual, m.AlertStateCritical) |
||||
}) |
||||
|
||||
Convey("Show return critical since sum is above 10", func() { |
||||
rule := m.AlertRule{CritLevel: 10, CritOperator: "<", Aggregator: "sum"} |
||||
|
||||
timeseries := []*m.TimeSeries{ |
||||
m.NewTimeSeries("test1", [][2]float64{{9, 0}, {9, 0}}), |
||||
} |
||||
|
||||
result := executor.ValidateRule(rule, timeseries) |
||||
So(result.State, ShouldEqual, m.AlertStateCritical) |
||||
}) |
||||
/* |
||||
Convey("Show return ok since avg is below 10", func() { |
||||
rule := m.AlertRule{CritLevel: 10, CritOperator: "<", Aggregator: "avg"} |
||||
|
||||
timeseries := []*m.TimeSeries{ |
||||
m.NewTimeSeries("test1", [][2]float64{{9, 0}, {9, 0}}), |
||||
} |
||||
|
||||
result := executor.ValidateRule(rule, timeseries) |
||||
So(result.State, ShouldEqual, m.AlertStateOk) |
||||
}) |
||||
*/ |
||||
}) |
||||
} |
Loading…
Reference in new issue