diff --git a/cmd/filterList.go b/cmd/filterList.go index a8f3879..3e2dc61 100644 --- a/cmd/filterList.go +++ b/cmd/filterList.go @@ -33,30 +33,29 @@ var FilterList = &cobra.Command{ PreRunE: PreRunDefaultProfile, Run: func(command *cobra.Command, args []string) { client := DefaultClient() - - - p:= model.UiApp() - _,err := p.Run(); if err != nil { - os.Exit(1) + + 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()) + a := model.FilterToApply() + d := model.FilterToDelete() + if a.Stream() != "" { + filterToPbQuery(a.Stream(), a.StartTime(), a.EndTime()) } - if d.FilterId() != ""{ - deleteFilter(&client,d.FilterId()) + 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{ +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") } @@ -72,70 +71,69 @@ func deleteFilter(client *HTTPClient, filterID string){ } // Convert a filter to executable pb query -func filterToPbQuery( query string, start string, end string){ +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) + 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) + // 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 - } + t, err := parseTimeToFormat(input) + if err != nil { + return "", err + } - return t.Format(time.RFC3339), nil + return t.Format(time.RFC3339), nil } // Converts User inputted time to string type RFC3339 time -func formatToRFC3339(time string) string{ +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{ + 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{ + } else { + rfc39990time, err := convertTime(time) + if err != nil { fmt.Println("error formatting time") } formattedTime = rfc39990time diff --git a/cmd/query.go b/cmd/query.go index dca9318..2485802 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -65,9 +65,8 @@ var query = &cobra.Command{ 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] } + query = args[0] start, err := command.Flags().GetString(startFlag) if err != nil { @@ -85,7 +84,7 @@ var query = &cobra.Command{ end = defaultEnd } - //TODO: Interactive Flag disabled + //TODO: Interactive Flag disabled // interactive, err := command.Flags().GetBool(interactiveFlag) // if err != nil { // return err @@ -107,7 +106,7 @@ var query = &cobra.Command{ } filterNameTrimmed := strings.Trim(filterName, " ") - //TODO: Interactive Flag disabled + //TODO: Interactive Flag disabled // if interactive { // p := tea.NewProgram(model.NewQueryModel(DefaultProfile, query, startTime, endTime), tea.WithAltScreen()) // if _, err := p.Run(); err != nil { diff --git a/pkg/model/savedFilters.go b/pkg/model/savedFilters.go index 77847c2..c6d96aa 100644 --- a/pkg/model/savedFilters.go +++ b/pkg/model/savedFilters.go @@ -30,8 +30,8 @@ import ( ) const ( - applyFilterButton ="a" - deleteFilterButton ="d" + applyFilterButton = "a" + deleteFilterButton = "d" ) var docStyle = lipgloss.NewStyle().Margin(1, 2) @@ -46,13 +46,13 @@ type FilterDetails struct { } type item struct { - id,title, stream, desc, from, to string + id, title, stream, desc, from, to string } var ( - titleStyles = lipgloss.NewStyle().PaddingLeft(0).Bold(true).Foreground(lipgloss.Color("9")) - queryStyle = lipgloss.NewStyle().PaddingLeft(0).Foreground(lipgloss.Color("7")) - itemStyle = lipgloss.NewStyle().PaddingLeft(4).Foreground(lipgloss.Color("8")) + titleStyles = lipgloss.NewStyle().PaddingLeft(0).Bold(true).Foreground(lipgloss.Color("9")) + queryStyle = lipgloss.NewStyle().PaddingLeft(0).Foreground(lipgloss.Color("7")) + itemStyle = lipgloss.NewStyle().PaddingLeft(4).Foreground(lipgloss.Color("8")) // selectedItemStyle = lipgloss.NewStyle().PaddingLeft(4).Foreground(lipgloss.Color("170")) selectedItemStyle = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.AdaptiveColor{Light: "16", Dark: "226"}) ) @@ -69,9 +69,9 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list } var str string - if i.from != "" || i.to != ""{ - str = fmt.Sprintf("From: %s\nTo: %s",i.from, i.to) - }else{ + if i.from != "" || i.to != "" { + str = fmt.Sprintf("From: %s\nTo: %s", i.from, i.to) + } else { str = "" } @@ -87,56 +87,52 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list fmt.Fprint(w, fn(tr(i.title)+"\n"+qr(i.desc)+"\n"+str)) } - func (d itemDelegate) ShortHelp() []key.Binding { - return []key.Binding{ - key.NewBinding( - key.WithKeys(applyFilterButton), - key.WithHelp(applyFilterButton, "apply selected filter"), - ), - key.NewBinding( - key.WithKeys(deleteFilterButton), - key.WithHelp(deleteFilterButton, "delete selected filter"), - ), - } + return []key.Binding{ + key.NewBinding( + key.WithKeys(applyFilterButton), + key.WithHelp(applyFilterButton, "apply selected filter"), + ), + key.NewBinding( + key.WithKeys(deleteFilterButton), + key.WithHelp(deleteFilterButton, "delete selected filter"), + ), + } } // FullHelp returns the extended list of keybindings. func (d itemDelegate) FullHelp() [][]key.Binding { - return [][]key.Binding{ - { - key.NewBinding( + return [][]key.Binding{ + { + key.NewBinding( key.WithKeys(applyFilterButton), key.WithHelp(applyFilterButton, "apply selected filter"), - ), - key.NewBinding( + ), + key.NewBinding( key.WithKeys(deleteFilterButton), key.WithHelp(deleteFilterButton, "delete selected filter"), - ), - }, - } + ), + }, + } } - - - var selectedFilterApply item var selectedFilterDelete item -func (i item) Title() string { return fmt.Sprintf("Filter:%s, Query:%s",i.title,i.desc) } +func (i item) Title() string { return fmt.Sprintf("Filter:%s, Query:%s", i.title, i.desc) } func (i item) Description() string { - if i.to =="" || i.from==""{ + if i.to == "" || i.from == "" { return "" } - return fmt.Sprintf("From:%s To:%s",i.from,i.to) - } + return fmt.Sprintf("From:%s To:%s", i.from, i.to) +} func (i item) FilterValue() string { return i.title } -func (i item) FilterId() string { return i.id } -func (i item) Stream() string { return i.desc} -func (i item) StartTime() string { return i.from } -func (i item) EndTime() string { return i.to } +func (i item) FilterId() string { return i.id } +func (i item) Stream() string { return i.desc } +func (i item) StartTime() string { return i.from } +func (i item) EndTime() string { return i.to } type modelFilter struct { list list.Model @@ -152,14 +148,14 @@ func (m modelFilter) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if msg.String() == "ctrl+c" { return m, tea.Quit } - if msg.String() == "a" ||msg.Type == tea.KeyEnter{ + if msg.String() == "a" || msg.Type == tea.KeyEnter { selectedFilterApply = m.list.SelectedItem().(item) return m, tea.Quit } if msg.String() == "d" { selectedFilterDelete = m.list.SelectedItem().(item) return m, tea.Quit - + } case tea.WindowSizeMsg: h, v := docStyle.GetFrameSize() @@ -244,25 +240,28 @@ func fetchFilters(client *http.Client, profile *config.Profile) []list.Item { to = fmt.Sprintf("%v", toValue) } // filtering only SQL type filters Filter_name is tile and Stream Name is desc - if string(queryBytes) != "null" {userFilter = item{ - id:filter.FilterId, - title: filter.FilterName, - stream: filter.StreamName, - desc: string(queryBytes), - from: from, - to: to, + if string(queryBytes) != "null" { + userFilter = item{ + id: filter.FilterId, + title: filter.FilterName, + stream: filter.StreamName, + desc: string(queryBytes), + from: from, + to: to, + } + userFilters = append(userFilters, userFilter) } - userFilters = append(userFilters, userFilter)} } return userFilters } // returns the selected filter by user in the iteractive list -func FilterToApply() item{ +func FilterToApply() item { return selectedFilterApply } + // returns the selected filter by user in the iteractive list -func FilterToDelete() item{ +func FilterToDelete() item { return selectedFilterDelete -} \ No newline at end of file +}