Some output tweaks; and some additional transition events upon resume (paused->unknown->{up|down})

This commit is contained in:
2026-04-11 03:04:47 +02:00
parent 46e78ec36f
commit 56a4a6ba25
4 changed files with 85 additions and 25 deletions

View File

@@ -5,8 +5,9 @@ package main
import (
"context"
"fmt"
"text/tabwriter"
"os"
"strings"
"text/tabwriter"
"time"
"git.ipng.ch/ipng/vpp-maglev/internal/grpcapi"
@@ -195,16 +196,27 @@ func runShowBackend(ctx context.Context, client grpcapi.MaglevClient, args []str
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(w, "name\t%s\n", info.Name)
fmt.Fprintf(w, "address\t%s\n", info.Address)
fmt.Fprintf(w, "state\t%s\n", info.State)
stateDur := ""
if len(info.Transitions) > 0 {
since := time.Since(time.Unix(0, info.Transitions[0].AtUnixNs))
stateDur = " for " + formatDuration(since)
}
fmt.Fprintf(w, "state\t%s%s\n", info.State, stateDur)
fmt.Fprintf(w, "enabled\t%v\n", info.Enabled)
fmt.Fprintf(w, "weight\t%d\n", info.Weight)
fmt.Fprintf(w, "healthcheck\t%s\n", info.Healthcheck)
for i, t := range info.Transitions {
ts := time.Unix(0, t.AtUnixNs)
label := ""
if i == 0 {
fmt.Fprintf(w, "transitions\t%s → %s\n", t.From, t.To)
} else {
fmt.Fprintf(w, "\t%s → %s\n", t.From, t.To)
label = "transitions"
}
fmt.Fprintf(w, "%s\t%s → %s\t%s\t%s\n",
label,
t.From, t.To,
ts.Format("2006-01-02 15:04:05.000"),
formatAgo(time.Since(ts)),
)
}
return w.Flush()
}
@@ -305,3 +317,38 @@ func runNotImplemented(_ context.Context, _ grpcapi.MaglevClient, _ []string) er
fmt.Println("not implemented yet")
return nil
}
// formatDuration formats a duration as Xd Xh Xm Xs without milliseconds.
func formatDuration(d time.Duration) string {
if d < 0 {
d = 0
}
d = d.Truncate(time.Second)
days := int(d.Hours()) / 24
d -= time.Duration(days) * 24 * time.Hour
hours := int(d.Hours())
d -= time.Duration(hours) * time.Hour
minutes := int(d.Minutes())
d -= time.Duration(minutes) * time.Minute
seconds := int(d.Seconds())
var b strings.Builder
if days > 0 {
fmt.Fprintf(&b, "%dd", days)
}
if hours > 0 {
fmt.Fprintf(&b, "%dh", hours)
}
if minutes > 0 {
fmt.Fprintf(&b, "%dm", minutes)
}
if seconds > 0 || b.Len() == 0 {
fmt.Fprintf(&b, "%ds", seconds)
}
return b.String()
}
func formatAgo(d time.Duration) string {
return formatDuration(d) + " ago"
}