forge
Explore
Log in

Don't you dare go Hollow

🔭 ask the shell feed for the next rows, not every row

Browse files at this commit

5d93c16076f8b5d48de8ee691ca012a5b03e1979Ahmet Kaan GümüşParent: e607b1e
  • feat
    • 🔭 ask the shell feed for the next rows, not every row
Show full message
feat: 🔭 ask the shell feed for the next rows, not every row

The feed polls events and exams for the upcoming badge and the calendar
dot, and until now `limit` truncated the newest-created end — an event
scheduled for tomorrow could sit behind a hundred rows created after it.
So the feed asked for everything: ~3.37 MB a poll at 5000 rows apiece,
re-fetched on every right-nav tab open.

The backend now takes `ends_after`, which keeps the rows whose window has
not finished and flips the order to soonest-first, so a limit finally cuts
the right end. 50 per source is about the next month at a busy school and
roughly 15 KB.

Both consumers re-filter against the live clock and sort for themselves,
so the order flip reaches neither of them.
src/api/__tests__/client/page.test.tsmodified+13 -0
@@ -13,6 +13,19 @@ describe("page helpers", () => {
1313 expect(pageQuery({ offset: 20 })).toBe("?offset=20");
1414 expect(pageQuery({ limit: 10, offset: 20 })).toBe("?limit=10&offset=20");
1515 });
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+ });
1629 });
1730
1831 describe("appendPageParams", () => {
src/api/__tests__/events/events.test.tsmodified+19 -0
@@ -29,6 +29,25 @@ describe("events API", () => {
2929 expect(init?.method).toBe("GET");
3030 });
3131
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+
3251 it("getEventById calls /events/:id", async () => {
3352 const mockEvent = { id: "e1", title: "Event" };
3453 mockFetchSuccess(mockEvent);
src/api/__tests__/exams/exams.test.tsmodified+19 -0
@@ -27,6 +27,25 @@ describe("exams API - core", () => {
2727 expect(init?.method).toBe("GET");
2828 });
2929
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+
3049 it("getExamChoiceImageBlob calls /exams/:id/questions/:qid/choices/:choiceId/image", async () => {
3150 mockFetchBlob(new Blob(["img"], { type: "image/png" }));
3251
src/api/client/page.tsmodified+18 -1
@@ -10,11 +10,28 @@ export type Page<T> = {
1010 offset: number;
1111 };
1212
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 {
1427 if (!params) return "";
1528 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=`.
1631 if (params.limit != null) query.set("limit", String(params.limit));
1732 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));
1835 const value = query.toString();
1936 return value ? `?${value}` : "";
2037 }
src/api/events/getEvents.tsmodified+11 -2
@@ -1,8 +1,17 @@
11 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";
39 import type { Event } from "../client";
410
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>> {
615 const data = await client<unknown>(`/events${pageQuery(params)}`, { signal });
716 return normalizePage<Event>(data);
817 }
src/api/exams/getExams.tsmodified+11 -2
@@ -1,8 +1,17 @@
11 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";
39 import type { Exam } from "../client";
410
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>> {
615 const data = await client<unknown>(`/exams${pageQuery(params)}`, { signal });
716 return normalizePage<Exam>(data);
817 }
src/stores/shell-feed-context.tsxmodified+15 -9
@@ -88,18 +88,24 @@ function createUserFeed(loggedIn: boolean): UserFeed {
8888 { initialValue: emptyPage<Message>() }
8989 );
9090
91- // No limit on events/exams: the backend has no schedule filter or ordering
92- // param (`GET /events`, `GET /exams` take limit/offset only and serve
93- // ORDER BY id DESC = creation order), so any cap drops an event scheduled
94- // for tomorrow that was created before the newest N rows. The server loads
95- // the full list either way and only slices it afterwards.
96- // ponytail: payload grows with school history — upgrade path is a backend
97- // `starts_after`/`ends_after` query param, then cap again.
91+ // `ends_after=now` keeps only rows whose window has not finished AND flips
92+ // the server order to soonest-first, so a cap finally truncates the archive
93+ // end instead of the upcoming end. Without it the whole school history rode
94+ // along (~3.4 MB/poll at 5k+5k rows) only to be filtered out client-side.
95+ //
96+ // 50: the feed drives "upcoming" badges and the calendar dot — it needs the
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+
98104 const [eventsRes, { refetch: refetchEvents }] = createResource(
99105 source,
100106 async () => {
101107 try {
102- return await getEvents();
108+ return await getEvents(upcoming());
103109 } catch {
104110 return emptyPage<Event>();
105111 }
@@ -111,7 +117,7 @@ function createUserFeed(loggedIn: boolean): UserFeed {
111117 source,
112118 async () => {
113119 try {
114- return await getExams();
120+ return await getExams(upcoming());
115121 } catch {
116122 return emptyPage<Exam>();
117123 }