-
Notifications
You must be signed in to change notification settings - Fork 69
search: add support for streaming search #485
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
Conversation
5c13ca5 to
c881c3e
Compare
|
I'll take a deeper look, but please add a changelog for these additions :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved but left a few comments, because I think the code can be made cleaner and easier to understand/maintain.
Also: no tests? 😬
internal/api/flags.go
Outdated
| return &Flags{ | ||
| trace: flagSet.Bool("trace", false, "Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracing"), | ||
| insecureSkipVerify: flagSet.Bool("insecure-skip-verify", false, "Skip validation of TLS certificates against trusted chains"), | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the duplication here? The NewFlags already returns a *Flags with both of these fields.
Edit: And wouldn't parsing fail if someone sets the get-curl flag, for example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edit: And wouldn't parsing fail if someone sets the get-curl flag, for example?
parsing only fails if someone calls search with -stream AND -get-curl, which is intended. -curl and -dump make a lot less sense for streaming than for the GraphQL API. I assume the use case for get-curl is to pipe the output to something like jq? The streaming endpoint does not return valid JSON, but a stream of events that contain JSON.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could make get curl work though? My original streaming client was curl :D But yeah, until we have stabalized the API properly I don't want people to start writing there own clients.
| } | ||
| return streamHandler(argsWOStream) | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you define -stream as a noop on the other flagset that you then use in streamHandler you wouldn't have to do this here, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, but then we mess with the auto-documentation. Suddenly the "streaming search" has a "-stream" flag which is odd. I traded odd code against odd documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not create a separate stream-search command?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is based on a discussion we had around exhaustive search which is documented in RFC 288. At some point we want phase out -stream and replace search with streaming search. That is part of the reason why I wrote it as handler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kind of weird though:
$ ./src search -stream -get-curl foo
flag provided but not defined: -get-curl
Usage of streaming search:
-display int
Limit the number of results that are displayed. Note that the statistics continue to report all results. (default -1)
-insecure-skip-verify
Skip validation of TLS certificates against trusted chains
-trace
Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracingGiven we intend on replacing search with stream search in the future, can't we make the stream search stuff not use its own flagset. I would think an approach like
streaming.Search(streaming.Opts{
Query: flagSet.Args(),
Trace: *trace,
Display: *display,
InsecureSkipVerify: *insecureSkipVerify,
})would make sense. Or even you pass in the http client you use that hides the trace/insecure stuff.
That way you can also move the large amounts of business logic out of ./cmd/src into ./internal/streaming
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually like this behavior, but since both reviewers commented on this I may be on the wrong track here. I refactored it to be a simple function call instead of a handler and moved parts of the logic to internal/streaming.
cmd/src/search_stream.go
Outdated
| }, | ||
| OnMatches: func(matches []streaming.EventMatch) { | ||
| for _, match := range matches { | ||
| if file, ok := match.(*streaming.EventFileMatch); ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a switch match := match.(type) instead of if and continue?
cmd/src/search_stream.go
Outdated
| }, | ||
| ) | ||
| if err != nil { | ||
| fmt.Printf("ExecuteTemplate: %s\n", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not print all of these error messages stderr? (You can use flagSet.Output() as below)
cmd/src/search_stream.go
Outdated
| defer resp.Body.Close() | ||
|
|
||
| // Process response. | ||
| err = streaming.Decoder{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd personally pull some of this out to make it more readable. All of these callbacks require only the template (and stderr, see below), so you could use closures to make it flatter:
func newDecoder(t *template.Template) streaming.Decoder {
return streaming.Decoder{
OnProgress: onProgress(t),
// [...]
}
}
func onProgress(t *template.Template) func(*streaming.Progress) {
return func(progress *streaming.Progress) {
// We only show the final progress.
if !progress.Done {
return
}
err = t.ExecuteTemplate(os.Stdout, "progress", progress)
if err != nil {
fmt.Printf("ExecuteTemplate: %s\n", err)
}
return
}
}
cmd/src/search_stream.go
Outdated
| err = t.ExecuteTemplate(os.Stdout, "commit", struct { | ||
| SourcegraphEndpoint string | ||
| *streaming.EventCommitMatch | ||
| }{ | ||
| SourcegraphEndpoint: cfg.Endpoint, | ||
| EventCommitMatch: commit, | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could clean up this render code a bit if you introduce something like a Context struct that has all of the fields defined but on which you only set the current struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I also went back and forth about this one. I think the current approach is pretty straightforward although maybe a bit verbose. If we use a common context struct then the templates get more difficult to read. I will see if I can find something better.
The PRs are stacked. You reviewed before I had the chance to push the 3rd PR with tests :-). EDIT: Acutally ended up closing the test-PR because (#486). I rewrote the tests and pushed here. |
|
@keegancsmith PTAL. Tests are now integrated in this PR. |
| } | ||
| return streamHandler(argsWOStream) | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kind of weird though:
$ ./src search -stream -get-curl foo
flag provided but not defined: -get-curl
Usage of streaming search:
-display int
Limit the number of results that are displayed. Note that the statistics continue to report all results. (default -1)
-insecure-skip-verify
Skip validation of TLS certificates against trusted chains
-trace
Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracingGiven we intend on replacing search with stream search in the future, can't we make the stream search stuff not use its own flagset. I would think an approach like
streaming.Search(streaming.Opts{
Query: flagSet.Args(),
Trace: *trace,
Display: *display,
InsecureSkipVerify: *insecureSkipVerify,
})would make sense. Or even you pass in the http client you use that hides the trace/insecure stuff.
That way you can also move the large amounts of business logic out of ./cmd/src into ./internal/streaming
This adds the flag `-stream` to the `search` sub-command. Searches submitted with `-stream` are routed to the endpoint `search/stream` instead of the GraphQL api. In addition, users can set `-display <int>` to set the display limit. We will add support for JSON in a follow-up PR.
This removes the dedicated flagSet for streaming search and move parts of the logic to internal streaming.
a96cc4b to
5c951db
Compare
This adds the flag `-stream` to the `search` sub-command. Searches submitted with `-stream` are routed to the endpoint `search/stream` instead of the GraphQL API. In addition, users can set `-display <int>` to set the display limit. We will add support for JSON in a follow-up PR.
Stacked on top of #484
This adds the flag
-streamto thesearchsub-command. Searchessubmitted with
-streamare routed to the endpointsearch/streaminstead of the GraphQL api.
In addition, users can set
-display <int>to set the display limit.
We will add support for JSON in a follow-up PR.