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

Rename #55

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 15 additions & 15 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ var (
defaultEnd = "now"

// save filter flags
saveFilterFlag = "save-as"
saveFilterShort = "s"
saveQueryFlag = "save-as"
saveQueryShort = "s"
// save filter with time flags
saveFilterTimeFlag = "with-time"
saveFilterTimeShort = "w"
saveQueryTimeFlag = "with-time"
saveQueryTimeShort = "w"

// interactiveFlag = "interactive"
// interactiveFlagShort = "i"
Expand Down Expand Up @@ -98,16 +98,16 @@ var query = &cobra.Command{
// return err
// }

keepTime, err := command.Flags().GetBool(saveFilterTimeFlag)
keepTime, err := command.Flags().GetBool(saveQueryTimeFlag)
if err != nil {
return err
}

filterName, err := command.Flags().GetString(saveFilterFlag)
savedQueryName, err := command.Flags().GetString(saveQueryFlag)
if err != nil {
return err
}
filterNameTrimmed := strings.Trim(filterName, " ")
savedQueryNameTrimmed := strings.Trim(savedQueryName, " ")

// TODO: Interactive Flag disabled
// if interactive {
Expand All @@ -120,20 +120,20 @@ var query = &cobra.Command{
// }

// Checks if there is filter name which is not empty. Empty filter name wont be allowed
if command.Flags().Changed(saveFilterFlag) {
if filterName == "" || len(filterNameTrimmed) == 0 || filterName == "=" {
if command.Flags().Changed(saveQueryFlag) {
if savedQueryName == "" || len(savedQueryNameTrimmed) == 0 || savedQueryName == "=" {
fmt.Println("please provide a filter name")
command.Help()
return nil
} else if filterName != "" {
} else if savedQueryName != "" {
if keepTime {
createFilterWithTime(query, filterNameTrimmed, start, end)
createFilterWithTime(query, savedQueryNameTrimmed, start, end)
} else {
// if there is no keep time filter pass empty values for startTime and endTime
createFilter(query, filterNameTrimmed)
createFilter(query, savedQueryNameTrimmed)
}
}
} else if filterName == "" && keepTime {
} else if savedQueryName == "" && keepTime {
fmt.Println("please provide a filter name")
command.Help()
return nil
Expand All @@ -145,11 +145,11 @@ var query = &cobra.Command{
}

var QueryCmd = func() *cobra.Command {
query.Flags().BoolP(saveFilterTimeFlag, saveFilterTimeShort, false, "Save the time range associated in the query to the filter") // save time for a filter flag; default value = false (boolean type)
query.Flags().BoolP(saveQueryTimeFlag, saveQueryTimeShort, false, "Save the time range associated in the query to the filter") // save time for a filter flag; default value = false (boolean type)
// query.Flags().BoolP(interactiveFlag, interactiveFlagShort, false, "open the query result in interactive mode")
query.Flags().StringP(startFlag, startFlagShort, defaultStart, "Start time for query. Takes date as '2024-10-12T07:20:50.52Z' or string like '10m', '1hr'")
query.Flags().StringP(endFlag, endFlagShort, defaultEnd, "End time for query. Takes date as '2024-10-12T07:20:50.52Z' or 'now'")
query.Flags().StringP(saveFilterFlag, saveFilterShort, "", "Save a query filter") // save filter flag. Default value = FILTER_NAME (type string)
query.Flags().StringP(saveQueryFlag, saveQueryShort, "", "Save a query filter") // save filter flag. Default value = FILTER_NAME (type string)
return query
}()

Expand Down
32 changes: 16 additions & 16 deletions cmd/filterlist.go → cmd/queryList.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,38 @@ import (
"github.com/spf13/cobra"
)

var FilterList = &cobra.Command{
var SavedQueryList = &cobra.Command{
Use: "list",
Example: "pb query list ",
Short: "List of saved filters",
Long: "\nShow the list of saved filters for current user",
Short: "List of saved queries",
Long: "\nShow the list of saved quries for active user",
PreRunE: PreRunDefaultProfile,
Run: func(_ *cobra.Command, _ []string) {
client := DefaultClient()

p := model.UIApp()
p := model.SavedQueriesMenu()
if _, err := p.Run(); err != nil {
os.Exit(1)
}

a := model.FilterToApply()
d := model.FilterToDelete()
a := model.QueryToApply()
d := model.QueryToDelete()
if a.Stream() != "" {
filterToPbQuery(a.Stream(), a.StartTime(), a.EndTime())
savedQueryToPbQuery(a.Stream(), a.StartTime(), a.EndTime())
}
if d.FilterID() != "" {
deleteFilter(&client, d.FilterID(), d.Title())
if d.SavedQueryID() != "" {
deleteSavedQuery(&client, d.SavedQueryID(), d.Title())
}
},
}

// Delete a saved filter from the list of filter
func deleteFilter(client *HTTPClient, filterID, title string) {
// Delete a saved query from the list.
func deleteSavedQuery(client *HTTPClient, savedQueryID, title string) {
fmt.Printf("\nAttempting to delete '%s'", title)
deleteURL := `filters/filter/` + filterID
deleteURL := `filters/filter/` + savedQueryID
req, err := client.NewRequest("DELETE", deleteURL, nil)
if err != nil {
fmt.Println("Failed to delete the filter with error: ", err)
fmt.Println("Failed to delete the saved query with error: ", err)
}

resp, err := client.client.Do(req)
Expand All @@ -66,12 +66,12 @@ func deleteFilter(client *HTTPClient, filterID, title string) {
defer resp.Body.Close()

if resp.StatusCode == 200 {
fmt.Printf("\nFilter deleted\n\n")
fmt.Printf("\nSaved Query deleted\n\n")
}
}

// Convert a filter to executable pb query
func filterToPbQuery(query string, start string, end string) {
// Convert a saved query to executable pb query
func savedQueryToPbQuery(query string, start string, end string) {
var timeStamps string
if start == "" || end == "" {
timeStamps = ``
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func main() {
stream.AddCommand(cmd.StatStreamCmd)

query.AddCommand(cmd.QueryCmd)
query.AddCommand(cmd.FilterList)
query.AddCommand(cmd.SavedQueryList)

cli.AddCommand(profile)
cli.AddCommand(query)
Expand Down
Loading
Loading