Files
coder/coderd/database/migrations/000596_template_usage_stats_session_usage.down.sql
T
Ehab Younes 817fc77612 feat: store session usage per app and family in template usage stats (#29109)
Store per-app session minutes in a child table instead of fixed family-minute columns, so adding an IDE no longer needs a rollup schema change. API, Prometheus, and telemetry output are unchanged.

- Add `template_usage_stats_session_apps`, keyed by start time, template, user, and app name, replacing the `ssh_mins`, `sftp_mins`, `reconnecting_pty_mins`, `vscode_mins`, and `jetbrains_mins` columns.
- Group app names into families at read time through the `codersdk` registry, so the registry never reaches SQL and attribution covers all history rather than only the buckets the rollup still revisits.
- Group `GetTemplateInsightsByTemplate` by app name so that Prometheus and the API report a family identically.
- Drop the `@app_families` query parameter and its dbauthz validation.
- Report a family total as the sum of its apps rather than the distinct minutes any of them was active. Numbers are unchanged today, because every agent reports one canonical name per family.
- Keep the child table window index-usable and count minutes with `COUNT(DISTINCT)` in the grouping, so a dashboard-sized range uses an index scan and a large deployment does not spill the sort to disk.

Part of #27410
2026-09-18 15:26:47 +02:00

81 lines
3.2 KiB
SQL

-- The fixed columns are NOT NULL with no default, so add them with a default
-- to fill existing rows, then drop the default to restore the original schema.
ALTER TABLE template_usage_stats
ADD COLUMN ssh_mins smallint DEFAULT 0 NOT NULL,
ADD COLUMN sftp_mins smallint DEFAULT 0 NOT NULL,
ADD COLUMN reconnecting_pty_mins smallint DEFAULT 0 NOT NULL,
ADD COLUMN vscode_mins smallint DEFAULT 0 NOT NULL,
ADD COLUMN jetbrains_mins smallint DEFAULT 0 NOT NULL;
ALTER TABLE template_usage_stats
ALTER COLUMN ssh_mins DROP DEFAULT,
ALTER COLUMN sftp_mins DROP DEFAULT,
ALTER COLUMN reconnecting_pty_mins DROP DEFAULT,
ALTER COLUMN vscode_mins DROP DEFAULT,
ALTER COLUMN jetbrains_mins DROP DEFAULT;
COMMENT ON COLUMN template_usage_stats.ssh_mins IS 'Total minutes the user has been using SSH.';
COMMENT ON COLUMN template_usage_stats.sftp_mins IS 'Total minutes the user has been using SFTP.';
COMMENT ON COLUMN template_usage_stats.reconnecting_pty_mins IS 'Total minutes the user has been using the reconnecting PTY.';
COMMENT ON COLUMN template_usage_stats.vscode_mins IS 'Total minutes the user has been using VSCode.';
COMMENT ON COLUMN template_usage_stats.jetbrains_mins IS 'Total minutes the user has been using JetBrains.';
-- Fold app names into the five families the fixed columns hold, capped at the
-- half hour a bucket covers. SQL cannot call into Go, so this copies
-- codersdk.appNameFamilies; a name it does not know counts as ssh rather than
-- losing its minutes. The per-app detail is lost either way.
UPDATE template_usage_stats AS tus
SET
ssh_mins = families.ssh_mins,
sftp_mins = families.sftp_mins,
reconnecting_pty_mins = families.reconnecting_pty_mins,
vscode_mins = families.vscode_mins,
jetbrains_mins = families.jetbrains_mins
FROM (
SELECT
apps.start_time,
apps.template_id,
apps.user_id,
LEAST(COALESCE(SUM(apps.usage_mins) FILTER (WHERE COALESCE(registry.family, 'ssh') = 'ssh'), 0), 30)::smallint AS ssh_mins,
LEAST(COALESCE(SUM(apps.usage_mins) FILTER (WHERE registry.family = 'sftp'), 0), 30)::smallint AS sftp_mins,
LEAST(COALESCE(SUM(apps.usage_mins) FILTER (WHERE registry.family = 'reconnecting_pty'), 0), 30)::smallint AS reconnecting_pty_mins,
LEAST(COALESCE(SUM(apps.usage_mins) FILTER (WHERE registry.family = 'vscode'), 0), 30)::smallint AS vscode_mins,
LEAST(COALESCE(SUM(apps.usage_mins) FILTER (WHERE registry.family = 'jetbrains'), 0), 30)::smallint AS jetbrains_mins
FROM
template_usage_stats_session_apps AS apps
LEFT JOIN (VALUES
('ssh', 'ssh'),
('zed', 'ssh'),
('sftp', 'sftp'),
('reconnecting_pty', 'reconnecting_pty'),
('jetbrains', 'jetbrains'),
('vscode', 'vscode'),
('vscode_insiders', 'vscode'),
('vscode_web', 'vscode'),
('code_server', 'vscode'),
('cursor', 'vscode'),
('windsurf', 'vscode'),
('positron', 'vscode'),
('vscodium', 'vscode'),
('codium', 'vscode'),
('antigravity', 'vscode'),
('trae', 'vscode'),
('kiro', 'vscode'),
('devin', 'vscode')
) AS registry(app, family)
ON
registry.app = apps.app_name
GROUP BY
apps.start_time, apps.template_id, apps.user_id
) AS families
WHERE
tus.start_time = families.start_time
AND tus.template_id = families.template_id
AND tus.user_id = families.user_id;
DROP TABLE template_usage_stats_session_apps;