Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
trueleo committed Sep 12, 2023
1 parent 27ba793 commit 0aed30b
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions pkg/model/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"pb/pkg/iterator"
"regexp"
"strings"
"sync"
"time"

"github.com/charmbracelet/bubbles/help"
Expand Down Expand Up @@ -155,6 +156,11 @@ func (m *QueryModel) currentFocus() string {
}

func (m *QueryModel) initIterator() {
iter := createIteratorFromModel(m)
m.queryIterator = iter
}

func createIteratorFromModel(m *QueryModel) *iterator.QueryIterator[QueryData, FetchResult] {
startTime := m.timeRange.start.Time()
endTime := m.timeRange.end.Time()

Expand All @@ -164,11 +170,10 @@ func (m *QueryModel) initIterator() {
regex := regexp.MustCompile(`^select\s+(?:\*|\w+(?:,\s*\w+)*)\s+from\s+(\w+)(?:\s+;)?$`)
matches := regex.FindStringSubmatch(m.query.Value())
if matches == nil {
m.queryIterator = nil
return
return nil
}
table := matches[1]
queryIterator := iterator.NewQueryIterator(
iter := iterator.NewQueryIterator(
startTime, endTime,
false,
func(t1, t2 time.Time) (QueryData, FetchResult) {
Expand All @@ -188,7 +193,7 @@ func (m *QueryModel) initIterator() {
count := res.Records[0]["count"].(float64)
return count > 0
})
m.queryIterator = &queryIterator
return &iter
}

func NewQueryModel(profile config.Profile, stream string, duration uint) QueryModel {
Expand Down Expand Up @@ -230,7 +235,7 @@ func NewQueryModel(profile config.Profile, stream string, duration uint) QueryMo
help := help.New()
help.Styles.FullDesc = lipgloss.NewStyle().Foreground(FocusSecondry)

return QueryModel{
model := QueryModel{
width: w,
height: h,
table: table,
Expand All @@ -242,12 +247,28 @@ func NewQueryModel(profile config.Profile, stream string, duration uint) QueryMo
queryIterator: nil,
status: NewStatusBar(profile.URL, stream, w),
}
model.queryIterator = createIteratorFromModel(&model)
return model
}

func (m QueryModel) Init() tea.Cmd {
// Just return `nil`, which means "no I/O right now, please."
m.initIterator()
return NewFetchTask(m.profile, m.query.Value(), m.timeRange.StartValueUtc(), m.timeRange.EndValueUtc())
return func() tea.Msg {
var ready sync.WaitGroup
ready.Add(1)
go func() {
m.initIterator()
for !m.queryIterator.Ready() {
time.Sleep(time.Millisecond * 100)
}
ready.Done()
}()
ready.Wait()
if m.queryIterator.Finished() {
return nil
}

return IteratorNext(m.queryIterator)()
}
}

func (m QueryModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand Down

0 comments on commit 0aed30b

Please sign in to comment.