forked from nathanielc/morgoth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work on scheduled queries for lossy counting
- Loading branch information
1 parent
b83a20e
commit 8b700da
Showing
9 changed files
with
136 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package influxdb | ||
|
||
import ( | ||
"fmt" | ||
"errors" | ||
"github.com/influxdb/influxdb/influxql" | ||
"time" | ||
) | ||
|
||
type Query struct { | ||
statement *influxql.SelectStatement | ||
startTL *influxql.TimeLiteral | ||
stopTL *influxql.TimeLiteral | ||
} | ||
|
||
func NewQuery(quertStr string) (*Query, error) { | ||
|
||
s, err := influxql.ParseStatement(quertStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stmt, ok := s.(*influxql.SelectStatement) | ||
if !ok { | ||
return nil, errors.New(fmt.Sprintf("Query must be a select statement '%s'", quertStr)) | ||
} | ||
|
||
|
||
//Add New BinaryExpr for time clause | ||
startTL := &influxql.TimeLiteral{} | ||
startExpr := &influxql.BinaryExpr{ | ||
Op: influxql.GT, | ||
LHS: &influxql.VarRef{Val: "time"}, | ||
RHS: startTL, | ||
} | ||
|
||
stopTL := &influxql.TimeLiteral{} | ||
stopExpr := &influxql.BinaryExpr{ | ||
Op: influxql.LT, | ||
LHS: &influxql.VarRef{Val: "time"}, | ||
RHS: stopTL, | ||
} | ||
if stmt.Condition != nil { | ||
stmt.Condition = &influxql.BinaryExpr{ | ||
Op: influxql.AND, | ||
LHS: stmt.Condition, | ||
RHS: &influxql.BinaryExpr{ | ||
Op: influxql.AND, | ||
LHS: startExpr, | ||
RHS: stopExpr, | ||
}, | ||
} | ||
} else { | ||
stmt.Condition = &influxql.BinaryExpr{ | ||
Op: influxql.AND, | ||
LHS: startExpr, | ||
RHS: stopExpr, | ||
} | ||
} | ||
|
||
return &Query{ | ||
statement: stmt, | ||
startTL: startTL, | ||
stopTL: stopTL, | ||
}, nil | ||
} | ||
|
||
func (self *Query) QueryForTimeRange(start, stop time.Time) string { | ||
|
||
self.startTL.Val = start | ||
self.stopTL.Val = stop | ||
|
||
return self.statement.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package influxdb_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/nvcook42/morgoth/Godeps/_workspace/src/github.com/stretchr/testify/assert" | ||
"github.com/nvcook42/morgoth/engine/influxdb" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestScheduleQueryShouldAddTimeWhereCondition(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
type testCase struct { | ||
quertStr string | ||
start time.Time | ||
stop time.Time | ||
expectedStr string | ||
} | ||
|
||
testCases := []testCase{ | ||
testCase{ | ||
quertStr: "SELECT value FROM kbps", | ||
start: time.Unix(1433378783, 0), | ||
stop: time.Unix(1433379783, 0), | ||
expectedStr: "SELECT value FROM kbps WHERE time > \"2015-06-04 00:46:23\" AND time < \"2015-06-04 01:03:03\"", | ||
}, | ||
} | ||
|
||
test := func(queryStr, start, stop, expectedStr) { | ||
q, err := influxdb.NewQuery(quertStr) | ||
assert.Nil(err) | ||
assert.Equal( | ||
expectedStr, | ||
q.QueryForTimeRange(start, stop), | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package engine | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type Query interface { | ||
QueryForTimeRange(start, stop time.Time) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.