Commit 3219d98a by Torkel Ödegaard

feat(alerting): starting work on condition evaluation

parent f872d5cf
package alerting
import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
. "github.com/smartystreets/goconvey/convey"
)
func TestQueryCondition(t *testing.T) {
Convey("when evaluating query condition", t, func() {
Convey("Given avg() and > 100", func() {
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]}
}`))
So(err, ShouldBeNil)
condition, err := NewQueryCondition(jsonModel)
So(err, ShouldBeNil)
Convey("Should set result to triggered when avg is above 100", func() {
context := &AlertResultContext{}
condition.Eval(context)
So(context.Triggered, ShouldBeTrue)
})
})
})
}
......@@ -14,7 +14,7 @@ type Engine struct {
clock clock.Clock
ticker *Ticker
scheduler Scheduler
handler AlertingHandler
handler AlertHandler
ruleReader RuleReader
log log.Logger
responseHandler ResultHandler
......
......@@ -90,6 +90,7 @@ func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
Handler: jsonAlert.Get("handler").MustInt64(),
Enabled: jsonAlert.Get("enabled").MustBool(),
Description: jsonAlert.Get("description").MustString(),
Severity: jsonAlert.Get("severity").MustString(),
Frequency: getTimeDurationStringToSeconds(jsonAlert.Get("frequency").MustString()),
}
......
......@@ -31,6 +31,16 @@ func (e *HandlerImpl) eval(rule *AlertRule) *AlertResultContext {
for _, condition := range rule.Conditions {
condition.Eval(result)
// break if condition could not be evaluated
if result.Error != nil {
break
}
// break if result has not triggered yet
if result.Triggered == false {
break
}
}
result.EndTime = time.Now()
......
......@@ -2,7 +2,7 @@ package alerting
import "time"
type AlertingHandler interface {
type AlertHandler interface {
Execute(rule *AlertRule, resultChan chan *AlertResultContext)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment