🔭 ask the shell feed for the next rows, not every row
Browse files at this commit5d93c16076f8b5d48de8ee691ca012a5b03e1979Parent: e607b1e- feat
- 🔭 ask the shell feed for the next rows, not every row
7 changed files +106 -14
- src/api/__tests__/client/page.test.ts +13 -0
- src/api/__tests__/events/events.test.ts +19 -0
- src/api/__tests__/exams/exams.test.ts +19 -0
- src/api/client/page.ts +18 -1
- src/api/events/getEvents.ts +11 -2
- src/api/exams/getExams.ts +11 -2
- src/stores/shell-feed-context.tsx +15 -9
src/api/__tests__/client/page.test.tsmodified+13 -0
| @@ -13,6 +13,19 @@ describe("page helpers", () => { | |||
| 13 | expect(pageQuery({ offset: 20 })).toBe("?offset=20"); | 13 | expect(pageQuery({ offset: 20 })).toBe("?offset=20"); |
| 14 | expect(pageQuery({ limit: 10, offset: 20 })).toBe("?limit=10&offset=20"); | 14 | expect(pageQuery({ limit: 10, offset: 20 })).toBe("?limit=10&offset=20"); |
| 15 | }); | 15 | }); |
| 16 | + | ||
| 17 | + it("formats the schedule window and omits undefined keys", () => { | ||
| 18 | + expect(pageQuery({ ends_after: 1_700_000_000_000 })).toBe("?ends_after=1700000000000"); | ||
| 19 | + expect(pageQuery({ starts_after: 1_700_000_000_000 })).toBe("?starts_after=1700000000000"); | ||
| 20 | + expect(pageQuery({ limit: 50, ends_after: 1_700_000_000_000 })).toBe( | ||
| 21 | + "?limit=50&ends_after=1700000000000" | ||
| 22 | + ); | ||
| 23 | + // Present-but-empty (`?ends_after=`) is a hard 400 server-side, so an | ||
| 24 | + // undefined filter must drop the key, not serialize as a blank value. | ||
| 25 | + expect(pageQuery({ limit: 50, ends_after: undefined, starts_after: undefined })).toBe( | ||
| 26 | + "?limit=50" | ||
| 27 | + ); | ||
| 28 | + }); | ||
| 16 | }); | 29 | }); |
| 17 | 30 | ||
| 18 | describe("appendPageParams", () => { | 31 | describe("appendPageParams", () => { |
src/api/__tests__/events/events.test.tsmodified+19 -0
| @@ -29,6 +29,25 @@ describe("events API", () => { | |||
| 29 | expect(init?.method).toBe("GET"); | 29 | expect(init?.method).toBe("GET"); |
| 30 | }); | 30 | }); |
| 31 | 31 | ||
| 32 | + it("getEvents sends the schedule window and drops undefined params", async () => { | ||
| 33 | + mockFetchSuccess({ items: [{ id: "e1" }], total: 1 }); | ||
| 34 | + | ||
| 35 | + await getEvents({ ends_after: 1_700_000_000_000, limit: 50 }); | ||
| 36 | + expect(lastFetchCall()[0]).toBe("/api/events?limit=50&ends_after=1700000000000"); | ||
| 37 | + | ||
| 38 | + mockFetchSuccess({ items: [], total: 0 }); | ||
| 39 | + await getEvents({ starts_after: 1_700_000_000_000 }); | ||
| 40 | + expect(lastFetchCall()[0]).toBe("/api/events?starts_after=1700000000000"); | ||
| 41 | + | ||
| 42 | + // A blank value (`?ends_after=`) is a hard 400 server-side, so an | ||
| 43 | + // undefined filter must leave the key out of the query string entirely. | ||
| 44 | + mockFetchSuccess({ items: [], total: 0 }); | ||
| 45 | + await getEvents({ limit: 10, ends_after: undefined, starts_after: undefined }); | ||
| 46 | + expect(lastFetchCall()[0]).toBe("/api/events?limit=10"); | ||
| 47 | + expect(lastFetchCall()[0]).not.toContain("ends_after"); | ||
| 48 | + expect(lastFetchCall()[0]).not.toContain("starts_after"); | ||
| 49 | + }); | ||
| 50 | + | ||
| 32 | it("getEventById calls /events/:id", async () => { | 51 | it("getEventById calls /events/:id", async () => { |
| 33 | const mockEvent = { id: "e1", title: "Event" }; | 52 | const mockEvent = { id: "e1", title: "Event" }; |
| 34 | mockFetchSuccess(mockEvent); | 53 | mockFetchSuccess(mockEvent); |
src/api/__tests__/exams/exams.test.tsmodified+19 -0
| @@ -27,6 +27,25 @@ describe("exams API - core", () => { | |||
| 27 | expect(init?.method).toBe("GET"); | 27 | expect(init?.method).toBe("GET"); |
| 28 | }); | 28 | }); |
| 29 | 29 | ||
| 30 | + it("getExams sends the schedule window and drops undefined params", async () => { | ||
| 31 | + mockFetchSuccess({ items: [{ id: "ex1" }], total: 1 }); | ||
| 32 | + | ||
| 33 | + await getExams({ ends_after: 1_700_000_000_000, limit: 50 }); | ||
| 34 | + expect(lastFetchCall()[0]).toBe("/api/exams?limit=50&ends_after=1700000000000"); | ||
| 35 | + | ||
| 36 | + mockFetchSuccess({ items: [], total: 0 }); | ||
| 37 | + await getExams({ starts_after: 1_700_000_000_000 }); | ||
| 38 | + expect(lastFetchCall()[0]).toBe("/api/exams?starts_after=1700000000000"); | ||
| 39 | + | ||
| 40 | + // A blank value (`?ends_after=`) is a hard 400 server-side, so an | ||
| 41 | + // undefined filter must leave the key out of the query string entirely. | ||
| 42 | + mockFetchSuccess({ items: [], total: 0 }); | ||
| 43 | + await getExams({ limit: 10, ends_after: undefined, starts_after: undefined }); | ||
| 44 | + expect(lastFetchCall()[0]).toBe("/api/exams?limit=10"); | ||
| 45 | + expect(lastFetchCall()[0]).not.toContain("ends_after"); | ||
| 46 | + expect(lastFetchCall()[0]).not.toContain("starts_after"); | ||
| 47 | + }); | ||
| 48 | + | ||
| 30 | it("getExamChoiceImageBlob calls /exams/:id/questions/:qid/choices/:choiceId/image", async () => { | 49 | it("getExamChoiceImageBlob calls /exams/:id/questions/:qid/choices/:choiceId/image", async () => { |
| 31 | mockFetchBlob(new Blob(["img"], { type: "image/png" })); | 50 | mockFetchBlob(new Blob(["img"], { type: "image/png" })); |
| 32 | 51 | ||
src/api/client/page.tsmodified+18 -1
| @@ -10,11 +10,28 @@ export type Page<T> = { | |||
| 10 | offset: number; | 10 | offset: number; |
| 11 | }; | 11 | }; |
| 12 | 12 | ||
| 13 | -export function pageQuery(params?: PageParams): string { | 13 | +/** |
| 14 | + * Schedule-window filters, UTC unix ms. Only `/events` and `/exams` honour | ||
| 15 | + * them; both are optional and AND-ed when both sent, and a row with no | ||
| 16 | + * schedule at all is excluded by either. Sending either flips the server | ||
| 17 | + * order to ASCENDING by schedule (soonest first) instead of newest-created. | ||
| 18 | + */ | ||
| 19 | +export type ScheduleWindowParams = { | ||
| 20 | + /** Keeps rows that have not begun: `starts_at > T`. */ | ||
| 21 | + starts_after?: number; | ||
| 22 | + /** Keeps rows whose window has not finished: `ends_at > T`, else `starts_at > T`. */ | ||
| 23 | + ends_after?: number; | ||
| 24 | +}; | ||
| 25 | + | ||
| 26 | +export function pageQuery(params?: PageParams & ScheduleWindowParams): string { | ||
| 14 | if (!params) return ""; | 27 | if (!params) return ""; |
| 15 | const query = new URLSearchParams(); | 28 | const query = new URLSearchParams(); |
| 29 | + // `!= null` is load-bearing: the API 400s on a present-but-empty value | ||
| 30 | + // (`?limit=`), so an undefined param must drop the key, never emit `key=`. | ||
| 16 | if (params.limit != null) query.set("limit", String(params.limit)); | 31 | if (params.limit != null) query.set("limit", String(params.limit)); |
| 17 | if (params.offset != null) query.set("offset", String(params.offset)); | 32 | if (params.offset != null) query.set("offset", String(params.offset)); |
| 33 | + if (params.starts_after != null) query.set("starts_after", String(params.starts_after)); | ||
| 34 | + if (params.ends_after != null) query.set("ends_after", String(params.ends_after)); | ||
| 18 | const value = query.toString(); | 35 | const value = query.toString(); |
| 19 | return value ? `?${value}` : ""; | 36 | return value ? `?${value}` : ""; |
| 20 | } | 37 | } |
src/api/events/getEvents.tsmodified+11 -2
| @@ -1,8 +1,17 @@ | |||
| 1 | import { client } from "../client"; | 1 | import { client } from "../client"; |
| 2 | -import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; | 2 | +import { |
| 3 | + normalizePage, | ||
| 4 | + pageQuery, | ||
| 5 | + type Page, | ||
| 6 | + type PageParams, | ||
| 7 | + type ScheduleWindowParams, | ||
| 8 | +} from "../client"; | ||
| 3 | import type { Event } from "../client"; | 9 | import type { Event } from "../client"; |
| 4 | 10 | ||
| 5 | -export async function getEvents(params?: PageParams, signal?: AbortSignal): Promise<Page<Event>> { | 11 | +export async function getEvents( |
| 12 | + params?: PageParams & ScheduleWindowParams, | ||
| 13 | + signal?: AbortSignal | ||
| 14 | +): Promise<Page<Event>> { | ||
| 6 | const data = await client<unknown>(`/events${pageQuery(params)}`, { signal }); | 15 | const data = await client<unknown>(`/events${pageQuery(params)}`, { signal }); |
| 7 | return normalizePage<Event>(data); | 16 | return normalizePage<Event>(data); |
| 8 | } | 17 | } |
src/api/exams/getExams.tsmodified+11 -2
| @@ -1,8 +1,17 @@ | |||
| 1 | import { client } from "../client"; | 1 | import { client } from "../client"; |
| 2 | -import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; | 2 | +import { |
| 3 | + normalizePage, | ||
| 4 | + pageQuery, | ||
| 5 | + type Page, | ||
| 6 | + type PageParams, | ||
| 7 | + type ScheduleWindowParams, | ||
| 8 | +} from "../client"; | ||
| 3 | import type { Exam } from "../client"; | 9 | import type { Exam } from "../client"; |
| 4 | 10 | ||
| 5 | -export async function getExams(params?: PageParams, signal?: AbortSignal): Promise<Page<Exam>> { | 11 | +export async function getExams( |
| 12 | + params?: PageParams & ScheduleWindowParams, | ||
| 13 | + signal?: AbortSignal | ||
| 14 | +): Promise<Page<Exam>> { | ||
| 6 | const data = await client<unknown>(`/exams${pageQuery(params)}`, { signal }); | 15 | const data = await client<unknown>(`/exams${pageQuery(params)}`, { signal }); |
| 7 | return normalizePage<Exam>(data); | 16 | return normalizePage<Exam>(data); |
| 8 | } | 17 | } |
src/stores/shell-feed-context.tsxmodified+15 -9
| @@ -88,18 +88,24 @@ function createUserFeed(loggedIn: boolean): UserFeed { | |||
| 88 | { initialValue: emptyPage<Message>() } | 88 | { initialValue: emptyPage<Message>() } |
| 89 | ); | 89 | ); |
| 90 | 90 | ||
| 91 | - // No limit on events/exams: the backend has no schedule filter or ordering | 91 | + // `ends_after=now` keeps only rows whose window has not finished AND flips |
| 92 | - // param (`GET /events`, `GET /exams` take limit/offset only and serve | 92 | + // the server order to soonest-first, so a cap finally truncates the archive |
| 93 | - // ORDER BY id DESC = creation order), so any cap drops an event scheduled | 93 | + // end instead of the upcoming end. Without it the whole school history rode |
| 94 | - // for tomorrow that was created before the newest N rows. The server loads | 94 | + // along (~3.4 MB/poll at 5k+5k rows) only to be filtered out client-side. |
| 95 | - // the full list either way and only slices it afterwards. | 95 | + // |
| 96 | - // ponytail: payload grows with school history — upgrade path is a backend | 96 | + // 50: the feed drives "upcoming" badges and the calendar dot — it needs the |
| 97 | - // `starts_after`/`ends_after` query param, then cap again. | 97 | + // next handful, not the archive. 50 soonest-first rows cover a busy month |
| 98 | + // per source. Both consumers already show only not-yet-ended items, so the | ||
| 99 | + // dropped rows were never rendered. | ||
| 100 | + // ponytail: a school with >50 simultaneously-open events loses the tail — | ||
| 101 | + // upgrade path is narrowing the window (add an upper bound), not a bigger cap. | ||
| 102 | + const upcoming = () => ({ ends_after: Date.now(), limit: 50 }); | ||
| 103 | + | ||
| 98 | const [eventsRes, { refetch: refetchEvents }] = createResource( | 104 | const [eventsRes, { refetch: refetchEvents }] = createResource( |
| 99 | source, | 105 | source, |
| 100 | async () => { | 106 | async () => { |
| 101 | try { | 107 | try { |
| 102 | - return await getEvents(); | 108 | + return await getEvents(upcoming()); |
| 103 | } catch { | 109 | } catch { |
| 104 | return emptyPage<Event>(); | 110 | return emptyPage<Event>(); |
| 105 | } | 111 | } |
| @@ -111,7 +117,7 @@ function createUserFeed(loggedIn: boolean): UserFeed { | |||
| 111 | source, | 117 | source, |
| 112 | async () => { | 118 | async () => { |
| 113 | try { | 119 | try { |
| 114 | - return await getExams(); | 120 | + return await getExams(upcoming()); |
| 115 | } catch { | 121 | } catch { |
| 116 | return emptyPage<Exam>(); | 122 | return emptyPage<Exam>(); |
| 117 | } | 123 | } |