mirror of
https://github.com/siderolabs/talos.git
synced 2025-10-08 22:21:16 +02:00
Implement the new summary dashboard with node info and logs. Replace the previous metrics dashboard with the new dashboard which has multiple screens for node summary, metrics and editing network config. Port the old metrics dashboard to the tview library and assign it to be a screen in the new dashboard, accessible by F2 key. Add a new resource, infos.cluster.talos.dev which contains the cluster name and id of a node. Disable the network config editor screen in the new dashboard until it is fully implemented with its backend. Closes siderolabs/talos#4790. Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
94 lines
2.0 KiB
Go
94 lines
2.0 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 implements specific widgets for the dashboard.
|
|
package components
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/siderolabs/gen/slices"
|
|
)
|
|
|
|
const (
|
|
noData = "..."
|
|
notAvailable = "n/a"
|
|
none = "<none>"
|
|
maxLogLines = 1000
|
|
)
|
|
|
|
// field represents a field in a widget consist of a name and a value, rendered next to each other.
|
|
type field struct {
|
|
Name string
|
|
Value string
|
|
}
|
|
|
|
func (f *field) render(nameWidth int) string {
|
|
return fmt.Sprintf("[::b]%s[::-] %s", padRight(f.Name, nameWidth), f.Value)
|
|
}
|
|
|
|
type fieldGroup struct {
|
|
fields []field
|
|
}
|
|
|
|
// String implements the Stringer interface.
|
|
func (fg *fieldGroup) String() string {
|
|
width := fg.maxFieldNameLength()
|
|
|
|
return strings.Join(
|
|
slices.Map(fg.fields, func(t field) string {
|
|
return t.render(width)
|
|
}),
|
|
"\n",
|
|
)
|
|
}
|
|
|
|
func (fg *fieldGroup) maxFieldNameLength() int {
|
|
max := 0
|
|
|
|
for _, f := range fg.fields {
|
|
if len(f.Name) > max {
|
|
max = len(f.Name)
|
|
}
|
|
}
|
|
|
|
return max
|
|
}
|
|
|
|
// padRight pads a string to the specified width by appending spaces to the end.
|
|
func padRight(s string, width int) string {
|
|
return fmt.Sprintf("%-*s", width, s)
|
|
}
|
|
|
|
func toHealthStatus(healthy bool) string {
|
|
if healthy {
|
|
return formatStatus("Healthy")
|
|
}
|
|
|
|
return formatStatus("Unhealthy")
|
|
}
|
|
|
|
func formatStatus(status any) string {
|
|
statusStr := capitalizeFirst(fmt.Sprintf("%v", status))
|
|
|
|
switch strings.ToLower(statusStr) {
|
|
case "running", "healthy", "true":
|
|
return fmt.Sprintf("[green]%s[-]", statusStr)
|
|
case "stopped", "unhealthy", "false":
|
|
return fmt.Sprintf("[red]%s[-]", statusStr)
|
|
default:
|
|
return statusStr
|
|
}
|
|
}
|
|
|
|
// capitalizeFirst capitalizes the first character of string.
|
|
func capitalizeFirst(s string) string {
|
|
if s == "" {
|
|
return s
|
|
}
|
|
|
|
return strings.ToUpper(string(s[0])) + strings.ToLower(s[1:])
|
|
}
|