Skip to content

Commit a7c6337

Browse files
fix(table): address review — parallel dispatch counts, unfiltered rowCount
- countActiveRunCells: run the per-dispatch COUNT queries + the sidecar count in parallel instead of serially (one round-trip per dispatch). - Optimistic Run-all estimate now reads the table definition's maintained, unfiltered rowCount (detail cache) instead of the rows query's filter-scoped totalCount — the dispatcher runs every row regardless of the active filter.
1 parent 89cf52a commit a7c6337

2 files changed

Lines changed: 19 additions & 20 deletions

File tree

apps/sim/hooks/queries/tables.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -234,22 +234,16 @@ function countNewlyInFlight(before: RowExecutions, after: RowExecutions): number
234234
return n
235235
}
236236

237-
/** First-page `totalCount` from the rows infinite-query cache (or `null` when
238-
* rows haven't loaded yet). Lets a Run-all estimate the full-table cell count
239-
* even though only a page of rows is in memory. */
240-
function readTotalRowCount(
237+
/** The table's maintained, unfiltered `rowCount` from the detail cache (or
238+
* `null` when the detail hasn't loaded). This is the right scope for a Run-all
239+
* estimate: the dispatcher runs every row regardless of the active view
240+
* filter, whereas the rows query's `totalCount` is filter-scoped. */
241+
function readTableRowCount(
241242
queryClient: ReturnType<typeof useQueryClient>,
242243
tableId: string
243244
): number | null {
244-
const entries = queryClient.getQueriesData<InfiniteData<TableRowsResponse, number>>({
245-
queryKey: tableKeys.rowsRoot(tableId),
246-
exact: false,
247-
})
248-
for (const [, data] of entries) {
249-
const tc = data?.pages?.[0]?.totalCount
250-
if (typeof tc === 'number') return tc
251-
}
252-
return null
245+
const def = queryClient.getQueryData<TableDefinition>(tableKeys.detail(tableId))
246+
return typeof def?.rowCount === 'number' ? def.rowCount : null
253247
}
254248

255249
/** Optimistically reflect a run on the "X running" badge + per-row gutter Stop
@@ -1467,7 +1461,7 @@ export function useRunColumn({ workspaceId, tableId }: RowMutationContext) {
14671461
// Run-all that's the table's totalCount; for a scoped run, the rowIds.
14681462
const scopeRowCount = targetRowIds
14691463
? targetRowIds.size
1470-
: (readTotalRowCount(queryClient, tableId) ?? Object.keys(stampedByRow).length)
1464+
: (readTableRowCount(queryClient, tableId) ?? Object.keys(stampedByRow).length)
14711465
const cellCountDelta = scopeRowCount * targetGroupIds.size
14721466
const bumped = bumpRunState(queryClient, tableId, stampedByRow, cellCountDelta)
14731467
return { snapshots, runStateSnapshot: bumped?.snapshot, didBumpRunState: bumped !== null }

apps/sim/lib/table/dispatcher.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,11 @@ export async function countActiveRunCells(
208208
dispatches?: DispatchRow[]
209209
): Promise<{ total: number; byRowId: Record<string, number> }> {
210210
const active = dispatches ?? (await listActiveDispatches(tableId))
211-
const sidecar = await countRunningCells(tableId)
212-
if (active.length === 0) return sidecar
211+
if (active.length === 0) return countRunningCells(tableId)
213212

214-
let total = 0
215-
for (const d of active) {
213+
const countRowsAhead = async (d: DispatchRow): Promise<number> => {
216214
const groupCount = d.scope.groupIds.length
217-
if (groupCount === 0) continue
215+
if (groupCount === 0) return 0
218216
const filters = [eq(userTableRows.tableId, tableId), gt(userTableRows.position, d.cursor)]
219217
if (d.scope.rowIds && d.scope.rowIds.length > 0) {
220218
filters.push(inArray(userTableRows.id, d.scope.rowIds))
@@ -223,8 +221,15 @@ export async function countActiveRunCells(
223221
.select({ rowsAhead: sql<number>`count(*)::int` })
224222
.from(userTableRows)
225223
.where(and(...filters))
226-
total += (row?.rowsAhead ?? 0) * groupCount
224+
return (row?.rowsAhead ?? 0) * groupCount
227225
}
226+
227+
// One round-trip per dispatch + the sidecar count, all in parallel.
228+
const [sidecar, perDispatch] = await Promise.all([
229+
countRunningCells(tableId),
230+
Promise.all(active.map(countRowsAhead)),
231+
])
232+
const total = perDispatch.reduce((sum, n) => sum + n, 0)
228233
return { total, byRowId: sidecar.byRowId }
229234
}
230235

0 commit comments

Comments
 (0)