2019-08-07 22:34:07 +00:00
|
|
|
package flux
|
|
|
|
|
|
|
|
import "github.com/influxdata/flux/ast"
|
|
|
|
|
|
|
|
// File creates a new *ast.File.
|
|
|
|
func File(name string, imports []*ast.ImportDeclaration, body []ast.Statement) *ast.File {
|
|
|
|
return &ast.File{
|
|
|
|
Name: name,
|
|
|
|
Imports: imports,
|
|
|
|
Body: body,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GreaterThan returns a greater than *ast.BinaryExpression.
|
|
|
|
func GreaterThan(lhs, rhs ast.Expression) *ast.BinaryExpression {
|
|
|
|
return &ast.BinaryExpression{
|
|
|
|
Operator: ast.GreaterThanOperator,
|
|
|
|
Left: lhs,
|
|
|
|
Right: rhs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LessThan returns a less than *ast.BinaryExpression.
|
|
|
|
func LessThan(lhs, rhs ast.Expression) *ast.BinaryExpression {
|
|
|
|
return &ast.BinaryExpression{
|
|
|
|
Operator: ast.LessThanOperator,
|
|
|
|
Left: lhs,
|
|
|
|
Right: rhs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-16 19:43:15 +00:00
|
|
|
// Equal returns an equal to *ast.BinaryExpression.
|
|
|
|
func Equal(lhs, rhs ast.Expression) *ast.BinaryExpression {
|
|
|
|
return &ast.BinaryExpression{
|
|
|
|
Operator: ast.EqualOperator,
|
|
|
|
Left: lhs,
|
|
|
|
Right: rhs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:29:49 +00:00
|
|
|
// Subtract returns a subtraction *ast.BinaryExpression.
|
|
|
|
func Subtract(lhs, rhs ast.Expression) *ast.BinaryExpression {
|
|
|
|
return &ast.BinaryExpression{
|
|
|
|
Operator: ast.SubtractionOperator,
|
|
|
|
Left: lhs,
|
|
|
|
Right: rhs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 20:58:50 +00:00
|
|
|
// Add returns a addition *ast.BinaryExpression.
|
|
|
|
func Add(lhs, rhs ast.Expression) *ast.BinaryExpression {
|
|
|
|
return &ast.BinaryExpression{
|
|
|
|
Operator: ast.AdditionOperator,
|
|
|
|
Left: lhs,
|
|
|
|
Right: rhs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 22:34:07 +00:00
|
|
|
// Member returns an *ast.MemberExpression where the key is p and the values is c.
|
|
|
|
func Member(p, c string) *ast.MemberExpression {
|
|
|
|
return &ast.MemberExpression{
|
|
|
|
Object: &ast.Identifier{Name: p},
|
2020-03-24 02:17:52 +00:00
|
|
|
Property: String(c),
|
2019-08-07 22:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// And returns an and *ast.LogicalExpression.
|
|
|
|
func And(lhs, rhs ast.Expression) *ast.LogicalExpression {
|
|
|
|
return &ast.LogicalExpression{
|
|
|
|
Operator: ast.AndOperator,
|
|
|
|
Left: lhs,
|
|
|
|
Right: rhs,
|
2019-09-09 19:11:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Or returns an or *ast.LogicalExpression.
|
|
|
|
func Or(lhs, rhs ast.Expression) *ast.LogicalExpression {
|
|
|
|
return &ast.LogicalExpression{
|
|
|
|
Operator: ast.OrOperator,
|
|
|
|
Left: lhs,
|
|
|
|
Right: rhs,
|
2019-08-07 22:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:28:42 +00:00
|
|
|
// If returns an *ast.ConditionalExpression
|
|
|
|
func If(test, consequent, alternate ast.Expression) *ast.ConditionalExpression {
|
|
|
|
return &ast.ConditionalExpression{
|
|
|
|
Test: test,
|
|
|
|
Consequent: consequent,
|
|
|
|
Alternate: alternate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 22:34:07 +00:00
|
|
|
// Pipe returns a *ast.PipeExpression that is a piped sequence of call expressions starting at base.
|
|
|
|
// It requires at least one call expression and will panic otherwise.
|
|
|
|
func Pipe(base ast.Expression, calls ...*ast.CallExpression) *ast.PipeExpression {
|
|
|
|
if len(calls) < 1 {
|
|
|
|
panic("must pipe forward to at least one *ast.CallExpression")
|
|
|
|
}
|
|
|
|
pe := appendPipe(base, calls[0])
|
|
|
|
for _, call := range calls[1:] {
|
|
|
|
pe = appendPipe(pe, call)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pe
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendPipe(base ast.Expression, next *ast.CallExpression) *ast.PipeExpression {
|
|
|
|
return &ast.PipeExpression{
|
|
|
|
Argument: base,
|
|
|
|
Call: next,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call returns a *ast.CallExpression that is a function call of fn with args.
|
|
|
|
func Call(fn ast.Expression, args *ast.ObjectExpression) *ast.CallExpression {
|
|
|
|
return &ast.CallExpression{
|
|
|
|
Callee: fn,
|
|
|
|
Arguments: []ast.Expression{
|
|
|
|
args,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-11 18:54:21 +00:00
|
|
|
// ExpressionStatement returns an *ast.ExpressionStatement of e.
|
2019-08-07 22:34:07 +00:00
|
|
|
func ExpressionStatement(e ast.Expression) *ast.ExpressionStatement {
|
|
|
|
return &ast.ExpressionStatement{Expression: e}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function returns an *ast.FunctionExpression with params with body b.
|
|
|
|
func Function(params []*ast.Property, b ast.Expression) *ast.FunctionExpression {
|
|
|
|
return &ast.FunctionExpression{
|
|
|
|
Params: params,
|
|
|
|
Body: b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
feat(notification/rule): add JSON body for the HTTP POST (#14994)
The body of the JSON webhook would be:
{
"version": 1,
"rule_name": notification._notification_rule_name,
"rule_id": notification._notification_rule_id,
"endpoint_name": notification._notification_endpoint_name,
"endpoint_id": notification._notification_endpoint_id,
"check_name": r._check_name,
"check_id": r._check_id,
"check_type": r._type,
"source_measurement": r._source_measurement,
"source_timestamp": r._source_timestamp,
"level": r._level,
"message": r._message,
}
2019-09-06 01:38:02 +00:00
|
|
|
// FuncBlock takes a series of statements and produces a function.
|
|
|
|
func FuncBlock(params []*ast.Property, stms ...ast.Statement) *ast.FunctionExpression {
|
|
|
|
b := &ast.Block{
|
|
|
|
Body: stms,
|
|
|
|
}
|
|
|
|
return &ast.FunctionExpression{
|
|
|
|
Params: params,
|
|
|
|
Body: b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 22:34:07 +00:00
|
|
|
// String returns an *ast.StringLiteral of s.
|
|
|
|
func String(s string) *ast.StringLiteral {
|
|
|
|
return &ast.StringLiteral{
|
|
|
|
Value: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 22:36:18 +00:00
|
|
|
// Bool returns an *ast.BooleanLiteral of b.
|
|
|
|
func Bool(b bool) *ast.BooleanLiteral {
|
|
|
|
return &ast.BooleanLiteral{
|
|
|
|
Value: b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:29:49 +00:00
|
|
|
// Duration returns an *ast.DurationLiteral for a single duration.
|
|
|
|
func Duration(m int64, u string) *ast.DurationLiteral {
|
|
|
|
return &ast.DurationLiteral{
|
|
|
|
Values: []ast.Duration{
|
|
|
|
{
|
|
|
|
Magnitude: m,
|
|
|
|
Unit: u,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 22:34:07 +00:00
|
|
|
// Identifier returns an *ast.Identifier of i.
|
|
|
|
func Identifier(i string) *ast.Identifier {
|
|
|
|
return &ast.Identifier{Name: i}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Float returns an *ast.FloatLiteral of f.
|
|
|
|
func Float(f float64) *ast.FloatLiteral {
|
|
|
|
return &ast.FloatLiteral{
|
|
|
|
Value: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
feat(notification/rule): add JSON body for the HTTP POST (#14994)
The body of the JSON webhook would be:
{
"version": 1,
"rule_name": notification._notification_rule_name,
"rule_id": notification._notification_rule_id,
"endpoint_name": notification._notification_endpoint_name,
"endpoint_id": notification._notification_endpoint_id,
"check_name": r._check_name,
"check_id": r._check_id,
"check_type": r._type,
"source_measurement": r._source_measurement,
"source_timestamp": r._source_timestamp,
"level": r._level,
"message": r._message,
}
2019-09-06 01:38:02 +00:00
|
|
|
// Integer returns an *ast.IntegerLiteral of i.
|
|
|
|
func Integer(i int64) *ast.IntegerLiteral {
|
|
|
|
return &ast.IntegerLiteral{
|
|
|
|
Value: i,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 11:31:46 +00:00
|
|
|
// Negative returns *ast.UnaryExpression for -(e).
|
|
|
|
func Negative(e ast.Expression) *ast.UnaryExpression {
|
|
|
|
return &ast.UnaryExpression{
|
|
|
|
Operator: ast.SubtractionOperator,
|
|
|
|
Argument: e,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 22:34:07 +00:00
|
|
|
// DefineVariable returns an *ast.VariableAssignment of id to the e. (e.g. id = <expression>)
|
|
|
|
func DefineVariable(id string, e ast.Expression) *ast.VariableAssignment {
|
|
|
|
return &ast.VariableAssignment{
|
|
|
|
ID: &ast.Identifier{
|
|
|
|
Name: id,
|
|
|
|
},
|
|
|
|
Init: e,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefineTaskOption returns an *ast.OptionStatement with the object provided. (e.g. option task = {...})
|
|
|
|
func DefineTaskOption(o *ast.ObjectExpression) *ast.OptionStatement {
|
|
|
|
return &ast.OptionStatement{
|
|
|
|
Assignment: DefineVariable("task", o),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Property returns an *ast.Property of key to e. (e.g. key: <expression>)
|
|
|
|
func Property(key string, e ast.Expression) *ast.Property {
|
|
|
|
return &ast.Property{
|
|
|
|
Key: &ast.Identifier{
|
|
|
|
Name: key,
|
|
|
|
},
|
|
|
|
Value: e,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:32:28 +00:00
|
|
|
// Dictionary returns an *ast.Property of string key to value expression.
|
|
|
|
func Dictionary(key string, v ast.Expression) *ast.Property {
|
|
|
|
return &ast.Property{
|
|
|
|
Key: String(key),
|
|
|
|
Value: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 22:34:07 +00:00
|
|
|
// Object returns an *ast.ObjectExpression with properties ps.
|
|
|
|
func Object(ps ...*ast.Property) *ast.ObjectExpression {
|
|
|
|
return &ast.ObjectExpression{
|
|
|
|
Properties: ps,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
feat(notification/rule): add _version: 1 to HTTP body (#15028)
This adds the _version: 1 correctly to the body of the HTTP POST.
Additionally, this fixes the imports when using secrets.
The POSTed JSON body now is:
```json
{
"_check_id": "046cac59e2aa3000",
"_check_name": "High CPU User Usage",
"_level": "crit",
"_measurement": "notifications",
"_message": "High CPU User Usage: rsavage.prod is crit",
"_notification_endpoint_id": "046cad0c83aec000",
"_notification_endpoint_name": "HTTP Endpoint",
"_notification_rule_id": "046dff53d4183000",
"_notification_rule_name": "HTTP Notification",
"_source_measurement": "cpu",
"_source_timestamp": 1567797375000000000,
"_start": "2019-09-06T19:15:59Z",
"_status_timestamp": 1567797376416632300,
"_stop": "2019-09-06T19:16:20.362006739Z",
"_time": "2019-09-06T19:16:20.609629338Z",
"_type": "threshold",
"_version": 1,
"cpu": "cpu-total",
"host": "rsavage.prod",
"usage_user": 91.12278069517379
}
```
2019-09-06 21:21:27 +00:00
|
|
|
// ObjectWith adds many properties to an existing named identifier.
|
|
|
|
func ObjectWith(name string, ps ...*ast.Property) *ast.ObjectExpression {
|
|
|
|
obj := Object(ps...)
|
|
|
|
obj.With = &ast.Identifier{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2019-09-03 20:58:50 +00:00
|
|
|
// Array returns *ast.ArrayExpression with elements es.
|
|
|
|
func Array(es ...ast.Expression) *ast.ArrayExpression {
|
|
|
|
return &ast.ArrayExpression{
|
|
|
|
Elements: es,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 22:34:07 +00:00
|
|
|
// FunctionParams returns a slice of *ast.Property for the parameters of a function.
|
|
|
|
func FunctionParams(args ...string) []*ast.Property {
|
|
|
|
var params []*ast.Property
|
|
|
|
for _, arg := range args {
|
|
|
|
params = append(params, &ast.Property{Key: &ast.Identifier{Name: arg}})
|
|
|
|
}
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
// Imports returns a []*ast.ImportDeclaration for each package in pkgs.
|
|
|
|
func Imports(pkgs ...string) []*ast.ImportDeclaration {
|
|
|
|
var is []*ast.ImportDeclaration
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
is = append(is, ImportDeclaration(pkg))
|
|
|
|
}
|
|
|
|
return is
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportDeclaration returns an *ast.ImportDeclaration for pkg.
|
|
|
|
func ImportDeclaration(pkg string) *ast.ImportDeclaration {
|
|
|
|
return &ast.ImportDeclaration{
|
|
|
|
Path: &ast.StringLiteral{
|
|
|
|
Value: pkg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|