Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Group user status counts by interval with support for a timezone offset
  • Loading branch information
SasSwart committed Jan 15, 2025
commit 6e67f5b70dedff6fe943202e02f6534a4e9cb81a
2 changes: 2 additions & 0 deletions coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,8 @@ func (s *MethodTestSuite) TestUser() {
check.Args(database.GetUserStatusCountsParams{
StartTime: time.Now().Add(-time.Hour * 24 * 30),
EndTime: time.Now(),
Interval: int32((time.Hour * 24).Seconds()),
TzOffset: 0,
}).Asserts(rbac.ResourceUser, policy.ActionRead)
}))
}
Expand Down
34 changes: 14 additions & 20 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 6 additions & 19 deletions coderd/database/queries/insights.sql
Original file line number Diff line number Diff line change
Expand Up @@ -789,25 +789,12 @@ WITH
-- dates_of_interest defines all points in time that are relevant to the query.
-- It includes the start_time, all status changes, all deletions, and the end_time.
dates_of_interest AS (
SELECT @start_time::timestamptz AS date

UNION

SELECT DISTINCT changed_at AS date
FROM user_status_changes
WHERE changed_at > @start_time::timestamptz
AND changed_at < @end_time::timestamptz

UNION

SELECT DISTINCT deleted_at AS date
FROM user_deleted
WHERE deleted_at > @start_time::timestamptz
AND deleted_at < @end_time::timestamptz

UNION

SELECT @end_time::timestamptz AS date
SELECT
(generate_series(
@start_time::timestamptz,
@end_time::timestamptz,
(@interval::int || ' seconds')::interval
) + (@tz_offset::int || ' seconds')::interval)::timestamptz AS date
),
-- latest_status_before_range defines the status of each user before the start_time.
-- We do not include users who were deleted before the start_time. We use this to ensure that
Expand Down
3 changes: 3 additions & 0 deletions coderd/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func (api *API) insightsUserStatusCounts(rw http.ResponseWriter, r *http.Request
p := httpapi.NewQueryParamParser()
vals := r.URL.Query()
tzOffset := p.Int(vals, 0, "tz_offset")
interval := p.Int(vals, int((24 * time.Hour).Seconds()), "interval")
p.ErrorExcessParams(vals)

if len(p.Errors) > 0 {
Expand All @@ -327,6 +328,8 @@ func (api *API) insightsUserStatusCounts(rw http.ResponseWriter, r *http.Request
rows, err := api.Database.GetUserStatusCounts(ctx, database.GetUserStatusCountsParams{
StartTime: sixtyDaysAgo,
EndTime: nextHourInLoc,
Interval: int32(interval),
TzOffset: int32(tzOffset),
})
if err != nil {
if httpapi.IsUnauthorizedError(err) {
Expand Down