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

Filter/list #46

Closed
wants to merge 6 commits into from
Closed
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
142 changes: 142 additions & 0 deletions cmd/filterList.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) 2024 Parseable, Inc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
"fmt"
"os"
"pb/pkg/model"
"strings"
"time"

"github.com/spf13/cobra"
)

var FilterList = &cobra.Command{
Use: "list",
Example: "pb query list ",
Short: "List of saved filter for a stream",
Long: "\nShow a list of saved filter for a stream ",
PreRunE: PreRunDefaultProfile,
Run: func(command *cobra.Command, args []string) {
client := DefaultClient()

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

a := model.FilterToApply()
d := model.FilterToDelete()
if a.Stream() != "" {
filterToPbQuery(a.Stream(), a.StartTime(), a.EndTime())
}
if d.FilterId() != "" {
deleteFilter(&client, d.FilterId())
}
},
}

// Delete a saved filter from the list of filter
func deleteFilter(client *HTTPClient, filterID string) {
deleteUrl := `filters/filter/` + filterID
req, err := client.NewRequest("DELETE", deleteUrl, nil)
if err != nil {
fmt.Println("Error deleting the filter")
}

resp, err := client.client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()

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

// Convert a filter to executable pb query
func filterToPbQuery(query string, start string, end string) {
var timeStamps string
if start == "" || end == "" {
timeStamps = ``
} else {
startFormatted := formatToRFC3339(start)
endFormatted := formatToRFC3339(end)
timeStamps = ` --from=` + startFormatted + ` --to=` + endFormatted
}
queryTemplate := `pb query run ` + query + timeStamps
fmt.Printf("\nCopy and paste the command")
fmt.Printf("\n\n%s\n\n", queryTemplate)
}

// Parses all UTC time format from string to time interface
func parseTimeToFormat(input string) (time.Time, error) {
// List of possible formats
formats := []string{
time.RFC3339,
"2006-01-02 15:04:05",
"2006-01-02",
"01/02/2006 15:04:05",
"02-Jan-2006 15:04:05 MST",
"2006-01-02T15:04:05Z",
"02-Jan-2006",
}

var err error
var t time.Time

for _, format := range formats {
t, err = time.Parse(format, input)
if err == nil {
return t, nil
}
}

return t, fmt.Errorf("unable to parse time: %s", input)
}

// Converts to RFC3339
func convertTime(input string) (string, error) {
t, err := parseTimeToFormat(input)
if err != nil {
return "", err
}

return t.Format(time.RFC3339), nil
}

// Converts User inputted time to string type RFC3339 time
func formatToRFC3339(time string) string {
var formattedTime string
if len(strings.Fields(time)) > 1 {
newTime := strings.Fields(time)[0:2]
rfc39990time, err := convertTime(strings.Join(newTime, " "))
if err != nil {
fmt.Println("error formatting time")
}
formattedTime = rfc39990time
} else {
rfc39990time, err := convertTime(time)
if err != nil {
fmt.Println("error formatting time")
}
formattedTime = rfc39990time
}
return formattedTime
}
50 changes: 26 additions & 24 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"io"
"os"
"pb/pkg/config"
"pb/pkg/model"
// "pb/pkg/model"
"strings"
"time"

tea "github.com/charmbracelet/bubbletea"
//! This dependancy is required by the interactive flag Do not remove
// tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)

Expand All @@ -45,8 +45,8 @@
saveFilterTimeFlag = "with-time"
saveFilterTimeShort = "w"

interactiveFlag = "interactive"
interactiveFlagShort = "i"
// interactiveFlag = "interactive"
// interactiveFlagShort = "i"
)

var query = &cobra.Command{
Expand All @@ -65,9 +65,9 @@
fmt.Println("please enter your query")
fmt.Printf("Example:\n pb query run \"select * from frontend\" --from=10m --to=now\n")
return nil
} else {
query = args[0]
}

Check failure on line 68 in cmd/query.go

View workflow job for this annotation

GitHub Actions / build

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)

Check failure on line 68 in cmd/query.go

View workflow job for this annotation

GitHub Actions / build

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)

query = args[0]

start, err := command.Flags().GetString(startFlag)
if err != nil {
Expand All @@ -85,15 +85,16 @@
end = defaultEnd
}

