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

feat: add profiler to query command in cli #21006

Merged
merged 9 commits into from
Mar 23, 2021
61 changes: 56 additions & 5 deletions cmd/influx/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
)

var queryFlags struct {
org organization
file string
raw bool
org organization
file string
raw bool
profiler bool
}

func cmdQuery(f *globalFlags, opts genericCLIOpts) *cobra.Command {
Expand All @@ -36,6 +37,7 @@ func cmdQuery(f *globalFlags, opts genericCLIOpts) *cobra.Command {
queryFlags.org.register(opts.viper, cmd, true)
cmd.Flags().StringVarP(&queryFlags.file, "file", "f", "", "Path to Flux query file")
cmd.Flags().BoolVarP(&queryFlags.raw, "raw", "r", false, "Display raw query results")
cmd.Flags().BoolVarP(&queryFlags.profiler, "profiler-enabled", "p", false, "Return profiler information with the query results")

return cmd
}
Expand Down Expand Up @@ -101,15 +103,64 @@ func fluxQueryF(cmd *cobra.Command, args []string) error {
}
u.RawQuery = params.Encode()

body, _ := json.Marshal(map[string]interface{}{
var body_map = map[string]interface{}{
"query": q,
"type": "flux",
"dialect": map[string]interface{}{
"annotations": []string{"group", "datatype", "default"},
"delimiter": ",",
"header": true,
},
})
}

if queryFlags.profiler {
body_map["extern"] = map[string]interface{}{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the map[string]interface{}{...} definition to a top-level const? IMO it'd make it more clear that it's a "magic" constant instead of something that depends on user input.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once it's moved, is there a docs page we could link to in a comment that explains the structure? In case something changes in the future and we need to figure out how this should be updated to match

"type": "File",
"imports": []interface{}{
map[string]interface{}{
"type": "ImportDeclaration",
"path": map[string]interface{}{
"type": "StringLiteral",
"value": "profiler",
},
},
},
"body": []interface{}{
map[string]interface{}{
"assignment": map[string]interface{}{
"member": map[string]interface{}{
"object": map[string]interface{}{
"name": "profiler",
"type": "Identifier",
},
"property": map[string]interface{}{
"name": "enabledProfilers",
"type": "Identifier",
},
"type": "MemberExpression",
},
"init": map[string]interface{}{
"type": "ArrayExpression",
"elements": []interface{}{
map[string]interface{}{
"type": "StringLiteral",
"value": "query",
},
map[string]interface{}{
"type": "StringLiteral",
"value": "operator",
},
},
},
"type": "MemberAssignment",
},
"type": "OptionStatement",
},
},
}
}

body, _ := json.Marshal(body_map)

req, _ := http.NewRequest("POST", u.String(), bytes.NewReader(body))
req.Header.Set("Authorization", "Token "+flags.config().Token)
Expand Down