fix(grid): reconcile an exact total when a page lands beyond it

Closes #10968
This commit is contained in:
弈秋 authored and GitHub committed 2026-10-05 23:57:50 +08:00
1 parent 7c28b6789e
commit f8b7ffe516
4 files changed
+92 -1

No files matched your search

@@ -230,6 +230,7 @@ import {
ELASTICSEARCH_PAGE_JUMP_WARNING_REQUESTS,
elasticsearchCursorPageJumpRequestCount,
hasCompleteLocalDataGridResult,
reconcileDataGridExactTotalWithObservedPage,
resolveDataGridPaginationTotal,
showDataGridRerunTotalCountAction,
type DataGridInexactTotalRowCountMode,
@@ -3462,6 +3463,22 @@ const serverKnownTotalRowCount = computed(() => (typeof manualTotalRowCount.valu
const displayedTotalRowCount = computed(() => serverKnownTotalRowCount.value ?? inferredBackendTotalRowCount.value);
const totalRowCountIsExact = computed(() => typeof manualTotalRowCount.value === "number" || props.totalRowCountIsExact !== false);
const totalRowCountLabelKey = computed(() => dataGridTotalRowCountLabelKey(totalRowCountIsExact.value, props.inexactTotalRowCountMode));
// The COUNT behind an exact total and the query serving a page are two
// separate snapshots: rows can land in between (a table being written to), and
// an agent result session serves the snapshot it was opened with. A page that
// lands with rows past the exact total must not render row indexes beyond the
// claimed end of the result (#10968) — adopt the observed extent instead.
watch(
() => [props.loading, props.pageOffset, props.result.rows.length, props.result.appended_from_row_count] as const,
([loading, offset, rowCount, appendedFromRowCount]) => {
if (loading || isInfiniteScrollPaginating.value || appendedFromRowCount !== undefined) return;
const exactTotal = serverKnownTotalRowCount.value;
if (!totalRowCountIsExact.value || typeof offset !== "number" || typeof exactTotal !== "number") return;
const reconciled = reconcileDataGridExactTotalWithObservedPage({ offset, rowCount, exactTotal });
if (reconciled !== undefined) manualTotalRowCount.value = reconciled;
},
{ flush: "post" },
);
// A backend can expose an exact display total while deliberately restricting
// offset pagination to a smaller safe range.
const paginationTotalRowCount = computed(() =>
@@ -117,6 +117,22 @@ describe("DataGrid manual count freshness", () => {
expect(totalText(host)).toContain("250");
});
it("reconciles a stale exact total when a page lands beyond it (#10968)", async () => {
const { host, state } = mountGrid();
await settle();
clickButton(host, String(i18n.global.t("grid.calculateTotalRowsInline")));
await settle();
expect(totalText(host)).toContain("250");
// The COUNT said 250, but the served snapshot holds more: the counted last
// page (offset 200, page size 100) comes back full, so row indexes reach
// 300 — adopt the observed extent instead of showing rows past the total.
state.pageOffset = 200;
state.result = pageResult();
await settle();
expect(totalText(host)).toContain("300");
});
it("does not let an old manual count override a refreshed automatic count", async () => {
let resolveCount!: (total: number) => void;
const countTotalRows = vi.fn(() => new Promise<number>((resolve) => (resolveCount = resolve)));
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { ELASTICSEARCH_PAGE_JUMP_WARNING_REQUESTS, elasticsearchCursorPageJumpRequestCount, hasCompleteLocalDataGridResult } from "@/lib/dataGrid/dataGridPagination";
import { ELASTICSEARCH_PAGE_JUMP_WARNING_REQUESTS, elasticsearchCursorPageJumpRequestCount, hasCompleteLocalDataGridResult, reconcileDataGridExactTotalWithObservedPage } from "@/lib/dataGrid/dataGridPagination";
describe("Elasticsearch cursor page jumps", () => {
it("counts only missing forward cursors", () => {
@@ -54,3 +54,33 @@ describe("complete local DataGrid results", () => {
).toBe(false);
});
});
describe("reconciling an exact total with an observed page (#10968)", () => {
it("adopts the observed extent when a page lands past the counted total", () => {
// Issue screenshot: COUNT said 2892, but the counted last page (offset 2800,
// 200 rows) came back full from a snapshot holding more rows.
expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: 200, exactTotal: 2892 })).toBe(3000);
});
it("keeps the counted total for pages that fit inside it", () => {
expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: 92, exactTotal: 2892 })).toBeUndefined();
expect(reconcileDataGridExactTotalWithObservedPage({ offset: 0, rowCount: 200, exactTotal: 2892 })).toBeUndefined();
expect(reconcileDataGridExactTotalWithObservedPage({ offset: 400, rowCount: 200, exactTotal: 2892 })).toBeUndefined();
});
it("keeps the counted total when the page comes back short at its end", () => {
expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: 92, exactTotal: 2892 })).toBeUndefined();
expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: 0, exactTotal: 2892 })).toBeUndefined();
});
it("extends a zero or missing total as soon as rows are on screen", () => {
expect(reconcileDataGridExactTotalWithObservedPage({ offset: 0, rowCount: 5, exactTotal: 0 })).toBe(5);
});
it("ignores malformed offsets, rows, and totals", () => {
expect(reconcileDataGridExactTotalWithObservedPage({ offset: -1, rowCount: 200, exactTotal: 2892 })).toBeUndefined();
expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: -1, exactTotal: 2892 })).toBeUndefined();
expect(reconcileDataGridExactTotalWithObservedPage({ offset: Number.NaN, rowCount: 200, exactTotal: 2892 })).toBeUndefined();
expect(reconcileDataGridExactTotalWithObservedPage({ offset: 2800, rowCount: 200, exactTotal: Number.NaN })).toBeUndefined();
});
});
@@ -73,6 +73,34 @@ export function resolveDataGridPaginationTotal(options: { paginationTotalRowCoun
return Math.min(total, options.maxRows);
}
export interface ReconcileDataGridExactTotalOptions {
offset: number;
rowCount: number;
exactTotal: number;
}
/**
* Total to display after a page landed with rows beyond the exact counted
* total, or `undefined` when the counted total still describes the page.
*
* The COUNT that produced the exact total and the query that served the page
* are two separate snapshots: rows can land between them (a table being
* written to), and an agent result session serves the snapshot it was opened
* with even after newer rows were counted. Rendering that page against the
* stale total shows row indexes past the claimed end of the result (#10968) —
* adopt the observed extent instead, so the displayed total always covers
* every row the grid actually shows.
*/
export function reconcileDataGridExactTotalWithObservedPage(options: ReconcileDataGridExactTotalOptions): number | undefined {
const { offset, rowCount, exactTotal } = options;
if (!Number.isSafeInteger(offset) || offset < 0) return undefined;
if (!Number.isSafeInteger(rowCount) || rowCount <= 0) return undefined;
if (!Number.isSafeInteger(exactTotal) || exactTotal < 0) return undefined;
const observedExtent = offset + rowCount;
if (observedExtent <= exactTotal) return undefined;
return observedExtent;
}
export function hasCompleteLocalDataGridResult(options: CompleteLocalDataGridResultOptions): boolean {
if (!options.isResultsContext || options.truncated === true || options.hasMore === true) return false;
if (options.pageLimit === undefined) return true;