style(alerting): rename level.level to level.value

This commit is contained in:
bergquist 2016-06-15 11:39:25 +02:00
parent a893152141
commit 777ca4cd7d
8 changed files with 92 additions and 20 deletions

View File

@ -63,13 +63,13 @@ func NewAlertRuleFromDBModel(ruleDef *m.Alert) (*AlertRule, error) {
critical := ruleDef.Settings.Get("critical")
model.Critical = Level{
Operator: critical.Get("op").MustString(),
Level: critical.Get("level").MustFloat64(),
Value: critical.Get("value").MustFloat64(),
}
warning := ruleDef.Settings.Get("warn")
model.Warning = Level{
Operator: warning.Get("op").MustString(),
Level: warning.Get("level").MustFloat64(),
Value: warning.Get("value").MustFloat64(),
}
model.Frequency = getTimeDurationStringToSeconds(ruleDef.Settings.Get("frequency").MustString())

View File

@ -3,6 +3,8 @@ package alerting
import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@ -28,5 +30,56 @@ func TestAlertRuleModel(t *testing.T) {
seconds := getTimeDurationStringToSeconds("1o")
So(seconds, ShouldEqual, 1)
})
Convey("", func() {
json := `
{
"name": "name2",
"description": "desc2",
"handler": 0,
"enabled": true,
"critical": {
"value": 20,
"op": ">"
},
"warn": {
"value": 10,
"op": ">"
},
"frequency": "60s",
"query": {
"from": "5m",
"refId": "A",
"to": "now",
"query": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)",
"datasourceId": 1
},
"transform": {
"method": "avg",
"name": "aggregation"
}
}
`
alertJSON, jsonErr := simplejson.NewJson([]byte(json))
So(jsonErr, ShouldBeNil)
alert := &models.Alert{
Id: 1,
OrgId: 1,
DashboardId: 1,
PanelId: 1,
Settings: alertJSON,
}
alertRule, err := NewAlertRuleFromDBModel(alert)
So(err, ShouldBeNil)
So(alertRule.Critical.Operator, ShouldEqual, ">")
So(alertRule.Critical.Value, ShouldEqual, 20)
So(alertRule.Warning.Operator, ShouldEqual, ">")
So(alertRule.Warning.Value, ShouldEqual, 10)
})
})
}

View File

