Utku Ozdemir 3735add87c
fix: reconnect to the logs stream in dashboard after reboot
The log stream displayed in the dashboard was stopping to work when a node was rebooted.
Rework the log data source to establish a per-node connection and use a retry loop to always reconnect until the dashboard is terminated.

Print the connection errors in the log stream in red color.

Closes siderolabs/talos#8388.

Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
2024-04-10 10:43:45 +02:00

67 lines
1.6 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package components
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// LogViewer represents the logs widget.
type LogViewer struct {
tview.Grid
logs tview.TextView
}
// NewLogViewer initializes LogViewer.
func NewLogViewer() *LogViewer {
widget := &LogViewer{
Grid: *tview.NewGrid(),
logs: *tview.NewTextView(),
}
widget.logs.ScrollToEnd().
SetDynamicColors(true).
SetMaxLines(maxLogLines).
SetText(noData).
SetBorderPadding(0, 0, 1, 1).
SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
_, _, _, pageSize := widget.logs.GetInnerRect()
lineOffset, columnOffset := widget.logs.GetScrollOffset()
//nolint:exhaustive
switch event.Key() {
case tcell.KeyCtrlD:
widget.logs.ScrollTo(lineOffset+(pageSize/2), columnOffset)
return nil
case tcell.KeyCtrlU:
widget.logs.ScrollTo(lineOffset-(pageSize/2), columnOffset)
return nil
}
return event
})
widget.SetRows(1, 0).SetColumns(0)
widget.AddItem(NewHorizontalLine(), 0, 0, 1, 1, 0, 0, false)
widget.AddItem(&widget.logs, 1, 0, 1, 1, 0, 0, true)
return widget
}
// WriteLog writes the log line to the widget.
func (widget *LogViewer) WriteLog(logLine, logError string) {
if logError != "" {
logLine = "[red]" + tview.Escape(logError) + "[-]\n"
} else {
logLine = tview.Escape(logLine) + "\n"
}
widget.logs.Write([]byte(logLine)) //nolint:errcheck
}