@@ -4,6 +4,7 @@ import { createLogger } from '@sim/logger'
44import { type SQL , sql } from 'drizzle-orm'
55import {
66 ceilHour ,
7+ cellRuns ,
78 floorHour ,
89 HOUR_MS ,
910 type InstantRange ,
@@ -120,18 +121,24 @@ function storedCells(scope: ActivityScope, span: InstantRange): SQL {
120121}
121122
122123/**
123- * The rollup hours of the scope's workspaces that have a pending dirty mark, as `h`: their cells
124- * are known to be stale until the mark is processed (or while it backs off after failing), so
125- * reads take those hours live. Read per workspace on the mark queue's key, in the read's snapshot.
124+ * The rollup cells of the scope's workspaces that have a pending dirty mark, as
125+ * `(workspace_id, h)`: known to be stale until the mark is processed (or while it backs off after
126+ * failing), so reads take exactly those workspace-hours live and every other workspace's stored
127+ * cell for the hour still serves. Read on the mark queue's key, in the read's snapshot.
126128 */
127- function markedHours ( scope : ActivityScope , span : InstantRange ) : SQL {
128- return sql `SELECT DISTINCT d.bucket_start AS h FROM ${ workspace } w
129+ function markedCells ( scope : ActivityScope , span : InstantRange ) : SQL {
130+ return sql `SELECT DISTINCT d.workspace_id, d. bucket_start AS h FROM ${ workspace } w
129131 JOIN ${ activityRollupDirty } d ON d.workspace_id = w.id
130132 AND d.bucket_start >= ${ utcTimestamp ( span . from ) } AND d.bucket_start < ${ utcTimestamp ( span . to ) }
131133 WHERE w.organization_id = ${ scope . organizationId }
132134 ${ scope . workspaceId ? sql `AND w.id = ${ scope . workspaceId } ` : sql `` } `
133135}
134136
137+ /** A stored cell `r` of workspace `w` whose workspace-hour is not among `marked` cells `m`. */
138+ function notMarked ( marked : SQL ) : SQL {
139+ return sql `NOT EXISTS (SELECT 1 FROM (${ marked } ) m WHERE m.workspace_id = w.id AND m.h = r.bucket_start)`
140+ }
141+
135142type DayRow = Record < string , unknown > & {
136143 day : string
137144 runs : string | number
@@ -145,7 +152,7 @@ type DayRow = Record<string, unknown> & {
145152/**
146153 * Workflow activity per local day of the viewer. Stored hours are summed by the day they fall
147154 * on; an hour that crosses a local midnight, or a change of UTC offset, belongs to two days, so it
148- * is read live with the edges instead. Both are found with the database's time zone rules, the
155+ * is read live with the edges instead, as is each marked cell (see { @link markedCells}) . Both are found with the database's time zone rules, the
149156 * ones the legacy reads use. Only zones off the whole hour have such hours.
150157 */
151158async function readWorkflowDays (
@@ -163,11 +170,13 @@ async function readWorkflowDays(
163170 ? sql `SELECT h FROM generate_series(${ utcTimestamp ( span . from ) } ,
164171 ${ utcTimestamp ( new Date ( span . to . getTime ( ) - HOUR_MS ) ) } , interval '1 hour') AS h
165172 WHERE date_trunc('day', ${ local ( sql `h` ) } ) <> date_trunc('day', ${ local ( last ) } )
166- OR ${ local ( sql `h` ) } - h <> ${ local ( last ) } - ${ last }
167- UNION ${ markedHours ( scope , span ) } `
173+ OR ${ local ( sql `h` ) } - h <> ${ local ( last ) } - ${ last } `
168174 : sql `SELECT NULL::timestamp AS h WHERE false`
169175 const live = sql `SELECT ${ day ( sql `a.started_at` ) } AS day, ${ workflowRunCounters }
170- FROM (${ organizationWorkflowRuns ( scope , sql `SELECT * FROM live_ranges` ) } ) a
176+ FROM (
177+ ${ organizationWorkflowRuns ( scope , sql `SELECT * FROM live_ranges` ) }
178+ UNION ALL ${ cellRuns ( sql `SELECT * FROM marked` ) }
179+ ) a
171180 GROUP BY 1`
172181 const stored = span
173182 ? sql `SELECT ${ day ( sql `r.bucket_start` ) } AS day, sum(r.runs) AS runs,
@@ -176,12 +185,16 @@ async function readWorkflowDays(
176185 sum(r.duration_count) AS duration_count
177186 FROM ${ storedCells ( scope , span ) }
178187 AND NOT EXISTS (SELECT 1 FROM straddles s WHERE s.h = r.bucket_start)
188+ AND ${ notMarked ( sql `SELECT * FROM marked` ) }
179189 GROUP BY 1`
180190 : null
181191 const rows = await tx . execute < DayRow > ( sql `
182192 WITH straddles AS (${ straddles } ), live_ranges AS (
183193 ${ rangeRows ( plan . live ) }
184194 UNION ALL SELECT h, h + interval '1 hour' FROM straddles
195+ ), marked AS (
196+ SELECT c.* FROM (${ span ? markedCells ( scope , span ) : sql `SELECT NULL::text AS workspace_id, NULL::timestamp AS h WHERE false` } ) c
197+ WHERE NOT EXISTS (SELECT 1 FROM straddles s WHERE s.h = c.h)
185198 )
186199 ${ stored ? sql `${ stored } UNION ALL ${ live } ` : live }
187200 ` )
@@ -244,7 +257,7 @@ async function timedChatDays(
244257
245258/**
246259 * Workflow runs of the window as breakdown cells: stored cells summed over the rollup span, and
247- * the live edges and marked hours (see {@link markedHours }), each keyed by workspace, workflow, and
260+ * the live edges and marked cells (see {@link markedCells }), each keyed by workspace, workflow, and
248261 * trigger. A stored cell's workflow is looked up now, so one deleted since the cell was computed
249262 * reads as deleted, as its runs do.
250263 */
@@ -254,10 +267,11 @@ function workflowCells(scope: ActivityScope, plan: RollupReadPlan, resolveWorkfl
254267 FROM (${ organizationWorkflowRuns ( scope , ranges ) } ) a
255268 GROUP BY 1, 2, 3`
256269 if ( ! plan . rollup ) return liveCells ( rangeRows ( plan . live ) )
257- const marked = markedHours ( scope , plan . rollup )
258- const live = liveCells (
259- sql `${ rangeRows ( plan . live ) } UNION ALL SELECT m.h, m.h + interval '1 hour' FROM (${ marked } ) m`
260- )
270+ const marked = markedCells ( scope , plan . rollup )
271+ const live = sql `${ liveCells ( rangeRows ( plan . live ) ) }
272+ UNION ALL SELECT a.workspace_id, a.workflow_id, a.trigger, ${ workflowRunCounters }
273+ FROM (${ cellRuns ( marked ) } ) a
274+ GROUP BY 1, 2, 3`
261275 const workflowId = resolveWorkflow
262276 ? sql `(SELECT f.id FROM ${ workflow } f WHERE f.id = nullif(r.workflow_id, ''))`
263277 : sql `NULL::text`
@@ -270,7 +284,7 @@ function workflowCells(scope: ActivityScope, plan: RollupReadPlan, resolveWorkfl
270284 sum(r.in_flight) AS in_flight, sum(r.duration_sum_ms) AS duration_sum_ms,
271285 sum(r.duration_count) AS duration_count
272286 FROM ${ storedCells ( scope , plan . rollup ) }
273- AND NOT EXISTS (SELECT 1 FROM ( ${ marked } ) m WHERE m.h = r.bucket_start)
287+ AND ${ notMarked ( marked ) }
274288 GROUP BY 1, 2, 3
275289 ) r
276290 UNION ALL ${ live } `
0 commit comments