Skip to content

View grouping and column aggregates (IS-85) - #109

Merged
ssalentin merged 10 commits into
mainfrom
feat/is-85-view-grouping
Nov 15, 2026
Merged

ssalentin merged 10 commits into
mainfrom
feat/is-85-view-grouping

Conversation

@ssalentin

Copy link
Copy Markdown
Owner

Implements #85: saved views can group by a property, and columns carry footer aggregates.

Schema. Migration v2 (view-grouping) adds group_by and aggregates TEXT columns to view. Existing v1 databases upgrade with their views kept; the upgrade path has its own test.

Store. group_by: {property, direction} (direction optional on the wire, defaults to asc) groups a query result. The group key is the raw stored cell value, dates truncated to the day, checkboxes 'true'/'false', multi_select the exact stored combination (documented v1 limitation). Responses carry groups: [{key, count}] with counts over the whole filtered set and the empty bucket last; rows come flat, sectioned by adjacent keys, group key leading the order. aggregates: {property: fn} computes over the whole filtered set: count, count_values, sum, min, max, average (over filled values, null when none). src/lib/group-key.ts mirrors the SQL key expression for the UI, so sectioning and the collapsed parameter agree with the store. Explicit bad requests fail 400 (bad_group/bad_aggregate) and change nothing; a dangling saved entry (property deleted, or its type no longer allows the function) is silently ignored at query time.

REST. Views POST and query POST accept group_by/aggregates; rows GET adds ?group=prop. An ungrouped response carries null groups/aggregates, so the shape stays additive.

MCP. query_collection_rows accepts the same payloads; the new save_collection_view tool creates a view with everything the UI can save (29 tools total). Views are not revision-tracked, so the tool takes no actor param and name is required.

UI. One collapsible section per group with its count in the header; collapsed keys travel in the collapsed URL parameter (percent-encoded, comma-joined, __empty__ sentinel) and their rows are hidden server-side, so the toggle is a plain link and pagination stays correct. A footer row aggregates every visible column (defaults while grouping without a saved config: sum for numbers, count_values otherwise). A minimal save-view form behind the existing write gate persists name plus current filters/sorts/grouping. Mobile cards get group headings. The filter and sort forms forward group and collapsed.

Tests. +19: test/collection-groups.test.ts (12 store-level cases: keys per type, whole-set counts, empty bucket, page-split groups, every aggregate fn, type violations, dangling views), REST and MCP grouping in test/collections-mcp.test.ts (that suite is the one isolating COLLECTIONS_DB to a temp dir), and the v1→v2 migration upgrade. 503 passed | 1 skipped on this branch (main: 484), svelte-check 0 errors, build green. Notes: min/max are also allowed on title (ordered strings like text); the v2 migration test asserts store fields, so it lands with the tests commit.

Verification lever. scripts/grouping-walk.sh reruns the acceptance against a copy of any instance: whole-set group counts (sum equals total, one key recounted row-by-row), the number sum recomputed with jq over all rows, the saved-view round trip over REST (views POST, query through {view: id}, views GET echo) and MCP (query_collection_rows, save_collection_view), the 400 refusals, and an isolation proof that the walk's view exists in the copy's database only. Every write stays below the temp dir; a passing run ends with WALK COMPLETE.

Evidence, measured 2026-11-15 against a copy of the live instance (Fixkosten, 22 rows, grouped by Status):

grouping acceptance walk

The capture shows group counts summing to the total (15 laufend + 3 neu + 4 offen = 22), the laufend count and the Betrag sum (1891.85) both verified against the rows, the saved view echoed by views GET and applied through {view: id}, the identical grouping over MCP, the source database holding zero walk views, and the two 400 refusals.

The UI on desktop (group sections with counts, one collapsed group, aggregate footer 22 / 1.891,85) and at 390px (card group headings, same collapse):

grouped table on desktop

grouped cards on mobile

Two review fixes on top of the implementation: save_collection_view had name optional in the zod schema (a view without a name is nonsense; now required, test updated), and it accepted an agent param recorded nowhere since views are not revision-tracked (dropped).

@ssalentin
ssalentin merged commit 7fc8a59 into main Nov 15, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant