#580 Measures: totals in the PostgreSQL moment tree and its overviews (#574)
–
- github
- github/openchronology/timeline-app
- github/openchronology/timeline-app#580
- pull request
- state:merged
Part of #574, part 3 of #570. Stacked on #577, #578 and #579: until they merge, this diff includes their commits. The PostgreSQL moment tree keeps **measure totals** at every node, and overview groups return them. This matches the browser index in #579. SQLite follows separately. ## Tree `oc_nodes` gains `own_totals` (the node's own moments) and `totals` (its subtree). Both are JSON maps of exact values by name, with zeros left out. NULL means all zero, as in trees built before measures. - **`avl.mjs`** keeps them as it keeps `event_count`: - A moment joining or leaving a time adds or takes away its values there. - Every rewritten node recomputes its subtree's totals from its children. - A moment whose values change without moving rewrites its path (new `PersistentTree.adjust`). Other metadata edits still touch no node. - **`tree.mjs`** computes them when a whole index is built. - **Declaration changes:** totals add up whatever values are stored, so changing declarations needs no rebuild. Renames and removals rewrite values (#577), and the tree follows those edits. - **Forks** carry the new columns as row data, and the retired twins gain them automatically. - **Separation views:** `oc_view_nodes` gains the same columns. ## Overview `oc_overview_v2` returns each group's `totals`. - **How it adds:** with `oc_add_totals`, it adds the cached totals of the whole subtrees it consumes and the own totals of the points it takes. It does this only where there are any, so timelines without measures pay nothing. - **Variants:** the view variant and the fork (lineage) variant follow, with the lineage rewrite patterns updated. - **Result type:** it changed, so `schema.sql` drops older versions of the three functions first, only when their result lacks `totals`. - **API:** the overview groups in `/query` carry `totals`, documented in OpenAPI. ## Migration Full rebuilds now record index version 3. `rebuildIndexes` rebuilds once any timeline that has measures and was indexed before this. That covers values saved by a release that has #572 but not this. ## Tests - `test/measure-totals-postgres.mjs`, added to CI, checks every overview group's totals at four thresholds against hand sums: - after 25 random sparse saves (adds, moves, value-only changes, deletions); - in a tag separation view; - in a copy reading through to its original, before and after 15 saves of its own, with the original checked unchanged; - after every value is removed. - Locally, `incremental-postgres` (AVL invariants, full-rebuild equivalence) passes. The fork read-through tests run in CI. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_011fFY8eruEi1xAkkMBni13B https://github.com/circagraph/timeline-app/pull/580
Related
- #570 Measures: typed quantities on moments and durations, totalled in every summary
- #572 Measures 1: declarations, values, validation and formats
- #574 Measures 3: totals in the summary trees (browser, PostgreSQL, SQLite)
- #577 Measures: declarations, exact values, validation and formats (#572)
- #578 Measures: presentation and parsing per measure, and editor fields (#573)
- #579 Measures: totals in the editor's index and its overview groups (#574)
- #581 Measures: totals in the desktop file's SQLite index (#574)
Open #580 Measures: totals in the PostgreSQL moment tree and its overviews (#574) in the timeline