interactive, err := command.Flags().GetBool(interactiveFlag)
if err != nil {
return err
}
//TODO: Interactive Flag disabled
// interactive, err := command.Flags().GetBool(interactiveFlag)
// if err != nil {
// return err
// }

startTime, endTime, err := parseTime(start, end)
if err != nil {
return err
}
// startTime, endTime, err := parseTime(start, end)
// if err != nil {
// return err
// }

keepTime, err := command.Flags().GetBool(saveFilterTimeFlag)
if err != nil {
Expand All @@ -106,14 +107,15 @@
}
filterNameTrimmed := strings.Trim(filterName, " ")

if interactive {
p := tea.NewProgram(model.NewQueryModel(DefaultProfile, query, startTime, endTime), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Printf("there's been an error: %v", err)
os.Exit(1)
}
return nil
}
//TODO: Interactive Flag disabled
// if interactive {
// p := tea.NewProgram(model.NewQueryModel(DefaultProfile, query, startTime, endTime), tea.WithAltScreen())
// if _, err := p.Run(); err != nil {
// fmt.Printf("there's been an error: %v", err)
// os.Exit(1)
// }
// return nil
// }

// Checks if there is filter name which is not empty. Empty filter name wont be allowed
if command.Flags().Changed(saveFilterFlag) {
Expand Down Expand Up @@ -143,7 +145,7 @@

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(interactiveFlag, interactiveFlagShort, false, "open the query result in interactive mode")
// 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)
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ go 1.22

require (
github.com/apache/arrow/go/v13 v13.0.0
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.26.4
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.26.6
github.com/charmbracelet/lipgloss v0.12.1
github.com/dustin/go-humanize v1.0.1
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8
golang.org/x/term v0.21.0
google.golang.org/grpc v1.64.0
)

require (
github.com/charmbracelet/x/ansi v0.1.2 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // indirect
github.com/charmbracelet/x/input v0.1.0 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.1.0 // indirect
Expand All @@ -37,17 +38,16 @@ require (

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/charmbracelet/lipgloss v0.7.1
github.com/evertras/bubble-table v0.15.2
github.com/muesli/termenv v0.15.2
github.com/pelletier/go-toml/v2 v2.0.9
github.com/sahilm/fuzzy v0.1.0 // indirect
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
Expand Down
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.16.1 h1:6uzpAAaT9ZqKssntbvZMlksWHruQLNxg49H5WdeuYSY=
github.com/charmbracelet/bubbles v0.16.1/go.mod h1:2QCp9LFlEsBQMvIYERr7Ww2H2bA7xen1idUDIzm/+Xc=
github.com/charmbracelet/bubbletea v0.26.4 h1:2gDkkzLZaTjMl/dQBpNVtnvcCxsh/FCkimep7FC9c40=
github.com/charmbracelet/bubbletea v0.26.4/go.mod h1:P+r+RRA5qtI1DOHNFn0otoNwB4rn+zNAzSj/EXz6xU0=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/charmbracelet/x/ansi v0.1.2 h1:6+LR39uG8DE6zAmbu023YlqjJHkYXDF1z36ZwzO4xZY=
github.com/charmbracelet/x/ansi v0.1.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0=
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
github.com/charmbracelet/bubbletea v0.26.6 h1:zTCWSuST+3yZYZnVSvbXwKOPRSNZceVeqpzOLN2zq1s=
github.com/charmbracelet/bubbletea v0.26.6/go.mod h1:dz8CWPlfCCGLFbBlTY4N7bjLiyOGDJEnd2Muu7pOWhk=
github.com/charmbracelet/lipgloss v0.12.1 h1:/gmzszl+pedQpjCOH+wFkZr/N90Snz40J/NR7A0zQcs=
github.com/charmbracelet/lipgloss v0.12.1/go.mod h1:V2CiwIuhx9S1S1ZlADfOj9HmxeMAORuz5izHb0zGbB8=
github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=
github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=
github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=
github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=
Expand Down Expand Up @@ -46,8 +46,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
Expand All @@ -72,8 +72,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func main() {
stream.AddCommand(cmd.StatStreamCmd)

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

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