Add zone table to status
This commit is contained in:
parent
b3e8b5111d
commit
12f0a08f2c
2 changed files with 47 additions and 2 deletions
31
main.go
31
main.go
|
@ -70,6 +70,27 @@ const routingTableTemplate = `
|
||||||
</table>
|
</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 hasPortRE = regexp.MustCompile(`:\d+$`)
|
||||||
|
|
||||||
var configFilePath = flag.String("config", "jrouter.yaml", "Path to configuration file to use")
|
var configFilePath = flag.String("config", "jrouter.yaml", "Path to configuration file to use")
|
||||||
|
@ -168,17 +189,23 @@ func main() {
|
||||||
|
|
||||||
// -------------------------------- Tables --------------------------------
|
// -------------------------------- Tables --------------------------------
|
||||||
routes := router.NewRoutingTable()
|
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()
|
rs := routes.Dump()
|
||||||
slices.SortFunc(rs, func(ra, rb router.Route) int {
|
slices.SortFunc(rs, func(ra, rb router.Route) int {
|
||||||
return cmp.Compare(ra.NetStart, rb.NetStart)
|
return cmp.Compare(ra.NetStart, rb.NetStart)
|
||||||
})
|
})
|
||||||
return rs, nil
|
return rs, nil
|
||||||
})
|
})
|
||||||
defer done()
|
|
||||||
|
|
||||||
zones := router.NewZoneTable()
|
zones := router.NewZoneTable()
|
||||||
zones.Upsert(cfg.EtherTalk.NetStart, cfg.EtherTalk.ZoneName, true)
|
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 ---------------------------------
|
// -------------------------------- Peers ---------------------------------
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"slices"
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -34,6 +35,13 @@ type Zone struct {
|
||||||
LastSeen time.Time
|
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 {
|
type zoneKey struct {
|
||||||
network ddp.Network
|
network ddp.Network
|
||||||
name string
|
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) {
|
func (zt *ZoneTable) Upsert(network ddp.Network, name string, local bool) {
|
||||||
zt.mu.Lock()
|
zt.mu.Lock()
|
||||||
defer zt.mu.Unlock()
|
defer zt.mu.Unlock()
|
||||||
|
|
Loading…
Reference in a new issue