Skip to content

feat: add total users insight #15486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Pass the zone into the query
  • Loading branch information
BrunoQuaresma committed Nov 14, 2024
commit 0e94c746329467b538c615fc3be3b8e3f5867268
1 change: 1 addition & 0 deletions coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2350,6 +2350,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
check.Args(database.GetAccumulatedUsersInsightsParams{
StartTime: dbtime.Time(time.Now()),
EndTime: dbtime.Time(time.Now()),
Timezone: "UTC",
}).Asserts(rbac.ResourceSystem, policy.ActionRead)
}))
s.Run("GetActiveUserCount", s.Subtest(func(db database.Store, check *expects) {
Expand Down
9 changes: 5 additions & 4 deletions coderd/database/queries.sql.go

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

2 changes: 1 addition & 1 deletion coderd/database/queries/insights.sql
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ WITH RECURSIVE date_series AS (
WHERE date + INTERVAL '1 day' <= @end_time::timestamptz
)
SELECT
d.date::date AS date,
(d.date AT TIME ZONE @timezone::text)::date AS date,
(SELECT COUNT(*) FROM users u WHERE u.created_at <= d.date) AS total
FROM
date_series d
Expand Down
2 changes: 2 additions & 0 deletions coderd/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,11 @@ func (api *API) insightsTotalUsers(rw http.ResponseWriter, r *http.Request) {
return
}

zone, _ := startTime.Zone()
Copy link
Member

@mafredri mafredri Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might find this in your tests too but Zone it not guaranteed to return a name. It's unfortunately safer to use the offset (integer) and turn it into a string like 2 => "2" and `-2 => "-2".

That's a bit unfortunate however since there may be a minor edge case during DST shift and could potentially produce an unwanted extra/missing row depending on the data. I haven't tried to verify if/how much of a problem it would be though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to use offset!

rows, err := api.Database.GetAccumulatedUsersInsights(ctx, database.GetAccumulatedUsersInsightsParams{
StartTime: startTime,
EndTime: endTime,
Timezone: zone,
})
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Expand Down
Loading