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

fix(query): Respect the now-time of the compiled query if it's provided #17670

Merged
merged 3 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 10 additions & 3 deletions http/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type QueryRequest struct {
Spec *flux.Spec `json:"spec,omitempty"`
AST *ast.Package `json:"ast,omitempty"`
Dialect QueryDialect `json:"dialect"`
Now time.Time `json:"now"`

// InfluxQL fields
Bucket string `json:"bucket,omitempty"`
Expand Down Expand Up @@ -252,12 +253,17 @@ func (r QueryRequest) proxyRequest(now func() time.Time) (*query.ProxyRequest, e
if err := r.Validate(); err != nil {
return nil, err
}

n := r.Now
if n.IsZero() {
n = now()
}

// Query is preferred over AST
var compiler flux.Compiler
if r.Query != "" {
switch r.Type {
case "influxql":
n := now()
compiler = &transpiler.Compiler{
Now: &n,
Query: r.Query,
Expand All @@ -267,15 +273,15 @@ func (r QueryRequest) proxyRequest(now func() time.Time) (*query.ProxyRequest, e
fallthrough
default:
compiler = lang.FluxCompiler{
Now: now(),
Now: n,
Extern: r.Extern,
Query: r.Query,
}
}
} else if r.AST != nil {
c := lang.ASTCompiler{
AST: r.AST,
Now: now(),
Now: n,
}
if r.Extern != nil {
c.PrependFile(r.Extern)
Expand Down Expand Up @@ -345,6 +351,7 @@ func QueryRequestFromProxyRequest(req *query.ProxyRequest) (*QueryRequest, error
case lang.ASTCompiler:
qr.Type = "flux"
qr.AST = c.AST
qr.Now = c.Now
default:
return nil, fmt.Errorf("unsupported compiler %T", c)
}
Expand Down
33 changes: 32 additions & 1 deletion http/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func TestQueryRequest_proxyRequest(t *testing.T) {
Query string
Type string
Dialect QueryDialect
Now time.Time
org *platform.Organization
}
tests := []struct {
Expand Down Expand Up @@ -264,9 +265,10 @@ func TestQueryRequest_proxyRequest(t *testing.T) {
Delimiter: ",",
DateTimeFormat: "RFC3339",
},
Now: time.Unix(1, 1),
org: &platform.Organization{},
},
now: func() time.Time { return time.Unix(1, 1) },
now: func() time.Time { return time.Unix(2, 2) },
want: &query.ProxyRequest{
Request: query.Request{
Compiler: lang.ASTCompiler{
Expand All @@ -282,6 +284,33 @@ func TestQueryRequest_proxyRequest(t *testing.T) {
},
},
},
{
name: "valid AST with calculated now",
fields: fields{
AST: &ast.Package{},
Type: "flux",
Dialect: QueryDialect{
Delimiter: ",",
DateTimeFormat: "RFC3339",
},
org: &platform.Organization{},
},
now: func() time.Time { return time.Unix(2, 2) },
want: &query.ProxyRequest{
Request: query.Request{
Compiler: lang.ASTCompiler{
AST: &ast.Package{},
Now: time.Unix(2, 2),
},
},
Dialect: &csv.Dialect{
ResultEncoderConfig: csv.ResultEncoderConfig{
NoHeader: false,
Delimiter: ',',
},
},
},
},
{
name: "valid AST with extern",
fields: fields{
Expand Down Expand Up @@ -345,6 +374,7 @@ func TestQueryRequest_proxyRequest(t *testing.T) {
},
org: &platform.Organization{},
},
now: func() time.Time { return time.Unix(1, 1) },
want: &query.ProxyRequest{
Request: query.Request{
Compiler: repl.Compiler{
Expand All @@ -371,6 +401,7 @@ func TestQueryRequest_proxyRequest(t *testing.T) {
Query: tt.fields.Query,
Type: tt.fields.Type,
Dialect: tt.fields.Dialect,
Now: tt.fields.Now,
Org: tt.fields.org,
}
got, err := r.proxyRequest(tt.now)
Expand Down