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