Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: store session usage per app and family in template usage stats - #29109

Open
EhabY wants to merge 6 commits into
mainfrom
feat/session-family-usage-rollup
Open

feat: store session usage per app and family in template usage stats#29109
EhabY wants to merge 6 commits into
mainfrom
feat/session-family-usage-rollup

Conversation

@EhabY

@EhabY EhabY commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Store per-app and per-family session minutes in template_usage_stats_session_apps and template_usage_stats_session_families, replacing the fixed family-minute columns. New apps and centrally defined families no longer require rollup schema changes; existing API, Prometheus, and telemetry outputs remain unchanged.

Family minutes deduplicate overlapping apps, and insights retain the cross-template minute caps. Family attribution comes from the shared app-to-family mapping. A digest with length-prefixed names skips child-row writes for unchanged buckets without confusing names containing delimiters.

Migration 000592: backfill existing family totals, including SFTP, without inventing historical per-app detail. Workspace web-app usage remains separate. The migration backfills retained history and alters template_usage_stats; exclusive locks acquired by the ALTERs last until the migration transaction commits. Downgrading discards per-app detail and families the old schema cannot represent. Already-merged migration 000590 is unchanged.

Follows merged #28337 and #29134; #28338 enables the producers. Covers the storage portion of #27413. Dynamic external projections (#27411 and the remaining #27413 work) and the connection-log migration (#27412) remain separate.

Design decisions and performance context
  • Separate app/family tables preserve overlapping-minute semantics and avoid collisions with workspace web-app slugs. Historical family-only buckets do not imply zero per-app usage.
  • Recomputed buckets use the current mapping; older buckets retain their recorded attribution. Digest checks preserve unchanged-bucket write skipping.
  • Previously recorded synthetic benchmarks used PostgreSQL 13/17 and approximately 302k history rows. Typical GetTemplateInsights latency was approximately unchanged (328 → 327 ms), while the overlapping-template case increased from 355 → 623 ms. GetTemplateInsightsByTemplate increased from 207 → 279 ms on typical data and 271 → 1,211 ms on the overlap fixture.
  • First rollup over approximately 300k agent stats increased from 0.94 → 1.88 s on typical data and 1.18 → 2.22 s on the overlap fixture. Steady-state reruns increased from 88 → 147 ms and 116 → 218 ms respectively. History storage increased from 68 MB to 208 MB. These are fixture measurements, not production guarantees.
Review guide and line breakdown

Counts for head 32b28f7b27, relative to the PR base. Additions/deletions are diff lines, not net growth. Categories are mutually exclusive.

Area Files Added Removed
Insights and rollup SQL 1 +488 -163
Migration SQL (up/down) 2 +142 -0
Handwritten Go 5 +220 -114
Tests and fixtures 12 +1,040 -138
Generated database code/schema 6 +577 -225
Total 26 +2,467 -640

Suggested review order:

  1. migrations/000592_template_usage_stats_session_usage.{up,down}.sql: schema, historical family backfill, and downgrade behavior.
  2. queries/insights.sql: app/family minute accounting, cross-template caps, and digest-gated child writes.
  3. Handwritten Go: registry validation, insights conversion, and Prometheus compatibility.
  4. Tests and fixtures: migration round trips, preserved history, overlapping usage, and digest ambiguity regressions.
  5. Generated database files: verify they match the source SQL/schema rather than reviewing them as separate logic.

Description updated by Coder Agents for @EhabY.

@EhabY
EhabY force-pushed the feat/session-family-usage-rollup branch from 83aa8d3 to cd9543f Compare September 9, 2026 14:33
@EhabY
EhabY removed this pull request from stack #29110 September 9, 2026 14:34
@EhabY
EhabY changed the base branch from feat/session-count-family-attribution to feat/session-count-generic-queries September 9, 2026 14:34
@EhabY
EhabY added this pull request to stack #29135 September 9, 2026 14:35
@EhabY
EhabY force-pushed the feat/session-family-usage-rollup branch from cd9543f to bdac30a Compare September 10, 2026 11:27
@EhabY
EhabY force-pushed the feat/session-family-usage-rollup branch from bdac30a to d34d4b6 Compare September 10, 2026 11:54
Base automatically changed from feat/session-count-generic-queries to main September 10, 2026 12:07
Replace the fixed per-family minute columns on template_usage_stats with
template_usage_stats_session_families and
template_usage_stats_session_apps, one row per bucket and name, so a
session reported under a new app name is stored and reported instead of
being dropped. Family attribution comes from the single codersdk
registry, which the rollup and the insights reads take as one jsonb
parameter of app name to family, so registering an app or a family needs
no SQL change, no column, and no probe.

Migration 000591 backfills the existing family totals into the child
table and drops the fixed columns. GetTemplateInsights keeps the per-user
aggregate as its only large grouping and applies the 30 minute
cross-template cap only to the buckets that need it, and the rollup scans
workspace_agent_stats once. At 302k history rows the 30 day read goes
from 2.4x the fixed-column baseline to about 1.1x on typical data, and
the (start_time, user_id) child indexes keep cross-template heavy data
within about 2x.
Record a digest of each bucket's session usage rows on template_usage_stats
and drive the child table deletes and upserts from the main upsert's
RETURNING set, so a rollup only touches child rows for buckets whose
session usage changed. Steady-state rollups measured 10% to 17% faster.
Length-prefix names in the digest input so registry changes cannot alias different child row sets. Pin child replacement and unchanged-row write elision with a regression.

Renumber the unmerged session usage migration to 000592 because main now uses 000591.

Generated by Coder Agents for @EhabY.
@EhabY
EhabY force-pushed the feat/session-family-usage-rollup branch from d34d4b6 to 32b28f7 Compare September 10, 2026 12:07
@EhabY
EhabY marked this pull request as ready for review September 10, 2026 12:12
@EhabY
EhabY requested a review from code-asher September 10, 2026 16:31
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