Skip to content

Commit 6e67f5b

Browse files
committed
Group user status counts by interval with support for a timezone offset
1 parent b4fde80 commit 6e67f5b

File tree

4 files changed

+25
-39
lines changed

4 files changed

+25
-39
lines changed

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,8 @@ func (s *MethodTestSuite) TestUser() {
17121712
check.Args(database.GetUserStatusCountsParams{
17131713
StartTime: time.Now().Add(-time.Hour * 24 * 30),
17141714
EndTime: time.Now(),
1715+
Interval: int32((time.Hour * 24).Seconds()),
1716+
TzOffset: 0,
17151717
}).Asserts(rbac.ResourceUser, policy.ActionRead)
17161718
}))
17171719
}

coderd/database/queries.sql.go

Lines changed: 14 additions & 20 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: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -789,25 +789,12 @@ WITH
789789
-- dates_of_interest defines all points in time that are relevant to the query.
790790
-- It includes the start_time, all status changes, all deletions, and the end_time.
791791
dates_of_interest AS (
792-
SELECT @start_time::timestamptz AS date
793-
794-
UNION
795-
796-
SELECT DISTINCT changed_at AS date
797-
FROM user_status_changes
798-
WHERE changed_at > @start_time::timestamptz
799-
AND changed_at < @end_time::timestamptz
800-
801-
UNION
802-
803-
SELECT DISTINCT deleted_at AS date
804-
FROM user_deleted
805-
WHERE deleted_at > @start_time::timestamptz
806-
AND deleted_at < @end_time::timestamptz
807-
808-
UNION
809-
810-
SELECT @end_time::timestamptz AS date
792+
SELECT
793+
(generate_series(
794+
@start_time::timestamptz,
795+
@end_time::timestamptz,
796+
(@interval::int || ' seconds')::interval
797+
) + (@tz_offset::int || ' seconds')::interval)::timestamptz AS date
811798
),
812799
-- latest_status_before_range defines the status of each user before the start_time.
813800
-- We do not include users who were deleted before the start_time. We use this to ensure that

coderd/insights.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ func (api *API) insightsUserStatusCounts(rw http.ResponseWriter, r *http.Request
306306
p := httpapi.NewQueryParamParser()
307307
vals := r.URL.Query()
308308
tzOffset := p.Int(vals, 0, "tz_offset")
309+
interval := p.Int(vals, int((24 * time.Hour).Seconds()), "interval")
309310
p.ErrorExcessParams(vals)
310311

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

0 commit comments

Comments
 (0)