2016-07-27 09:18:10 -05:00
|
|
|
package conditions
|
2016-07-20 02:30:31 -05:00
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
2016-07-20 02:30:31 -05:00
|
|
|
"testing"
|
|
|
|
|
2016-07-20 07:28:02 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2017-01-13 05:32:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/null"
|
2016-07-20 02:30:31 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-07-20 07:28:02 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2016-07-27 09:18:10 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
2016-07-20 07:28:02 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
2016-07-20 02:30:31 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestQueryCondition(t *testing.T) {
|
|
|
|
|
|
|
|
Convey("when evaluating query condition", t, func() {
|
|
|
|
|
2016-07-21 09:19:28 -05:00
|
|
|
queryConditionScenario("Given avg() and > 100", func(ctx *queryConditionTestContext) {
|
|
|
|
|
|
|
|
ctx.reducer = `{"type": "avg"}`
|
2016-08-16 03:22:16 -05:00
|
|
|
ctx.evaluator = `{"type": "gt", "params": [100]}`
|
2016-07-21 09:19:28 -05:00
|
|
|
|
2016-07-27 09:18:10 -05:00
|
|
|
Convey("Can read query condition from json model", func() {
|
|
|
|
ctx.exec()
|
|
|
|
|
|
|
|
So(ctx.condition.Query.From, ShouldEqual, "5m")
|
|
|
|
So(ctx.condition.Query.To, ShouldEqual, "now")
|
|
|
|
So(ctx.condition.Query.DatasourceId, ShouldEqual, 1)
|
|
|
|
|
|
|
|
Convey("Can read query reducer", func() {
|
|
|
|
reducer, ok := ctx.condition.Reducer.(*SimpleReducer)
|
|
|
|
So(ok, ShouldBeTrue)
|
|
|
|
So(reducer.Type, ShouldEqual, "avg")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Can read evaluator", func() {
|
2016-08-18 04:44:17 -05:00
|
|
|
evaluator, ok := ctx.condition.Evaluator.(*ThresholdEvaluator)
|
2016-07-27 09:18:10 -05:00
|
|
|
So(ok, ShouldBeTrue)
|
2016-08-16 01:12:55 -05:00
|
|
|
So(evaluator.Type, ShouldEqual, "gt")
|
2016-07-27 09:18:10 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-07-22 06:14:09 -05:00
|
|
|
Convey("should fire when avg is above 100", func() {
|
2016-09-28 02:15:48 -05:00
|
|
|
points := tsdb.NewTimeSeriesPointsFromArgs(120, 0)
|
|
|
|
ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", points)}
|
2016-11-03 09:26:17 -05:00
|
|
|
cr, err := ctx.exec()
|
2016-07-21 09:19:28 -05:00
|
|
|
|
2016-11-03 09:26:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cr.Firing, ShouldBeTrue)
|
2016-07-21 09:19:28 -05:00
|
|
|
})
|
|
|
|
|
2016-07-22 06:14:09 -05:00
|
|
|
Convey("Should not fire when avg is below 100", func() {
|
2016-09-28 02:15:48 -05:00
|
|
|
points := tsdb.NewTimeSeriesPointsFromArgs(90, 0)
|
|
|
|
ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", points)}
|
2016-11-03 09:26:17 -05:00
|
|
|
cr, err := ctx.exec()
|
2016-07-21 09:19:28 -05:00
|
|
|
|
2016-11-03 09:26:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cr.Firing, ShouldBeFalse)
|
2016-07-21 09:19:28 -05:00
|
|
|
})
|
2016-09-08 06:28:41 -05:00
|
|
|
|
|
|
|
Convey("Should fire if only first serie matches", func() {
|
|
|
|
ctx.series = tsdb.TimeSeriesSlice{
|
2016-09-28 02:15:48 -05:00
|
|
|
tsdb.NewTimeSeries("test1", tsdb.NewTimeSeriesPointsFromArgs(120, 0)),
|
|
|
|
tsdb.NewTimeSeries("test2", tsdb.NewTimeSeriesPointsFromArgs(0, 0)),
|
2016-09-08 06:28:41 -05:00
|
|
|
}
|
2016-11-03 09:26:17 -05:00
|
|
|
cr, err := ctx.exec()
|
2016-09-08 06:28:41 -05:00
|
|
|
|
2016-11-03 09:26:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cr.Firing, ShouldBeTrue)
|
2016-09-08 06:28:41 -05:00
|
|
|
})
|
2016-09-08 07:33:10 -05:00
|
|
|
|
2017-01-13 05:32:30 -06:00
|
|
|
Convey("No series", func() {
|
|
|
|
Convey("Should set NoDataFound when condition is gt", func() {
|
|
|
|
ctx.series = tsdb.TimeSeriesSlice{}
|
|
|
|
cr, err := ctx.exec()
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cr.Firing, ShouldBeFalse)
|
|
|
|
So(cr.NoDataFound, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should be firing when condition is no_value", func() {
|
|
|
|
ctx.evaluator = `{"type": "no_value", "params": []}`
|
|
|
|
ctx.series = tsdb.TimeSeriesSlice{}
|
|
|
|
cr, err := ctx.exec()
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cr.Firing, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-09-08 07:33:10 -05:00
|
|
|
Convey("Empty series", func() {
|
2017-01-13 05:32:30 -06:00
|
|
|
Convey("Should set Firing if eval match", func() {
|
|
|
|
ctx.evaluator = `{"type": "no_value", "params": []}`
|
|
|
|
ctx.series = tsdb.TimeSeriesSlice{
|
|
|
|
tsdb.NewTimeSeries("test1", tsdb.NewTimeSeriesPointsFromArgs()),
|
|
|
|
}
|
|
|
|
cr, err := ctx.exec()
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cr.Firing, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
|
2016-09-08 07:33:10 -05:00
|
|
|
Convey("Should set NoDataFound both series are empty", func() {
|
|
|
|
ctx.series = tsdb.TimeSeriesSlice{
|
2016-09-28 02:15:48 -05:00
|
|
|
tsdb.NewTimeSeries("test1", tsdb.NewTimeSeriesPointsFromArgs()),
|
|
|
|
tsdb.NewTimeSeries("test2", tsdb.NewTimeSeriesPointsFromArgs()),
|
2016-09-08 07:33:10 -05:00
|
|
|
}
|
2016-11-03 09:26:17 -05:00
|
|
|
cr, err := ctx.exec()
|
2016-09-08 07:33:10 -05:00
|
|
|
|
2016-11-03 09:26:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cr.NoDataFound, ShouldBeTrue)
|
2016-09-08 07:33:10 -05:00
|
|
|
})
|
|
|
|
|
2016-09-16 07:58:10 -05:00
|
|
|
Convey("Should set NoDataFound both series contains null", func() {
|
|
|
|
ctx.series = tsdb.TimeSeriesSlice{
|
2016-09-28 02:15:48 -05:00
|
|
|
tsdb.NewTimeSeries("test1", tsdb.TimeSeriesPoints{tsdb.TimePoint{null.FloatFromPtr(nil), null.FloatFrom(0)}}),
|
|
|
|
tsdb.NewTimeSeries("test2", tsdb.TimeSeriesPoints{tsdb.TimePoint{null.FloatFromPtr(nil), null.FloatFrom(0)}}),
|
2016-09-16 07:58:10 -05:00
|
|
|
}
|
2016-11-03 09:26:17 -05:00
|
|
|
cr, err := ctx.exec()
|
2016-09-16 07:58:10 -05:00
|
|
|
|
2016-11-03 09:26:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cr.NoDataFound, ShouldBeTrue)
|
2016-09-16 07:58:10 -05:00
|
|
|
})
|
|
|
|
|
2016-09-08 07:33:10 -05:00
|
|
|
Convey("Should not set NoDataFound if one serie is empty", func() {
|
|
|
|
ctx.series = tsdb.TimeSeriesSlice{
|
2016-09-28 02:15:48 -05:00
|
|
|
tsdb.NewTimeSeries("test1", tsdb.NewTimeSeriesPointsFromArgs()),
|
|
|
|
tsdb.NewTimeSeries("test2", tsdb.NewTimeSeriesPointsFromArgs(120, 0)),
|
2016-09-08 07:33:10 -05:00
|
|
|
}
|
2016-11-03 09:26:17 -05:00
|
|
|
cr, err := ctx.exec()
|
2016-09-08 07:33:10 -05:00
|
|
|
|
2016-11-03 09:26:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cr.NoDataFound, ShouldBeFalse)
|
2016-09-08 07:33:10 -05:00
|
|
|
})
|
|
|
|
})
|
2016-07-20 07:28:02 -05:00
|
|
|
})
|
2016-07-21 09:19:28 -05:00
|
|
|
})
|
|
|
|
}
|
2016-07-20 07:28:02 -05:00
|
|
|
|
2016-07-21 09:19:28 -05:00
|
|
|
type queryConditionTestContext struct {
|
|
|
|
reducer string
|
|
|
|
evaluator string
|
|
|
|
series tsdb.TimeSeriesSlice
|
2016-07-27 09:29:28 -05:00
|
|
|
result *alerting.EvalContext
|
2016-07-27 09:18:10 -05:00
|
|
|
condition *QueryCondition
|
2016-07-21 09:19:28 -05:00
|
|
|
}
|
2016-07-20 02:30:31 -05:00
|
|
|
|
2016-07-21 09:19:28 -05:00
|
|
|
type queryConditionScenarioFunc func(c *queryConditionTestContext)
|
|
|
|
|
2016-11-03 09:26:17 -05:00
|
|
|
func (ctx *queryConditionTestContext) exec() (*alerting.ConditionResult, error) {
|
2016-07-21 09:19:28 -05:00
|
|
|
jsonModel, err := simplejson.NewJson([]byte(`{
|
2016-07-20 02:30:31 -05:00
|
|
|
"type": "query",
|
|
|
|
"query": {
|
|
|
|
"params": ["A", "5m", "now"],
|
|
|
|
"datasourceId": 1,
|
|
|
|
"model": {"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
|
|
|
|
},
|
2016-07-21 09:19:28 -05:00
|
|
|
"reducer":` + ctx.reducer + `,
|
|
|
|
"evaluator":` + ctx.evaluator + `
|
2016-07-20 02:30:31 -05:00
|
|
|
}`))
|
2016-07-21 09:19:28 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2016-07-21 14:54:12 -05:00
|
|
|
condition, err := NewQueryCondition(jsonModel, 0)
|
2016-07-21 09:19:28 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2016-07-27 09:18:10 -05:00
|
|
|
ctx.condition = condition
|
|
|
|
|
2017-09-21 11:04:06 -05:00
|
|
|
condition.HandleRequest = func(context context.Context, dsInfo *m.DataSource, req *tsdb.TsdbQuery) (*tsdb.Response, error) {
|
2016-07-21 09:19:28 -05:00
|
|
|
return &tsdb.Response{
|
|
|
|
Results: map[string]*tsdb.QueryResult{
|
|
|
|
"A": {Series: ctx.series},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-11-03 09:26:17 -05:00
|
|
|
return condition.Eval(ctx.result)
|
2016-07-21 09:19:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func queryConditionScenario(desc string, fn queryConditionScenarioFunc) {
|
|
|
|
Convey(desc, func() {
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(query *m.GetDataSourceByIdQuery) error {
|
|
|
|
query.Result = &m.DataSource{Id: 1, Type: "graphite"}
|
|
|
|
return nil
|
2016-07-20 02:30:31 -05:00
|
|
|
})
|
|
|
|
|
2016-07-21 09:19:28 -05:00
|
|
|
ctx := &queryConditionTestContext{}
|
2016-07-27 09:29:28 -05:00
|
|
|
ctx.result = &alerting.EvalContext{
|
|
|
|
Rule: &alerting.Rule{},
|
2016-07-21 09:19:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn(ctx)
|
2016-07-20 02:30:31 -05:00
|
|
|
})
|
|
|
|
}
|