feat(console): sort the monitor list and put monitors in an order of one's own
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 10m49s
ci / test (pull_request) Successful in 14m39s
ci / build (pull_request) Canceled after 14m22s
ci / image (pull_request) Canceled after 0s

The monitor list gets a sort switcher: the organization's own order, by
default, or by name, status (problems first), uptime (worst first) or
latency (slowest first). The choice is remembered in a cookie; paging
follows it.

"Arrange monitors" shows the whole tree with each group's monitors to
drag, or move with up and down, into the order wanted; every change is
saved at once through PUT /api/v1/org/monitors/order, which needs write
access to every monitor given and is audited. Monitors without a position
follow the positioned ones, by name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-25 23:35:02 +01:00
1 parent ca852c8299
commit b0d4bcc002
16 files changed
+3389 -2653

No files matched your search

+85 -15
View File
@@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"net"
"net/http"
"net/url"
@@ -79,10 +80,10 @@ func (s *Server) dashboard(c *gin.Context) {
}
v.Root = buildGroupNodes(data.tree, data.rows)
v.Empty = len(data.rows) == 0 && len(v.Root.Children) == 0
v.CanWrite = writableAnywhere(p, data.tree)
if !s.dashboardPage(c, &v, data) {
return
}
v.CanWrite = writableAnywhere(p, data.tree)
v.Range = data.rng.Key
var earliest time.Time
for _, m := range data.byID {
@@ -113,14 +114,64 @@ const groupMonitorsShown = 50
// ungrouped names the pseudo-group of monitors outside any group.
const ungrouped = "none"
func monitorKey(m web.MonitorRow) cursor {
id, _ := uuid.Parse(m.ID)
return cursor{S: strings.ToLower(m.Name), ID: id}
// monitorSort is an order of the monitor list: the organization's own,
// or by name, status (problems first), uptime (worst first) or latency
// (slowest first). Monitors without a position or data come last.
type monitorSort string
var monitorSorts = []monitorSort{"custom", "name", "status", "uptime", "latency"}
const sortCookie = "monitor_sort"
// sortOf returns the order asked for, remembering it in a cookie, or the
// one remembered, or the organization's own.
func sortOf(c *gin.Context) monitorSort {
if q := monitorSort(c.Query("sort")); slices.Contains(monitorSorts, q) {
c.SetSameSite(http.SameSiteLaxMode)
c.SetCookie(sortCookie, string(q), 365*24*3600, "/", "", c.Request.TLS != nil, true)
return q
}
if v, err := c.Cookie(sortCookie); err == nil && slices.Contains(monitorSorts, monitorSort(v)) {
return monitorSort(v)
}
return "custom"
}
func sortMonitors(ms []web.MonitorRow) {
// key orders m by o as a string, so pages can continue after it.
func (o monitorSort) key(m web.MonitorRow) cursor {
id, _ := uuid.Parse(m.ID)
name := strings.ToLower(m.Name)
var k string
switch o {
case "name":
k = name
case "status":
k = fmt.Sprintf("%d|%s", statusRank[m.Status], name)
case "uptime":
if m.Ratio < 0 {
k = "1|" + name
} else {
k = fmt.Sprintf("0|%.9f|%s", m.Ratio, name)
}
case "latency":
if m.LatencyMS < 0 {
k = "1|" + name
} else {
k = fmt.Sprintf("0|%010d|%s", math.MaxInt32-m.LatencyMS, name)
}
default:
if m.Position < 0 {
k = "1|" + name
} else {
k = fmt.Sprintf("0|%010d|%s", m.Position, name)
}
}
return cursor{S: k, ID: id}
}
func (o monitorSort) sort(ms []web.MonitorRow) {
slices.SortFunc(ms, func(a, b web.MonitorRow) int {
ka, kb := monitorKey(a), monitorKey(b)
ka, kb := o.key(a), o.key(b)
if c := strings.Compare(ka.S, kb.S); c != 0 {
return c
}
@@ -135,7 +186,7 @@ var dashboardStatuses = []web.Status{web.StatusUp, web.StatusDegraded, web.Statu
// value is empty, keeping the search and range but not the page.
func dashboardURL(c *gin.Context, key, value string) string {
q := url.Values{}
for _, k := range []string{"q", "range", "status"} {
for _, k := range []string{"q", "range", "status", "arrange"} {
if v := c.Query(k); v != "" {
q.Set(k, v)
}
@@ -176,6 +227,20 @@ func (s *Server) dashboardPage(c *gin.Context, v *web.DashboardView, data monito
if !ok {
return false
}
order := sortOf(c)
v.Sort = string(order)
for _, o := range monitorSorts {
v.Sorts = append(v.Sorts, web.SortOption{Key: string(o), Active: o == order, URL: dashboardURL(c, "sort", string(o))})
}
// Arranging by hand shows the whole tree in the organization's order.
arranging := order == "custom" && v.CanWrite && c.Query("q") == "" && c.Query("status") == "" && c.Query("group") == ""
v.Arrange = arranging && c.Query("arrange") == "1"
if arranging {
v.ArrangeURL = dashboardURL(c, "arrange", "1")
if v.Arrange {
v.ArrangeURL = dashboardURL(c, "arrange", "")
}
}
link := func(group string) string {
q := url.Values{"group": {group}}
if r := c.Query("range"); r != "" {
@@ -185,8 +250,8 @@ func (s *Server) dashboardPage(c *gin.Context, v *web.DashboardView, data monito
}
var capNode func(n *web.GroupNode)
capNode = func(n *web.GroupNode) {
sortMonitors(n.Monitors)
if len(n.Monitors) > groupMonitorsShown {
order.sort(n.Monitors)
if len(n.Monitors) > groupMonitorsShown && !v.Arrange {
n.More = len(n.Monitors) - groupMonitorsShown
n.Monitors = n.Monitors[:groupMonitorsShown]
n.MoreURL = link(cmp.Or(n.ID, ungrouped))
@@ -213,8 +278,8 @@ func (s *Server) dashboardPage(c *gin.Context, v *web.DashboardView, data monito
hits = append(hits, m)
}
}
sortMonitors(hits)
v.Results, info = pageSlice(hits, pg, monitorKey)
order.sort(hits)
v.Results, info = pageSlice(hits, pg, order.key)
case group != "":
var focus web.GroupNode
found := false
@@ -239,8 +304,8 @@ func (s *Server) dashboardPage(c *gin.Context, v *web.DashboardView, data monito
c.Redirect(http.StatusSeeOther, "/")
return false
}
sortMonitors(focus.Monitors)
focus.Monitors, info = pageSlice(focus.Monitors, pg, monitorKey)
order.sort(focus.Monitors)
focus.Monitors, info = pageSlice(focus.Monitors, pg, order.key)
for i := range focus.Children {
capNode(&focus.Children[i])
}
@@ -448,7 +513,10 @@ func (s *Server) loadMonitorData(ctx context.Context, p *principal, rng web.Time
continue
}
d.byID[m.ID] = m
row := web.MonitorRow{ID: m.ID.String(), Name: m.Name, Type: m.Type, Target: m.Target, Latency: "–", Uptime: "–"}
row := web.MonitorRow{ID: m.ID.String(), Name: m.Name, Type: m.Type, Target: m.Target, Latency: "–", Uptime: "–", Position: -1, LatencyMS: -1}
if m.Position != nil {
row.Position = int(*m.Position)
}
keys := idStrings(regionsOf[m.ID])
if check.Passive(check.Type(m.Type)) {
// The target of a heartbeat is its secret ping token.
@@ -509,8 +577,10 @@ func (s *Server) loadMonitorData(ctx context.Context, p *principal, rng web.Time
sum += l
}
row.Latency = fmt.Sprintf("%d ms", sum/int32(len(latencies)))
row.LatencyMS = int(sum / int32(len(latencies)))
}
row.Uptime = web.SLAText(d.slaFor(m, rng.Window))
row.Ratio = d.slaFor(m, rng.Window)
row.Uptime = web.SLAText(row.Ratio)
row.Bars = web.UptimeBars(d.buckets[m.ID], now, rng.Bucket, rng.Bars(), rng.Layout)
d.rows = append(d.rows, row)
}