📚 read every page of exams, events and users instead of capping
Browse files at this commit7ef0f9983f745aed7e962da1040ae867176e0037Parent: bb77bbc- fix
- 📚 read every page of exams, events and users instead of capping
8 changed files +64 -102
- src/components/ui/truncation-notice.test.tsx +0 -27
- src/components/ui/truncation-notice.tsx +0 -26
- src/lib/capped-list.test.ts +18 -1
- src/lib/capped-list.ts +28 -0
- src/pages/admin-users-page.tsx +3 -12
- src/pages/events-page.test.tsx +9 -13
- src/pages/events-page.tsx +2 -11
- src/pages/exams-page.tsx +4 -12
src/components/ui/truncation-notice.test.tsxdeleted+0 -27
| @@ -1,27 +0,0 @@ | |||
| 1 | -import { cleanup, fireEvent, render, screen } from "@solidjs/testing-library"; | ||
| 2 | -import { afterEach, expect, test, vi } from "vitest"; | ||
| 3 | -import { TruncationNotice } from "@/components/ui/truncation-notice"; | ||
| 4 | -import { PreferencesProvider } from "@/stores/preferences-context"; | ||
| 5 | - | ||
| 6 | -afterEach(cleanup); | ||
| 7 | - | ||
| 8 | -test("says how many rows were left out and loads the rest on request", () => { | ||
| 9 | - const onLoadAll = vi.fn(); | ||
| 10 | - render(() => ( | ||
| 11 | - <PreferencesProvider> | ||
| 12 | - <TruncationNotice shown={100} total={340} onLoadAll={onLoadAll} /> | ||
| 13 | - </PreferencesProvider> | ||
| 14 | - )); | ||
| 15 | - expect(screen.getByRole("status").textContent).toContain("340"); | ||
| 16 | - fireEvent.click(screen.getByRole("button")); | ||
| 17 | - expect(onLoadAll).toHaveBeenCalledOnce(); | ||
| 18 | -}); | ||
| 19 | - | ||
| 20 | -test("stays hidden when every row is on screen", () => { | ||
| 21 | - const { container } = render(() => ( | ||
| 22 | - <PreferencesProvider> | ||
| 23 | - <TruncationNotice shown={12} total={12} onLoadAll={() => undefined} /> | ||
| 24 | - </PreferencesProvider> | ||
| 25 | - )); | ||
| 26 | - expect(container.textContent).toBe(""); | ||
| 27 | -}); | ||
src/components/ui/truncation-notice.tsxdeleted+0 -26
| @@ -1,26 +0,0 @@ | |||
| 1 | -import { Show } from "solid-js"; | ||
| 2 | -import { Alert } from "@/components/ui/alert"; | ||
| 3 | -import { Button } from "@/components/ui/button"; | ||
| 4 | -import { useT } from "@/stores/preferences-context"; | ||
| 5 | - | ||
| 6 | -/** Says a capped list left rows out, with a way to fetch the rest. */ | ||
| 7 | -export function TruncationNotice(props: { shown: number; total: number; loading?: boolean; onLoadAll: () => void }) { | ||
| 8 | - const t = useT(); | ||
| 9 | - return ( | ||
| 10 | - <Show when={props.total > props.shown}> | ||
| 11 | - <Alert role="status" class="flex min-h-0 flex-wrap items-center justify-between gap-3 py-3"> | ||
| 12 | - <span class="min-w-0 flex-1">{t("common.truncatedNotice", { shown: props.shown, total: props.total })}</span> | ||
| 13 | - <Button | ||
| 14 | - type="button" | ||
| 15 | - size="sm" | ||
| 16 | - variant="outline" | ||
| 17 | - class="shrink-0" | ||
| 18 | - disabled={props.loading} | ||
| 19 | - onClick={() => props.onLoadAll()} | ||
| 20 | - > | ||
| 21 | - {t("common.loadAll")} | ||
| 22 | - </Button> | ||
| 23 | - </Alert> | ||
| 24 | - </Show> | ||
| 25 | - ); | ||
| 26 | -} | ||
src/lib/capped-list.test.tsmodified+18 -1
| @@ -1,5 +1,5 @@ | |||
| 1 | import type { Page, PageParams } from "@/api/client"; | 1 | import type { Page, PageParams } from "@/api/client"; |
| 2 | -import { isTruncated, loadCappedList } from "./capped-list"; | 2 | +import { isTruncated, loadAllPages, loadCappedList } from "./capped-list"; |
| 3 | 3 | ||
| 4 | const page = (items: number[], total: number): Page<number> => ({ items, total, limit: null, offset: 0 }); | 4 | const page = (items: number[], total: number): Page<number> => ({ items, total, limit: null, offset: 0 }); |
| 5 | 5 | ||
| @@ -23,3 +23,20 @@ it("never reports a total below the rows it holds", async () => { | |||
| 23 | expect(list.total).toBe(3); | 23 | expect(list.total).toBe(3); |
| 24 | expect(isTruncated(undefined)).toBe(false); | 24 | expect(isTruncated(undefined)).toBe(false); |
| 25 | }); | 25 | }); |
| 26 | + | ||
| 27 | +it("reads every page of a paged list, in order", async () => { | ||
| 28 | + const all = Array.from({ length: 7 }, (_, index) => index); | ||
| 29 | + const fetch = vi.fn(async (params?: PageParams) => { | ||
| 30 | + const offset = params?.offset ?? 0; | ||
| 31 | + return page(all.slice(offset, offset + (params?.limit ?? all.length)), all.length); | ||
| 32 | + }); | ||
| 33 | + expect(await loadAllPages(fetch, 3)).toEqual(all); | ||
| 34 | + expect(fetch).toHaveBeenCalledTimes(3); | ||
| 35 | + expect(fetch).toHaveBeenCalledWith({ limit: 3, offset: 6 }); | ||
| 36 | +}); | ||
| 37 | + | ||
| 38 | +it("stops after one request when the first page holds everything", async () => { | ||
| 39 | + const fetch = vi.fn(async () => page([1, 2], 2)); | ||
| 40 | + expect(await loadAllPages(fetch, 100)).toEqual([1, 2]); | ||
| 41 | + expect(fetch).toHaveBeenCalledTimes(1); | ||
| 42 | +}); | ||
src/lib/capped-list.tsmodified+28 -0
| @@ -28,3 +28,31 @@ export async function loadCappedList<T>( | |||
| 28 | export function isTruncated(list: CappedList<unknown> | undefined): boolean { | 28 | export function isTruncated(list: CappedList<unknown> | undefined): boolean { |
| 29 | return !!list && list.total > list.items.length; | 29 | return !!list && list.total > list.items.length; |
| 30 | } | 30 | } |
| 31 | + | ||
| 32 | +/** Requests in flight at once while a list's later pages are read. */ | ||
| 33 | +const PAGE_CONCURRENCY = 3; | ||
| 34 | + | ||
| 35 | +/** | ||
| 36 | + * Every row of a list the backend can only page (`limit` / `offset`, no | ||
| 37 | + * search or filter): the first page names the total, the rest are read in | ||
| 38 | + * parallel, in order. The page then searches, filters and pages in memory | ||
| 39 | + * over the whole list, so there is no "first 100 of 161" cut to explain. | ||
| 40 | + */ | ||
| 41 | +export async function loadAllPages<T>( | ||
| 42 | + fetch: (params?: PageParams) => Promise<Page<T>>, | ||
| 43 | + pageSize: number = LIST_CAP, | ||
| 44 | +): Promise<T[]> { | ||
| 45 | + const first = await fetch({ limit: pageSize, offset: 0 }); | ||
| 46 | + const items = Array.isArray(first.items) ? [...first.items] : []; | ||
| 47 | + const total = typeof first.total === "number" ? first.total : items.length; | ||
| 48 | + if (items.length < pageSize || items.length >= total) return items; | ||
| 49 | + const offsets: number[] = []; | ||
| 50 | + for (let offset = items.length; offset < total; offset += pageSize) offsets.push(offset); | ||
| 51 | + const pages: T[][] = new Array(offsets.length); | ||
| 52 | + for (let start = 0; start < offsets.length; start += PAGE_CONCURRENCY) { | ||
| 53 | + const batch = offsets.slice(start, start + PAGE_CONCURRENCY); | ||
| 54 | + const results = await Promise.all(batch.map((offset) => fetch({ limit: pageSize, offset }))); | ||
| 55 | + results.forEach((page, index) => { pages[start + index] = Array.isArray(page.items) ? page.items : []; }); | ||
| 56 | + } | ||
| 57 | + return items.concat(...pages); | ||
| 58 | +} | ||
src/pages/admin-users-page.tsxmodified+3 -12
| @@ -15,8 +15,7 @@ import { Button } from "@/components/ui/button"; | |||
| 15 | import { DataTableSkeleton } from "@/components/ui/data-table"; | 15 | import { DataTableSkeleton } from "@/components/ui/data-table"; |
| 16 | import { IconPlus } from "@/components/ui/icons"; | 16 | import { IconPlus } from "@/components/ui/icons"; |
| 17 | import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; | 17 | import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 18 | -import { TruncationNotice } from "@/components/ui/truncation-notice"; | 18 | +import { loadAllPages } from "@/lib/capped-list"; |
| 19 | -import { loadCappedList } from "@/lib/capped-list"; | ||
| 20 | import { createFlash } from "@/lib/flash"; | 19 | import { createFlash } from "@/lib/flash"; |
| 21 | import { useAuth } from "@/stores/auth-context"; | 20 | import { useAuth } from "@/stores/auth-context"; |
| 22 | import { useT } from "@/stores/preferences-context"; | 21 | import { useT } from "@/stores/preferences-context"; |
| @@ -32,7 +31,7 @@ export default function AdminUsersPage() { | |||
| 32 | } | 31 | } |
| 33 | 32 | ||
| 34 | /** Staff rosters run longer than other lists, so the first load holds more. */ | 33 | /** Staff rosters run longer than other lists, so the first load holds more. */ |
| 35 | -const USER_LIST_CAP = 200; | 34 | +const USER_PAGE_SIZE = 200; |
| 36 | 35 | ||
| 37 | function AdminUsersContent() { | 36 | function AdminUsersContent() { |
| 38 | const auth = useAuth(); | 37 | const auth = useAuth(); |
| @@ -44,10 +43,8 @@ function AdminUsersContent() { | |||
| 44 | const [creating, setCreating] = createSignal(false); | 43 | const [creating, setCreating] = createSignal(false); |
| 45 | const [editTarget, setEditTarget] = createSignal<User | null>(null); | 44 | const [editTarget, setEditTarget] = createSignal<User | null>(null); |
| 46 | 45 | ||
| 47 | - const [loadAll, setLoadAll] = createSignal(false); | ||
| 48 | const [list, { refetch }] = createResource( | 46 | const [list, { refetch }] = createResource( |
| 49 | - () => (loadAll() ? "all" : "capped"), | 47 | + async () => ({ items: await loadAllPages(getUsers, USER_PAGE_SIZE) }), |
| 50 | - (scope) => loadCappedList(getUsers, USER_LIST_CAP, scope === "all"), | ||
| 51 | ); | 48 | ); |
| 52 | const allUsers = () => list()?.items ?? []; | 49 | const allUsers = () => list()?.items ?? []; |
| 53 | const visibleUsers = createMemo(() => { | 50 | const visibleUsers = createMemo(() => { |
| @@ -89,12 +86,6 @@ function AdminUsersContent() { | |||
| 89 | 86 | ||
| 90 | <section class="space-y-4 p-0"> | 87 | <section class="space-y-4 p-0"> |
| 91 | <Suspense fallback={<DataTableSkeleton columns={6} rows={8} />}> | 88 | <Suspense fallback={<DataTableSkeleton columns={6} rows={8} />}> |
| 92 | - <TruncationNotice | ||
| 93 | - shown={list()?.items.length ?? 0} | ||
| 94 | - total={list()?.total ?? 0} | ||
| 95 | - loading={list.loading} | ||
| 96 | - onLoadAll={() => setLoadAll(true)} | ||
| 97 | - /> | ||
| 98 | <Show when={list()}> | 89 | <Show when={list()}> |
| 99 | <UserTable | 90 | <UserTable |
| 100 | title={t("admin.title")} | 91 | title={t("admin.title")} |
src/pages/events-page.test.tsxmodified+9 -13
| @@ -37,11 +37,12 @@ afterEach(() => { | |||
| 37 | vi.clearAllMocks(); | 37 | vi.clearAllMocks(); |
| 38 | }); | 38 | }); |
| 39 | 39 | ||
| 40 | -test("a capped event list says rows were left out and loads them on request", async () => { | 40 | +test("the event list reads every page instead of stopping at the first 100", async () => { |
| 41 | - getEvents.mockImplementation(async (params?: { limit?: number }) => { | 41 | + const all = Array.from({ length: 150 }, (_, i) => event(i + 1)); |
| 42 | - const all = Array.from({ length: 3 }, (_, i) => event(i + 1)); | 42 | + getEvents.mockImplementation(async (params?: { limit?: number; offset?: number }) => { |
| 43 | - const items = params?.limit ? all.slice(0, 2) : all; | 43 | + const offset = params?.offset ?? 0; |
| 44 | - return { items, total: all.length, limit: params?.limit ?? null, offset: 0 }; | 44 | + const items = all.slice(offset, offset + (params?.limit ?? all.length)); |
| 45 | + return { items, total: all.length, limit: params?.limit ?? null, offset }; | ||
| 45 | }); | 46 | }); |
| 46 | 47 | ||
| 47 | render(() => ( | 48 | render(() => ( |
| @@ -50,12 +51,7 @@ test("a capped event list says rows were left out and loads them on request", as | |||
| 50 | </PreferencesProvider> | 51 | </PreferencesProvider> |
| 51 | )); | 52 | )); |
| 52 | 53 | ||
| 53 | - const notice = await screen.findByRole("status"); | 54 | + await waitFor(() => expect(getEvents).toHaveBeenCalledWith({ limit: 100, offset: 100 })); |
| 54 | - expect(getEvents).toHaveBeenCalledWith({ limit: 100 }); | 55 | + expect(getEvents).toHaveBeenCalledWith({ limit: 100, offset: 0 }); |
| 55 | - expect(notice.textContent).toContain("3"); | 56 | + expect(screen.queryByRole("button", { name: /Tümünü yükle|Load all/ })).toBeNull(); |
| 56 | - | ||
| 57 | - fireEvent.click(await screen.findByRole("button", { name: /Tümünü yükle|Load all/ })); | ||
| 58 | - | ||
| 59 | - await waitFor(() => expect(getEvents).toHaveBeenLastCalledWith(undefined)); | ||
| 60 | - await waitFor(() => expect(screen.queryByRole("status")).toBeNull()); | ||
| 61 | }); | 57 | }); |
src/pages/events-page.tsxmodified+2 -11
| @@ -18,11 +18,10 @@ import { IconEdit, IconEye, IconPlus, IconTrash } from "@/components/ui/icons"; | |||
| 18 | import { DropdownSelect } from "@/components/ui/select"; | 18 | import { DropdownSelect } from "@/components/ui/select"; |
| 19 | import { SidePanel } from "@/components/ui/side-panel"; | 19 | import { SidePanel } from "@/components/ui/side-panel"; |
| 20 | import { TableRowActions } from "@/components/ui/table-row-actions"; | 20 | import { TableRowActions } from "@/components/ui/table-row-actions"; |
| 21 | -import { TruncationNotice } from "@/components/ui/truncation-notice"; | ||
| 22 | import { cn } from "@/lib/cn"; | 21 | import { cn } from "@/lib/cn"; |
| 23 | import { createUrlEnum } from "@/lib/url-state"; | 22 | import { createUrlEnum } from "@/lib/url-state"; |
| 24 | import { createNow } from "@/lib/create-now"; | 23 | import { createNow } from "@/lib/create-now"; |
| 25 | -import { LIST_CAP, loadCappedList } from "@/lib/capped-list"; | 24 | +import { loadAllPages } from "@/lib/capped-list"; |
| 26 | import { createFlash } from "@/lib/flash"; | 25 | import { createFlash } from "@/lib/flash"; |
| 27 | import { formatDateTime } from "@/lib/format"; | 26 | import { formatDateTime } from "@/lib/format"; |
| 28 | import { hasMinRole } from "@/lib/roles"; | 27 | import { hasMinRole } from "@/lib/roles"; |
| @@ -85,10 +84,8 @@ function EventsContent() { | |||
| 85 | .sort(byDefault); | 84 | .sort(byDefault); |
| 86 | }; | 85 | }; |
| 87 | 86 | ||
| 88 | - const [loadAll, setLoadAll] = createSignal(false); | ||
| 89 | const [list, { refetch }] = createResource( | 87 | const [list, { refetch }] = createResource( |
| 90 | - () => (loadAll() ? "all" : "capped"), | 88 | + async () => ({ items: await loadAllPages(getEvents) }), |
| 91 | - (scope) => loadCappedList(getEvents, LIST_CAP, scope === "all"), | ||
| 92 | ); | 89 | ); |
| 93 | const rows = createMemo(() => filterEvents(list()?.items ?? [])); | 90 | const rows = createMemo(() => filterEvents(list()?.items ?? [])); |
| 94 | 91 | ||
| @@ -279,12 +276,6 @@ function EventsContent() { | |||
| 279 | <Show when={list.error}> | 276 | <Show when={list.error}> |
| 280 | <Alert variant="destructive">{formatApiError(list.error)}</Alert> | 277 | <Alert variant="destructive">{formatApiError(list.error)}</Alert> |
| 281 | </Show> | 278 | </Show> |
| 282 | - <TruncationNotice | ||
| 283 | - shown={list()?.items.length ?? 0} | ||
| 284 | - total={list()?.total ?? 0} | ||
| 285 | - loading={list.loading} | ||
| 286 | - onLoadAll={() => setLoadAll(true)} | ||
| 287 | - /> | ||
| 288 | <DataTable | 279 | <DataTable |
| 289 | urlState | 280 | urlState |
| 290 | title={t("events.title")} | 281 | title={t("events.title")} |
src/pages/exams-page.tsxmodified+4 -12
| @@ -22,13 +22,12 @@ import { DropdownSelect } from "@/components/ui/select"; | |||
| 22 | import { SearchableSelect } from "@/components/ui/searchable-select"; | 22 | import { SearchableSelect } from "@/components/ui/searchable-select"; |
| 23 | import { SidePanel } from "@/components/ui/side-panel"; | 23 | import { SidePanel } from "@/components/ui/side-panel"; |
| 24 | import { TableRowActions } from "@/components/ui/table-row-actions"; | 24 | import { TableRowActions } from "@/components/ui/table-row-actions"; |
| 25 | -import { TruncationNotice } from "@/components/ui/truncation-notice"; | ||
| 26 | import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; | 25 | import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 27 | import { createNow } from "@/lib/create-now"; | 26 | import { createNow } from "@/lib/create-now"; |
| 28 | import { EXAM_KINDS } from "@/api/client"; | 27 | import { EXAM_KINDS } from "@/api/client"; |
| 29 | import { examKindLabel } from "@/lib/exam-labels"; | 28 | import { examKindLabel } from "@/lib/exam-labels"; |
| 30 | import { examDisplayStatus, examStatusMessageKey, examStatusTone, isSittableExam, type ExamDisplayStatus } from "@/lib/exam-status"; | 29 | import { examDisplayStatus, examStatusMessageKey, examStatusTone, isSittableExam, type ExamDisplayStatus } from "@/lib/exam-status"; |
| 31 | -import { LIST_CAP, loadCappedList } from "@/lib/capped-list"; | 30 | +import { loadAllPages } from "@/lib/capped-list"; |
| 32 | import { createFlash } from "@/lib/flash"; | 31 | import { createFlash } from "@/lib/flash"; |
| 33 | import { countExamQuestions } from "@/lib/exam-publish"; | 32 | import { countExamQuestions } from "@/lib/exam-publish"; |
| 34 | import { formatDateTime } from "@/lib/format"; | 33 | import { formatDateTime } from "@/lib/format"; |
| @@ -151,15 +150,14 @@ function ExamsContent() { | |||
| 151 | statusLabel(exam.displayStatus), | 150 | statusLabel(exam.displayStatus), |
| 152 | ); | 151 | ); |
| 153 | 152 | ||
| 154 | - const [loadAll, setLoadAll] = createSignal(false); | ||
| 155 | const [list, { refetch: refetchExams }] = createResource( | 153 | const [list, { refetch: refetchExams }] = createResource( |
| 156 | () => { | 154 | () => { |
| 157 | if (sections() === undefined) return null; | 155 | if (sections() === undefined) return null; |
| 158 | - return `${loadAll() ? "all" : "capped"}|${visibleCourses().map((row) => row.id).join(",")}`; | 156 | + return `${visibleCourses().map((row) => row.id).join(",")}`; |
| 159 | }, | 157 | }, |
| 160 | async () => { | 158 | async () => { |
| 161 | - const page = await loadCappedList(getExams, LIST_CAP, loadAll()); | 159 | + const items = await loadAllPages(getExams); |
| 162 | - const items = page.items; | 160 | + const page = { items }; |
| 163 | const known = new Map(visibleCourses().map((row) => [row.id, row.label])); | 161 | const known = new Map(visibleCourses().map((row) => [row.id, row.label])); |
| 164 | const missing = [...new Set(items.map((exam) => exam.class_course))].filter((instanceId) => !known.has(instanceId)); | 162 | const missing = [...new Set(items.map((exam) => exam.class_course))].filter((instanceId) => !known.has(instanceId)); |
| 165 | if (missing.length > 0) { | 163 | if (missing.length > 0) { |
| @@ -384,12 +382,6 @@ function ExamsContent() { | |||
| 384 | <Show when={list.error}> | 382 | <Show when={list.error}> |
| 385 | <Alert variant="destructive">{formatApiError(list.error)}</Alert> | 383 | <Alert variant="destructive">{formatApiError(list.error)}</Alert> |
| 386 | </Show> | 384 | </Show> |
| 387 | - <TruncationNotice | ||
| 388 | - shown={list()?.items.length ?? 0} | ||
| 389 | - total={list()?.total ?? 0} | ||
| 390 | - loading={list.loading} | ||
| 391 | - onLoadAll={() => setLoadAll(true)} | ||
| 392 | - /> | ||
| 393 | <DataTable | 385 | <DataTable |
| 394 | title={t("exams.title")} | 386 | title={t("exams.title")} |
| 395 | description={t("exams.subtitle")} | 387 | description={t("exams.subtitle")} |