Skip to content

Commit

Permalink
Go fmt'ed all files
Browse files Browse the repository at this point in the history
  • Loading branch information
Koustavd18 committed Aug 5, 2024
1 parent c68f3a2 commit c3d25e0
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 119 deletions.
124 changes: 61 additions & 63 deletions cmd/filterList.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

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,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
Expand All @@ -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 {
Expand Down
103 changes: 51 additions & 52 deletions pkg/model/savedFilters.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
)

const (
applyFilterButton ="a"
deleteFilterButton ="d"
applyFilterButton = "a"
deleteFilterButton = "d"
)

var docStyle = lipgloss.NewStyle().Margin(1, 2)
Expand All @@ -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"})
)
Expand All @@ -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 = ""
}

Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
}
}

0 comments on commit c3d25e0

Please sign in to comment.