Skip to content

Commit 0aed30b

Browse files
committed
Fix
1 parent 27ba793 commit 0aed30b

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

pkg/model/query.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"pb/pkg/iterator"
2828
"regexp"
2929
"strings"
30+
"sync"
3031
"time"
3132

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

157158
func (m *QueryModel) initIterator() {
159+
iter := createIteratorFromModel(m)
160+
m.queryIterator = iter
161+
}
162+
163+
func createIteratorFromModel(m *QueryModel) *iterator.QueryIterator[QueryData, FetchResult] {
158164
startTime := m.timeRange.start.Time()
159165
endTime := m.timeRange.end.Time()
160166

@@ -164,11 +170,10 @@ func (m *QueryModel) initIterator() {
164170
regex := regexp.MustCompile(`^select\s+(?:\*|\w+(?:,\s*\w+)*)\s+from\s+(\w+)(?:\s+;)?$`)
165171
matches := regex.FindStringSubmatch(m.query.Value())
166172
if matches == nil {
167-
m.queryIterator = nil
168-
return
173+
return nil
169174
}
170175
table := matches[1]
171-
queryIterator := iterator.NewQueryIterator(
176+
iter := iterator.NewQueryIterator(
172177
startTime, endTime,
173178
false,
174179
func(t1, t2 time.Time) (QueryData, FetchResult) {
@@ -188,7 +193,7 @@ func (m *QueryModel) initIterator() {
188193
count := res.Records[0]["count"].(float64)
189194
return count > 0
190195
})
191-
m.queryIterator = &queryIterator
196+
return &iter
192197
}
193198

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

233-
return QueryModel{
238+
model := QueryModel{
234239
width: w,
235240
height: h,
236241
table: table,
@@ -242,12 +247,28 @@ func NewQueryModel(profile config.Profile, stream string, duration uint) QueryMo
242247
queryIterator: nil,
243248
status: NewStatusBar(profile.URL, stream, w),
244249
}
250+
model.queryIterator = createIteratorFromModel(&model)
251+
return model
245252
}
246253

247254
func (m QueryModel) Init() tea.Cmd {
248-
// Just return `nil`, which means "no I/O right now, please."
249-
m.initIterator()
250-
return NewFetchTask(m.profile, m.query.Value(), m.timeRange.StartValueUtc(), m.timeRange.EndValueUtc())
255+
return func() tea.Msg {
256+
var ready sync.WaitGroup
257+
ready.Add(1)
258+
go func() {
259+
m.initIterator()
260+
for !m.queryIterator.Ready() {
261+
time.Sleep(time.Millisecond * 100)
262+
}
263+
ready.Done()
264+
}()
265+
ready.Wait()
266+
if m.queryIterator.Finished() {
267+
return nil
268+
}
269+
270+
return IteratorNext(m.queryIterator)()
271+
}
251272
}
252273

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

0 commit comments

Comments
 (0)