From 12f0a08f2c9adf222b71ae49761f43d43cfec196 Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Fri, 26 Apr 2024 13:39:30 +1000 Subject: [PATCH] Add zone table to status --- main.go | 31 +++++++++++++++++++++++++++++-- router/zones.go | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 8c2eed6..1520972 100644 --- a/main.go +++ b/main.go @@ -70,6 +70,27 @@ const routingTableTemplate = ` ` +const zoneTableTemplate = ` + + + + + + + + +{{range $zone := . }} + + + + + + +{{end}} + +
NetworkNameLocalLast seen
{{$zone.Network}}{{$zone.Name}}{{if $zone.Local}}✅{{else}}❌{{end}}{{$zone.LastSeenAgo}}
+` + var hasPortRE = regexp.MustCompile(`:\d+$`) var configFilePath = flag.String("config", "jrouter.yaml", "Path to configuration file to use") @@ -168,17 +189,23 @@ func main() { // -------------------------------- Tables -------------------------------- routes := router.NewRoutingTable() - _, done := status.AddItem(ctx, "Routing table", routingTableTemplate, func(context.Context) (any, error) { + status.AddItem(ctx, "Routing table", routingTableTemplate, func(context.Context) (any, error) { rs := routes.Dump() slices.SortFunc(rs, func(ra, rb router.Route) int { return cmp.Compare(ra.NetStart, rb.NetStart) }) return rs, nil }) - defer done() zones := router.NewZoneTable() zones.Upsert(cfg.EtherTalk.NetStart, cfg.EtherTalk.ZoneName, true) + status.AddItem(ctx, "Zone table", zoneTableTemplate, func(context.Context) (any, error) { + zs := zones.Dump() + slices.SortFunc(zs, func(za, zb router.Zone) int { + return cmp.Compare(za.Name, zb.Name) + }) + return zs, nil + }) // -------------------------------- Peers --------------------------------- var wg sync.WaitGroup diff --git a/router/zones.go b/router/zones.go index c7e4a2b..c05f6ff 100644 --- a/router/zones.go +++ b/router/zones.go @@ -17,6 +17,7 @@ package router import ( + "fmt" "slices" "sort" "sync" @@ -34,6 +35,13 @@ type Zone struct { LastSeen time.Time } +func (z Zone) LastSeenAgo() string { + if z.LastSeen.IsZero() { + return "never" + } + return fmt.Sprintf("%v ago", time.Since(z.LastSeen).Truncate(time.Millisecond)) +} + type zoneKey struct { network ddp.Network name string @@ -50,6 +58,16 @@ func NewZoneTable() *ZoneTable { } } +func (zt *ZoneTable) Dump() []Zone { + zt.mu.Lock() + defer zt.mu.Unlock() + zs := make([]Zone, 0, len(zt.zones)) + for _, z := range zt.zones { + zs = append(zs, *z) + } + return zs +} + func (zt *ZoneTable) Upsert(network ddp.Network, name string, local bool) { zt.mu.Lock() defer zt.mu.Unlock()