feat(usage): add GET /v1/usage/summary aggregate route #135

Merged
sharang merged 1 commits from feat/usage-summary into main 2026-11-17 08:33:49 +00:00
Owner

Backlog t80. The portal's dashboard, billing and products pages fold at most 1000 events from GET /v1/usage/events for their 30-day view, so busy workspaces see figures that are too low. This adds an aggregate route the portal can read instead.

Endpoint

GET /v1/usage/summary?tenant_id=…&since=…[&until=…&product=…&metric=…]

{
  "tenant_id": "…",
  "since": "2026-10-18T00:00:00Z",
  "events": 1205,
  "totals": [
    { "product": "pruefwerk", "metric": "scan.completed", "events": 700, "quantity": 700, "last_at": "2026-11-07T12:00:00Z" }
  ],
  "daily": [
    { "day": "2026-11-01", "metric": "scan.completed", "events": 200, "quantity": 250 }
  ]
}
  • totals: per product × metric. It carries the event count, the quantity sum and the latest occurred_at (UTC). It is ordered by product, then metric.
  • daily: per UTC day × metric, summed across products, which is all the charts use. It is ordered by day, then metric.
  • Every matching event is counted; there is no row cap. Postgres computes the figures with GROUP BY, and Memory runs the same fold. Ordering uses COLLATE "C", so both stores return identical, byte-ordered output.
  • totals and daily are always arrays, never null. An unknown or malformed tenant_id returns an empty summary.

Auth / scoping

The new route uses the same filters, the same tenant pinning for user tokens and the same route group (usage.read) as GET /v1/usage/events. The portal service clients portal-svc-stage and portal-svc-prod already have usage.read, so no AUTHZ_RULES change is needed.

Changes

  • internal/store/usage_summary.go: SummarizeUsage for both stores. The shared filter helpers usageWhere (Postgres) and UsageFilter.matches (Memory) are now also used by ListUsageEvents; its behaviour is unchanged.
  • internal/server/usage.go: the new handler. The list and summary handlers now share the query parsing in usageFilterFrom.
  • openapi.yaml: the new path and the UsageSummary schema. The spec test covers the path.

Tests

  • eachStore (Memory and Postgres), with 1,215 seeded events:
    • The list route still stops at 1000 rows, while the summary counts all 1200 events in the window.
    • Exact totals and daily buckets, with two events straddling UTC midnight.
    • since/until are inclusive; events from other tenants are excluded; product/metric filters work.
    • An empty tenant and a malformed tenant id both return []; bad input returns 400.
  • Internal tests: the route group is usage.read (a usage.read client can read the summary but cannot push events); user tokens are pinned to their own tenant; store errors return 500.
  • Local runs: gofmt and go vet clean; golangci-lint v2.12.2 reports 0 issues; the CI test line (-race -coverpkg) passes with the Postgres harness running, total coverage 80.9 %.

Notes

  • Found while testing, not fixed here: on Postgres, GET /v1/usage/events?tenant_id=not-a-uuid returns 500. pgx reports the cast error from rows.Err(), so the isInvalidUUID check after Query never fires; the memory store returns {"items":[]}.
  • go test ./migrations/ fails on main too (TestSeed_canInsertAndQuery: intake_reports FK on tenant delete). CI doesn't run that package.
  • The portal PR that switches to this route follows; it needs this PR deployed first.

🤖 Generated with Claude Code

Backlog t80. The portal's dashboard, billing and products pages fold at most 1000 events from `GET /v1/usage/events` for their 30-day view, so busy workspaces see figures that are too low. This adds an aggregate route the portal can read instead. ## Endpoint `GET /v1/usage/summary?tenant_id=…&since=…[&until=…&product=…&metric=…]` ```json { "tenant_id": "…", "since": "2026-10-18T00:00:00Z", "events": 1205, "totals": [ { "product": "pruefwerk", "metric": "scan.completed", "events": 700, "quantity": 700, "last_at": "2026-11-07T12:00:00Z" } ], "daily": [ { "day": "2026-11-01", "metric": "scan.completed", "events": 200, "quantity": 250 } ] } ``` - `totals`: per product × metric. It carries the event count, the quantity sum and the latest `occurred_at` (UTC). It is ordered by product, then metric. - `daily`: per UTC day × metric, summed across products, which is all the charts use. It is ordered by day, then metric. - Every matching event is counted; there is no row cap. Postgres computes the figures with `GROUP BY`, and Memory runs the same fold. Ordering uses `COLLATE "C"`, so both stores return identical, byte-ordered output. - `totals` and `daily` are always arrays, never `null`. An unknown or malformed `tenant_id` returns an empty summary. ## Auth / scoping The new route uses the same filters, the same tenant pinning for user tokens and the same route group (`usage.read`) as `GET /v1/usage/events`. The portal service clients `portal-svc-stage` and `portal-svc-prod` already have `usage.read`, so **no AUTHZ_RULES change is needed**. ## Changes - `internal/store/usage_summary.go`: `SummarizeUsage` for both stores. The shared filter helpers `usageWhere` (Postgres) and `UsageFilter.matches` (Memory) are now also used by `ListUsageEvents`; its behaviour is unchanged. - `internal/server/usage.go`: the new handler. The list and summary handlers now share the query parsing in `usageFilterFrom`. - `openapi.yaml`: the new path and the `UsageSummary` schema. The spec test covers the path. ## Tests - `eachStore` (Memory and Postgres), with 1,215 seeded events: - The list route still stops at 1000 rows, while the summary counts all 1200 events in the window. - Exact totals and daily buckets, with two events straddling UTC midnight. - `since`/`until` are inclusive; events from other tenants are excluded; `product`/`metric` filters work. - An empty tenant and a malformed tenant id both return `[]`; bad input returns 400. - Internal tests: the route group is `usage.read` (a `usage.read` client can read the summary but cannot push events); user tokens are pinned to their own tenant; store errors return 500. - Local runs: `gofmt` and `go vet` clean; `golangci-lint` v2.12.2 reports 0 issues; the CI test line (`-race -coverpkg`) passes with the Postgres harness running, total coverage 80.9 %. ## Notes - Found while testing, not fixed here: on Postgres, `GET /v1/usage/events?tenant_id=not-a-uuid` returns 500. pgx reports the cast error from `rows.Err()`, so the `isInvalidUUID` check after `Query` never fires; the memory store returns `{"items":[]}`. - `go test ./migrations/` fails on `main` too (`TestSeed_canInsertAndQuery`: `intake_reports` FK on tenant delete). CI doesn't run that package. - The portal PR that switches to this route follows; it needs this PR deployed first. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sharang added 1 commit 2026-11-17 08:18:41 +00:00
feat(usage): add GET /v1/usage/summary aggregate route
ci / shared (pull_request) Successful in 11s
ci / test (pull_request) Successful in 7m32s
ci / image (pull_request) Skipped
9138ac586b
The portal's 30-day views folded at most 1000 events from the usage list,
so busy tenants saw figures that were too low. The summary aggregates
every matching event in the store (GROUP BY in Postgres, the same fold in
Memory): counts, quantity sums and the latest occurred_at per product and
metric, and counts and sums per UTC day and metric.

Same filters, tenant pinning for user tokens and route group (usage.read)
as GET /v1/usage/events, so the portal's service client needs no new
AUTHZ grant. Both stores share the filter (usageWhere / matches).

Refs: t80

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sharang merged commit 2b3cea5974 into main 2026-11-17 08:33:49 +00:00
Sign in to join this conversation.