@@ -162,40 +162,31 @@ FROM
162
162
insights;
163
163
164
164
-- name: GetTemplateInsightsByTemplate :many
165
- WITH
166
- -- The purpose of this CTE is to flatten the data so that a user is
167
- -- only counted once per interval/template.
168
- insightsAS (
169
- SELECT
170
- template_id,
171
- user_id,
172
- -- See motivation in GetTemplateInsights for LEAST(SUM(n), 30).
173
- LEAST(SUM (usage_mins),30 )AS usage_mins,
174
- LEAST(SUM (ssh_mins),30 )AS ssh_mins,
175
- LEAST(SUM (sftp_mins),30 )AS sftp_mins,
176
- LEAST(SUM (reconnecting_pty_mins),30 )AS reconnecting_pty_mins,
177
- LEAST(SUM (vscode_mins),30 )AS vscode_mins,
178
- LEAST(SUM (jetbrains_mins),30 )AS jetbrains_mins
179
- FROM
180
- template_usage_stats
181
- WHERE
182
- start_time>= @start_time::timestamptz
183
- AND end_time<= @end_time::timestamptz
184
- GROUP BY
185
- start_time, template_id, user_id
186
- )
165
+ WITH agent_stats_by_interval_and_userAS (
166
+ SELECT
167
+ date_trunc(' minute' ,was .created_at )AS created_at_trunc,
168
+ was .template_id ,
169
+ was .user_id ,
170
+ CASE WHENSUM (was .session_count_vscode )> 0 THEN60 ELSE0 ENDAS usage_vscode_seconds,
171
+ CASE WHENSUM (was .session_count_jetbrains )> 0 THEN60 ELSE0 ENDAS usage_jetbrains_seconds,
172
+ CASE WHENSUM (was .session_count_reconnecting_pty )> 0 THEN60 ELSE0 ENDAS usage_reconnecting_pty_seconds,
173
+ CASE WHENSUM (was .session_count_ssh )> 0 THEN60 ELSE0 ENDAS usage_ssh_seconds
174
+ FROM workspace_agent_stats was
175
+ WHERE
176
+ was .created_at >= @start_time::timestamptz
177
+ AND was .created_at < @end_time::timestamptz
178
+ AND was .connection_count > 0
179
+ GROUP BY created_at_trunc,was .template_id ,was .user_id
180
+ )
187
181
188
182
SELECT
189
183
template_id,
190
- COUNT (DISTINCT user_id)AS active_users,
191
- (SUM (usage_mins)* 60 )::bigint AS usage_total_seconds,-- Includes app usage.
192
- (SUM (ssh_mins)* 60 )::bigint AS usage_ssh_seconds,
193
- (SUM (sftp_mins)* 60 )::bigint AS usage_sftp_seconds,
194
- (SUM (reconnecting_pty_mins)* 60 )::bigint AS usage_reconnecting_pty_seconds,
195
- (SUM (vscode_mins)* 60 )::bigint AS usage_vscode_seconds,
196
- (SUM (jetbrains_mins)* 60 )::bigint AS usage_jetbrains_seconds
197
- FROM
198
- insights
184
+ COALESCE(COUNT (DISTINCT user_id))::bigint AS active_users,
185
+ COALESCE(SUM (usage_vscode_seconds),0 )::bigint AS usage_vscode_seconds,
186
+ COALESCE(SUM (usage_jetbrains_seconds),0 )::bigint AS usage_jetbrains_seconds,
187
+ COALESCE(SUM (usage_reconnecting_pty_seconds),0 )::bigint AS usage_reconnecting_pty_seconds,
188
+ COALESCE(SUM (usage_ssh_seconds),0 )::bigint AS usage_ssh_seconds
189
+ FROM agent_stats_by_interval_and_user
199
190
GROUP BY template_id;
200
191
201
192
-- name: GetTemplateAppInsights :many