@ -3,7 +3,7 @@ package alerting
type compareFn func(float64, float64) bool
func evalCondition(level Level, result float64) bool {
return operators[level.Operator](result, level.Level)
return operators[level.Operator](result, level.Value)
}
var operators = map[string]compareFn{

View File

@ -102,7 +102,7 @@ func (e *ExecutorImpl) evaluateRule(rule *AlertRule, series tsdb.TimeSeriesSlice
transformedValue, _ := rule.Transformer.Transform(serie)
critResult := evalCondition(rule.Critical, transformedValue)
e.log.Debug("Alert execution Crit", "name", serie.Name, "transformedValue", transformedValue, "operator", rule.Critical.Operator, "level", rule.Critical.Level, "result", critResult)
e.log.Debug("Alert execution Crit", "name", serie.Name, "transformedValue", transformedValue, "operator", rule.Critical.Operator, "level", rule.Critical.Value, "result", critResult)
if critResult {
triggeredAlert = append(triggeredAlert, &TriggeredAlert{
State: alertstates.Critical,
@ -112,7 +112,7 @@ func (e *ExecutorImpl) evaluateRule(rule *AlertRule, series tsdb.TimeSeriesSlice
}
warnResult := evalCondition(rule.Warning, transformedValue)
e.log.Debug("Alert execution Warn", "name", serie.Name, "transformedValue", transformedValue, "operator", rule.Warning.Operator, "level", rule.Warning.Level, "result", warnResult)
e.log.Debug("Alert execution Warn", "name", serie.Name, "transformedValue", transformedValue, "operator", rule.Warning.Operator, "level", rule.Warning.Value, "result", warnResult)
if warnResult {
triggeredAlert = append(triggeredAlert, &TriggeredAlert{
State: alertstates.Warn,

View File

@ -16,7 +16,7 @@ func TestAlertingExecutor(t *testing.T) {
Convey("single time serie", func() {
Convey("Show return ok since avg is above 2", func() {
rule := &AlertRule{
Critical: Level{Level: 10, Operator: ">"},
Critical: Level{Value: 10, Operator: ">"},
Transformer: transformers.NewAggregationTransformer("avg"),
}
@ -30,7 +30,7 @@ func TestAlertingExecutor(t *testing.T) {
Convey("Show return critical since below 2", func() {
rule := &AlertRule{
Critical: Level{Level: 10, Operator: "<"},
Critical: Level{Value: 10, Operator: "<"},
Transformer: transformers.NewAggregationTransformer("avg"),
}
@ -44,7 +44,7 @@ func TestAlertingExecutor(t *testing.T) {
Convey("Show return critical since sum is above 10", func() {
rule := &AlertRule{
Critical: Level{Level: 10, Operator: ">"},
Critical: Level{Value: 10, Operator: ">"},
Transformer: transformers.NewAggregationTransformer("sum"),
}
@ -58,7 +58,7 @@ func TestAlertingExecutor(t *testing.T) {
Convey("Show return ok since avg is below 10", func() {
rule := &AlertRule{
Critical: Level{Level: 10, Operator: ">"},
Critical: Level{Value: 10, Operator: ">"},
Transformer: transformers.NewAggregationTransformer("avg"),
}
@ -72,7 +72,7 @@ func TestAlertingExecutor(t *testing.T) {
Convey("Show return ok since min is below 10", func() {
rule := &AlertRule{
Critical: Level{Level: 10, Operator: ">"},
Critical: Level{Value: 10, Operator: ">"},
Transformer: transformers.NewAggregationTransformer("avg"),
}
@ -86,7 +86,7 @@ func TestAlertingExecutor(t *testing.T) {
Convey("Show return ok since max is above 10", func() {
rule := &AlertRule{
Critical: Level{Level: 10, Operator: ">"},
Critical: Level{Value: 10, Operator: ">"},
Transformer: transformers.NewAggregationTransformer("max"),
}
@ -103,7 +103,7 @@ func TestAlertingExecutor(t *testing.T) {
Convey("muliple time series", func() {
Convey("both are ok", func() {
rule := &AlertRule{
Critical: Level{Level: 10, Operator: ">"},
Critical: Level{Value: 10, Operator: ">"},
Transformer: transformers.NewAggregationTransformer("avg"),
}
@ -118,7 +118,7 @@ func TestAlertingExecutor(t *testing.T) {
Convey("first serie is good, second is critical", func() {
rule := &AlertRule{
Critical: Level{Level: 10, Operator: ">"},
Critical: Level{Value: 10, Operator: ">"},
Transformer: transformers.NewAggregationTransformer("avg"),
}
@ -133,8 +133,8 @@ func TestAlertingExecutor(t *testing.T) {
Convey("first serie is warn, second is critical", func() {
rule := &AlertRule{
Critical: Level{Level: 10, Operator: ">"},
Warning: Level{Level: 5, Operator: ">"},
Critical: Level{Value: 10, Operator: ">"},
Warning: Level{Value: 5, Operator: ">"},
Transformer: transformers.NewAggregationTransformer("avg"),
}

View File

@ -63,6 +63,12 @@ func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
continue
}
enabled, hasEnabled := jsonAlert.CheckGet("enabled")
if !hasEnabled || !enabled.MustBool() {
continue
}
alert := &m.Alert{
DashboardId: e.Dash.Id,
OrgId: e.OrgId,

View File

@ -42,7 +42,7 @@ func TestAlertRuleExtraction(t *testing.T) {
"handler": 1,
"enabled": true,
"critical": {
"level": 20,
"value": 20,
"op": ">"
},
"frequency": "60s",
@ -56,7 +56,7 @@ func TestAlertRuleExtraction(t *testing.T) {
"type": "aggregation"
},
"warn": {
"level": 10,
"value": 10,
"op": ">"
}
}
@ -77,7 +77,7 @@ func TestAlertRuleExtraction(t *testing.T) {
"handler": 0,
"enabled": true,
"critical": {
"level": 20,
"value": 20,
"op": ">"
},
"frequency": "60s",
@ -91,7 +91,7 @@ func TestAlertRuleExtraction(t *testing.T) {
"name": "aggregation"
},
"warn": {
"level": 10,
"value": 10,
"op": ">"
}
}
@ -107,6 +107,19 @@ func TestAlertRuleExtraction(t *testing.T) {
{
"datasource": "InfluxDB",
"id": 2,
"alert": {
"name": "name2",
"description": "desc2",
"enabled": false,
"critical": {
"level": 20,
"op": ">"
},
"warn": {
"level": 10,
"op": ">"
}
},
"targets": [
{
"dsType": "influxdb",

View File

@ -38,7 +38,7 @@ type TriggeredAlert struct {
type Level struct {
Operator string
Level float64
Value float64
}
type AlertQuery struct {