feat: page long and truncated lists
Lists that were cut off by a fixed LIMIT, or could grow without bound, are paged by keyset: the cursor holds the sort key and an ID, so pages neither skip nor repeat items as others are added. The audit logs had a timestamp-only cursor that could skip entries with equal times. The API returns at most 100 items per list (limit takes up to 500) with next_cursor and prev_cursor, passed back as after and before. The console shows Newer/Older (or Previous/Next) links and To the start, without JavaScript. Paged: incidents (optionally of one monitor), a monitor's results and evidence, a checker's results, the audit logs, members, maintenance windows, firing Alertmanager alerts, the monitors API and the admin lists of status pages and organizations, which move to their own admin pages. A monitor's page shows its latest incidents and results and links to all of them. The dashboard caps every group at 50 of its own monitors with a link to a paged view of the group, and gains a search by name or target. Lists filtered by permission after reading may show fewer items on a page. Closes #116 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
98fb5d3a82
commit
d7c5cd711e
63 files changed
+5427
-3041
No files matched your search
@@ -0,0 +1,184 @@
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// pages follows next_cursor through a list and back through prev_cursor,
|
||||
// returning the IDs in order both ways.
|
||||
func pages(t *testing.T, b *browser, path, key string) (forward, backward []string) {
|
||||
t.Helper()
|
||||
sep := "?"
|
||||
if strings.Contains(path, "?") {
|
||||
sep = "&"
|
||||
}
|
||||
ids := func(body map[string]any) []string {
|
||||
var out []string
|
||||
for _, it := range body[key].([]any) {
|
||||
out = append(out, it.(map[string]any)["id"].(string))
|
||||
}
|
||||
return out
|
||||
}
|
||||
var last map[string]any
|
||||
cursor := ""
|
||||
for range 20 {
|
||||
q := path + sep + "limit=2"
|
||||
if cursor != "" {
|
||||
q += "&after=" + url.QueryEscape(cursor)
|
||||
}
|
||||
st, body := b.do("GET", q, nil)
|
||||
if st != http.StatusOK {
|
||||
t.Fatalf("GET %s = %d %v", q, st, body)
|
||||
}
|
||||
forward = append(forward, ids(body)...)
|
||||
last = body
|
||||
if cursor, _ = body["next_cursor"].(string); cursor == "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
// Back from the last page to the first.
|
||||
page := ids(last)
|
||||
backward = append(page, backward...)
|
||||
prev, _ := last["prev_cursor"].(string)
|
||||
for prev != "" {
|
||||
st, body := b.do("GET", path+sep+"limit=2&before="+url.QueryEscape(prev), nil)
|
||||
if st != http.StatusOK {
|
||||
t.Fatalf("before = %d %v", st, body)
|
||||
}
|
||||
backward = append(ids(body), backward...)
|
||||
prev, _ = body["prev_cursor"].(string)
|
||||
}
|
||||
return forward, backward
|
||||
}
|
||||
|
||||
func TestPaging(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
owner := e.browser(t)
|
||||
owner.signup("o@"+uniq("pg")+".test", uniq("pg-"))
|
||||
var monitors []string
|
||||
for _, name := range []string{"e", "a", "d", "b", "c"} {
|
||||
_, m := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": name, "type": "heartbeat", "interval_seconds": 3600})
|
||||
monitors = append(monitors, m["id"].(string))
|
||||
}
|
||||
|
||||
// Monitors page by name.
|
||||
fwd, back := pages(t, owner, "/api/v1/org/monitors", "monitors")
|
||||
if len(fwd) != 5 || !slices.Equal(fwd, back) {
|
||||
t.Errorf("monitors forward %v, backward %v", fwd, back)
|
||||
}
|
||||
_, first := owner.do("GET", "/api/v1/org/monitors?limit=1", nil)
|
||||
if first["monitors"].([]any)[0].(map[string]any)["name"] != "a" {
|
||||
t.Errorf("monitors are not by name: %v", first["monitors"])
|
||||
}
|
||||
|
||||
// Incidents with the same start time: none skipped or repeated.
|
||||
started := time.Now().Add(-time.Hour).Truncate(time.Second)
|
||||
if err := e.asSystem(t, func(tx pgx.Tx) error {
|
||||
for _, m := range monitors {
|
||||
if _, err := tx.Exec(context.Background(), `INSERT INTO incidents (org_id, monitor_id, started_at, resolved_at, cause)
|
||||
SELECT org_id, id, $2::timestamptz, $2::timestamptz + interval '1 minute', '{}' FROM monitors WHERE id = $1`, m, started); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fwd, back = pages(t, owner, "/api/v1/org/incidents", "incidents")
|
||||
if len(fwd) != 5 || !slices.Equal(fwd, back) {
|
||||
t.Errorf("incidents forward %v, backward %v", fwd, back)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, id := range fwd {
|
||||
if seen[id] {
|
||||
t.Errorf("incident %s repeated", id)
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
fwd, _ = pages(t, owner, "/api/v1/org/incidents?monitor_id="+monitors[0], "incidents")
|
||||
if len(fwd) != 1 {
|
||||
t.Errorf("incidents of one monitor = %v", fwd)
|
||||
}
|
||||
|
||||
// Invalid requests.
|
||||
st, body := owner.do("GET", "/api/v1/org/incidents?after=nonsense!", nil)
|
||||
expect(t, "bad cursor", st, body, http.StatusUnprocessableEntity, "invalid_cursor")
|
||||
st, body = owner.do("GET", "/api/v1/org/incidents?limit=501", nil)
|
||||
expect(t, "limit too large", st, body, http.StatusUnprocessableEntity, "invalid_limit")
|
||||
|
||||
// The console pages with links.
|
||||
_, _, html := owner.page("/incidents?limit=2")
|
||||
if !strings.Contains(html, `rel="next"`) || strings.Contains(html, `rel="prev"`) {
|
||||
t.Error("the first console page lacks a next link or has a previous one")
|
||||
}
|
||||
if _, _, html := owner.page(nextLink(t, html)); !strings.Contains(html, `rel="prev"`) || !strings.Contains(html, "To the start") {
|
||||
t.Error("a later console page lacks the way back")
|
||||
}
|
||||
}
|
||||
|
||||
var nextHref = regexp.MustCompile(`href="([^"]+)" rel="next"`)
|
||||
|
||||
// nextLink returns the console's link to the next page.
|
||||
func nextLink(t *testing.T, html string) string {
|
||||
t.Helper()
|
||||
m := nextHref.FindStringSubmatch(html)
|
||||
if m == nil {
|
||||
t.Fatal("no next link")
|
||||
}
|
||||
return strings.ReplaceAll(m[1], "&", "&")
|
||||
}
|
||||
|
||||
// shown counts the monitors a dashboard page links to.
|
||||
func shown(html string) int {
|
||||
return strings.Count(html, `href="/monitors/`) - strings.Count(html, `href="/monitors/new"`)
|
||||
}
|
||||
|
||||
func TestDashboardPaging(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
owner := e.browser(t)
|
||||
owner.signup("o@"+uniq("dash")+".test", uniq("dash-"))
|
||||
_, g := owner.do("POST", "/api/v1/org/groups", map[string]any{"name": "Fleet"})
|
||||
group := g["id"].(string)
|
||||
for i := range 55 {
|
||||
st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{
|
||||
"name": "node-" + strconv.Itoa(100+i), "type": "heartbeat", "interval_seconds": 3600, "group_id": group,
|
||||
})
|
||||
if st != http.StatusCreated {
|
||||
t.Fatalf("create = %d %v", st, body)
|
||||
}
|
||||
}
|
||||
owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "loner", "type": "heartbeat", "interval_seconds": 3600})
|
||||
|
||||
_, _, html := owner.page("/")
|
||||
if n := shown(html); n != 51 {
|
||||
t.Errorf("dashboard shows %d monitors, want 50 of the group and the ungrouped one", n)
|
||||
}
|
||||
if !strings.Contains(html, "Show all 55") || !strings.Contains(html, "/?group="+group) {
|
||||
t.Error("the group lacks its show-all link")
|
||||
}
|
||||
|
||||
_, _, html = owner.page("/?group=" + group)
|
||||
if n := shown(html); n != 50 || !strings.Contains(html, `rel="next"`) || !strings.Contains(html, "Fleet") {
|
||||
t.Errorf("focused group shows %d monitors or lacks paging", n)
|
||||
}
|
||||
if _, _, page2 := owner.page(nextLink(t, html)); shown(page2) != 5 || !strings.Contains(page2, "node-154") {
|
||||
t.Error("the second page of the group is wrong")
|
||||
}
|
||||
|
||||
if _, _, html := owner.page("/?q=node-107"); shown(html) != 1 || !strings.Contains(html, "node-107") {
|
||||
t.Error("search does not find the one monitor")
|
||||
}
|
||||
if _, _, html := owner.page("/?group=none"); !strings.Contains(html, "loner") || strings.Contains(html, "node-100") {
|
||||
t.Error("the ungrouped view is wrong")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user