mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): worked on alert condition eval tests
This commit is contained in:
parent
3ad38eefd2
commit
f0fc336e88
@ -14,51 +14,79 @@ func TestQueryCondition(t *testing.T) {
|
|||||||
|
|
||||||
Convey("when evaluating query condition", t, func() {
|
Convey("when evaluating query condition", t, func() {
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetDataSourceByIdQuery) error {
|
queryConditionScenario("Given avg() and > 100", func(ctx *queryConditionTestContext) {
|
||||||
query.Result = &m.DataSource{Id: 1, Type: "graphite"}
|
|
||||||
return nil
|
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",
|
"type": "query",
|
||||||
"query": {
|
"query": {
|
||||||
"params": ["A", "5m", "now"],
|
"params": ["A", "5m", "now"],
|
||||||
"datasourceId": 1,
|
"datasourceId": 1,
|
||||||
"model": {"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
|
"model": {"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
|
||||||
},
|
},
|
||||||
"reducer": {"type": "avg", "params": []},
|
"reducer":` + ctx.reducer + `,
|
||||||
"evaluator": {"type": ">", "params": [100]}
|
"evaluator":` + ctx.evaluator + `
|
||||||
}`))
|
}`))
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
condition, err := NewQueryCondition(jsonModel)
|
condition, err := NewQueryCondition(jsonModel)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
Convey("Should set result to triggered when avg is above 100", func() {
|
condition.HandleRequest = func(req *tsdb.Request) (*tsdb.Response, error) {
|
||||||
context := &AlertResultContext{
|
return &tsdb.Response{
|
||||||
Rule: &AlertRule{},
|
Results: map[string]*tsdb.QueryResult{
|
||||||
}
|
"A": {Series: ctx.series},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
condition.HandleRequest = func(req *tsdb.Request) (*tsdb.Response, error) {
|
condition.Eval(ctx.result)
|
||||||
return &tsdb.Response{
|
}
|
||||||
Results: map[string]*tsdb.QueryResult{
|
|
||||||
"A": &tsdb.QueryResult{
|
|
||||||
Series: tsdb.TimeSeriesSlice{
|
|
||||||
tsdb.NewTimeSeries("test1", [][2]float64{{120, 0}}),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
condition.Eval(context)
|
func queryConditionScenario(desc string, fn queryConditionScenarioFunc) {
|
||||||
|
Convey(desc, func() {
|
||||||
|
|
||||||
So(context.Error, ShouldBeNil)
|
bus.AddHandler("test", func(query *m.GetDataSourceByIdQuery) error {
|
||||||
So(context.Triggered, ShouldBeTrue)
|
query.Result = &m.DataSource{Id: 1, Type: "graphite"}
|
||||||
})
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ctx := &queryConditionTestContext{}
|
||||||
|
ctx.result = &AlertResultContext{
|
||||||
|
Rule: &AlertRule{},
|
||||||
|
}
|
||||||
|
|
||||||
|
fn(ctx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,6 @@ func (e *HandlerImpl) eval(context *AlertResultContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
context.EndTime = time.Now()
|
context.EndTime = time.Now()
|
||||||
context.DoneChan <- true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (e *HandlerImpl) executeQuery(job *AlertJob) (tsdb.TimeSeriesSlice, error) {
|
// func (e *HandlerImpl) executeQuery(job *AlertJob) (tsdb.TimeSeriesSlice, error) {
|
||||||
|
@ -19,26 +19,26 @@ func TestAlertingExecutor(t *testing.T) {
|
|||||||
handler := NewHandler()
|
handler := NewHandler()
|
||||||
|
|
||||||
Convey("Show return triggered with single passing condition", func() {
|
Convey("Show return triggered with single passing condition", func() {
|
||||||
rule := &AlertRule{
|
context := NewAlertResultContext(&AlertRule{
|
||||||
Conditions: []AlertCondition{&conditionStub{
|
Conditions: []AlertCondition{&conditionStub{
|
||||||
triggered: true,
|
triggered: true,
|
||||||
}},
|
}},
|
||||||
}
|
})
|
||||||
|
|
||||||
result := handler.eval(rule)
|
handler.eval(context)
|
||||||
So(result.Triggered, ShouldEqual, true)
|
So(context.Triggered, ShouldEqual, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Show return false with not passing condition", func() {
|
Convey("Show return false with not passing condition", func() {
|
||||||
rule := &AlertRule{
|
context := NewAlertResultContext(&AlertRule{
|
||||||
Conditions: []AlertCondition{
|
Conditions: []AlertCondition{
|
||||||
&conditionStub{triggered: true},
|
&conditionStub{triggered: true},
|
||||||
&conditionStub{triggered: false},
|
&conditionStub{triggered: false},
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
|
|
||||||
result := handler.eval(rule)
|
handler.eval(context)
|
||||||
So(result.Triggered, ShouldEqual, false)
|
So(context.Triggered, ShouldEqual, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Convey("Show return critical since below 2", func() {
|
// Convey("Show return critical since below 2", func() {
|
||||||
|
@ -8,7 +8,7 @@ json-tree {
|
|||||||
&::before {
|
&::before {
|
||||||
pointer-events: none;
|
pointer-events: none;
|
||||||
}
|
}
|
||||||
&::before, & > .key {
|
&::before, & > .json-tree-key {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -41,7 +41,7 @@ json-tree {
|
|||||||
content: '\25b6';
|
content: '\25b6';
|
||||||
position: absolute;
|
position: absolute;
|
||||||
left: 0px;
|
left: 0px;
|
||||||
font-size: 10px;
|
font-size: 8px;
|
||||||
transition: transform .1s ease;
|
transition: transform .1s ease;
|
||||||
}
|
}
|
||||||
&.expanded::before {
|
&.expanded::before {
|
||||||
|
Loading…
Reference in New Issue
Block a user