Skip to content

feat(site): display user status history as an indication of license usage #16020

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
move aggregation logic for GetUserStatusChanges into the SQL
  • Loading branch information
SasSwart committed Jan 3, 2025
commit ce1bda5f8b8ab3f3a362f51a1e5eec396cf83ecd
2 changes: 1 addition & 1 deletion coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -2413,7 +2413,7 @@ func (q *querier) GetUserNotificationPreferences(ctx context.Context, userID uui
return q.db.GetUserNotificationPreferences(ctx, userID)
}

func (q *querier) GetUserStatusChanges(ctx context.Context, arg database.GetUserStatusChangesParams) ([]database.UserStatusChange, error) {
func (q *querier) GetUserStatusChanges(ctx context.Context, arg database.GetUserStatusChangesParams) ([]database.GetUserStatusChangesRow, error) {
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceUser); err != nil {
return nil, err
}
Expand Down
21 changes: 18 additions & 3 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5666,7 +5666,7 @@ func (q *FakeQuerier) GetUserNotificationPreferences(_ context.Context, userID u
return out, nil
}

func (q *FakeQuerier) GetUserStatusChanges(_ context.Context, arg database.GetUserStatusChangesParams) ([]database.UserStatusChange, error) {
func (q *FakeQuerier) GetUserStatusChanges(_ context.Context, arg database.GetUserStatusChangesParams) ([]database.GetUserStatusChangesRow, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

Expand All @@ -5675,12 +5675,27 @@ func (q *FakeQuerier) GetUserStatusChanges(_ context.Context, arg database.GetUs
return nil, err
}

result := make([]database.UserStatusChange, 0)
result := make([]database.GetUserStatusChangesRow, 0)
for _, change := range q.userStatusChanges {
if change.ChangedAt.Before(arg.StartTime) || change.ChangedAt.After(arg.EndTime) {
continue
}
result = append(result, change)
if !slices.ContainsFunc(result, func(r database.GetUserStatusChangesRow) bool {
return r.ChangedAt.Equal(change.ChangedAt) && r.NewStatus == change.NewStatus
}) {
result = append(result, database.GetUserStatusChangesRow{
NewStatus: change.NewStatus,
ChangedAt: change.ChangedAt,
Count: 1,
})
} else {
for i, r := range result {
if r.ChangedAt.Equal(change.ChangedAt) && r.NewStatus == change.NewStatus {
result[i].Count++
break
}
}
}
}

return result, nil
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/dbmetrics/querymetrics.go

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

4 changes: 2 additions & 2 deletions coderd/database/dbmock/dbmock.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/querier.go

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

74 changes: 51 additions & 23 deletions coderd/database/queries.sql.go

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

37 changes: 32 additions & 5 deletions coderd/database/queries/insights.sql
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,36 @@ JOIN workspace_build_parameters wbp ON (utp.workspace_build_ids @> ARRAY[wbp.wor
GROUP BY utp.num, utp.template_ids, utp.name, utp.type, utp.display_name, utp.description, utp.options, wbp.value;

-- name: GetUserStatusChanges :many
WITH last_status_per_day AS (
-- First get the last status change for each user for each day
SELECT DISTINCT ON (date_trunc('day', changed_at), user_id)
date_trunc('day', changed_at)::timestamptz AS date,
new_status,
user_id
FROM user_status_changes
WHERE changed_at >= @start_time::timestamptz
AND changed_at < @end_time::timestamptz
ORDER BY
date_trunc('day', changed_at),
user_id,
changed_at DESC -- This ensures we get the last status for each day
),
daily_counts AS (
-- Then count unique users per status per day
SELECT
date,
new_status,
COUNT(*) AS count
FROM last_status_per_day
GROUP BY
date,
new_status
)
SELECT
*
FROM user_status_changes
WHERE changed_at >= @start_time::timestamptz
AND changed_at < @end_time::timestamptz
ORDER BY changed_at;
date::timestamptz AS changed_at,
new_status,
count::bigint
FROM daily_counts
ORDER BY
new_status ASC,
date ASC;
9 changes: 2 additions & 7 deletions coderd/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,19 +340,14 @@ func (api *API) insightsUserStatusCountsOverTime(rw http.ResponseWriter, r *http
StatusCounts: make(map[codersdk.UserStatus][]codersdk.UserStatusChangeCount),
}

slices.SortFunc(rows, func(a, b database.UserStatusChange) int {
return a.ChangedAt.Compare(b.ChangedAt)
})

for _, row := range rows {
date := row.ChangedAt.Truncate(24 * time.Hour)
status := codersdk.UserStatus(row.NewStatus)
if _, ok := resp.StatusCounts[status]; !ok {
resp.StatusCounts[status] = make([]codersdk.UserStatusChangeCount, 0)
}
resp.StatusCounts[status] = append(resp.StatusCounts[status], codersdk.UserStatusChangeCount{
Date: date,
Count: 1,
Date: row.ChangedAt,
Count: row.Count,
})
}

Expand Down