Files
fusion/packages/core/src/productivity-analytics.ts
gsxdsm c15c78feeb feat: migrate storage from SQLite to PostgreSQL (#1793)
# Migrate storage from SQLite to PostgreSQL — full dashboard cutover

Migrates Fusion's storage layer to the embedded PostgreSQL
`AsyncDataLayer` (the default backend) and **completes the
satellite-store + feature cutover** so every dashboard and Command
Center surface works in PG mode.

## Status — every surface works in embedded-PG mode

Verified live against a running embedded-Postgres dashboard (all
**200**, zero 5xx) and gate-tested (**23 files / 99 tests** on embedded
PG, plus engine-core 294 and ci-shape 63 in the blocking merge gate;
core/engine/cli/dashboard typecheck clean).

| Area | Surfaces | State |
|---|---|---|
| Satellite stores | workflows, todos, insights, research, missions,
goals, mailbox | ✅ |
| Views | artifacts, documents, evals | ✅ |
| Command Center | activity, productivity, team, tokens, tools,
**workflows**, **github**, **signals**, **plugin-activations**, **live**
(all 10) | ✅ |
| Run execution | insight generation, research run execution | ✅
(store-path; AI step needs a provider) |
| Live updates | SSE push for mission/research/insight events | ✅ |
| Workflow editing | create / update / delete / select (+ id counter) |
✅ |
| Engine | mission autopilot, incident-signal ingestion, regression
storm-guard, agent wake-on-message | ✅ |
| Core | tasks, agents, secrets, automations, memory, chat, usage, PRs,
git | ✅ |

## Approach

Each satellite store gets an `Async<Store>` wrapper exposing the sync
store's method names over the existing `async-*-store.ts` helpers;
`get<Store>Store()` returns a `Sync | Async` union; consumers `await`
(harmless on sync), and engine/CLI paths that can't convert use
`instanceof Sync` graceful fallback. Analytics aggregators branch on
`"ping" in dbOrLayer` to run schema-qualified raw SQL over `project.*`
(snake_case) in PG. Executors/orchestrators/autopilot are
await-converted to drive the union store; the async store wrappers
extend `EventEmitter` so SSE live-push fires in both backends.

Not-yet-ported capabilities degrade gracefully (never 500) and are
individually called out in commits.

## Sync with main

The branch is kept continuously merged with `main` (currently through
FN-7845, 2026-07-12); the earlier "final rebase deferred" note no longer
applies. Use **Create a merge commit** (or squash) to land it — GitHub's
rebase-merge cannot replay a merge-maintained branch.

## Residual Review Findings

Multi-agent code review of the PostgreSQL satellite-store ports (U1–U5)
applied 3 safe fixes (see `fix(review): apply autofix feedback`). The
following are **real but gated** — recorded here as follow-up work
rather than auto-applied. All are SQLite→PostgreSQL
**concurrency/atomicity regressions**: the sync stores were immune only
by SQLite's single-writer, single-threaded-handler execution; the async
ports open multi-await read-modify-write windows. **Reachability is low
today** because the execution engines that generate concurrent same-run
mutations (insight run executor, research orchestrator/dispatcher) are
`instanceof`-gated to sync mode in PG. No process-crash class survived
(all engine fallbacks correctly guard the sync store).

- **[P1] Research `appendResearchEvent` dual-write is non-atomic**
(`packages/core/src/async-research-store.ts`, corroborated: adversarial
+ reliability). The `research_run_events` insert (own transaction) and
the `run.events` jsonb update are separate writes — a crash between
them, or two concurrent appends, splits the table count from the jsonb
array. **Fix:** perform the seq-insert and the jsonb update in one
`layer.transactionImmediate`.
- **[P1] Research run terminal-reversion via stale full-row persist**
(`async-research-store.ts` `persistResearchRun`/`updateResearchStatus`).
Concurrent `PATCH /runs/:id/status` + `POST /runs/:id/events` can revert
a terminal run to `running` by overwriting the whole row, bypassing the
transition guard. **Fix:** scoped column `UPDATE`s with a `WHERE status
…` guard, or optimistic version column.
- **[P2] `updateResearchRun`/`updateInsightRun` read-then-write TOCTOU**
— concurrent PATCHes last-writer-wins on the lifecycle merge. **Fix:**
`SELECT … FOR UPDATE` / enclosing transaction.
- **[P2] `upsertRun`/`createRunOrThrowConflict` check-then-create race**
(`async-insight-store.ts`) — two callers can each create an "active"
run. **Fix:** partial unique index on `(projectId, trigger) WHERE status
IN ('pending','running')`.
- **[P3] `createResearchRetryRun` return-value divergence** — sync
returns the pre-update `queued` snapshot; async returns the reloaded
`retry_waiting` run (persisted state is identical). Pick one side for
cross-backend parity.
- **[P2/perf] Mission `getMissionWithHierarchy`/`getMissionHealth` N+1
fan-out** — O(milestones×slices) sequential round-trips hold one pool
slot per request; can starve the pool for large hierarchies. **Fix:**
batched/joined reads.
- **Testing gaps:** no PG-mode concurrency tests (interleaved
status/event mutations), no sync↔async parity assertion for the
lifecycle-error codes, and no mission status/health rollup parity test
vs the sync `MissionStore`.

~~Out of scope (deferred): AI run *execution* (insight/research) +
mission autopilot + live SSE mission events remain sync-gated/degraded
in PG mode.~~ **Since ported** — insight/research run execution, mission
autopilot, and SSE live push all run on the async layer now, which also
makes the concurrency findings above genuinely reachable; they remain
open follow-ups.







---

## Update — 2026-07-12: production-readiness hardening & live acceptance

Everything below landed on this branch since the description above was
written:

**Production blockers from review — fixed**
- `recoverStaleTransitionPending` ported to the async layer (backend
moves write + clear the crash-safe marker; startup/maintenance sweeps no
longer throw).
- Lost-update class fixed: `atomicWriteTaskJson`/`WithAudit` write
changed columns only (full-row upserts silently resurrected stale fields
across concurrent store instances — the "task stuck unplanned forever"
bug).
- First-boot **auto-migration**: booting the PG backend over a project
with a legacy `fusion.db` migrates it automatically (loud failure,
SQLite kept as backup), and the dashboard shows a one-time **"your data
was migrated" banner** with the backup paths and a Need-help Discord
link.
- `pg_dump`/`pg_restore` discovered from common install locations for
embedded-mode backups.
- The PG suite is part of the blocking merge gate (`test:pg-gate`).

**Multi-project isolation (PR #2007, merged into this branch)**
- `project_id` partition key on tasks / archived tasks / config,
`taskProjectScope` threaded through every scan/claim/count, per-project
config rows, layer bound to the project at startup.
- Review P1 follow-up: the shared cold-storage `archive.archived_tasks`
table is also partitioned and all archived-board reads/counts/searches
are scoped.
- Schema drift self-heal generalized to schema-qualified columns so
existing databases upgrade in place.

**Other changes**
- Node settings sync **removed** in PG mode (409
`settings-sync-disabled-postgres`) — nodes share state by connecting to
the same database; auth sync kept (per-machine file).
- Perf (review findings): `listTasks` pushes column filter + ORDER BY +
LIMIT/OFFSET into SQL; `getConversation` capped to the most recent 200
messages.
- Fixed a false "operator action required" pause-abort log fired on
every successfully auto-merged task.

**Live acceptance — PASSED (2026-07-12)**
A sandboxed instance (isolated HOME, embedded PG, real Opus executor)
ran a task through the complete cycle: create → triage (AI spec) →
execute → in-review → AI squash-merge landed on the project's `main` →
done. A write+read sweep of every data surface (settings, comments,
documents, attachments + artifact bridge + artifact edit, chat with real
generation, goals, missions, agent mail, secrets, workflows, memory, CC
analytics) was green on embedded PG.

**Known remaining work**
- The per-project `config` PK re-key has no upgrade path for
pre-isolation embedded-PG databases (needs a real `DROP
CONSTRAINT`/re-key migration; fresh databases are fine).
- `pg_dump`/`pg_restore` binaries are not yet bundled in release
artifacts (PATH/common-location discovery only).
- The satellite-store concurrency findings listed above.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Phil Larson <hello@phillarson.xyz>
Co-authored-by: fusion-merge <fusion-merge@local>
2026-07-13 19:07:58 -07:00

474 lines
18 KiB
TypeScript

import { sql } from "drizzle-orm";
import type { Database } from "./db.js";
import type { AsyncDataLayer } from "./postgres/data-layer.js";
/**
* Productivity analytics: files modified (count + language distribution) from
* `tasks.modifiedFiles`, commit associations from `task_commit_associations`,
* pull requests from `pull_requests`, LOC from merge-time commit diff stats,
* and estimated human hours saved derived from the same LOC source.
*
* **LOC availability.** Fusion persists nullable `additions`/`deletions` on
* `task_commit_associations` when merge paths can capture git shortstat output.
* LOC is reported as a real value only when at least one in-range association
* has non-null stats. If the range has no recorded stats, the documented
* unavailable sentinel — `{ value: null, unavailable: true }` — is preserved,
* **never `0`**, so missing historical data is not mistaken for "zero lines
* changed". Human-hours-saved uses the same sentinel because it is a
* conservative estimate over real LOC rather than an independent data source.
*
* Inclusivity: `from`/`to` bounds are inclusive. Tasks are filtered by
* `updatedAt` (the last time the task — and therefore its modifiedFiles — was
* touched); completed-task durations by `executionCompletedAt`; commit
* associations by `authoredAt`; PRs by `createdAt`.
*/
/*
FNXC:CommandCenterProductivity 2026-06-19-12:00:
Human hours saved is intentionally a rough headline estimate from already-aggregated changed LOC. Use one conservative exported rate so dashboards, CSV exports, and docs can cite the same assumption without adding a new data source or implying precision.
*/
export const HUMAN_LINES_PER_HOUR = 15;
export interface ProductivityAnalyticsQuery {
/** ISO-8601 lower bound (inclusive). */
from?: string;
/** ISO-8601 upper bound (inclusive). */
to?: string;
}
/** A single language's modified-file count. */
export interface LanguageCount {
/** Lowercased file extension (no dot), or `other` when none. */
language: string;
count: number;
}
/**
* LOC summary. `value` is null and `unavailable` true when no in-range commit
* association has diff stats — never `0` for unknown data.
*/
export interface LocSummary {
value: number | null;
unavailable: boolean;
}
/**
* Estimated human hours saved. `value` is an estimate in hours. It is null and
* `unavailable` true when the underlying LOC source is unavailable — never `0`
* for unknown data.
*/
export interface HoursSavedSummary {
value: number | null;
unavailable: boolean;
}
/**
* FNXC:CommandCenterProductivity 2026-06-19-12:00:
* Task-duration productivity stats are derived from `tasks.cumulativeActiveMs` for done tasks completed in the selected range. Missing qualifying durations are unavailable, not zero, so old or untracked tasks do not read as instant work.
*/
export interface TaskDurationSummary {
completedTasks: number;
averageMs: number | null;
medianMs: number | null;
p90Ms: number | null;
totalMs: number | null;
unavailable: boolean;
}
/**
* FNXC:CommandCenterProductivity 2026-06-30-10:17:
* Operators need average and median task active duration over time from real completed-task `cumulativeActiveMs` history. Trend buckets are emitted only for days with qualifying completed tasks; missing history must stay absent/unavailable, never fabricated as zero-duration chart points.
*/
export interface TaskDurationTrendBucket {
bucket: string;
completedTasks: number;
averageMs: number | null;
medianMs: number | null;
unavailable: boolean;
}
export interface ProductivityAnalytics {
from: string | null;
to: string | null;
/** Total modified-file paths across matched tasks. */
modifiedFiles: number;
/** Modified files grouped by language (extension), descending by count. */
byLanguage: LanguageCount[];
/** Rows in `task_commit_associations` in range. */
commits: number;
/** Rows in `pull_requests` in range. */
pullRequests: number;
/** LOC from commit association diff stats when at least one in-range row has stats. */
loc: LocSummary;
/** Estimated human-hours equivalent derived from `loc` when LOC is available. */
hoursSaved: HoursSavedSummary;
/** Active execution duration for done tasks completed in range. */
taskDuration: TaskDurationSummary;
/** Per-day active execution duration for done tasks completed in range. */
taskDurationTrend: TaskDurationTrendBucket[];
}
interface CountRow {
count: number;
}
interface CommitStatsRow {
count: number;
additions: number | null;
deletions: number | null;
statsRows: number;
}
interface ModifiedFilesRow {
modifiedFiles: string | null;
}
interface TaskDurationRow {
cumulativeActiveMs: number;
executionCompletedAt: string;
}
/** Extract a coarse language key from a file path (its lowercased extension). */
function languageOf(path: string): string {
const base = path.split("/").pop() ?? path;
const dot = base.lastIndexOf(".");
if (dot <= 0 || dot === base.length - 1) return "other";
return base.slice(dot + 1).toLowerCase();
}
function median(sortedValues: readonly number[]): number | null {
if (sortedValues.length === 0) return null;
const middle = Math.floor(sortedValues.length / 2);
if (sortedValues.length % 2 === 1) return sortedValues[middle] ?? null;
return ((sortedValues[middle - 1] ?? 0) + (sortedValues[middle] ?? 0)) / 2;
}
function nearestRankPercentile(sortedValues: readonly number[], percentile: number): number | null {
if (sortedValues.length === 0) return null;
const index = Math.min(
sortedValues.length - 1,
Math.max(0, Math.ceil(percentile * sortedValues.length) - 1),
);
return sortedValues[index] ?? null;
}
/**
* Aggregate productivity metrics over a date range. Empty range yields zeroed
* structures (not nulls); LOC and task duration remain unavailable sentinels
* unless at least one in-range row carries real source data.
*/
export async function aggregateProductivityAnalytics(
dbOrLayer: Database | AsyncDataLayer,
query: ProductivityAnalyticsQuery = {},
): Promise<ProductivityAnalytics> {
// FNXC:PostgresCommandCenterAnalytics 2026-06-27-10:00:
// Backend (PostgreSQL) path. The async connection does not put `project` on
// the search_path, so every table is schema-qualified (project.*) and uses
// snake_case columns. `modified_files` is jsonb (postgres-js returns it
// already parsed), so the language/file count runs over the parsed array
// rather than JSON.parse. pull_requests.created_at is a bigint epoch-ms
// column (mirrors the SQLite INTEGER column), so ISO bounds are converted to
// epoch ms. Semantics (range columns, COALESCE/SUM/COUNT, statsRows gate, LOC
// unavailable sentinel, duration percentiles) mirror the sync branch exactly.
if ("ping" in dbOrLayer) {
return aggregateProductivityAnalyticsAsync(dbOrLayer, query);
}
const db = dbOrLayer as Database;
// Modified files: read the JSON array off tasks updated in range.
const taskClauses: string[] = [
"modifiedFiles IS NOT NULL",
"modifiedFiles NOT IN ('', '[]')",
];
const taskParams: string[] = [];
if (query.from !== undefined) {
taskClauses.push("updatedAt >= ?");
taskParams.push(query.from);
}
if (query.to !== undefined) {
taskClauses.push("updatedAt <= ?");
taskParams.push(query.to);
}
const taskRows = db
.prepare(
`SELECT modifiedFiles FROM tasks WHERE ${taskClauses.join(" AND ")}`,
)
.all(...taskParams) as ModifiedFilesRow[];
let modifiedFiles = 0;
const langMap = new Map<string, number>();
for (const row of taskRows) {
if (!row.modifiedFiles) continue;
let files: unknown;
try {
files = JSON.parse(row.modifiedFiles);
} catch {
continue;
}
if (!Array.isArray(files)) continue;
for (const f of files) {
if (typeof f !== "string" || f.length === 0) continue;
modifiedFiles += 1;
const lang = languageOf(f);
langMap.set(lang, (langMap.get(lang) ?? 0) + 1);
}
}
const byLanguage: LanguageCount[] = [...langMap.entries()]
.map(([language, count]) => ({ language, count }))
.sort((a, b) => b.count - a.count);
// Commits from task_commit_associations (by authoredAt).
const commitClauses: string[] = [];
const commitParams: string[] = [];
if (query.from !== undefined) {
commitClauses.push("authoredAt >= ?");
commitParams.push(query.from);
}
if (query.to !== undefined) {
commitClauses.push("authoredAt <= ?");
commitParams.push(query.to);
}
const commitWhere =
commitClauses.length > 0 ? `WHERE ${commitClauses.join(" AND ")}` : "";
const commitStats = db
.prepare(
`SELECT
COUNT(*) AS count,
SUM(additions) AS additions,
SUM(deletions) AS deletions,
COUNT(CASE WHEN additions IS NOT NULL OR deletions IS NOT NULL THEN 1 END) AS statsRows
FROM task_commit_associations ${commitWhere}`,
)
.get(...commitParams) as CommitStatsRow;
const commits = commitStats.count;
const loc: LocSummary = commitStats.statsRows > 0
? { value: (commitStats.additions ?? 0) + (commitStats.deletions ?? 0), unavailable: false }
: { value: null, unavailable: true };
const hoursSaved: HoursSavedSummary = loc.unavailable || loc.value === null
? { value: null, unavailable: true }
: { value: Math.round((loc.value / HUMAN_LINES_PER_HOUR) * 10) / 10, unavailable: false };
const durationClauses: string[] = [
`"column" = 'done'`,
"executionCompletedAt IS NOT NULL",
"cumulativeActiveMs IS NOT NULL",
"cumulativeActiveMs > 0",
];
const durationParams: string[] = [];
if (query.from !== undefined) {
durationClauses.push("executionCompletedAt >= ?");
durationParams.push(query.from);
}
if (query.to !== undefined) {
durationClauses.push("executionCompletedAt <= ?");
durationParams.push(query.to);
}
const durationRows = db
.prepare(
`SELECT cumulativeActiveMs, executionCompletedAt FROM tasks WHERE ${durationClauses.join(" AND ")} ORDER BY executionCompletedAt ASC`,
)
.all(...durationParams) as TaskDurationRow[];
const durations = durationRows.map((row) => row.cumulativeActiveMs).sort((a, b) => a - b);
const totalDurationMs = durations.reduce((sum, durationMs) => sum + durationMs, 0);
const taskDuration: TaskDurationSummary = durations.length > 0
? {
completedTasks: durations.length,
averageMs: totalDurationMs / durations.length,
medianMs: median(durations),
p90Ms: nearestRankPercentile(durations, 0.9),
totalMs: totalDurationMs,
unavailable: false,
}
: {
completedTasks: 0,
averageMs: null,
medianMs: null,
p90Ms: null,
totalMs: null,
unavailable: true,
};
const durationBuckets = new Map<string, number[]>();
for (const row of durationRows) {
const bucket = row.executionCompletedAt.slice(0, 10);
const bucketDurations = durationBuckets.get(bucket) ?? [];
bucketDurations.push(row.cumulativeActiveMs);
durationBuckets.set(bucket, bucketDurations);
}
const taskDurationTrend: TaskDurationTrendBucket[] = [...durationBuckets.entries()].map(([bucket, bucketDurations]) => {
const sortedBucketDurations = [...bucketDurations].sort((a, b) => a - b);
const bucketTotalMs = sortedBucketDurations.reduce((sum, durationMs) => sum + durationMs, 0);
return {
bucket,
completedTasks: sortedBucketDurations.length,
averageMs: sortedBucketDurations.length > 0 ? bucketTotalMs / sortedBucketDurations.length : null,
medianMs: median(sortedBucketDurations),
unavailable: sortedBucketDurations.length === 0,
};
});
// Pull requests. `pull_requests.createdAt` is an INTEGER epoch-ms column, so
// convert the ISO bounds to epoch ms for comparison.
const prClauses: string[] = [];
const prParams: number[] = [];
if (query.from !== undefined) {
prClauses.push("createdAt >= ?");
prParams.push(Date.parse(query.from));
}
if (query.to !== undefined) {
prClauses.push("createdAt <= ?");
prParams.push(Date.parse(query.to));
}
const prWhere = prClauses.length > 0 ? `WHERE ${prClauses.join(" AND ")}` : "";
const pullRequests = (
db
.prepare(`SELECT COUNT(*) AS count FROM pull_requests ${prWhere}`)
.get(...prParams) as CountRow
).count;
return {
from: query.from ?? null,
to: query.to ?? null,
modifiedFiles,
byLanguage,
commits,
pullRequests,
loc,
hoursSaved,
taskDuration,
taskDurationTrend,
};
}
/**
* FNXC:PostgresCommandCenterAnalytics 2026-06-27-10:00:
* PostgreSQL implementation of {@link aggregateProductivityAnalytics}. Mirrors
* the sync SQLite aggregation one-for-one against the real `project.*` tables.
*/
async function aggregateProductivityAnalyticsAsync(
layer: AsyncDataLayer,
query: ProductivityAnalyticsQuery,
): Promise<ProductivityAnalytics> {
// Modified files: tasks updated in range whose modified_files is a non-empty
// jsonb array. postgres-js returns jsonb already parsed.
const mfFrom = query.from !== undefined ? sql`AND updated_at >= ${query.from}` : sql``;
const mfTo = query.to !== undefined ? sql`AND updated_at <= ${query.to}` : sql``;
const taskRows = (await layer.db.execute(
sql`SELECT modified_files AS "modifiedFiles" FROM project.tasks
WHERE modified_files IS NOT NULL
AND jsonb_typeof(modified_files) = 'array'
AND jsonb_array_length(modified_files) > 0
${mfFrom} ${mfTo}`,
)) as Array<{ modifiedFiles: unknown }>;
let modifiedFiles = 0;
const langMap = new Map<string, number>();
for (const row of taskRows) {
const files = row.modifiedFiles;
if (!Array.isArray(files)) continue;
for (const f of files) {
if (typeof f !== "string" || f.length === 0) continue;
modifiedFiles += 1;
const lang = languageOf(f);
langMap.set(lang, (langMap.get(lang) ?? 0) + 1);
}
}
const byLanguage: LanguageCount[] = [...langMap.entries()]
.map(([language, count]) => ({ language, count }))
.sort((a, b) => b.count - a.count);
// Commits + LOC from task_commit_associations (by authored_at).
const cFrom = query.from !== undefined ? sql`AND authored_at >= ${query.from}` : sql``;
const cTo = query.to !== undefined ? sql`AND authored_at <= ${query.to}` : sql``;
const commitStatsRows = (await layer.db.execute(
sql`SELECT
count(*)::int AS count,
COALESCE(SUM(additions), 0)::int AS additions,
COALESCE(SUM(deletions), 0)::int AS deletions,
COUNT(CASE WHEN additions IS NOT NULL OR deletions IS NOT NULL THEN 1 END)::int AS "statsRows"
FROM project.task_commit_associations
WHERE 1=1 ${cFrom} ${cTo}`,
)) as Array<{ count: number; additions: number; deletions: number; statsRows: number }>;
const commitStats = commitStatsRows[0] ?? { count: 0, additions: 0, deletions: 0, statsRows: 0 };
const commits = commitStats.count;
const loc: LocSummary = commitStats.statsRows > 0
? { value: (commitStats.additions ?? 0) + (commitStats.deletions ?? 0), unavailable: false }
: { value: null, unavailable: true };
const hoursSaved: HoursSavedSummary = loc.unavailable || loc.value === null
? { value: null, unavailable: true }
: { value: Math.round((loc.value / HUMAN_LINES_PER_HOUR) * 10) / 10, unavailable: false };
// Active-execution duration for done tasks completed in range.
const dFrom = query.from !== undefined ? sql`AND execution_completed_at >= ${query.from}` : sql``;
const dTo = query.to !== undefined ? sql`AND execution_completed_at <= ${query.to}` : sql``;
const durationRows = (await layer.db.execute(
sql`SELECT cumulative_active_ms AS "cumulativeActiveMs", execution_completed_at AS "executionCompletedAt"
FROM project.tasks
WHERE "column" = 'done'
AND execution_completed_at IS NOT NULL
AND cumulative_active_ms IS NOT NULL
AND cumulative_active_ms > 0
${dFrom} ${dTo}
ORDER BY cumulative_active_ms ASC`,
)) as Array<{ cumulativeActiveMs: number; executionCompletedAt: string }>;
const durations = durationRows.map((row) => Number(row.cumulativeActiveMs));
const totalDurationMs = durations.reduce((sum, durationMs) => sum + durationMs, 0);
const taskDuration: TaskDurationSummary = durations.length > 0
? {
completedTasks: durations.length,
averageMs: totalDurationMs / durations.length,
medianMs: median(durations),
p90Ms: nearestRankPercentile(durations, 0.9),
totalMs: totalDurationMs,
unavailable: false,
}
: {
completedTasks: 0,
averageMs: null,
medianMs: null,
p90Ms: null,
totalMs: null,
unavailable: true,
};
const durationBuckets = new Map<string, number[]>();
for (const row of durationRows) {
const bucket = row.executionCompletedAt.slice(0, 10);
const bucketDurations = durationBuckets.get(bucket) ?? [];
bucketDurations.push(Number(row.cumulativeActiveMs));
durationBuckets.set(bucket, bucketDurations);
}
const taskDurationTrend: TaskDurationTrendBucket[] = [...durationBuckets.entries()].map(([bucket, bucketDurations]) => {
const sortedBucketDurations = [...bucketDurations].sort((a, b) => a - b);
const bucketTotalMs = sortedBucketDurations.reduce((sum, durationMs) => sum + durationMs, 0);
return {
bucket,
completedTasks: sortedBucketDurations.length,
averageMs: sortedBucketDurations.length > 0 ? bucketTotalMs / sortedBucketDurations.length : null,
medianMs: sortedBucketDurations.length > 0 ? median(sortedBucketDurations) : null,
unavailable: false,
};
});
// Pull requests. pull_requests.created_at is a bigint epoch-ms column, so the
// ISO bounds are converted to epoch ms for comparison (mirrors sync branch).
const prFrom = query.from !== undefined ? sql`AND created_at >= ${Date.parse(query.from)}` : sql``;
const prTo = query.to !== undefined ? sql`AND created_at <= ${Date.parse(query.to)}` : sql``;
const prRows = (await layer.db.execute(
sql`SELECT count(*)::int AS count FROM project.pull_requests WHERE 1=1 ${prFrom} ${prTo}`,
)) as Array<{ count: number }>;
const pullRequests = prRows[0]?.count ?? 0;
return {
from: query.from ?? null,
to: query.to ?? null,
modifiedFiles,
byLanguage,
commits,
pullRequests,
loc,
hoursSaved,
taskDuration,
taskDurationTrend,
};
}