From 3db5a85139db5e632c094d3e56c0b6e67c612e47 Mon Sep 17 00:00:00 2001 From: Cory LaNou Date: Thu, 3 Sep 2015 15:04:47 -0500 Subject: [PATCH] all tests passing for top --- influxql/functions.go | 5 +++-- influxql/functions_test.go | 26 +++++++++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/influxql/functions.go b/influxql/functions.go index 0872e11049..948407b82b 100644 --- a/influxql/functions.go +++ b/influxql/functions.go @@ -1432,10 +1432,11 @@ func MapTop(itr Iterator, c *Call) interface{} { collected++ } } + o := positionOut{callArgs: topCallArgs(c), points: points} + sort.Sort(topMapOut{o}) + points = o.points // If we got more than we needed, sort them and return the top if collected > needed { - o := positionOut{callArgs: topCallArgs(c), points: points} - sort.Sort(topMapOut{o}) points = o.points[:needed] } diff --git a/influxql/functions_test.go b/influxql/functions_test.go index b0ea9d1bc5..326ab50f0b 100644 --- a/influxql/functions_test.go +++ b/influxql/functions_test.go @@ -18,10 +18,11 @@ type point struct { } type testIterator struct { - values []point - lastTags map[string]string - nextFunc func() (timestamp int64, value interface{}) - tagsFunc func() map[string]string + values []point + lastTags map[string]string + nextFunc func() (timestamp int64, value interface{}) + tagsFunc func() map[string]string + bucketTimeFunc func() int64 } func (t *testIterator) Next() (timestamp int64, value interface{}) { @@ -45,6 +46,13 @@ func (t *testIterator) Tags() map[string]string { return t.lastTags } +func (t *testIterator) BucketTime() int64 { + if t.bucketTimeFunc != nil { + return t.bucketTimeFunc() + } + return -1 +} + func TestMapMeanNoValues(t *testing.T) { iter := &testIterator{} if got := MapMean(iter); got != nil { @@ -497,19 +505,19 @@ func TestMapTop(t *testing.T) { call: &Call{Name: "top", Args: []Expr{&VarRef{Val: "field1"}, &NumberLiteral{Val: 2}}}, }, { - name: "int64 - basic with extra tag", + name: "int64 - basic with tag", iter: &testIterator{ values: []point{ {"", 10, int64(99), map[string]string{"host": "a"}}, - {"", 10, int64(53), map[string]string{"host": "b"}}, - {"", 20, int64(88), map[string]string{"host": "a"}}, + {"", 20, int64(53), map[string]string{"host": "b"}}, + {"", 30, int64(88), map[string]string{"host": "a"}}, }, }, exp: positionOut{ callArgs: []string{"host"}, points: PositionPoints{ positionPoint{10, int64(99), map[string]string{"host": "a"}}, - positionPoint{20, int64(88), map[string]string{"host": "a"}}, + positionPoint{20, int64(53), map[string]string{"host": "b"}}, }, }, call: &Call{Name: "top", Args: []Expr{&VarRef{Val: "field1"}, &VarRef{Val: "host"}, &NumberLiteral{Val: 2}}}, @@ -519,7 +527,7 @@ func TestMapTop(t *testing.T) { iter: &testIterator{ values: []point{ {"", 20, int64(99), map[string]string{"host": "a"}}, - {"", 10, int64(53), map[string]string{"host": "b"}}, + {"", 10, int64(53), map[string]string{"host": "a"}}, {"", 10, int64(99), map[string]string{"host": "a"}}, }, },