Skip to content

Commit

Permalink
Successful query switching w/o deadlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
spacez320 committed Jan 7, 2024
1 parent 29c5358 commit d6ac48e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 50 deletions.
113 changes: 71 additions & 42 deletions internal/lib/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var (
DISPLAY_MODE_TABLE,
DISPLAY_MODE_GRAPH,
} // Display modes considered for use in the current session.
interruptChan = make(chan bool) // Channel for interrupting displays.
)

///////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -78,7 +79,7 @@ var (
// the display.
func display(driver DisplayDriver, f func()) {
// Execute the update function.
go func() { f() }()
go f()

switch driver {
case DISPLAY_TVIEW:
Expand Down Expand Up @@ -123,12 +124,23 @@ func StreamDisplay(query string) {
func() {
// Print labels as the first line, if they are present.
if labels := store.GetLabels(query); len(labels) > 0 {
fmt.Fprintln(resultsView, labels)
appTview.QueueUpdateDraw(func() {
fmt.Fprintln(resultsView, labels)
})
}

// Print results.
for {
fmt.Fprintln(resultsView, (GetResult(query)).Value)
// Listen for an interrupt event to stop result consumption in
// preparation for some display change.
select {
case <-interruptChan:
// We've received an interrupt.
return
default:
// We can display the next result.
fmt.Fprintln(resultsView, (GetResult(query)).Value)
}
}
},
)
Expand All @@ -152,7 +164,8 @@ func TableDisplay(query string, filters []string) {
DISPLAY_TVIEW,
func() {
var (
i = 0 // Used to determine the next row index.
nextCellContent string // Next cell to add to the table.
i = 0 // Used to determine the next row index.
)

// Determine the value indexes to populate into the graph. If no filter is
Expand All @@ -165,42 +178,47 @@ func TableDisplay(query string, filters []string) {

// Create the table header.
if labels := store.GetLabels(query); len(labels) > 0 {
// Labels to apply.
labels = FilterSlice(labels, valueIndexes)
// Row to contain the labels.
headerRow := resultsView.InsertRow(i)

for j, label := range labels {
headerRow.SetCellSimple(i, j, tableCellPadding+label+tableCellPadding)
}
appTview.QueueUpdateDraw(func() {
// Row to contain the labels.
headerRow := resultsView.InsertRow(i)

appTview.Draw()
for j, label := range FilterSlice(labels, valueIndexes) {
headerRow.SetCellSimple(i, j, tableCellPadding+label+tableCellPadding)
}
})
i += 1
}

// Print results.
for {
values := FilterSlice((GetResult(query)).Values, valueIndexes)
// Row to contain the result.
row := resultsView.InsertRow(i)

for j, value := range values {
var nextCellContent string

// Extrapolate the field types in order to print them out.
switch value.(type) {
case int64:
nextCellContent = strconv.FormatInt(value.(int64), 10)
case float64:
nextCellContent = strconv.FormatFloat(value.(float64), 'f', -1, 64)
default:
nextCellContent = value.(string)
}
row.SetCellSimple(i, j, tableCellPadding+nextCellContent+tableCellPadding)
// Listen for an interrupt event to stop result consumption in
// preparation for some display change.
select {
case <-interruptChan:
// We've received an interrupt.
return
default:
// We can display the next result.
appTview.QueueUpdateDraw(func() {
var (
row = resultsView.InsertRow(i) // Row to contain the result.
)

for j, value := range FilterSlice((GetResult(query)).Values, valueIndexes) {
// Extrapolate the field types in order to print them out.
switch value.(type) {
case int64:
nextCellContent = strconv.FormatInt(value.(int64), 10)
case float64:
nextCellContent = strconv.FormatFloat(value.(float64), 'f', -1, 64)
default:
nextCellContent = value.(string)
}
row.SetCellSimple(i, j, tableCellPadding+nextCellContent+tableCellPadding)
}
})
i += 1
}

appTview.Draw()
i += 1
// }
}
},
)
Expand All @@ -209,10 +227,12 @@ func TableDisplay(query string, filters []string) {
// Creates a graph of results for the results pane.
func GraphDisplay(query string, filters []string) {
var (
helpText = "(ESC) Quit" // Text to display in the help pane.
valueIndex = 0 // Index of the result value to graph.
helpText = "(ESC) Quit | (TAB) Next Query" // Text to display in the help pane.
valueIndex = 0 // Index of the result value to graph.
)

helpText += fmt.Sprintf("\nQuery: %v", query)

// Determine the values to populate into the graph. If no filter is provided,
// the first value is taken.
if len(filters) > 0 {
Expand Down Expand Up @@ -245,13 +265,22 @@ func GraphDisplay(query string, filters []string) {
DISPLAY_TERMDASH,
func() {
for {
value := (GetResult(query)).Values[valueIndex]
// Listen for an interrupt event to stop result consumption in
// preparation for some display change.
select {
case <-interruptChan:
// We've received an interrupt.
return
default:
// We can display the next result.
value := (GetResult(query)).Values[valueIndex]

switch value.(type) {
case int64:
resultWidget.Add([]int{int(value.(int64))})
case float64:
resultWidget.Add([]int{int(value.(float64))})
switch value.(type) {
case int64:
resultWidget.Add([]int{int(value.(int64))})
case float64:
resultWidget.Add([]int{int(value.(float64))})
}
}
}
},
Expand Down
3 changes: 1 addition & 2 deletions internal/lib/display_termdash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package lib

import (
"context"
"os"

"github.com/mum4k/termdash"
"github.com/mum4k/termdash/container"
Expand Down Expand Up @@ -41,9 +40,9 @@ func keyboardTermdashHandler(key *terminalapi.Keyboard) {
currentCtx = context.WithValue(currentCtx, "quit", true)
cancel()
appTermdash.Close()
os.Exit(0)
case keyboard.KeyTab:
// When a user presses Tab, stop the display but continue running.
interruptChan <- true
currentCtx = context.WithValue(currentCtx, "advanceQuery", true)
cancel()
appTermdash.Close()
Expand Down
15 changes: 10 additions & 5 deletions internal/lib/display_tview.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package lib
import (
"context"
"fmt"
"os"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
Expand All @@ -24,11 +23,16 @@ func keyboardTviewHandler(key tcell.Key) {
// When a user presses Esc, close the application.
currentCtx = context.WithValue(currentCtx, "quit", true)
appTview.Stop()
os.Exit(0)
case tcell.KeyTab:
// When a user presses Tab, stop the display but continue running.
currentCtx = context.WithValue(currentCtx, "advanceQuery", true)
appTview.Stop()
// This is wrapped in a goroutine to avoid deadlocks with tview.
//
// See: https://github.com/rivo/tview/issues/784
go func() {
// When a user presses Tab, stop the display but continue running.
interruptChan <- true
currentCtx = context.WithValue(currentCtx, "advanceQuery", true)
appTview.Stop()
}()
}
}

Expand All @@ -45,6 +49,7 @@ func initDisplayTviewTable(helpText string) (
resultsView.SetBorders(true).SetDoneFunc(keyboardTviewHandler)
resultsView.SetBorder(true).SetTitle("Results")

// slog.Debug("Initializing the rest of the display.")
initDisplayTview(resultsView, helpView, logsView, helpText)

return
Expand Down
9 changes: 8 additions & 1 deletion pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ func (s *Storage) NewResults(query string) {
func (s *Storage) Put(query string, value string, values ...interface{}) Result {
s.NewResults(query)
result := (*s)[query].put(value, values...)
PutEvents[query] <- result

// Send a non-blocking put event. Put events are lossy and clients may lose
// information if not actively listening.
select {
case PutEvents[query] <- result:
default:
}

return result
}

Expand Down

0 comments on commit d6ac48e

Please sign in to comment.