feat(usage): add owner-only usage per team

AG
7d ago
+1153-12

Adds the owner-only usage report per team from docs/specs/team-model.md (sections 4 to 7), for sessions and searches. Depends on the organization teams from #326.

Routes

  • GET /api/v1/organizations/{organizationId}/usage/teams?days=7|30|90
  • GET /api/v1/organizations/{organizationId}/usage/teams/audit

Both are owner-only (admins and the bootstrap bearer get 403 forbidden) and need both teams and usage_people selected: a team of one shows one person's figures, so an installation that turned the People view off does not get them through teams. Neither feature is selected by default.

Grouping

  • Each session read is counted in exactly one row: a team, a crossTeam row for a set of teams (null for a person with no team), noTeam, unattributed or notResolved. The rows' sessions sum to total.sessions.
  • Sessions come only from the read API's session list, where each session carries its own user set. The per-person aggregate is not used, because it counts a shared session once per person.
  • A person's team is decided once per report at teamAsOf (the end of the range, or now when that is earlier): the assignment open then, else the one that closed last inside the range, else none. One query per report, limited to the organization.
  • searches come from usage_people_daily.
  • notResolved stays 0 until the read API can group by user set. The grouping function already takes those buckets.
  • spend is null in every row and listed in unavailable.

Bounds

The People view's bounds apply (pages of at most 200, at most 3 calls and 2 seconds), with a 5-minute cache of its own. A report that did not read every session is labelled truncated, and its session figures are lower bounds. If the read API cannot be read, session figures are null and searches are still served.

Audit

Every served report, cached or not, writes an organization-level usage.teams.viewed row. The People audit list is unchanged.

Not changed

People view behaviour (src/usage/people.ts only exports existing helpers). No migration, no new table, no new feature name, no dashboard change.

Checks run locally at this head

  • tsc --noEmit: clean
  • test/usage-teams.test.ts: 10 of 10
  • test/sessions.test.ts: 50 of 50

The full suite is left to CI.

šŸ¤– Generated with Claude Code


Proof this PR
Open in [code]smith Ā· Need help on this PR? Tag @codesmith-bot with what you need.


Summary by cubic

Adds owner-only usage per team: a report and an audit list under /api/v1/organizations/{organizationId}/usage/teams, requiring both the teams and usage_people features. Admins and the bootstrap bearer get 403 forbidden.

  • Each session read from the read API's session list is counted in exactly one row (a team, a cross-team set, no team, unattributed, or not resolved), so the rows sum to total.sessions; searches come from usage_people_daily. A session listed twice for one workspace counts once, while the same session id in two workspaces counts in each.
  • A person's team is resolved once per report at teamAsOf — the open assignment, else the one that closed last inside the range — so moves and removals count under the later team.
  • Reads are bounded like the People view with their own 5-minute cache, keyed per History reader identity; incomplete reads are flagged truncated, and an unreachable read API leaves session figures null while still serving searches.
  • Every served report, cached or not, writes a usage.teams.viewed audit row.
  • No migration or new table.