Skip to content

Commit

Permalink
Make storage hold multiple result series
Browse files Browse the repository at this point in the history
  • Loading branch information
spacez320 committed Jan 2, 2024
1 parent 1413d20 commit 9f5d464
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 68 deletions.
58 changes: 29 additions & 29 deletions internal/lib/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,6 @@ import (
"golang.org/x/exp/slog"
)

// Entrypoint for 'query' mode.
func Query(queries []string, attempts int, delay int, port string) chan int {
var (
done = make(chan int, 1) // Signals overall completion.
doneQuery = make(chan bool, len(queries)) // Signals query completions.
)

// Start the RPC server.
initServer(port)

// Execute the queries.
for _, query := range queries {
go runQuery(query, attempts, delay, doneQuery)
}

go func() {
// Wait for the queries to finish.
for i := 0; i < len(queries); i++ {
<-doneQuery
}

// Signal overall completion.
done <- 1
}()

return done
}

// Executes a query.
func runQuery(query string, attempts int, delay int, doneQuery chan bool) {
// This loop executes as long as attempts has not been reached or
Expand Down Expand Up @@ -69,7 +41,7 @@ func runQuery(query string, attempts int, delay int, doneQuery chan bool) {
// Interpret results.
cmd_output, cmd_output_err := io.ReadAll(stdout)
e(cmd_output_err)
AddResult(string(cmd_output))
AddResult(query, string(cmd_output))
slog.Debug(fmt.Sprintf("Result is: \n%s\n", cmd_output))

// Clean-up.
Expand All @@ -83,3 +55,31 @@ func runQuery(query string, attempts int, delay int, doneQuery chan bool) {

doneQuery <- true // Signals that this query is finished.
}

// Entrypoint for 'query' mode.
func Query(queries []string, attempts int, delay int, port string) chan int {
var (
done = make(chan int, 1) // Signals overall completion.
doneQuery = make(chan bool, len(queries)) // Signals query completions.
)

// Start the RPC server.
initServer(port)

// Execute the queries.
for _, query := range queries {
go runQuery(query, attempts, delay, doneQuery)
}

go func() {
// Wait for the queries to finish.
for i := 0; i < len(queries); i++ {
<-doneQuery
}

// Signal overall completion.
done <- 1
}()

return done
}
52 changes: 27 additions & 25 deletions internal/lib/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ import (
//
///////////////////////////////////////////////////////////////////////////////////////////////////

// Represents the display mode.
type display_ int

// Used to provide an io.Writer implementation of termdash text widgets.
type termdashTextWriter struct {
text text.Text
Expand All @@ -56,6 +53,9 @@ func (t *termdashTextWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

// Represents the display mode.
type DisplayMode int

// Represents the result mode value.
type ResultMode int

Expand All @@ -78,9 +78,9 @@ const (

// Display constants. Each result mode uses a specific display.
const (
DISPLAY_RAW display_ = iota + 1 // Used for direct output.
DISPLAY_TVIEW // Used when tview is the TUI driver.
DISPLAY_TERMDASH // Used when termdash is the TUI driver.
DISPLAY_RAW DisplayMode = iota + 1 // Used for direct output.
DISPLAY_TVIEW // Used when tview is the TUI driver.
DISPLAY_TERMDASH // Used when termdash is the TUI driver.

)

Expand All @@ -98,9 +98,10 @@ var (
// Global configuration.
config Config
// Display mode, dictated by the results.
mode display_
mode DisplayMode

// Stored results.
results storage.Results
store = storage.NewStorage()

// Widget for displaying logs. Publicly offered to allow log configuration.
// Only applicable for tview result modes.
Expand Down Expand Up @@ -232,9 +233,9 @@ func getRelativePerc(limitingPerc, globalRelativePerc int) int {
//
// TODO In the future, multiple result stores could be implemented by making
// this a function of an interface.
func AddResult(result string) {
func AddResult(query, result string) {
result = strings.TrimSpace(result)
results.Put(result, TokenizeResult(result)...)
store.Put(query, result, TokenizeResult(result)...)
}

// Creates a result with filtered values.
Expand Down Expand Up @@ -308,19 +309,19 @@ func TokenizeResult(result string) (parsedResult []interface{}) {
///////////////////////////////////////////////////////////////////////////////////////////////////

// Entry-point function for results.
func Results(resultMode ResultMode, labels, filters []string, inputConfig Config) {
func Results(resultMode ResultMode, query string, labels, filters []string, inputConfig Config) {
// Assign global config.
config = inputConfig
// Set up labelling or any schema for the results store.
results.Labels = labels
store.PutLabels(query, labels)

switch resultMode {
case RESULT_MODE_RAW:
mode = DISPLAY_RAW
RawResults()
case RESULT_MODE_STREAM:
mode = DISPLAY_TVIEW
StreamResults()
StreamResults(query)
case RESULT_MODE_TABLE:
// Pass logs into the logs view pane.
slog.SetDefault(slog.New(slog.NewTextHandler(
Expand All @@ -329,10 +330,10 @@ func Results(resultMode ResultMode, labels, filters []string, inputConfig Config
)))

mode = DISPLAY_TVIEW
TableResults(filters)
TableResults(query, filters)
case RESULT_MODE_GRAPH:
mode = DISPLAY_TERMDASH
GraphResults(filters)
GraphResults(query, filters)
default:
slog.Error(fmt.Sprintf("Invalid result mode: %d\n", resultMode))
os.Exit(1)
Expand All @@ -349,7 +350,7 @@ func RawResults() {
}

// Update the results pane with new results as they are generated.
func StreamResults() {
func StreamResults(query string) {
var (
helpText = "(ESC) Quit" // Text to display in the help pane.
helpView = tview.NewTextView() // Help text container.
Expand Down Expand Up @@ -391,8 +392,8 @@ func StreamResults() {
display(
func() {
// Print labels as the first line, if they are present.
if len(results.Labels) > 0 {
fmt.Fprintln(resultsView, results.Labels)
if labels := store.GetLabels(query); len(labels) > 0 {
fmt.Fprintln(resultsView, labels)
}

// Print results.
Expand All @@ -404,7 +405,7 @@ func StreamResults() {
}

// Creates a table of results for the results pane.
func TableResults(filters []string) {
func TableResults(query string, filters []string) {
var (
helpText = "(ESC) Quit" // Text to display in the help pane.
helpView = tview.NewTextView() // Help text container.
Expand Down Expand Up @@ -442,7 +443,7 @@ func TableResults(filters []string) {
// provided, the index is assumed to be zero.
if len(filters) > 0 {
for _, filter := range filters {
valueIndexes = append(valueIndexes, results.GetValueIndex(filter))
valueIndexes = append(valueIndexes, store.GetValueIndex(query, filter))
}
}

Expand All @@ -457,9 +458,9 @@ func TableResults(filters []string) {
)

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

Expand All @@ -472,6 +473,7 @@ func TableResults(filters []string) {
}

for {
/* slog.Debug(fmt.Sprintf("Receiving value %v", <-storage.PutEvents)) */
// Retrieve specific next values.
values := FilterSlice((<-storage.PutEvents).Values, valueIndexes)
// Row to contain the result.
Expand Down Expand Up @@ -500,7 +502,7 @@ func TableResults(filters []string) {
}

// Creates a graph of results for the results pane.
func GraphResults(filters []string) {
func GraphResults(query string, filters []string) {
var (
helpText = "(ESC) Quit" // Text to display in the help pane.
valueIndex = 0 // Index of the result value to graph.
Expand All @@ -509,12 +511,12 @@ func GraphResults(filters []string) {
// Determine the values to populate into the graph. If no filter is provided,
// the first value is taken.
if len(filters) > 0 {
valueIndex = results.GetValueIndex(filters[0])
valueIndex = store.GetValueIndex(query, filters[0])
}

// Initialize the results view.
resultWidget, err := sparkline.New(
sparkline.Label(results.Labels[valueIndex]),
sparkline.Label(store.GetLabels(query)[valueIndex]),
sparkline.Color(cell.ColorGreen),
)
e(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/lib/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// Establish the RPC server to allow access to stored results.
func initServer(port string) {
rpc.Register(&results)
rpc.Register(&store)
rpc.HandleHTTP()

listener, err := net.Listen("tcp", fmt.Sprintf(":%v", port))
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
////////////////////////////////////////////////////////////////////////////////

// Represents the mode value.
type mode_ int
type queryMode int

// Queries provided as flags.
type queries_ []string
Expand All @@ -42,8 +42,8 @@ func (q *queries_) Set(query string) error {

// Mode constants.
const (
MODE_QUERY mode_ = iota + 1 // For running in 'query' mode.
MODE_READ // For running in 'read' mode.
MODE_QUERY queryMode = iota + 1 // For running in 'query' mode.
MODE_READ // For running in 'read' mode.
)

var (
Expand Down Expand Up @@ -132,6 +132,7 @@ func main() {
if !silent {
lib.Results(
lib.ResultMode(resultMode),
queries[0], // TODO Until result modes support >1 query.
parseCommaDelimitedArg(valueLabels),
parseCommaDelimitedArg(filters),
lib.Config{
Expand Down
Loading

0 comments on commit 9f5d464

Please sign in to comment.