From 282ab27f810baea2698f22ff9a1c28a8b24d2d5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sat, 28 Nov 2026 14:35:24 +0100 Subject: [PATCH] feat(statuspages): page every group on its own and filter outages Paging all components of a status page at once left out whole groups on every page, which confused visitors. Every group now shows its first 25 components and pages them on its own (?p.=N), keeping the pages of the other groups. Links jump back to the group. Visitors can also show only the components with an outage or degraded (?problems=1); groups without any are left out. The search stays. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3 --- internal/i18n/locales/de.json | 2 + internal/i18n/locales/en.json | 2 + internal/statuspage/statuspage.go | 31 +- internal/statuspage/statuspage_test.go | 51 +- internal/web/public.go | 207 +-- internal/web/public.templ | 73 +- internal/web/public_templ.go | 1604 ++++++++++++------------ 7 files changed, 1057 insertions(+), 913 deletions(-) diff --git a/internal/i18n/locales/de.json b/internal/i18n/locales/de.json index 526f787..5b08d26 100644 --- a/internal/i18n/locales/de.json +++ b/internal/i18n/locales/de.json @@ -1266,6 +1266,8 @@ "page.search_submit": "Suchen", "page.search_clear": "Alle anzeigen", "page.search_none": "Keine Komponente passt zur Suche.", + "page.filter_problems": "Nur Ausfälle ({{.Count}})", + "page.filter_no_problems": "Keine Komponente hat einen Ausfall.", "page.updated": "Aktualisiert {{.Time}}", "pager.earlier": "Frühere", "pager.first": "Zum Anfang", diff --git a/internal/i18n/locales/en.json b/internal/i18n/locales/en.json index 0856778..1cd6e85 100644 --- a/internal/i18n/locales/en.json +++ b/internal/i18n/locales/en.json @@ -1266,6 +1266,8 @@ "page.search_submit": "Search", "page.search_clear": "Show all", "page.search_none": "No component matches the search.", + "page.filter_problems": "Outages only ({{.Count}})", + "page.filter_no_problems": "No component has an outage.", "page.updated": "Updated {{.Time}}", "pager.earlier": "Earlier", "pager.first": "To the start", diff --git a/internal/statuspage/statuspage.go b/internal/statuspage/statuspage.go index c62f954..9d4d7b2 100644 --- a/internal/statuspage/statuspage.go +++ b/internal/statuspage/statuspage.go @@ -19,6 +19,7 @@ import ( "log/slog" "net" "net/http" + "net/url" "os" "path/filepath" "strconv" @@ -333,22 +334,40 @@ func (s *Server) serve(c *gin.Context) { s.localizeRequest(c) c.Header("Cache-Control", "public, max-age=30") assets.PageSize = componentsPerPage - if n, err := strconv.Atoi(c.Query("page")); err == nil && n > 1 { - assets.Page = n - 1 - } + assets.Pages = sectionPages(c.Request.URL.Query()) if q := c.Query("q"); len(q) <= maxComponentQuery { assets.Query = q } + assets.OnlyProblems = c.Query("problems") == "1" web.Render(c, http.StatusOK, web.PublicStatusPage(e.Snap.WithMaintenance(s.now()), assets)) } -// componentsPerPage is how many components a status page lists at a time; -// organizations put thousands on one. -const componentsPerPage = 50 +// componentsPerPage is how many of its own components a section of a +// status page lists at a time; organizations put thousands on one. +const componentsPerPage = 25 // maxComponentQuery bounds the search for components. const maxComponentQuery = 100 +// maxPagedSections bounds the sections a request pages. +const maxPagedSections = 100 + +// sectionPages reads the pages of sections a request asks for, as +// "p.=". +func sectionPages(q url.Values) map[string]int { + pages := map[string]int{} + for k, vs := range q { + key, ok := strings.CutPrefix(k, "p.") + if !ok || key == "" || len(vs) == 0 || len(pages) >= maxPagedSections { + continue + } + if n, err := strconv.Atoi(vs[0]); err == nil && n > 1 { + pages[key] = n - 1 + } + } + return pages +} + // serveComponent renders one component's public detail page from the cached // snapshot when the status page has enabled ShowDetails. func (s *Server) serveComponent(c *gin.Context, e entry, compID string, assets web.PublicAssets) { diff --git a/internal/statuspage/statuspage_test.go b/internal/statuspage/statuspage_test.go index aecf901..d0ff4ee 100644 --- a/internal/statuspage/statuspage_test.go +++ b/internal/statuspage/statuspage_test.go @@ -610,34 +610,61 @@ func TestEventsRefreshPagesAtOnce(t *testing.T) { } } -// Pages with many components list them a page at a time and let visitors -// search them. +// Every group of a page with many components lists them a page at a time, +// and visitors can search them or show those with an outage only. func TestComponentPagingAndSearch(t *testing.T) { + comp := func(id, name, status string) agentapi.Component { + return agentapi.Component{ID: id, Name: name, Status: status, Days: []agentapi.Day{{Date: "2026-11-26", Uptime: 1}}} + } core := agentapi.Section{Name: "Core"} for i := 0; i < 120; i++ { - core.Components = append(core.Components, agentapi.Component{ID: fmt.Sprintf("c%03d", i), Name: fmt.Sprintf("web-%03d", i), Status: agentapi.PageOperational, Days: []agentapi.Day{{Date: "2026-11-26", Uptime: 1}}}) + st := agentapi.PageOperational + if i == 7 { + st = agentapi.PageDegraded + } + core.Components = append(core.Components, comp(fmt.Sprintf("c%03d", i), fmt.Sprintf("web-%03d", i), st)) } edge := agentapi.Section{Name: "Edge"} for i := 0; i < 10; i++ { - edge.Components = append(edge.Components, agentapi.Component{ID: fmt.Sprintf("e%d", i), Name: fmt.Sprintf("edge-%d", i), Status: agentapi.PageOperational}) + st := agentapi.PageOperational + if i == 3 { + st = agentapi.PageOutage + } + edge.Components = append(edge.Components, comp(fmt.Sprintf("e%d", i), fmt.Sprintf("edge-%d", i), st)) } core.Children = []agentapi.Section{edge} + other := agentapi.Section{Name: "Other"} + for i := 0; i < 30; i++ { + other.Components = append(other.Components, comp(fmt.Sprintf("o%02d", i), fmt.Sprintf("other-%02d", i), agentapi.PageOperational)) + } now := time.Now() h := newHarness(t, &fakeSource{pages: map[string]agentapi.PageSnapshot{ - "|acme": {Title: "Acme status", Status: agentapi.PageOperational, ShowDetails: true, Sections: []agentapi.Section{core}}, + "|acme": {Title: "Acme status", Status: agentapi.PageOperational, ShowDetails: true, Sections: []agentapi.Section{core, other}}, + "|calm": {Title: "Calm", Status: agentapi.PageOperational, Sections: []agentapi.Section{other}}, }}, t.TempDir(), &now) + if _, body := h.get("status.example.test", "/calm?problems=1"); !strings.Contains(body, "Keine Komponente hat einen Ausfall.") || strings.Contains(body, "other-00") { + t.Errorf("outages only without any lacks the notice") + } tests := []struct { path string has, hasNot []string }{ - {"/acme", []string{"web-000", "web-049", "Seite 1 von 3", `href="/acme?page=2"`, `Core<", ">Edge<", ">130<", "Seite 3 von 3"}, []string{"web-099", `rel="next"`}}, - {"/acme?page=99", []string{"Seite 3 von 3", "edge-9"}, nil}, - {"/acme?page=x", []string{"Seite 1 von 3"}, nil}, - {"/acme?q=EDGE-", []string{"edge-0", "edge-9", `value="EDGE-"`, "Alle anzeigen"}, []string{"web-", "Seite 1"}}, - {"/acme?q=edge", []string{"edge-0", "edge-9"}, []string{"web-"}}, + // Every group shows its first page. + {"/acme", []string{"web-000", "web-024", "Seite 1 von 5", "edge-0", "edge-9", "other-00", "other-24", "Seite 1 von 2", + `id="g-0"`, `id="g-0.0"`, `id="g-1"`, `href="/acme?p.0=2#g-0"`, `href="/acme?p.1=2#g-1"`, `Core<", ">Edge<", ">130<", "checked", "Alle anzeigen"}, + []string{"web-000", "edge-0", ">Other<", "Seite 1 von"}}, + {"/acme?problems=1&q=edge", []string{"edge-3"}, []string{"web-007", "edge-0"}}, + {"/acme?q=EDGE-", []string{"edge-0", "edge-9", `value="EDGE-"`, "Alle anzeigen"}, []string{"web-", "other-"}}, + {"/acme?q=edge", []string{"edge-0", "edge-9", ">Core<"}, []string{"web-", ">Other<"}}, {"/acme?q=web-11", []string{"web-110", "web-119"}, []string{"web-109", "edge-"}}, {"/acme?q=nothing", []string{"Keine Komponente passt zur Suche."}, []string{"web-", "edge-"}}, } diff --git a/internal/web/public.go b/internal/web/public.go index 4afda57..47f8b2a 100644 --- a/internal/web/public.go +++ b/internal/web/public.go @@ -31,33 +31,104 @@ type PublicAssets struct { Now time.Time // NoRefresh leaves out the periodic reload, on pages with a form. NoRefresh bool - // PageSize is how many components the page lists at a time; 0 lists - // all, as in a single-file export. + // PageSize is how many of a section's own components the page lists + // at a time; 0 lists all, as in a single-file export. PageSize int - // Page is the zero-based page of components to show and Query what the - // visitor searched them for. - Page int - Query string + // Pages holds the zero-based page shown of the sections paged, by + // their PublicSection.Key. + Pages map[string]int + // Query is what the visitor searched the components for; OnlyProblems + // keeps those with an outage or degraded. + Query string + OnlyProblems bool } -// ComponentPage is the part of a status page's components one page of it -// lists: the sections holding them, keeping the status and count of the -// whole section. -type ComponentPage struct { - Sections []agentapi.Section - // Page is the zero-based page shown, of Pages; Matches counts the - // components the query found, of all of them. +// PublicSection is a section of a status page as a request for it shows +// it: the components of one page of the section's own, its status and +// count those of the whole section. +type PublicSection struct { + Name string + Status string + Total int + // Key identifies the section in the URLs paging it, e.g. "0.2". + Key string + Components []agentapi.Component + Children []PublicSection + // Page is the zero-based page of the section's own components shown, + // of Pages. Page, Pages int - Matches int - Query string } -// PageComponents picks the components of page from sections, of those whose -// name or section contains query. size 0 shows all of them. -func PageComponents(sections []agentapi.Section, page, size int, query string) ComponentPage { - query = strings.TrimSpace(query) - needle := strings.ToLower(query) +// ComponentView is what a request for a status page shows of its +// components: every section paged on its own, filtered by a search and to +// those with problems if the visitor asked. +type ComponentView struct { + Sections []PublicSection + // Total counts all components, Matches those the filters leave and + // Problems those with an outage or degraded. + Total, Matches, Problems int + Query string + OnlyProblems bool + + base string + size int + pages map[string]int +} + +// Filterable reports whether the page lists enough components to offer the +// search and filter, or uses them. +func (v ComponentView) Filterable() bool { + return v.size > 0 && v.Total > v.size || v.Query != "" || v.OnlyProblems +} + +// Filtered reports whether a search or filter is in use. +func (v ComponentView) Filtered() bool { return v.Query != "" || v.OnlyProblems } + +// PageHref links page of the section key, keeping the other sections' pages +// and the filters. +func (v ComponentView) PageHref(key string, page int) string { + q := url.Values{} + if v.Query != "" { + q.Set("q", v.Query) + } + if v.OnlyProblems { + q.Set("problems", "1") + } + for k, p := range v.pages { + if k != key && p > 0 { + q.Set("p."+k, strconv.Itoa(p+1)) + } + } + if page > 0 { + q.Set("p."+key, strconv.Itoa(page+1)) + } + href := BackHref(v.base) + if len(q) > 0 { + href += "?" + q.Encode() + } + return href + "#g-" + key +} + +// ResetHref links the page without search, filter and paging. +func (v ComponentView) ResetHref() string { return BackHref(v.base) } + +// Problem reports whether a component counts as an outage for the filter. +func Problem(c agentapi.Component) bool { + return c.Status == agentapi.PageOutage || c.Status == agentapi.PageDegraded +} + +// PublicComponents decides which components of sections a request for the +// page shows, as a.Query, a.OnlyProblems and a.Pages ask. +func PublicComponents(sections []agentapi.Section, a PublicAssets) ComponentView { + v := ComponentView{ + Query: strings.TrimSpace(a.Query), OnlyProblems: a.OnlyProblems, + base: a.ComponentBase, size: a.PageSize, pages: a.Pages, + } + needle := strings.ToLower(v.Query) match := func(path []string, c agentapi.Component) bool { + if v.OnlyProblems && !Problem(c) { + return false + } if needle == "" || strings.Contains(strings.ToLower(c.Name), needle) { return true } @@ -68,90 +139,46 @@ func PageComponents(sections []agentapi.Section, page, size int, query string) C } return false } - // Components are counted in the order the page renders them: a - // section's own, then its children's. Unnamed sections are top-level - // lists without children. - var count func(sec agentapi.Section, path []string) int - count = func(sec agentapi.Section, path []string) int { + filtered := v.Filtered() + var walk func(sec agentapi.Section, key string, path []string) (PublicSection, bool) + walk = func(sec agentapi.Section, key string, path []string) (PublicSection, bool) { + ps := PublicSection{Name: sec.Name, Status: secStatusName(sec), Total: SectionTotal(sec), Key: key, Pages: 1} if sec.Name != "" { path = append(path, sec.Name) } - n := 0 + var own []agentapi.Component for _, c := range sec.Components { + v.Total++ + if Problem(c) { + v.Problems++ + } if match(path, c) { - n++ + own = append(own, c) } } + v.Matches += len(own) + if v.size > 0 && len(own) > v.size { + ps.Pages = (len(own) + v.size - 1) / v.size + ps.Page = min(max(v.pages[key], 0), ps.Pages-1) + own = own[ps.Page*v.size : min((ps.Page+1)*v.size, len(own))] + } + ps.Components = own + // Unnamed sections are top-level lists without children. if sec.Name != "" { - for _, child := range sec.Children { - n += count(child, path) - } - } - return n - } - total := 0 - for _, sec := range sections { - total += count(sec, nil) - } - out := ComponentPage{Pages: 1, Matches: total, Query: query} - if needle == "" && (size <= 0 || total <= size) { - out.Sections = sections - return out - } - from, to := 0, total - if size > 0 && total > size { - out.Pages = (total + size - 1) / size - out.Page = min(max(page, 0), out.Pages-1) - from, to = out.Page*size, min((out.Page+1)*size, total) - } - idx := 0 - var cut func(sec agentapi.Section, path []string) (agentapi.Section, bool) - cut = func(sec agentapi.Section, path []string) (agentapi.Section, bool) { - part := agentapi.Section{Name: sec.Name, Status: secStatusName(sec), Total: SectionTotal(sec)} - if sec.Name != "" { - path = append(path, sec.Name) - } - for _, c := range sec.Components { - if !match(path, c) { - continue - } - if idx >= from && idx < to { - part.Components = append(part.Components, c) - } - idx++ - } - if sec.Name != "" { - for _, child := range sec.Children { - if p, ok := cut(child, path); ok { - part.Children = append(part.Children, p) + for i, child := range sec.Children { + if c, ok := walk(child, key+"."+strconv.Itoa(i), path); ok { + ps.Children = append(ps.Children, c) } } } - return part, len(part.Components) > 0 || len(part.Children) > 0 + return ps, !filtered || len(ps.Components) > 0 || len(ps.Children) > 0 } - for _, sec := range sections { - if p, ok := cut(sec, nil); ok { - out.Sections = append(out.Sections, p) + for i, sec := range sections { + if ps, ok := walk(sec, strconv.Itoa(i), nil); ok { + v.Sections = append(v.Sections, ps) } } - return out -} - -// ComponentPageHref links page of the components of the status page at -// base, keeping the search for query. -func ComponentPageHref(base string, page int, query string) string { - v := url.Values{} - if page > 0 { - v.Set("page", strconv.Itoa(page+1)) - } - if query != "" { - v.Set("q", query) - } - href := BackHref(base) - if len(v) > 0 { - href += "?" + v.Encode() - } - return href + return v } // BarPath draws the bars of one status of a chart. diff --git a/internal/web/public.templ b/internal/web/public.templ index b7f7d2c..45d9202 100644 --- a/internal/web/public.templ +++ b/internal/web/public.templ @@ -94,18 +94,21 @@ templ PublicStatusPage(s agentapi.PageSnapshot, a PublicAssets) {

{ i18n.T(ctx, "page.status."+s.Status) }

@pageMaintenance(i18n.T(ctx, "page.maintenance_ongoing"), s.Ongoing(a.Now)) - if cp := PageComponents(s.Sections, a.Page, a.PageSize, a.Query); len(s.Sections) > 0 { - if cp.Pages > 1 || cp.Query != "" { - @publicComponentSearch(cp, a) + if v := PublicComponents(s.Sections, a); len(s.Sections) > 0 { + if v.Filterable() { + @publicComponentFilter(v, a) }
- for _, sec := range cp.Sections { - @publicSection(sec, s, a) + for _, sec := range v.Sections { + @publicSection(sec, v, s, a) } - if cp.Matches == 0 { -

{ i18n.T(ctx, "page.search_none") }

+ if v.Filtered() && v.Matches == 0 { + if v.OnlyProblems && v.Query == "" { +

{ i18n.T(ctx, "page.filter_no_problems") }

+ } else { +

{ i18n.T(ctx, "page.search_none") }

+ } } - @publicComponentPager(cp, a)
} @publicIncidents(s.Incidents) @@ -165,29 +168,33 @@ templ PublicNoticePage(s agentapi.PageSnapshot, a PublicAssets, title, message s } } -// publicComponentSearch finds components on pages listing too many to scan. -templ publicComponentSearch(cp ComponentPage, a PublicAssets) { - } -// publicComponentPager turns the pages of components. -templ publicComponentPager(cp ComponentPage, a PublicAssets) { - if cp.Pages > 1 { -