Skip to content

Commit

Permalink
Allow duration / duration in TICKscript (influxdata#909)
Browse files Browse the repository at this point in the history
* allow duration / duration in TICKscript

* CHANGELOG.md
  • Loading branch information
Nathaniel Cook authored Sep 19, 2016
1 parent ef1def4 commit 1c3533b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [#869](https://github.com/influxdata/kapacitor/issues/869): Add ability to set alert message as a field
- [#854](https://github.com/influxdata/kapacitor/issues/854): Add `.create` property to InfluxDBOut node, which when set will create the database
and retention policy on task start.
- [#909](https://github.com/influxdata/kapacitor/pull/909): Allow duration / duration in TICKscript.

### Bugfixes

Expand Down
18 changes: 18 additions & 0 deletions tick/stateful/evaluation_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,24 @@ var evaluationFuncs = map[operationKey]*evaluationFnInfo{
},
returnType: ast.TDuration,
},
operationKey{operator: ast.TokenDiv, leftType: ast.TDuration, rightType: ast.TDuration}: {
f: func(scope *Scope, executionState ExecutionState, leftNode, rightNode NodeEvaluator) (resultContainer, *ErrSide) {
var left time.Duration
var right time.Duration
var err error

if left, err = leftNode.EvalDuration(scope, executionState); err != nil {
return emptyResultContainer, &ErrSide{error: err, IsLeft: true}
}

if right, err = rightNode.EvalDuration(scope, executionState); err != nil {
return emptyResultContainer, &ErrSide{error: err, IsRight: true}
}

return resultContainer{Int64Value: int64(left / right), IsInt64Value: true}, nil
},
returnType: ast.TInt,
},

// -----------------------------------------
// String concatenation func
Expand Down
11 changes: 7 additions & 4 deletions tick/stateful/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@ func TestExpression_EvalDuration(t *testing.T) {
// Left is time.Duration(5), Right is time.Duration(5)
keyStruct{time.Duration(5), time.Duration(5), ast.TokenPlus}: time.Duration(10),
keyStruct{time.Duration(5), time.Duration(5), ast.TokenMinus}: time.Duration(0),
keyStruct{time.Duration(5), time.Duration(5), ast.TokenDiv}: int64(1),

// Left is time.Duration(5), Right is time.Duration(10)
keyStruct{time.Duration(5), time.Duration(10), ast.TokenPlus}: time.Duration(15),
keyStruct{time.Duration(5), time.Duration(10), ast.TokenMinus}: time.Duration(-5),
keyStruct{time.Duration(5), time.Duration(10), ast.TokenDiv}: int64(0),

// Left is time.Duration(5), Right is int64(2)
keyStruct{time.Duration(5), int64(2), ast.TokenMult}: time.Duration(10),
Expand All @@ -191,10 +193,12 @@ func TestExpression_EvalDuration(t *testing.T) {
// Left is time.Duration(10), Right is time.Duration(5)
keyStruct{time.Duration(10), time.Duration(5), ast.TokenPlus}: time.Duration(15),
keyStruct{time.Duration(10), time.Duration(5), ast.TokenMinus}: time.Duration(5),
keyStruct{time.Duration(10), time.Duration(5), ast.TokenDiv}: int64(2),

// Left is time.Duration(10), Right is time.Duration(10)
keyStruct{time.Duration(10), time.Duration(10), ast.TokenPlus}: time.Duration(20),
keyStruct{time.Duration(10), time.Duration(10), ast.TokenMinus}: time.Duration(0),
keyStruct{time.Duration(10), time.Duration(10), ast.TokenDiv}: int64(1),

// Left is time.Duration(10), Right is int64(2)
keyStruct{time.Duration(10), int64(2), ast.TokenMult}: time.Duration(20),
Expand Down Expand Up @@ -235,13 +239,9 @@ func TestExpression_EvalDuration(t *testing.T) {

// Muiltiply Divide durations
keyStruct{time.Duration(5), time.Duration(5), ast.TokenMult}: errors.New("mismatched type to binary operator. got duration * duration. see bool(), int(), float(), string(), duration()"),
keyStruct{time.Duration(5), time.Duration(5), ast.TokenDiv}: errors.New("mismatched type to binary operator. got duration / duration. see bool(), int(), float(), string(), duration()"),
keyStruct{time.Duration(5), time.Duration(10), ast.TokenMult}: errors.New("mismatched type to binary operator. got duration * duration. see bool(), int(), float(), string(), duration()"),
keyStruct{time.Duration(5), time.Duration(10), ast.TokenDiv}: errors.New("mismatched type to binary operator. got duration / duration. see bool(), int(), float(), string(), duration()"),
keyStruct{time.Duration(10), time.Duration(10), ast.TokenMult}: errors.New("mismatched type to binary operator. got duration * duration. see bool(), int(), float(), string(), duration()"),
keyStruct{time.Duration(10), time.Duration(10), ast.TokenDiv}: errors.New("mismatched type to binary operator. got duration / duration. see bool(), int(), float(), string(), duration()"),
keyStruct{time.Duration(10), time.Duration(5), ast.TokenMult}: errors.New("mismatched type to binary operator. got duration * duration. see bool(), int(), float(), string(), duration()"),
keyStruct{time.Duration(10), time.Duration(5), ast.TokenDiv}: errors.New("mismatched type to binary operator. got duration / duration. see bool(), int(), float(), string(), duration()"),

// Add/Subtract duration with int/float
keyStruct{time.Duration(5), int64(2), ast.TokenPlus}: errors.New("mismatched type to binary operator. got duration + int. see bool(), int(), float(), string(), duration()"),
Expand Down Expand Up @@ -1503,6 +1503,9 @@ func runCompiledEvalTests(
if !isExpectedResultOk && !isErrorOk {
t.Fatalf("Couldn't find an expected result/error for: lhs: %v, rhs: %v, op: %v", lhs, rhs, op)
}
if isExpectedResultOk && isErrorOk {
t.Fatalf("Found both an expected result and an expected error for: lhs: %v, rhs: %v, op: %v", lhs, rhs, op)
}

// Test simple const values compares
emptyScope := stateful.NewScope()
Expand Down

0 comments on commit 1c3533b

Please sign in to comment.