@@ -143,32 +143,59 @@ FROM
143
143
insights;
144
144
145
145
-- name: GetTemplateInsightsByTemplate :many
146
- WITH agent_stats_by_interval_and_user AS (
147
- SELECT
148
- date_trunc(' minute' , was .created_at ) AS created_at_trunc,
149
- was .template_id ,
150
- was .user_id ,
151
- CASE WHEN SUM (was .session_count_vscode ) > 0 THEN 60 ELSE 0 END AS usage_vscode_seconds,
152
- CASE WHEN SUM (was .session_count_jetbrains ) > 0 THEN 60 ELSE 0 END AS usage_jetbrains_seconds,
153
- CASE WHEN SUM (was .session_count_reconnecting_pty ) > 0 THEN 60 ELSE 0 END AS usage_reconnecting_pty_seconds,
154
- CASE WHEN SUM (was .session_count_ssh ) > 0 THEN 60 ELSE 0 END AS usage_ssh_seconds
155
- FROM workspace_agent_stats was
156
- WHERE
157
- was .created_at >= @start_time::timestamptz
158
- AND was .created_at < @end_time::timestamptz
159
- AND was .connection_count > 0
160
- GROUP BY created_at_trunc, was .template_id , was .user_id
161
- )
146
+ -- GetTemplateInsightsByTemplate is used for Prometheus metrics. Keep
147
+ -- in sync with GetTemplateInsights and UpsertTemplateUsageStats.
148
+ WITH
149
+ -- This CTE is used to truncate agent usage into minute buckets, then
150
+ -- flatten the users agent usage within the template so that usage in
151
+ -- multiple workspaces under one template is only counted once for
152
+ -- every minute (per user).
153
+ insights AS (
154
+ SELECT
155
+ template_id,
156
+ user_id,
157
+ COUNT (DISTINCT CASE WHEN session_count_ssh > 0 THEN date_trunc(' minute' , created_at) ELSE NULL END) AS ssh_mins,
158
+ -- TODO(mafredri): Enable when we have the column.
159
+ -- COUNT(DISTINCT CASE WHEN session_count_sftp > 0 THEN date_trunc('minute', created_at) ELSE NULL END) AS sftp_mins,
160
+ COUNT (DISTINCT CASE WHEN session_count_reconnecting_pty > 0 THEN date_trunc(' minute' , created_at) ELSE NULL END) AS reconnecting_pty_mins,
161
+ COUNT (DISTINCT CASE WHEN session_count_vscode > 0 THEN date_trunc(' minute' , created_at) ELSE NULL END) AS vscode_mins,
162
+ COUNT (DISTINCT CASE WHEN session_count_jetbrains > 0 THEN date_trunc(' minute' , created_at) ELSE NULL END) AS jetbrains_mins,
163
+ -- NOTE(mafredri): The agent stats are currently very unreliable, and
164
+ -- sometimes the connections are missing, even during active sessions.
165
+ -- Since we can't fully rely on this, we check for "any connection
166
+ -- within this bucket". A better solution here would be preferable.
167
+ MAX (connection_count) > 0 AS has_connection
168
+ FROM
169
+ workspace_agent_stats
170
+ WHERE
171
+ created_at >= @start_time::timestamptz
172
+ AND created_at < @end_time::timestamptz
173
+ -- Inclusion criteria to filter out empty results.
174
+ AND (
175
+ session_count_ssh > 0
176
+ -- TODO(mafredri): Enable when we have the column.
177
+ -- OR session_count_sftp > 0
178
+ OR session_count_reconnecting_pty > 0
179
+ OR session_count_vscode > 0
180
+ OR session_count_jetbrains > 0
181
+ )
182
+ GROUP BY
183
+ template_id, user_id
184
+ )
162
185
163
186
SELECT
164
187
template_id,
165
- COALESCE(COUNT (DISTINCT user_id))::bigint AS active_users,
166
- COALESCE(SUM (usage_vscode_seconds), 0 )::bigint AS usage_vscode_seconds,
167
- COALESCE(SUM (usage_jetbrains_seconds), 0 )::bigint AS usage_jetbrains_seconds,
168
- COALESCE(SUM (usage_reconnecting_pty_seconds), 0 )::bigint AS usage_reconnecting_pty_seconds,
169
- COALESCE(SUM (usage_ssh_seconds), 0 )::bigint AS usage_ssh_seconds
170
- FROM agent_stats_by_interval_and_user
171
- GROUP BY template_id;
188
+ COUNT (DISTINCT user_id)::bigint AS active_users,
189
+ (SUM (vscode_mins) * 60 )::bigint AS usage_vscode_seconds,
190
+ (SUM (jetbrains_mins) * 60 )::bigint AS usage_jetbrains_seconds,
191
+ (SUM (reconnecting_pty_mins) * 60 )::bigint AS usage_reconnecting_pty_seconds,
192
+ (SUM (ssh_mins) * 60 )::bigint AS usage_ssh_seconds
193
+ FROM
194
+ insights
195
+ WHERE
196
+ has_connection
197
+ GROUP BY
198
+ template_id;
172
199
173
200
-- name: GetTemplateAppInsights :many
174
201
-- GetTemplateAppInsights returns the aggregate usage of each app in a given
0 commit comments