Skip to content

Commit 71eea7a

Browse files
committed
WIP
1 parent 0baa80b commit 71eea7a

File tree

7 files changed

+167
-0
lines changed

7 files changed

+167
-0
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,10 @@ func (q *querier) GetUnexpiredLicenses(ctx context.Context) ([]database.License,
14641464
return q.db.GetUnexpiredLicenses(ctx)
14651465
}
14661466

1467+
func (q *querier) GetUserActivityInsights(ctx context.Context, arg database.GetUserActivityInsightsParams) ([]database.GetUserActivityInsightsRow, error) {
1468+
panic("not implemented")
1469+
}
1470+
14671471
func (q *querier) GetUserByEmailOrUsername(ctx context.Context, arg database.GetUserByEmailOrUsernameParams) (database.User, error) {
14681472
return fetch(q.log, q.auth, q.db.GetUserByEmailOrUsername)(ctx, arg)
14691473
}

coderd/database/dbfake/dbfake.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,15 @@ func (q *FakeQuerier) GetUnexpiredLicenses(_ context.Context) ([]database.Licens
28162816
return results, nil
28172817
}
28182818

2819+
func (q *FakeQuerier) GetUserActivityInsights(ctx context.Context, arg database.GetUserActivityInsightsParams) ([]database.GetUserActivityInsightsRow, error) {
2820+
err := validateDatabaseType(arg)
2821+
if err != nil {
2822+
return nil, err
2823+
}
2824+
2825+
panic("not implemented")
2826+
}
2827+
28192828
func (q *FakeQuerier) GetUserByEmailOrUsername(_ context.Context, arg database.GetUserByEmailOrUsernameParams) (database.User, error) {
28202829
if err := validateDatabaseType(arg); err != nil {
28212830
return database.User{}, err

coderd/database/dbmetrics/dbmetrics.go

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

coderd/database/dbmock/dbmock.go

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

coderd/database/querier.go

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

coderd/database/queries.sql.go

Lines changed: 86 additions & 0 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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,48 @@ WHERE
2121
GROUP BY workspace_agent_stats.user_id, users.username, users.avatar_url
2222
ORDER BY user_id ASC;
2323

24+
-- name: GetUserActivityInsights :many
25+
-- GetUserActivityInsights returns the ranking with top active users.
26+
-- The result can be filtered on template_ids, meaning only user data from workspaces
27+
-- based on those templates will be included.
28+
WITH app_stats_by_user_and_agent AS (
29+
SELECT
30+
s.start_time,
31+
60 as seconds,
32+
w.template_id,
33+
was.user_id
34+
FROM workspace_app_stats was
35+
JOIN workspaces w ON (
36+
w.id = was.workspace_id
37+
AND CASE WHEN COALESCE(array_length(@template_ids::uuid[], 1), 0) > 0 THEN w.template_id = ANY(@template_ids::uuid[]) ELSE TRUE END
38+
)
39+
-- This table contains both 1 minute entries and >1 minute entries,
40+
-- to calculate this with our uniqueness constraints, we generate series
41+
-- for the longer intervals.
42+
CROSS JOIN LATERAL generate_series(
43+
date_trunc('minute', was.session_started_at),
44+
-- Subtract 1 microsecond to avoid creating an extra series.
45+
date_trunc('minute', was.session_ended_at - '1 microsecond'::interval),
46+
'1 minute'::interval
47+
) s(start_time)
48+
WHERE
49+
s.start_time >= @start_time::timestamptz
50+
-- Subtract one minute because the series only contains the start time.
51+
AND s.start_time < (@end_time::timestamptz) - '1 minute'::interval
52+
GROUP BY s.start_time, w.template_id, was.user_id
53+
)
54+
55+
SELECT
56+
users.id,
57+
users.username,
58+
users.avatar_url,
59+
array_agg(DISTINCT template_id)::uuid[] AS template_ids,
60+
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
64+
ORDER BY user_id ASC;
65+
2466
-- name: GetTemplateInsights :one
2567
-- GetTemplateInsights has a granularity of 5 minutes where if a session/app was
2668
-- in use during a minute, we will add 5 minutes to the total usage for that

0 commit comments

Comments
 (0)