From f0fc336e88a556feaf3c006784da3f8198acfb2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 21 Jul 2016 16:19:28 +0200 Subject: [PATCH] feat(alerting): worked on alert condition eval tests --- pkg/services/alerting/conditions_test.go | 86 ++++++++++++++++-------- pkg/services/alerting/handler.go | 1 - pkg/services/alerting/handler_test.go | 16 ++--- public/sass/components/_jsontree.scss | 4 +- 4 files changed, 67 insertions(+), 40 deletions(-) diff --git a/pkg/services/alerting/conditions_test.go b/pkg/services/alerting/conditions_test.go index bbeb16597c6..a9f340c37a8 100644 --- a/pkg/services/alerting/conditions_test.go +++ b/pkg/services/alerting/conditions_test.go @@ -14,51 +14,79 @@ func TestQueryCondition(t *testing.T) { Convey("when evaluating query condition", t, func() { - bus.AddHandler("test", func(query *m.GetDataSourceByIdQuery) error { - query.Result = &m.DataSource{Id: 1, Type: "graphite"} - return nil + queryConditionScenario("Given avg() and > 100", func(ctx *queryConditionTestContext) { + + ctx.reducer = `{"type": "avg"}` + ctx.evaluator = `{"type": ">", "params": [100]}` + + Convey("should trigger when avg is above 100", func() { + ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", [][2]float64{{120, 0}})} + ctx.exec() + + So(ctx.result.Error, ShouldBeNil) + So(ctx.result.Triggered, ShouldBeTrue) + }) + + Convey("Should not trigger when avg is below 100", func() { + ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", [][2]float64{{90, 0}})} + ctx.exec() + + So(ctx.result.Error, ShouldBeNil) + So(ctx.result.Triggered, ShouldBeFalse) + }) }) + }) +} - Convey("Given avg() and > 100", func() { +type queryConditionTestContext struct { + reducer string + evaluator string + series tsdb.TimeSeriesSlice + result *AlertResultContext +} - jsonModel, err := simplejson.NewJson([]byte(`{ +type queryConditionScenarioFunc func(c *queryConditionTestContext) + +func (ctx *queryConditionTestContext) exec() { + jsonModel, err := simplejson.NewJson([]byte(`{ "type": "query", "query": { "params": ["A", "5m", "now"], "datasourceId": 1, "model": {"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"} }, - "reducer": {"type": "avg", "params": []}, - "evaluator": {"type": ">", "params": [100]} + "reducer":` + ctx.reducer + `, + "evaluator":` + ctx.evaluator + ` }`)) - So(err, ShouldBeNil) + So(err, ShouldBeNil) - condition, err := NewQueryCondition(jsonModel) - So(err, ShouldBeNil) + condition, err := NewQueryCondition(jsonModel) + So(err, ShouldBeNil) - Convey("Should set result to triggered when avg is above 100", func() { - context := &AlertResultContext{ - Rule: &AlertRule{}, - } + condition.HandleRequest = func(req *tsdb.Request) (*tsdb.Response, error) { + return &tsdb.Response{ + Results: map[string]*tsdb.QueryResult{ + "A": {Series: ctx.series}, + }, + }, nil + } - condition.HandleRequest = func(req *tsdb.Request) (*tsdb.Response, error) { - return &tsdb.Response{ - Results: map[string]*tsdb.QueryResult{ - "A": &tsdb.QueryResult{ - Series: tsdb.TimeSeriesSlice{ - tsdb.NewTimeSeries("test1", [][2]float64{{120, 0}}), - }, - }, - }, - }, nil - } + condition.Eval(ctx.result) +} - condition.Eval(context) +func queryConditionScenario(desc string, fn queryConditionScenarioFunc) { + Convey(desc, func() { - So(context.Error, ShouldBeNil) - So(context.Triggered, ShouldBeTrue) - }) + bus.AddHandler("test", func(query *m.GetDataSourceByIdQuery) error { + query.Result = &m.DataSource{Id: 1, Type: "graphite"} + return nil }) + ctx := &queryConditionTestContext{} + ctx.result = &AlertResultContext{ + Rule: &AlertRule{}, + } + + fn(ctx) }) } diff --git a/pkg/services/alerting/handler.go b/pkg/services/alerting/handler.go index 77218d473ff..b54f74563c5 100644 --- a/pkg/services/alerting/handler.go +++ b/pkg/services/alerting/handler.go @@ -55,7 +55,6 @@ func (e *HandlerImpl) eval(context *AlertResultContext) { } context.EndTime = time.Now() - context.DoneChan <- true } // func (e *HandlerImpl) executeQuery(job *AlertJob) (tsdb.TimeSeriesSlice, error) { diff --git a/pkg/services/alerting/handler_test.go b/pkg/services/alerting/handler_test.go index 68389891431..2841ca0b117 100644 --- a/pkg/services/alerting/handler_test.go +++ b/pkg/services/alerting/handler_test.go @@ -19,26 +19,26 @@ func TestAlertingExecutor(t *testing.T) { handler := NewHandler() Convey("Show return triggered with single passing condition", func() { - rule := &AlertRule{ + context := NewAlertResultContext(&AlertRule{ Conditions: []AlertCondition{&conditionStub{ triggered: true, }}, - } + }) - result := handler.eval(rule) - So(result.Triggered, ShouldEqual, true) + handler.eval(context) + So(context.Triggered, ShouldEqual, true) }) Convey("Show return false with not passing condition", func() { - rule := &AlertRule{ + context := NewAlertResultContext(&AlertRule{ Conditions: []AlertCondition{ &conditionStub{triggered: true}, &conditionStub{triggered: false}, }, - } + }) - result := handler.eval(rule) - So(result.Triggered, ShouldEqual, false) + handler.eval(context) + So(context.Triggered, ShouldEqual, false) }) // Convey("Show return critical since below 2", func() { diff --git a/public/sass/components/_jsontree.scss b/public/sass/components/_jsontree.scss index 668382180f7..011566f8731 100644 --- a/public/sass/components/_jsontree.scss +++ b/public/sass/components/_jsontree.scss @@ -8,7 +8,7 @@ json-tree { &::before { pointer-events: none; } - &::before, & > .key { + &::before, & > .json-tree-key { cursor: pointer; } } @@ -41,7 +41,7 @@ json-tree { content: '\25b6'; position: absolute; left: 0px; - font-size: 10px; + font-size: 8px; transition: transform .1s ease; } &.expanded::before {