Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eval.Keep now works with non referenced vars #994

Merged
merged 1 commit into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ See the API docs for more details.

### Bugfixes

- [#984](https://github.com/influxdata/kapacitor/issues/984): Fix bug where keeping a list of fields that where not referenced in the eval expressions would cause an error.

## v1.0.2 [2016-10-06]

### Release Notes
Expand Down
16 changes: 12 additions & 4 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,19 @@ func (e *EvalNode) eval(now time.Time, group models.GroupID, fields models.Field
if l := len(e.e.KeepList); l != 0 {
newFields = make(models.Fields, l)
for _, f := range e.e.KeepList {
v, err := vars.Get(f)
if err != nil {
return nil, nil, err
// Try the vars scope first
if vars.Has(f) {
v, err := vars.Get(f)
if err != nil {
return nil, nil, err
}
newFields[f] = v
} else if v, ok := fields[f]; ok {
// Try the raw fields next, since it may not have been a referenced var.
newFields[f] = v
} else {
return nil, nil, fmt.Errorf("cannot keep field %q, field does not exist", f)
}
newFields[f] = v
}
} else {
newFields = make(models.Fields, len(fields)+len(e.e.AsList))
Expand Down
3 changes: 3 additions & 0 deletions integrations/data/TestStream_Eval_KeepSome.srpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dbname
rpname
types value0=0,value1=1,other=5 0000000001
17 changes: 10 additions & 7 deletions integrations/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,26 +886,27 @@ stream
.measurement('types')
|eval(lambda: "value0" + "value1", lambda: "value0" - "value1")
.as( 'pos', 'neg')
.keep('value0', 'pos', 'neg')
|httpOut('TestStream_Eval_Keep')
.keep('value0', 'pos', 'neg', 'other')
|httpOut('TestStream_Eval_KeepSome')
`
er := kapacitor.Result{
Series: imodels.Rows{
{
Name: "types",
Tags: nil,
Columns: []string{"time", "neg", "pos", "value0"},
Columns: []string{"time", "neg", "other", "pos", "value0"},
Values: [][]interface{}{[]interface{}{
time.Date(1971, 1, 1, 0, 0, 0, 0, time.UTC),
-1.0,
5.0,
1.0,
0.0,
}},
},
},
}

testStreamerWithOutput(t, "TestStream_Eval_Keep", script, 2*time.Second, er, false, nil)
testStreamerWithOutput(t, "TestStream_Eval_KeepSome", script, 2*time.Second, er, false, nil)
}

func TestStream_Eval_KeepSomeWithHidden(t *testing.T) {
Expand Down Expand Up @@ -1019,7 +1020,7 @@ stream
|eval(lambda: string("value"))
.as('value_tag')
.tags('value_tag')
.keep('value')
.keep('value', 'another')
|groupBy('value_tag')
|httpOut('TestStream_Eval_Tags')
`
Expand All @@ -1028,18 +1029,20 @@ stream
{
Name: "types",
Tags: map[string]string{"value_tag": "0"},
Columns: []string{"time", "value"},
Columns: []string{"time", "another", "value"},
Values: [][]interface{}{[]interface{}{
time.Date(1971, 1, 1, 0, 0, 0, 0, time.UTC),
2.0,
0.0,
}},
},
{
Name: "types",
Tags: map[string]string{"value_tag": "1"},
Columns: []string{"time", "value"},
Columns: []string{"time", "another", "value"},
Values: [][]interface{}{[]interface{}{
time.Date(1971, 1, 1, 0, 0, 0, 0, time.UTC),
2.0,
1.0,
}},
},
Expand Down