@@ -23,13 +23,13 @@ import (
2323 "math"
2424 "net/http"
2525 "os"
26- "pb/pkg/config"
27- "pb/pkg/iterator"
28- "regexp"
2926 "strings"
3027 "sync"
3128 "time"
3229
30+ "pb/pkg/config"
31+ "pb/pkg/iterator"
32+
3333 "github.com/charmbracelet/bubbles/help"
3434 "github.com/charmbracelet/bubbles/key"
3535 "github.com/charmbracelet/bubbles/textarea"
@@ -167,33 +167,31 @@ func createIteratorFromModel(m *QueryModel) *iterator.QueryIterator[QueryData, F
167167 startTime = startTime .Truncate (time .Minute )
168168 endTime = endTime .Truncate (time .Minute ).Add (time .Minute )
169169
170- regex := regexp .MustCompile (`^select\s+(?:\*|\w+(?:,\s*\w+)*)\s+from\s+(\w+)(?:\s+;)?$` )
171- matches := regex .FindStringSubmatch (m .query .Value ())
172- if matches == nil {
173- return nil
170+ table := streamNameFromQuery (m .query .Value ())
171+ if table != "" {
172+ iter := iterator .NewQueryIterator (
173+ startTime , endTime ,
174+ false ,
175+ func (t1 , t2 time.Time ) (QueryData , FetchResult ) {
176+ client := & http.Client {
177+ Timeout : time .Second * 50 ,
178+ }
179+ return fetchData (client , & m .profile , m .query .Value (), t1 .UTC ().Format (time .RFC3339 ), t2 .UTC ().Format (time .RFC3339 ))
180+ },
181+ func (t1 , t2 time.Time ) bool {
182+ client := & http.Client {
183+ Timeout : time .Second * 50 ,
184+ }
185+ res , err := fetchData (client , & m .profile , "select count(*) as count from " + table , m .timeRange .StartValueUtc (), m .timeRange .EndValueUtc ())
186+ if err == fetchErr {
187+ return false
188+ }
189+ count := res .Records [0 ]["count" ].(float64 )
190+ return count > 0
191+ })
192+ return & iter
174193 }
175- table := matches [1 ]
176- iter := iterator .NewQueryIterator (
177- startTime , endTime ,
178- false ,
179- func (t1 , t2 time.Time ) (QueryData , FetchResult ) {
180- client := & http.Client {
181- Timeout : time .Second * 50 ,
182- }
183- return fetchData (client , & m .profile , m .query .Value (), t1 .UTC ().Format (time .RFC3339 ), t2 .UTC ().Format (time .RFC3339 ))
184- },
185- func (t1 , t2 time.Time ) bool {
186- client := & http.Client {
187- Timeout : time .Second * 50 ,
188- }
189- res , err := fetchData (client , & m .profile , "select count(*) as count from " + table , m .timeRange .StartValueUtc (), m .timeRange .EndValueUtc ())
190- if err == fetchErr {
191- return false
192- }
193- count := res .Records [0 ]["count" ].(float64 )
194- return count > 0
195- })
196- return & iter
194+ return nil
197195}
198196
199197func NewQueryModel (profile config.Profile , queryStr string , startTime , endTime time.Time ) QueryModel {
@@ -653,3 +651,15 @@ func countDigits(num int) int {
653651 numDigits := int (math .Log10 (math .Abs (float64 (num )))) + 1
654652 return numDigits
655653}
654+
655+ func streamNameFromQuery (query string ) string {
656+ stream := ""
657+ tokens := strings .Split (query , " " )
658+ for i , token := range tokens {
659+ if token == "from" {
660+ stream = tokens [i + 1 ]
661+ break
662+ }
663+ }
664+ return stream
665+ }
0 commit comments