Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

perf(issues): fetch the issue list's first page in one request (#9088)

Ray Walker4d526f51402c
diff --git a/packages/core/issues/queries.test.ts b/packages/core/issues/queries.test.ts
index 92470aa575..1f7ef40c10 100644
--- a/packages/core/issues/queries.test.ts
+++ b/packages/core/issues/queries.test.ts
@@ -1,5 +1,5 @@
// @vitest-environment node
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from "vitest";
import { QueryClient, QueryObserver } from "@tanstack/react-query";
import { setApiInstance } from "../api";
@@ -8,21 +8,27 @@ import type {
Issue,
IssueTableRowsRequest,
IssueTableRowsResponse,
+ ListIssuesCache,
ListIssuesParams,
ListIssuesResponse,
} from "../types";
+import { pruneDeletedIssueFromListCaches } from "./delete-cache";
import {
CHILDREN_BY_PARENTS_CHUNK_SIZE,
+ ISSUE_PAGE_SIZE,
PROJECT_GANTT_MAX_ISSUES,
PROJECT_GANTT_PAGE_LIMIT,
childrenByParentsOptions,
childIssuesOptions,
+ flattenIssueBuckets,
issueIdentifierOptions,
issueKeys,
+ issueListOptions,
issueTableRowPageOptions,
projectGanttIssuesOptions,
sourceContextPreviewOptions,
} from "./queries";
+import { onIssueUpdated } from "./ws-updaters";
const WS_ID = "ws-1";
const PROJECT_ID = "project-1";
@@ -315,6 +321,84 @@ describe("issueTableRowPageOptions", () => {
});
});
+describe("issueListOptions", () => {
+ const todoIssue = makeIssue(1, { status: "todo", status_category: "unstarted" });
+ const startedIssue = makeIssue(2, { status: "in_progress", status_category: "started" });
+ const doneIssue = makeIssue(3, { status: "done", status_category: "done" });
+ // A custom status the server did not resolve to a category.
+ const customIssue = makeIssue(4, { status: "qa" });
+
+ let qc: QueryClient;
+ let listIssues: Mock<(params?: ListIssuesParams) => Promise<ListIssuesResponse>>;
+
+ beforeEach(() => {
+ qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
+ // Like the real client, the fake ignores `status_category`: every call
+ // returns the same first page.
+ listIssues = vi.fn<(params?: ListIssuesParams) => Promise<ListIssuesResponse>>().mockResolvedValue({
+ issues: [todoIssue, startedIssue, doneIssue, customIssue],
+ total: 120,
+ });
+ installFakeApi(listIssues);
+ });
+
+ afterEach(() => {
+ qc.clear();
+ });
+
+ async function fetchList() {
+ const options = issueListOptions(WS_ID);
+ await qc.fetchQuery(options);
+ return () => qc.getQueryData<ListIssuesCache>(options.queryKey)!;
+ }
+
+ it("sends one list request per fetch", async () => {
+ await fetchList();
+
+ expect(listIssues).toHaveBeenCalledTimes(1);
+ expect(listIssues).toHaveBeenCalledWith({ limit: ISSUE_PAGE_SIZE, offset: 0 });
+ });
+
+ it("puts each issue in its own category's bucket only", async () => {
+ const cache = (await fetchList())();
+
+ expect(cache.byStatus).toEqual({
+ unstarted: { issues: [todoIssue, customIssue], total: 2 },
+ started: { issues: [startedIssue], total: 1 },
+ done: { issues: [doneIssue], total: 1 },
+ closed: { issues: [], total: 0 },
+ });
+ expect(flattenIssueBuckets(cache).map((issue) => issue.id)).toEqual([
+ todoIssue.id,
+ customIssue.id,
+ startedIssue.id,
+ doneIssue.id,
+ ]);
+ });
+
+ it("drops a deleted issue from every bucket", async () => {
+ const readCache = await fetchList();
+
+ pruneDeletedIssueFromListCaches(qc, WS_ID, todoIssue.id);
+
+ expect(flattenIssueBuckets(readCache()).map((issue) => issue.id)).not.toContain(todoIssue.id);
+ });
+
+ it("keeps only the updated copy of an issue moved to done", async () => {
+ const readCache = await fetchList();
+
+ onIssueUpdated(
+ qc,
+ WS_ID,
+ { id: todoIssue.id, status: "done", status_category: "done" },
+ { statusChanged: true },
+ );
+
+ const copies = flattenIssueBuckets(readCache()).filter((issue) => issue.id === todoIssue.id);
+ expect(copies).toEqual([{ ...todoIssue, status: "done", status_category: "done" }]);
+ });
+});
+
describe("projectGanttIssuesOptions", () => {
let qc: QueryClient;
diff --git a/packages/core/issues/queries.ts b/packages/core/issues/queries.ts
index b7c968abd2..af3665a8f2 100644
--- a/packages/core/issues/queries.ts
+++ b/packages/core/issues/queries.ts
@@ -19,6 +19,7 @@ import type {
ListIssuesCache,
} from "../types";
import { ALL_STATUSES } from "./config";
+import { issueColumnCategory } from "./status-category";
export function issueTasksOptions(issueId: string) {
return queryOptions({
@@ -242,7 +243,7 @@ export type AssigneeGroupedIssuesFilter = Omit<
"group_by" | "limit" | "offset" | "group_assignee_type" | "group_assignee_id"
>;
-/** Page size per status column. */
+/** Size of the issue list's first page, all categories together. */
export const ISSUE_PAGE_SIZE = 50;
/**
@@ -267,17 +268,20 @@ export function flattenIssueBuckets(data: ListIssuesCache) {
return out;
}
+/**
+ * One request for the list's first page, grouped into the category buckets
+ * here. Every reader of this cache looks an issue up by id and none pages a
+ * bucket, so one shared window serves them all. Each bucket's `total` is its
+ * own row count. A custom status the server did not resolve falls back to
+ * `issueColumnCategory`'s bucket, so no row is dropped.
+ */
async function fetchFirstPages(filter: MyIssuesFilter = {}, sort?: IssueSortParam): Promise<ListIssuesCache> {
- const responses = await Promise.all(
- PAGINATED_CATEGORIES.map((category) =>
- api.listIssues({ status_category: category, limit: ISSUE_PAGE_SIZE, offset: 0, ...sort, ...filter }),
- ),
- );
+ const res = await api.listIssues({ limit: ISSUE_PAGE_SIZE, offset: 0, ...sort, ...filter });
const byStatus: ListIssuesCache["byStatus"] = {};
- PAGINATED_CATEGORIES.forEach((status: IssueStatusCategory, i: number) => {
- const res = responses[i]!;
- byStatus[status] = { issues: res.issues, total: res.total };
- });
+ for (const category of PAGINATED_CATEGORIES) {
+ const issues = res.issues.filter((issue) => issueColumnCategory(issue) === category);
+ byStatus[category] = { issues, total: issues.length };
+ }
return { byStatus };
}
@@ -361,7 +365,7 @@ export function issueTableFacetsOptions(
* `Issue[]` for consumers. Mutations and ws-updaters must use
* `setQueryData<ListIssuesCache>(...)` and preserve the byStatus shape.
*
- * Fetches the first page of each paginated status in parallel.
+ * Fetches the first page in one request and buckets it by category.
*/
export function issueListOptions(wsId: string, sort?: IssueSortParam) {
return queryOptions({
diff --git a/packages/core/types/api.ts b/packages/core/types/api.ts
index 8fb02b9f99..50b07951bd 100644
--- a/packages/core/types/api.ts
+++ b/packages/core/types/api.ts
@@ -148,7 +148,8 @@ export interface ListIssuesParams {
/**
* Filter by lifecycle category rather than by exact key, so one bucket holds
* all concrete and custom statuses in that phase. Task views use exact
- * status keys for their columns instead.
+ * status keys for their columns instead. The web client's
+ * `ApiClient.listIssues` does not send it.
*/
status_category?: IssueStatusCategory;
/** Multi-value form of `status_category`. OR within the field. */
@@ -512,16 +513,16 @@ export interface WorkingAgentSummary {
running_task_count: number;
}
-/** Per-status bucket in the paginated issue cache. `total` is the server count (all pages), not the length of `issues`. */
+/** Per-category bucket in the issue list cache. The list's fetch sets `total` to the bucket's row count. */
export interface IssueStatusBucket {
issues: Issue[];
total: number;
}
/**
- * Frontend cache shape for the issue list. Data is bucketed by status so
- * each column can paginate independently. Assembled from per-status
- * `api.listIssues` responses by the query functions in `issues/queries.ts`.
+ * Frontend cache shape for the issue list. Data is bucketed by status
+ * category. Assembled from one `api.listIssues` response by the query
+ * functions in `issues/queries.ts`.
*/
export interface ListIssuesCache {
/** Bucketed by status CATEGORY — see PAGINATED_CATEGORIES. (MUL-6243) */
diff --git a/packages/views/issues/components/issue-chip.tsx b/packages/views/issues/components/issue-chip.tsx
index 0427e71b77..9b808778b6 100644
--- a/packages/views/issues/components/issue-chip.tsx
+++ b/packages/views/issues/components/issue-chip.tsx
@@ -62,7 +62,8 @@ export function IssueChip({
const { data: issues = [] } = useQuery(issueListOptions(wsId));
const listIssue = issues.find((i) => i.id === issueId);
- // Fallback fetch for issues outside the first page of the list (e.g. Done).
+ // Fallback fetch for issues outside the list's first page (the first
+ // ISSUE_PAGE_SIZE issues across all categories).
const { data: detailIssue } = useQuery({
...issueDetailOptions(wsId, issueId),
enabled: !listIssue,