mirror of
https://github.com/grafana/grafana.git
synced 2024-12-01 21:19:28 -06:00
test(alerting): add tests for simple reducer
This commit is contained in:
parent
4fd8b2ace4
commit
f2436fc7cd
33
pkg/services/alerting/conditions/reducer_test.go
Normal file
33
pkg/services/alerting/conditions/reducer_test.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package conditions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/tsdb"
|
||||||
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSimpleReducer(t *testing.T) {
|
||||||
|
Convey("Test simple reducer", t, func() {
|
||||||
|
Convey("can calculate avg of time serie", func() {
|
||||||
|
result := testReducer("avg", 1, 2, 3)
|
||||||
|
So(result, ShouldEqual, float64(2))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func testReducer(typ string, datapoints ...float64) float64 {
|
||||||
|
reducer := NewSimpleReducer(typ)
|
||||||
|
var timeserie [][2]float64
|
||||||
|
dummieTimestamp := float64(521452145)
|
||||||
|
|
||||||
|
for _, v := range datapoints {
|
||||||
|
timeserie = append(timeserie, [2]float64{v, dummieTimestamp})
|
||||||
|
}
|
||||||
|
|
||||||
|
tsdb := &tsdb.TimeSeries{
|
||||||
|
Name: "test time serie",
|
||||||
|
Points: timeserie,
|
||||||
|
}
|
||||||
|
return reducer.Reduce(tsdb)
|
||||||
|
}
|
@ -1,15 +0,0 @@
|
|||||||
package alerting
|
|
||||||
|
|
||||||
type compareFn func(float64, float64) bool
|
|
||||||
|
|
||||||
func evalCondition(level Level, result float64) bool {
|
|
||||||
return operators[level.Operator](result, level.Value)
|
|
||||||
}
|
|
||||||
|
|
||||||
var operators = map[string]compareFn{
|
|
||||||
">": func(num1, num2 float64) bool { return num1 > num2 },
|
|
||||||
">=": func(num1, num2 float64) bool { return num1 >= num2 },
|
|
||||||
"<": func(num1, num2 float64) bool { return num1 < num2 },
|
|
||||||
"<=": func(num1, num2 float64) bool { return num1 <= num2 },
|
|
||||||
"": func(num1, num2 float64) bool { return false },
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user