Skip to content

feat: support created_at filter for the GET /users endpoint #15633

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

Merged
merged 17 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
20 changes: 20 additions & 0 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5648,6 +5648,26 @@ func (q *FakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
users = usersFilteredByRole
}

if !params.CreatedAtBefore.IsZero() {
usersFilteredByCreatedAt := make([]database.User, 0, len(users))
for i, user := range users {
if user.CreatedAt.Before(params.CreatedAtBefore) {
usersFilteredByCreatedAt = append(usersFilteredByCreatedAt, users[i])
}
}
users = usersFilteredByCreatedAt
}

if !params.CreatedAtAfter.IsZero() {
usersFilteredByCreatedAt := make([]database.User, 0, len(users))
for i, user := range users {
if user.CreatedAt.After(params.CreatedAtAfter) {
usersFilteredByCreatedAt = append(usersFilteredByCreatedAt, users[i])
}
}
users = usersFilteredByCreatedAt
}

if !params.LastSeenBefore.IsZero() {
usersFilteredByLastSeen := make([]database.User, 0, len(users))
for i, user := range users {
Expand Down
2 changes: 2 additions & 0 deletions coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ func (q *sqlQuerier) GetAuthorizedUsers(ctx context.Context, arg GetUsersParams,
pq.Array(arg.RbacRole),
arg.LastSeenBefore,
arg.LastSeenAfter,
arg.CreatedAtBefore,
arg.CreatedAtAfter,
arg.OffsetOpt,
arg.LimitOpt,
)
Expand Down
35 changes: 25 additions & 10 deletions coderd/database/queries.sql.go

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

11 changes: 11 additions & 0 deletions coderd/database/queries/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ WHERE
last_seen_at >= @last_seen_after
ELSE true
END
-- Filter by created_at
AND CASE
WHEN @created_at_before :: timestamp with time zone != '0001-01-01 00:00:00Z' THEN
created_at <= @created_at_before
ELSE true
END
AND CASE
WHEN @created_at_after :: timestamp with time zone != '0001-01-01 00:00:00Z' THEN
created_at >= @created_at_after
ELSE true
END
-- End of filters

-- Authorize Filter clause will be injected below in GetAuthorizedUsers
Expand Down
12 changes: 7 additions & 5 deletions coderd/searchquery/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ func Users(query string) (database.GetUsersParams, []codersdk.ValidationError) {

parser := httpapi.NewQueryParamParser()
filter := database.GetUsersParams{
Search: parser.String(values, "", "search"),
Status: httpapi.ParseCustomList(parser, values, []database.UserStatus{}, "status", httpapi.ParseEnum[database.UserStatus]),
RbacRole: parser.Strings(values, []string{}, "role"),
LastSeenAfter: parser.Time3339Nano(values, time.Time{}, "last_seen_after"),
LastSeenBefore: parser.Time3339Nano(values, time.Time{}, "last_seen_before"),
Search: parser.String(values, "", "search"),
Status: httpapi.ParseCustomList(parser, values, []database.UserStatus{}, "status", httpapi.ParseEnum[database.UserStatus]),
RbacRole: parser.Strings(values, []string{}, "role"),
LastSeenAfter: parser.Time3339Nano(values, time.Time{}, "last_seen_after"),
LastSeenBefore: parser.Time3339Nano(values, time.Time{}, "last_seen_before"),
CreatedAtAfter: parser.Time3339Nano(values, time.Time{}, "created_at_after"),
CreatedAtBefore: parser.Time3339Nano(values, time.Time{}, "created_at_before"),
}
parser.ErrorExcessParams(values)
return filter, parser.Errors
Expand Down
18 changes: 10 additions & 8 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,16 @@ func (api *API) GetUsers(rw http.ResponseWriter, r *http.Request) ([]database.Us
}

userRows, err := api.Database.GetUsers(ctx, database.GetUsersParams{
AfterID: paginationParams.AfterID,
Search: params.Search,
Status: params.Status,
RbacRole: params.RbacRole,
LastSeenBefore: params.LastSeenBefore,
LastSeenAfter: params.LastSeenAfter,
OffsetOpt: int32(paginationParams.Offset),
LimitOpt: int32(paginationParams.Limit),
AfterID: paginationParams.AfterID,
Search: params.Search,
Status: params.Status,
RbacRole: params.RbacRole,
LastSeenBefore: params.LastSeenBefore,
LastSeenAfter: params.LastSeenAfter,
CreatedAtAfter: params.CreatedAtAfter,
CreatedAtBefore: params.CreatedAtBefore,
OffsetOpt: int32(paginationParams.Offset),
LimitOpt: int32(paginationParams.Limit),
})
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Expand Down
31 changes: 31 additions & 0 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,37 @@ func TestUsersFilter(t *testing.T) {
return u.LastSeenAt.Before(end) && u.LastSeenAt.After(start)
},
},
{
Name: "CreatedAtBefore",
Filter: codersdk.UsersRequest{
SearchQuery: `created_at_before:"2023-01-31T23:59:59Z"`,
},
FilterF: func(_ codersdk.UsersRequest, u codersdk.User) bool {
end := time.Date(2023, 1, 31, 23, 59, 59, 0, time.UTC)
return u.CreatedAt.Before(end)
},
},
{
Name: "CreatedAtAfter",
Filter: codersdk.UsersRequest{
SearchQuery: `created_at_after:"2023-01-01T00:00:00Z"`,
},
FilterF: func(_ codersdk.UsersRequest, u codersdk.User) bool {
start := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
return u.CreatedAt.After(start)
},
},
{
Name: "CreatedAtRange",
Filter: codersdk.UsersRequest{
SearchQuery: `created_at_after:"2023-01-01T00:00:00Z" created_at_before:"2023-01-31T23:59:59Z"`,
},
Copy link
Member

Choose a reason for hiding this comment

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

All the users created in the setup of this test have the same creation date. We should add some users created within the range, and before the CreatedBefore testcase timestamp. Right now those testcases are just comparing empty slices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ethanndickson

I've updated the PR with the tests, please do have a look

FilterF: func(_ codersdk.UsersRequest, u codersdk.User) bool {
start := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(2023, 1, 31, 23, 59, 59, 0, time.UTC)
return u.CreatedAt.After(start) && u.CreatedAt.Before(end)
},
},
}

for _, c := range testCases {
Expand Down
8 changes: 6 additions & 2 deletions docs/admin/users/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ to use the Coder's filter query:

- To find active users, use the filter `status:active`.
- To find admin users, use the filter `role:admin`.
- To find users have not been active since July 2023:
- To find users who have not been active since July 2023:
`status:active last_seen_before:"2023-07-01T00:00:00Z"`
- To find users who were created between January 1 and January 18, 2023:
`created_at_before:"2023-01-18T00:00:00Z" created_at_after:"2023-01-01T23:59:59Z"`

The following filters are supported:

Expand All @@ -195,6 +197,8 @@ The following filters are supported:
- `role` - Represents the role of the user. You can refer to the
[TemplateRole documentation](https://pkg.go.dev/github.com/coder/coder/v2/codersdk#TemplateRole)
for a list of supported user roles.
- `last_seen_before` and `last_seen_after` - The last time a used has used the
- `last_seen_before` and `last_seen_after` - The last time a user has used the
platform (e.g. logging in, any API requests, connecting to workspaces). Uses
the RFC3339Nano format.
- `created_at_before` and `created_at_after` - The time a user was created. Uses
Copy link
Member

Choose a reason for hiding this comment

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

I don't think created_at_{after|before} reads very well. I didn't really think about the phrasing when I wrote it earlier. Let's just go with created_before and created_after.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ethanndickson

updated the PR!

the RFC3339Nano format.
Loading