Skip to content

Commit 7109d92

Browse files
committed
Adjust SQL query
1 parent 1c6798f commit 7109d92

File tree

2 files changed

+90
-14
lines changed

2 files changed

+90
-14
lines changed

coderd/database/queries.sql.go

Lines changed: 45 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/insights.sql

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ ORDER BY user_id ASC;
2525
-- GetUserActivityInsights returns the ranking with top active users.
2626
-- The result can be filtered on template_ids, meaning only user data from workspaces
2727
-- based on those templates will be included.
28-
WITH app_stats_by_user_and_agent AS (
28+
WITH app_stats AS (
2929
SELECT
3030
s.start_time,
31-
60 as seconds,
31+
was.user_id,
3232
w.template_id,
33-
was.user_id
33+
60 as seconds
3434
FROM workspace_app_stats was
3535
JOIN workspaces w ON (
3636
w.id = was.workspace_id
@@ -50,17 +50,55 @@ WITH app_stats_by_user_and_agent AS (
5050
-- Subtract one minute because the series only contains the start time.
5151
AND s.start_time < (@end_time::timestamptz) - '1 minute'::interval
5252
GROUP BY s.start_time, w.template_id, was.user_id
53+
), session_stats AS (
54+
SELECT
55+
date_trunc('minute', was.created_at) as start_time,
56+
was.user_id,
57+
was.template_id,
58+
CASE WHEN
59+
SUM(was.session_count_vscode) > 0 OR
60+
SUM(was.session_count_jetbrains) > 0 OR
61+
SUM(was.session_count_reconnecting_pty) > 0 OR
62+
SUM(was.session_count_ssh) > 0
63+
THEN 60 ELSE 0 END as seconds
64+
FROM workspace_agent_stats was
65+
WHERE
66+
was.created_at >= @start_time::timestamptz
67+
AND was.created_at @end_time::timestamptz
68+
AND was.connection_count > 0
69+
AND CASE WHEN COALESCE(array_length(@template_ids::uuid[], 1), 0) > 0 THEN w.template_id = ANY(@template_ids::uuid[]) ELSE TRUE END
70+
GROUP BY date_trunc('minute', was.created_at), was.user_id, was.template_id
71+
), combined_stats AS (
72+
SELECT
73+
user_id,
74+
template_id,
75+
start_time,
76+
seconds
77+
FROM app_stats
78+
UNION ALL
79+
SELECT
80+
user_id,
81+
template_id,
82+
start_time,
83+
seconds
84+
FROM session_stats
85+
), distinct_combined_stats AS (
86+
SELECT DISTINCT
87+
user_id,
88+
template_id,
89+
start_time,
90+
seconds
91+
FROM combined_stats
5392
)
54-
5593
SELECT
5694
users.id as user_id,
5795
users.username,
5896
users.avatar_url,
5997
array_agg(DISTINCT template_id)::uuid[] AS template_ids,
6098
SUM(seconds) AS usage_seconds
61-
FROM app_stats_by_user_and_agent
62-
JOIN users ON (users.id = app_stats_by_user_and_agent.user_id)
63-
GROUP BY user_id, username, avatar_url
99+
FROM distinct_combined_stats
100+
JOIN users ON (users.id = distinct_combined_stats.user_id)
101+
GROUP BY users.id, username, avatar_url
64102
ORDER BY user_id ASC;
65103

66104
-- name: GetTemplateInsights :one

0 commit comments

Comments
 (0)