Skip to content

feat(api): store per-asset headline figures for the overview - #1375

Merged
royalTreasure merged 2 commits into
useconcord:mainfrom
Glorious21:feat/overview-cache
Nov 26, 2026
Merged

royalTreasure merged 2 commits into
useconcord:mainfrom
Glorious21:feat/overview-cache

Conversation

@Glorious21

@Glorious21 Glorious21 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

closes #575

Summary

The dashboard polls /overview every 4 to 30 seconds. For every asset, each call replayed the events for supply and holders, rebuilt the controls log, and re-read every corporate action and the latest run. The figures now live in one asset_stats row per asset, which overview() and assetSummary() read, so a poll costs the same however long the history is.

Changes

  • services/api/src/schema.ts: ORG_MIGRATIONS entry 6 (after main's v4 matching windows and v5 tolerance) adds asset_stats (asset_id, supply, holder_count, event_count, incomplete, last_run, open_reconciliation, open_controls, open_actions, updated_at), cascading from assets.
  • services/api/src/concord.ts
    • computeStats() is the previous computation, unchanged.
    • refreshStats() recomputes the figures and stores the row.
    • assetSummary() reads the row. If it's missing (for example after an upgrade), it computes and stores it. An asset that has never synced is always recomputed: its controls log runs to the clock and it has nothing to replay.
  • Where the row is rewritten. Every place that can move a figure:
    • The end of every sync pass, including one that adds no events (the controls log runs to the read-through time) and one that fails after committing some pages.
    • rebuild, before its (possibly deferred) re-index.
    • reconcile and deleteRegister, whose cascade can remove the latest run.
    • setNote, for any scope.
    • importInstructions.
    • Both result writes in reconcileAction, which covers createAction and reconcileDue.
  • Unchanged: the response shape of /overview and /assets/:id. Unwatching an asset deletes its row by cascade.
  • Across processes: the CLI runs the same code against the same database file, so a change made from the CLI rewrites the row the server reads.
  • Migration checklist: the org-v6.db fixture and its make-db-fixtures.ts entry, upgrade tests from v5 and v6, the pinned digest in tenancy.test.ts, the cascade test seed row, and docs/schema.md, which documents the table and what rewrites it. CHANGELOG.md gets an entry. The older fixtures are byte-for-byte unchanged.

Verification

  • Consistency test ("keeps the overview's stored figures equal to a full recompute after every kind of change"). It walks one asset through:

    1. First sync.
    2. A sync that adds nothing.
    3. A sync with new events.
    4. An instructions import.
    5. A reconciliation, then sign-offs on a reconciliation break, a controls break and an action break.
    6. A dividend, and a scheduled split reconciled by reconcileDue after a later sync.
    7. A deleted register.
    8. A deferred rebuild, and the sync after it.

    After each step, the served overview must equal the overview recomputed from scratch with every asset_stats row deleted. I confirmed it fails at the matching step when the hook is removed from reconcile, from the sync pass, or from reconcileAction.

  • Latency independent of event count. With 10,007 events indexed, a poll of overview() runs no SQL against events, runs, actions, instructions, break_notes or openings. It reads only assets and asset_stats.

  • Commands run:

    • pnpm --filter @concord/api test: 309/311 pass.
    • pnpm --filter @concord/api typecheck and eslint services/api/src: clean.
  • The two failing tests are unrelated to this change. They fail only on my Windows checkout: README CLI examples because README.md is checked out with CRLF line endings, and resolves relative CONCORD_DATA_DIR because it expects POSIX absolute paths.

Note for merging

#1366 (holdings checkpoints, #576) is also rebased onto main as ORG_MIGRATIONS entry 6 with an org-v6.db fixture. Whichever of the two merges second needs its migration renumbered to 7, with an org-v7.db fixture, the digest appended and the doc wording updated. I'll do that on whichever lands second.

@drips-wave

drips-wave Bot commented Nov 24, 2026

Copy link
Copy Markdown

@Glorious21 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@royalTreasure royalTreasure left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Glorious21. Storing per-asset headline figures fits #575. CI is green, but it can't be merged yet: migration v4 is now taken. #1353 (per-asset matching windows) just landed on main as ORG_MIGRATIONS v4, so this branch conflicts and its v4 migration and org-v4.db fixture no longer line up.

To fix:

  1. git fetch upstream && git rebase upstream/main (please rebase, don't merge main in).
  2. Renumber your migration to the next free version (v5 today). Several open PRs add a migration, so whichever merges first takes v5 and the rest move up again.
  3. Regenerate the fixture with services/api/test/fixtures/make-db-fixtures.ts as org-v5.db (keep main's org-v4.db unchanged), and update the pinned schema digest and migration tests.
  4. Update the version number in `docs/schema.md` and in your `CHANGELOG.md` line to match. Run pnpm build && pnpm typecheck && pnpm lint && pnpm test and force-push.

The dashboard polls the overview every few seconds, and each call replayed
events, rebuilt the controls log and re-read every action and run per
asset. An asset_stats row now holds supply, holder and event counts,
incompleteness, the latest run and the open-break counts. overview() and
assetSummary() read it; a missing row is recomputed and stored.

The row is rewritten at the end of every change that can move a figure: a
sync pass (including one that adds nothing, since the controls log runs to
the read-through time, and one that fails after committing pages), a
rebuild, a reconciliation, a deleted register, a sign-off, an
instructions import, a corporate action's reconciliation and an asset
settings change (its matching windows decide which controls break). An asset that
was never synced is always recomputed.

ORG_MIGRATIONS v6 adds the table, with the org-v6 fixture, upgrade tests,
the pinned digest, the cascade test row and docs.

Closes useconcord#575
@Glorious21

Copy link
Copy Markdown
Contributor Author

Thanks @royalTreasure, done. The branch is rebased onto main (no merge commit) and force-pushed as 4910933.

  1. Rebase: git rebase upstream/main. main now has feat(api): add per-asset instructionWindowHours and movementWindowHours settings #1353 (v4, matching windows) and Per-asset reconciliation tolerance (reconcilePositions already supports it) #564 (v5, tolerance), so v5 was taken as well.
  2. Renumbered to v6, the next free version. main's v4 and v5 are untouched. My migration's SQL is unchanged, so its pinned digest is the same value, now in sixth place.
  3. Fixture: org-v6.db comes from make-db-fixtures.ts (ORG_MIGRATIONS.slice(0, 6), one asset_stats row). Every existing fixture, main's org-v4.db and org-v5.db included, is byte-for-byte unchanged. The upgrade test now runs from org-v5.db (the prior version), and the "keeps its rows" test runs on org-v6.db.
  4. Docs: docs/schema.md and the CHANGELOG.md line now say migration 6.

One addition from the rebase. #1353 made updateAsset set the instruction and movement matching windows. Those decide which controls break, so they can move the stored open_controls count. updateAsset now refreshes the stored figures too. The consistency test gained a step that narrows the instruction window, checks that the served overview equals a full recompute, and restores it. Without the new refresh, that step fails.

Results:

  • pnpm build, pnpm typecheck and pnpm lint: clean.
  • pnpm test: core 154/154, web 62/62, API 351/353. The two API failures are the Windows-checkout tests from before (README CRLF, POSIX CONCORD_DATA_DIR).

#1366 is also on v6 now. Whichever of the two lands second, I'll move it to v7.

@royalTreasure royalTreasure left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Glorious21, and thanks for rebasing. The change still fits #575 and CI was green, but migration v6 was just taken: #1366 (holdings checkpoints) merged into main as ORG_MIGRATIONS v6, so this branch conflicts again and its v6 migration and org-v6.db fixture no longer line up.

To fix:

  1. git fetch upstream && git rebase upstream/main (please rebase, don't merge main in).
  2. Renumber your migration to the next free version (v7 today). Other open PRs also add a migration, so whichever merges first takes v7 and the rest move up again.
  3. Regenerate the fixture with services/api/test/fixtures/make-db-fixtures.ts as org-v7.db (keep main's org-v6.db unchanged), and update the pinned schema digest and migration tests.
  4. Update the version number in docs/schema.md and in your CHANGELOG.md line to match. Run pnpm build && pnpm typecheck && pnpm lint && pnpm test and force-push.

@royalTreasure

Copy link
Copy Markdown
Contributor

Thanks @Glorious21! Each asset's headline figures are stored in asset_stats, so the polled overview reads one row instead of replaying events. This closes #575. I rebuilt it on current main with the migration as ORG_MIGRATIONS v14. Register staleness (from #1374) is computed live because it changes with the clock, voiding or deleting an action refreshes the figures, and two checkpoint tests now allow for the checkpoints the refresh rewrites. Verified with the full CI matrix before merging.

@royalTreasure
royalTreasure merged commit b01d6bb into useconcord:main Nov 26, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache per-asset headline figures so overview() does not replay events, controls and actions on every poll

2 participants