-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Update query command to output JSON #29
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"os" | ||
"pb/pkg/model" | ||
"strconv" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
durationFlag = "duration" | ||
durationFlagShort = "d" | ||
defaultDuration = "10" | ||
|
||
startFlag = "from" | ||
startFlagShort = "f" | ||
defaultStart = "1m" | ||
|
||
endFlag = "to" | ||
endFlagShort = "t" | ||
defaultEnd = "now" | ||
) | ||
|
||
var queryInteractive = &cobra.Command{ | ||
Use: "i [stream-name] --duration 10", | ||
Example: " pb query frontend --duration 10", | ||
Short: "Interactive query table view", | ||
Long: "\n command is used to open a prompt to query a stream.", | ||
Args: cobra.ExactArgs(1), | ||
PreRunE: PreRunDefaultProfile, | ||
RunE: func(command *cobra.Command, args []string) error { | ||
stream := args[0] | ||
duration, _ := command.Flags().GetString(durationFlag) | ||
|
||
if duration == "" { | ||
duration = defaultDuration | ||
} | ||
durationInt, err := strconv.Atoi(duration) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
p := tea.NewProgram(model.NewQueryModel(DefaultProfile, stream, uint(durationInt)), tea.WithAltScreen()) | ||
if _, err := p.Run(); err != nil { | ||
fmt.Printf("there's been an error: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
var queryJSON = &cobra.Command{ | ||
Use: "query [query] --from=10m --to=now", | ||
Example: " pb query \"select * from frontend\" --from=10m --to=now", | ||
Short: "Run SQL query", | ||
Long: "\nquery command is used to run query. Output format is json string", | ||
Args: cobra.ExactArgs(1), | ||
PreRunE: PreRunDefaultProfile, | ||
RunE: func(command *cobra.Command, args []string) error { | ||
query := args[0] | ||
start, _ := command.Flags().GetString(startFlag) | ||
end, _ := command.Flags().GetString(endFlag) | ||
|
||
if start == "" { | ||
start = defaultStart | ||
} | ||
if end == "" { | ||
end = defaultEnd | ||
} | ||
|
||
client := DefaultClient() | ||
return fetchData(&client, query, start, end) | ||
}, | ||
} | ||
|
||
var QueryInteractiveCmd = func() *cobra.Command { | ||
queryInteractive.Flags().StringP(durationFlag, durationFlagShort, defaultDuration, "specify the duration in minutes for which queries should be executed. Defaults to 10 minutes") | ||
return queryInteractive | ||
}() | ||
|
||
var QueryCmd = func() *cobra.Command { | ||
queryJSON.Flags().StringP(startFlag, startFlagShort, defaultStart, "Specify start datetime of query. Supports RFC3999 time format and durations (ex. 10m, 1hr ..) ") | ||
queryJSON.Flags().StringP(endFlag, endFlagShort, defaultEnd, "Specify end datetime of query. Supports RFC3999 time format and literal - now ") | ||
queryJSON.AddCommand(queryInteractive) | ||
return queryJSON | ||
}() | ||
|
||
func fetchData(client *HTTPClient, query string, startTime string, endTime string) (err error) { | ||
queryTemplate := `{ | ||
"query": "%s", | ||
"startTime": "%s", | ||
"endTime": "%s" | ||
} | ||
` | ||
|
||
finalQuery := fmt.Sprintf(queryTemplate, query, startTime, endTime) | ||
|
||
req, err := client.NewRequest("POST", "query", bytes.NewBuffer([]byte(finalQuery))) | ||
if err != nil { | ||
return | ||
} | ||
resp, err := client.client.Do(req) | ||
if err != nil { | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != 200 { | ||
body, _ := io.ReadAll(resp.Body) | ||
fmt.Println(string(body)) | ||
} else { | ||
io.Copy(os.Stdout, resp.Body) | ||
} | ||
return | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
licence header