Skip to content

Commit

Permalink
Merge pull request #3 from nitisht/filter/list
Browse files Browse the repository at this point in the history
fix linting issues
  • Loading branch information
Koustavd18 authored Aug 6, 2024
2 parents 9798538 + 20c9a32 commit d8750df
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 80 deletions.
3 changes: 1 addition & 2 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import (
"io"
"net/http"
"net/url"
"time"

"pb/pkg/config"
"time"
)

type HTTPClient struct {
Expand Down
15 changes: 7 additions & 8 deletions cmd/filterList.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ package cmd
import (
"fmt"
"os"
"pb/pkg/model"
"strings"
"time"

"pb/pkg/model"

"github.com/spf13/cobra"
)

Expand All @@ -32,10 +31,10 @@ var FilterList = &cobra.Command{
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) {
Run: func(_ *cobra.Command, _ []string) {
client := DefaultClient()

p := model.UiApp()
p := model.UIApp()
_, err := p.Run()
if err != nil {
os.Exit(1)
Expand All @@ -46,16 +45,16 @@ var FilterList = &cobra.Command{
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)
deleteURL := `filters/filter/` + filterID
req, err := client.NewRequest("DELETE", deleteURL, nil)
if err != nil {
fmt.Println("Error deleting the filter")
}
Expand Down
1 change: 0 additions & 1 deletion cmd/pre.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"errors"
"os"

"pb/pkg/config"

"github.com/spf13/cobra"
Expand Down
1 change: 0 additions & 1 deletion cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"net/url"
"os"

"pb/pkg/config"
"pb/pkg/model/credential"
"pb/pkg/model/defaultprofile"
Expand Down
56 changes: 28 additions & 28 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"fmt"
"io"
"os"
"pb/pkg/config"
"strings"
"time"

"pb/pkg/config"
// "pb/pkg/model"

//! This dependancy is required by the interactive flag Do not remove
//! This dependency is required by the interactive flag Do not remove
// tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -183,32 +183,32 @@ func fetchData(client *HTTPClient, query string, startTime string, endTime strin
}

// Returns start and end time for query in RFC3339 format
func parseTime(start, end string) (time.Time, time.Time, error) {
if start == defaultStart && end == defaultEnd {
return time.Now().Add(-1 * time.Minute), time.Now(), nil
}

startTime, err := time.Parse(time.RFC3339, start)
if err != nil {
// try parsing as duration
duration, err := time.ParseDuration(start)
if err != nil {
return time.Time{}, time.Time{}, err
}
startTime = time.Now().Add(-1 * duration)
}

endTime, err := time.Parse(time.RFC3339, end)
if err != nil {
if end == "now" {
endTime = time.Now()
} else {
return time.Time{}, time.Time{}, err
}
}

return startTime, endTime, nil
}
// func parseTime(start, end string) (time.Time, time.Time, error) {
// if start == defaultStart && end == defaultEnd {
// return time.Now().Add(-1 * time.Minute), time.Now(), nil
// }

// startTime, err := time.Parse(time.RFC3339, start)
// if err != nil {
// // try parsing as duration
// duration, err := time.ParseDuration(start)
// if err != nil {
// return time.Time{}, time.Time{}, err
// }
// startTime = time.Now().Add(-1 * duration)
// }

// endTime, err := time.Parse(time.RFC3339, end)
// if err != nil {
// if end == "now" {
// endTime = time.Now()
// } else {
// return time.Time{}, time.Time{}, err
// }
// }

// return startTime, endTime, nil
// }

// create a request body for saving filter without time_filter
func createFilter(query string, filterName string) (err error) {
Expand Down
3 changes: 1 addition & 2 deletions cmd/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import (
"fmt"
"io"
"os"
"pb/pkg/model/role"
"strings"
"sync"

"pb/pkg/model/role"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
Expand Down
1 change: 0 additions & 1 deletion cmd/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"

"pb/pkg/config"

"github.com/apache/arrow/go/v13/arrow/array"
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"errors"
"os"

"pb/cmd"
"pb/pkg/config"

Expand Down
3 changes: 1 addition & 2 deletions pkg/model/credential/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
package credential

import (
"strings"

"pb/pkg/model/button"
"strings"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
Expand Down
1 change: 0 additions & 1 deletion pkg/model/defaultprofile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package defaultprofile
import (
"fmt"
"io"

"pb/pkg/config"

"github.com/charmbracelet/bubbles/list"
Expand Down
5 changes: 2 additions & 3 deletions pkg/model/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import (
"math"
"net/http"
"os"
"pb/pkg/config"
"pb/pkg/iterator"
"strings"
"sync"
"time"

"pb/pkg/config"
"pb/pkg/iterator"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
Expand Down
3 changes: 1 addition & 2 deletions pkg/model/role/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ package role

import (
"fmt"
"strings"

"pb/pkg/model/button"
"pb/pkg/model/selection"
"strings"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
Expand Down
53 changes: 27 additions & 26 deletions pkg/model/savedFilters.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
//
// 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 model

import (
"encoding/json"
"fmt"
"io"
"net/http"
"pb/pkg/config"
"strings"
"time"

"pb/pkg/config"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -39,14 +39,15 @@ var docStyle = lipgloss.NewStyle().Margin(1, 2)

// FilterDetails represents the structure of filter data
type FilterDetails struct {
FilterId string `json:"filter_id"`
FilterID string `json:"filter_id"`
FilterName string `json:"filter_name"`
StreamName string `json:"stream_name"`
QueryField map[string]interface{} `json:"query"`
TimeFilter map[string]interface{} `json:"time_filter"`
}

type item struct {
// Item represents the structure of the filter item
type Item struct {
id, title, stream, desc, from, to string
}

Expand All @@ -64,7 +65,7 @@ func (d itemDelegate) Height() int { return 4 }
func (d itemDelegate) Spacing() int { return 1 }
func (d itemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(item)
i, ok := listItem.(Item)
if !ok {
return
}
Expand Down Expand Up @@ -118,24 +119,24 @@ func (d itemDelegate) FullHelp() [][]key.Binding {
}

var (
selectedFilterApply item
selectedFilterDelete item
selectedFilterApply Item
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 {
func (i Item) Description() string {
if i.to == "" || i.from == "" {
return ""
}
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) 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 }

type modelFilter struct {
list list.Model
Expand All @@ -152,11 +153,11 @@ func (m modelFilter) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
if msg.String() == "a" || msg.Type == tea.KeyEnter {
selectedFilterApply = m.list.SelectedItem().(item)
selectedFilterApply = m.list.SelectedItem().(Item)
return m, tea.Quit
}
if msg.String() == "d" {
selectedFilterDelete = m.list.SelectedItem().(item)
selectedFilterDelete = m.list.SelectedItem().(Item)
return m, tea.Quit

}
Expand All @@ -174,8 +175,8 @@ func (m modelFilter) View() string {
return docStyle.Render(m.list.View())
}

// Interactive list for the user to display all the available filters (only saved SQL filters )
func UiApp() *tea.Program {
// UIApp lists interactive list for the user to display all the available filters (only saved SQL filters )
func UIApp() *tea.Program {
userConfig, err := config.ReadConfigFromFile()
if err != nil {
fmt.Println("Error reading Default Profile")
Expand Down Expand Up @@ -223,12 +224,12 @@ func fetchFilters(client *http.Client, profile *config.Profile) []list.Item {
var filters []FilterDetails
err = json.Unmarshal(body, &filters)
if err != nil {
fmt.Println("Error unmarshaling response:", err)
fmt.Println("Error unmarshalling response:", err)
return nil
}
var userFilters []list.Item
for _, filter := range filters {
var userFilter item
var userFilter Item
queryBytes, _ := json.Marshal(filter.QueryField["filter_query"])

// Extract "from" and "to" from time_filter
Expand All @@ -241,8 +242,8 @@ func fetchFilters(client *http.Client, profile *config.Profile) []list.Item {
}
// filtering only SQL type filters Filter_name is tile and Stream Name is desc
if string(queryBytes) != "null" {
userFilter = item{
id: filter.FilterId,
userFilter = Item{
id: filter.FilterID,
title: filter.FilterName,
stream: filter.StreamName,
desc: string(queryBytes),
Expand All @@ -255,12 +256,12 @@ func fetchFilters(client *http.Client, profile *config.Profile) []list.Item {
return userFilters
}

// returns the selected filter by user in the iteractive list
func FilterToApply() item {
// FilterToApply returns the selected filter by user in the interactive list to apply
func FilterToApply() Item {
return selectedFilterApply
}

// returns the selected filter by user in the iteractive list
func FilterToDelete() item {
// FilterToDelete returns the selected filter by user in the interactive list to delete
func FilterToDelete() Item {
return selectedFilterDelete
}
3 changes: 1 addition & 2 deletions pkg/model/timeinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ package model

import (
"fmt"
"time"

"pb/pkg/model/datetime"
"time"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
Expand Down

0 comments on commit d8750df

Please sign in to comment.