Add zone table to status

This commit is contained in:
Josh Deprez 2024-04-26 13:39:30 +10:00
parent b3e8b5111d
commit 12f0a08f2c
No known key found for this signature in database
2 changed files with 47 additions and 2 deletions

31
main.go
View file

@ -70,6 +70,27 @@ const routingTableTemplate = `
</table>
`
const zoneTableTemplate = `
<table>
<thead><tr>
<th>Network</th>
<th>Name</th>
<th>Local</th>
<th>Last seen</th>
</tr></thead>
<tbody>
{{range $zone := . }}
<tr>
<td>{{$zone.Network}}</td>
<td>{{$zone.Name}}</td>
<td>{{if $zone.Local}}{{else}}{{end}}</td>
<td>{{$zone.LastSeenAgo}}</td>
</tr>
{{end}}
</tbody>
</table>
`
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

View file

@ -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()