2019-08-07 22:34:07 +00:00
|
|
|
package check_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/flux/ast"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/notification"
|
|
|
|
"github.com/influxdata/influxdb/v2/notification/check"
|
2019-12-18 20:23:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-08-07 22:34:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestThreshold_GenerateFlux(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
threshold check.Threshold
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
script string
|
|
|
|
}
|
|
|
|
|
|
|
|
var l float64 = 10
|
|
|
|
var u float64 = 40
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2019-08-19 14:39:51 +00:00
|
|
|
name: "all levels with yield and stop",
|
|
|
|
args: args{
|
|
|
|
threshold: check.Threshold{
|
|
|
|
Base: check.Base{
|
|
|
|
ID: 10,
|
|
|
|
Name: "moo",
|
2019-08-28 16:25:54 +00:00
|
|
|
Tags: []influxdb.Tag{
|
2019-08-19 14:39:51 +00:00
|
|
|
{Key: "aaa", Value: "vaaa"},
|
|
|
|
{Key: "bbb", Value: "vbbb"},
|
|
|
|
},
|
|
|
|
Every: mustDuration("1h"),
|
2020-03-24 02:17:52 +00:00
|
|
|
StatusMessageTemplate: "whoa! {r[\"usage_user\"]}",
|
2019-08-19 14:39:51 +00:00
|
|
|
Query: influxdb.DashboardQuery{
|
2019-12-18 20:23:06 +00:00
|
|
|
Text: `from(bucket: "foo") |> range(start: -1d, stop: now()) |> filter(fn: (r) => r._field == "usage_user") |> aggregateWindow(every: 1m, fn: mean) |> yield()`,
|
2019-08-19 14:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Thresholds: []check.ThresholdConfig{
|
|
|
|
check.Greater{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Ok,
|
|
|
|
},
|
|
|
|
Value: l,
|
|
|
|
},
|
|
|
|
check.Lesser{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Info,
|
|
|
|
},
|
|
|
|
Value: u,
|
|
|
|
},
|
|
|
|
check.Range{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Warn,
|
|
|
|
},
|
|
|
|
Min: l,
|
|
|
|
Max: u,
|
|
|
|
Within: true,
|
|
|
|
},
|
|
|
|
check.Range{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Critical,
|
|
|
|
},
|
|
|
|
Min: l,
|
|
|
|
Max: u,
|
2019-09-09 19:11:08 +00:00
|
|
|
Within: false,
|
2019-08-19 14:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
script: `package main
|
2019-08-23 21:46:03 +00:00
|
|
|
import "influxdata/influxdb/monitor"
|
2019-08-19 14:39:51 +00:00
|
|
|
import "influxdata/influxdb/v1"
|
|
|
|
|
|
|
|
data = from(bucket: "foo")
|
|
|
|
|> range(start: -1h)
|
2019-12-18 20:23:06 +00:00
|
|
|
|> filter(fn: (r) =>
|
|
|
|
(r._field == "usage_user"))
|
2019-08-29 22:36:18 +00:00
|
|
|
|> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
|
2019-08-19 14:39:51 +00:00
|
|
|
|
|
|
|
option task = {name: "moo", every: 1h}
|
|
|
|
|
|
|
|
check = {
|
|
|
|
_check_id: "000000000000000a",
|
|
|
|
_check_name: "moo",
|
2019-08-30 14:54:16 +00:00
|
|
|
_type: "threshold",
|
2019-08-19 14:39:51 +00:00
|
|
|
tags: {aaa: "vaaa", bbb: "vbbb"},
|
|
|
|
}
|
|
|
|
ok = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] > 10.0)
|
2019-08-19 14:39:51 +00:00
|
|
|
info = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] < 40.0)
|
2019-08-19 14:39:51 +00:00
|
|
|
warn = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] < 40.0 and r["usage_user"] > 10.0)
|
2019-08-19 14:39:51 +00:00
|
|
|
crit = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] < 10.0 or r["usage_user"] > 40.0)
|
2019-08-27 17:29:49 +00:00
|
|
|
messageFn = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
("whoa! {r[\"usage_user\"]}")
|
2019-08-19 14:39:51 +00:00
|
|
|
|
|
|
|
data
|
2020-03-24 02:17:52 +00:00
|
|
|
|> v1["fieldsAsCols"]()
|
|
|
|
|> monitor["check"](
|
2019-08-19 14:39:51 +00:00
|
|
|
data: check,
|
|
|
|
messageFn: messageFn,
|
|
|
|
ok: ok,
|
|
|
|
info: info,
|
|
|
|
warn: warn,
|
|
|
|
crit: crit,
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all levels with yield",
|
|
|
|
args: args{
|
|
|
|
threshold: check.Threshold{
|
|
|
|
Base: check.Base{
|
|
|
|
ID: 10,
|
|
|
|
Name: "moo",
|
2019-08-28 16:25:54 +00:00
|
|
|
Tags: []influxdb.Tag{
|
2019-08-19 14:39:51 +00:00
|
|
|
{Key: "aaa", Value: "vaaa"},
|
|
|
|
{Key: "bbb", Value: "vbbb"},
|
|
|
|
},
|
|
|
|
Every: mustDuration("1h"),
|
2020-03-24 02:17:52 +00:00
|
|
|
StatusMessageTemplate: "whoa! {r[\"usage_user\"]}",
|
2019-08-19 14:39:51 +00:00
|
|
|
Query: influxdb.DashboardQuery{
|
2019-12-18 20:23:06 +00:00
|
|
|
Text: `from(bucket: "foo") |> range(start: -1d) |> filter(fn: (r) => r._field == "usage_user") |> aggregateWindow(every: 1m, fn: mean) |> yield()`,
|
2019-08-19 14:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Thresholds: []check.ThresholdConfig{
|
|
|
|
check.Greater{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Ok,
|
|
|
|
},
|
|
|
|
Value: l,
|
|
|
|
},
|
|
|
|
check.Lesser{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Info,
|
|
|
|
},
|
|
|
|
Value: u,
|
|
|
|
},
|
|
|
|
check.Range{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Warn,
|
|
|
|
},
|
|
|
|
Min: l,
|
|
|
|
Max: u,
|
|
|
|
Within: true,
|
|
|
|
},
|
|
|
|
check.Range{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Critical,
|
|
|
|
},
|
|
|
|
Min: l,
|
|
|
|
Max: u,
|
2019-09-09 19:11:08 +00:00
|
|
|
Within: false,
|
2019-08-19 14:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
script: `package main
|
2019-08-23 21:46:03 +00:00
|
|
|
import "influxdata/influxdb/monitor"
|
2019-08-19 14:39:51 +00:00
|
|
|
import "influxdata/influxdb/v1"
|
|
|
|
|
|
|
|
data = from(bucket: "foo")
|
|
|
|
|> range(start: -1h)
|
2019-12-18 20:23:06 +00:00
|
|
|
|> filter(fn: (r) =>
|
|
|
|
(r._field == "usage_user"))
|
2019-08-29 22:36:18 +00:00
|
|
|
|> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
|
2019-08-19 14:39:51 +00:00
|
|
|
|
|
|
|
option task = {name: "moo", every: 1h}
|
|
|
|
|
|
|
|
check = {
|
|
|
|
_check_id: "000000000000000a",
|
|
|
|
_check_name: "moo",
|
2019-08-30 14:54:16 +00:00
|
|
|
_type: "threshold",
|
2019-08-19 14:39:51 +00:00
|
|
|
tags: {aaa: "vaaa", bbb: "vbbb"},
|
|
|
|
}
|
|
|
|
ok = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] > 10.0)
|
2019-08-19 14:39:51 +00:00
|
|
|
info = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] < 40.0)
|
2019-08-19 14:39:51 +00:00
|
|
|
warn = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] < 40.0 and r["usage_user"] > 10.0)
|
2019-08-19 14:39:51 +00:00
|
|
|
crit = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] < 10.0 or r["usage_user"] > 40.0)
|
2019-08-27 17:29:49 +00:00
|
|
|
messageFn = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
("whoa! {r[\"usage_user\"]}")
|
2019-08-19 14:39:51 +00:00
|
|
|
|
|
|
|
data
|
2020-03-24 02:17:52 +00:00
|
|
|
|> v1["fieldsAsCols"]()
|
|
|
|
|> monitor["check"](
|
|
|
|
data: check,
|
|
|
|
messageFn: messageFn,
|
|
|
|
ok: ok,
|
|
|
|
info: info,
|
|
|
|
warn: warn,
|
|
|
|
crit: crit,
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all levels with yield and space in field name",
|
|
|
|
args: args{
|
|
|
|
threshold: check.Threshold{
|
|
|
|
Base: check.Base{
|
|
|
|
ID: 10,
|
|
|
|
Name: "moo",
|
|
|
|
Tags: []influxdb.Tag{
|
|
|
|
{Key: "aaa", Value: "vaaa"},
|
|
|
|
{Key: "bbb", Value: "vbbb"},
|
|
|
|
},
|
|
|
|
Every: mustDuration("1h"),
|
|
|
|
StatusMessageTemplate: "whoa! {r[\"usage user\"]}",
|
|
|
|
Query: influxdb.DashboardQuery{
|
|
|
|
Text: `from(bucket: "foo") |> range(start: -1d) |> filter(fn: (r) => r._field == "usage user") |> aggregateWindow(every: 1m, fn: mean) |> yield()`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Thresholds: []check.ThresholdConfig{
|
|
|
|
check.Greater{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Ok,
|
|
|
|
},
|
|
|
|
Value: l,
|
|
|
|
},
|
|
|
|
check.Lesser{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Info,
|
|
|
|
},
|
|
|
|
Value: u,
|
|
|
|
},
|
|
|
|
check.Range{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Warn,
|
|
|
|
},
|
|
|
|
Min: l,
|
|
|
|
Max: u,
|
|
|
|
Within: true,
|
|
|
|
},
|
|
|
|
check.Range{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Critical,
|
|
|
|
},
|
|
|
|
Min: l,
|
|
|
|
Max: u,
|
|
|
|
Within: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
script: `package main
|
|
|
|
import "influxdata/influxdb/monitor"
|
|
|
|
import "influxdata/influxdb/v1"
|
|
|
|
|
|
|
|
data = from(bucket: "foo")
|
|
|
|
|> range(start: -1h)
|
|
|
|
|> filter(fn: (r) =>
|
|
|
|
(r._field == "usage user"))
|
|
|
|
|> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
|
|
|
|
|
|
|
|
option task = {name: "moo", every: 1h}
|
|
|
|
|
|
|
|
check = {
|
|
|
|
_check_id: "000000000000000a",
|
|
|
|
_check_name: "moo",
|
|
|
|
_type: "threshold",
|
|
|
|
tags: {aaa: "vaaa", bbb: "vbbb"},
|
|
|
|
}
|
|
|
|
ok = (r) =>
|
|
|
|
(r["usage user"] > 10.0)
|
|
|
|
info = (r) =>
|
|
|
|
(r["usage user"] < 40.0)
|
|
|
|
warn = (r) =>
|
|
|
|
(r["usage user"] < 40.0 and r["usage user"] > 10.0)
|
|
|
|
crit = (r) =>
|
|
|
|
(r["usage user"] < 10.0 or r["usage user"] > 40.0)
|
|
|
|
messageFn = (r) =>
|
|
|
|
("whoa! {r[\"usage user\"]}")
|
|
|
|
|
|
|
|
data
|
|
|
|
|> v1["fieldsAsCols"]()
|
|
|
|
|> monitor["check"](
|
2019-08-19 14:39:51 +00:00
|
|
|
data: check,
|
|
|
|
messageFn: messageFn,
|
|
|
|
ok: ok,
|
|
|
|
info: info,
|
|
|
|
warn: warn,
|
|
|
|
crit: crit,
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all levels without yield",
|
2019-08-07 22:34:07 +00:00
|
|
|
args: args{
|
|
|
|
threshold: check.Threshold{
|
|
|
|
Base: check.Base{
|
|
|
|
ID: 10,
|
|
|
|
Name: "moo",
|
2019-08-28 16:25:54 +00:00
|
|
|
Tags: []influxdb.Tag{
|
2019-08-07 22:34:07 +00:00
|
|
|
{Key: "aaa", Value: "vaaa"},
|
|
|
|
{Key: "bbb", Value: "vbbb"},
|
|
|
|
},
|
|
|
|
Every: mustDuration("1h"),
|
2020-03-24 02:17:52 +00:00
|
|
|
StatusMessageTemplate: "whoa! {r[\"usage_user\"]}",
|
2019-08-07 22:34:07 +00:00
|
|
|
Query: influxdb.DashboardQuery{
|
2019-12-18 20:23:06 +00:00
|
|
|
Text: `from(bucket: "foo") |> range(start: -1d) |> filter(fn: (r) => r._field == "usage_user") |> aggregateWindow(every: 1m, fn: mean)`,
|
2019-08-07 22:34:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Thresholds: []check.ThresholdConfig{
|
2019-08-15 20:51:03 +00:00
|
|
|
check.Greater{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Ok,
|
|
|
|
},
|
|
|
|
Value: l,
|
2019-08-07 22:34:07 +00:00
|
|
|
},
|
2019-08-15 20:51:03 +00:00
|
|
|
check.Lesser{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Info,
|
|
|
|
},
|
|
|
|
Value: u,
|
2019-08-07 22:34:07 +00:00
|
|
|
},
|
2019-08-15 20:51:03 +00:00
|
|
|
check.Range{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Warn,
|
|
|
|
},
|
|
|
|
Min: l,
|
|
|
|
Max: u,
|
|
|
|
Within: true,
|
2019-08-07 22:34:07 +00:00
|
|
|
},
|
2019-08-15 20:51:03 +00:00
|
|
|
check.Range{
|
|
|
|
ThresholdConfigBase: check.ThresholdConfigBase{
|
|
|
|
Level: notification.Critical,
|
|
|
|
},
|
|
|
|
Min: l,
|
|
|
|
Max: u,
|
|
|
|
Within: true,
|
2019-08-07 22:34:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
script: `package main
|
2019-08-23 21:46:03 +00:00
|
|
|
import "influxdata/influxdb/monitor"
|
2019-08-19 13:00:24 +00:00
|
|
|
import "influxdata/influxdb/v1"
|
2019-08-07 22:34:07 +00:00
|
|
|
|
|
|
|
data = from(bucket: "foo")
|
2019-08-19 13:00:24 +00:00
|
|
|
|> range(start: -1h)
|
2019-12-18 20:23:06 +00:00
|
|
|
|> filter(fn: (r) =>
|
|
|
|
(r._field == "usage_user"))
|
2019-08-29 22:36:18 +00:00
|
|
|
|> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
|
2019-08-07 22:34:07 +00:00
|
|
|
|
|
|
|
option task = {name: "moo", every: 1h}
|
|
|
|
|
2019-08-19 13:00:24 +00:00
|
|
|
check = {
|
|
|
|
_check_id: "000000000000000a",
|
|
|
|
_check_name: "moo",
|
2019-08-30 14:54:16 +00:00
|
|
|
_type: "threshold",
|
2019-08-19 13:00:24 +00:00
|
|
|
tags: {aaa: "vaaa", bbb: "vbbb"},
|
|
|
|
}
|
2019-08-07 22:34:07 +00:00
|
|
|
ok = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] > 10.0)
|
2019-08-07 22:34:07 +00:00
|
|
|
info = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] < 40.0)
|
2019-08-07 22:34:07 +00:00
|
|
|
warn = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] < 40.0 and r["usage_user"] > 10.0)
|
2019-08-07 22:34:07 +00:00
|
|
|
crit = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
(r["usage_user"] < 40.0 and r["usage_user"] > 10.0)
|
2019-08-27 17:29:49 +00:00
|
|
|
messageFn = (r) =>
|
2020-03-24 02:17:52 +00:00
|
|
|
("whoa! {r[\"usage_user\"]}")
|
2019-08-07 22:34:07 +00:00
|
|
|
|
|
|
|
data
|
2020-03-24 02:17:52 +00:00
|
|
|
|> v1["fieldsAsCols"]()
|
|
|
|
|> monitor["check"](
|
2019-08-19 13:00:24 +00:00
|
|
|
data: check,
|
2019-08-07 22:34:07 +00:00
|
|
|
messageFn: messageFn,
|
|
|
|
ok: ok,
|
|
|
|
info: info,
|
|
|
|
warn: warn,
|
|
|
|
crit: crit,
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
// TODO(desa): change this to GenerateFlux() when we don't need to code
|
2019-08-23 21:46:03 +00:00
|
|
|
// around the monitor package not being available.
|
2019-08-29 22:36:18 +00:00
|
|
|
p, err := tt.args.threshold.GenerateFluxAST()
|
2019-08-07 22:34:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-12-18 20:23:06 +00:00
|
|
|
assert.Equal(t, tt.wants.script, ast.Format(p))
|
2019-08-07 22:34:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|