Merge pull request #15235 from influxdata/fix/state-changes-be

fix(notification/rule): ensure stateChanges rule has enough data in window
pull/15261/head
Michael Desa 2019-09-24 14:59:27 -04:00 committed by GitHub
commit f2805626c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 4 deletions

View File

@ -68,7 +68,7 @@ all_statuses
f, err := s.GenerateFlux(e)
if err != nil {
panic(err)
t.Fatal(err)
}
if f != want {
@ -142,7 +142,7 @@ all_statuses
f, err := s.GenerateFlux(e)
if err != nil {
panic(err)
t.Fatal(err)
}
if f != want {
@ -214,7 +214,79 @@ all_statuses
f, err := s.GenerateFlux(e)
if err != nil {
panic(err)
t.Fatal(err)
}
if f != want {
t.Errorf("scripts did not match. want:\n%v\n\ngot:\n%v", want, f)
}
}
func TestHTTP_GenerateFlux_bearer_every_second(t *testing.T) {
want := `package main
// foo
import "influxdata/influxdb/monitor"
import "http"
import "json"
import "experimental"
import "influxdata/influxdb/secrets"
option task = {name: "foo", every: 5s, offset: 1s}
headers = {"Content-Type": "application/json", "Authorization": "Bearer " + secrets.get(key: "000000000000000e-token")}
endpoint = http.endpoint(url: "http://localhost:7777")
notification = {
_notification_rule_id: "0000000000000001",
_notification_rule_name: "foo",
_notification_endpoint_id: "0000000000000002",
_notification_endpoint_name: "foo",
}
statuses = monitor.from(start: -10s)
crit = statuses
|> filter(fn: (r) =>
(r._level == "crit"))
all_statuses = crit
|> filter(fn: (r) =>
(r._time > experimental.subDuration(from: now(), d: 5s)))
all_statuses
|> monitor.notify(data: notification, endpoint: endpoint(mapFn: (r) => {
body = {r with _version: 1}
return {headers: headers, data: json.encode(v: body)}
}))`
s := &rule.HTTP{
Base: rule.Base{
ID: 1,
Name: "foo",
Every: mustDuration("5s"),
Offset: mustDuration("1s"),
EndpointID: 2,
TagRules: []notification.TagRule{},
StatusRules: []notification.StatusRule{
{
CurrentLevel: notification.Critical,
},
},
},
}
e := &endpoint.HTTP{
Base: endpoint.Base{
ID: 2,
Name: "foo",
},
URL: "http://localhost:7777",
AuthMethod: "bearer",
Token: influxdb.SecretField{
Key: "000000000000000e-token",
},
}
f, err := s.GenerateFlux(e)
if err != nil {
t.Fatal(err)
}
if f != want {

View File

@ -244,12 +244,24 @@ func (b *Base) generateStateChanges(r notification.StatusRule) (ast.Statement, *
return flux.DefineVariable(name, pipe), flux.Identifier(name)
}
// increaseDur increases the duration of leading duration in a duration literal.
// It is used so that we will have overlapping windows. If the unit of the literal
// is `s`, we double the interval; otherwise we increase the value by 1. The reason
// for this is to that we query the minimal amount of time that is likely to have data
// in the time range.
//
// This is currently a hack around https://github.com/influxdata/flux/issues/1877
func increaseDur(d *ast.DurationLiteral) *ast.DurationLiteral {
dur := &ast.DurationLiteral{}
for i, v := range d.Values {
value := v
if i == 0 {
value.Magnitude++
switch v.Unit {
case "s", "ms", "us", "ns":
value.Magnitude *= 2
default:
value.Magnitude += 1
}
}
dur.Values = append(dur.Values, value)
}