(
tableId: string,
opts?: { includeUnclaimedPreStamps?: boolean }
)
| 228 | * |
| 229 | * Hits the `(table_id, status)` partial index on table_row_executions. */ |
| 230 | export async function countRunningCells( |
| 231 | tableId: string, |
| 232 | opts?: { includeUnclaimedPreStamps?: boolean } |
| 233 | ): Promise<{ total: number; byRowId: Record<string, number> }> { |
| 234 | // `pending` + null-executionId rows are unclaimed pre-stamps. With an active |
| 235 | // dispatch they're real queued work (include); with none they're abandoned |
| 236 | // orphans that would pin the badge above zero forever (exclude). |
| 237 | const excludeOrphanPreStamps = !opts?.includeUnclaimedPreStamps |
| 238 | const rows = await db |
| 239 | .select({ |
| 240 | rowId: tableRowExecutions.rowId, |
| 241 | runningCount: sql<number>`count(*)::int`, |
| 242 | }) |
| 243 | .from(tableRowExecutions) |
| 244 | .where( |
| 245 | and( |
| 246 | eq(tableRowExecutions.tableId, tableId), |
| 247 | inArray(tableRowExecutions.status, ['queued', 'running', 'pending']), |
| 248 | excludeOrphanPreStamps |
| 249 | ? or(ne(tableRowExecutions.status, 'pending'), isNotNull(tableRowExecutions.executionId)) |
| 250 | : undefined |
| 251 | ) |
| 252 | ) |
| 253 | .groupBy(tableRowExecutions.rowId) |
| 254 | let total = 0 |
| 255 | const byRowId: Record<string, number> = {} |
| 256 | for (const r of rows) { |
| 257 | if (r.runningCount > 0) { |
| 258 | byRowId[r.rowId] = r.runningCount |
| 259 | total += r.runningCount |
| 260 | } |
| 261 | } |
| 262 | return { total, byRowId } |
| 263 | } |
| 264 | |
| 265 | /** Authoritative "cells queued or running" count for the table, derived from |
| 266 | * active dispatches so it survives reload and matches the live count. For each |
no test coverage detected