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 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
Prev Previous commit
Next Next commit
update the query to created_at_before and created_at_after
  • Loading branch information
joobisb committed Dec 4, 2024
commit e29a208749c1d72bd7f4abb3a27bb4a57201d43c
14 changes: 12 additions & 2 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5648,10 +5648,20 @@ func (q *FakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
users = usersFilteredByRole
}

if !params.CreatedAt.IsZero() {
if !params.CreatedAtBefore.IsZero() {
usersFilteredByCreatedAt := make([]database.User, 0, len(users))
for i, user := range users {
if user.CreatedAt.Equal(params.CreatedAt) {
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])
}
}
Expand Down
3 changes: 2 additions & 1 deletion coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ func (q *sqlQuerier) GetAuthorizedUsers(ctx context.Context, arg GetUsersParams,
pq.Array(arg.RbacRole),
arg.LastSeenBefore,
arg.LastSeenAfter,
arg.CreatedAt,
arg.CreatedAtBefore,
arg.CreatedAtAfter,
arg.OffsetOpt,
arg.LimitOpt,
)
Expand Down
33 changes: 20 additions & 13 deletions coderd/database/queries.sql.go

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

9 changes: 7 additions & 2 deletions coderd/database/queries/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,13 @@ WHERE
END
-- Filter by created_at
AND CASE
WHEN @created_at :: timestamp with time zone != '0001-01-01 00:00:00Z' THEN
DATE(created_at) = DATE(@created_at)
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
Expand Down
13 changes: 7 additions & 6 deletions coderd/searchquery/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +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"),
CreatedAt: parser.Time3339Nano(values, time.Time{}, "created_at"),
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
19 changes: 10 additions & 9 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +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,
CreatedAt: params.CreatedAt,
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
30 changes: 25 additions & 5 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,6 @@ func TestUsersFilter(t *testing.T) {

firstUser, err := client.User(ctx, codersdk.Me)
require.NoError(t, err, "fetch me")
createdAt := firstUser.CreatedAt

// Noon on Jan 18 is the "now" for this test for last_seen timestamps.
// All these values are equal
Expand Down Expand Up @@ -1657,13 +1656,34 @@ func TestUsersFilter(t *testing.T) {
},
},
{
Name: "CreatedAt",
Name: "CreatedAtBefore",
Filter: codersdk.UsersRequest{
SearchQuery: fmt.Sprintf(`created_at:%q`, createdAt.Format(time.RFC3339)),
SearchQuery: `created_at_before:"2023-01-31T23:59:59Z"`,
},
FilterF: func(_ codersdk.UsersRequest, u codersdk.User) bool {
target := time.Date(2023, 1, 18, 12, 0, 0, 0, time.UTC)
return u.CreatedAt.Equal(target)
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)
},
},
}
Expand Down
6 changes: 4 additions & 2 deletions docs/admin/users/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ to use the Coder's filter query:
- To find admin users, use the filter `role:admin`.
- To find users who have not been active since July 2023:
`status:active last_seen_before:"2023-07-01T00:00:00Z"`
- To find users created at January 18, 2023: `created_at:"2023-01-18T00: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 @@ -199,4 +200,5 @@ The following filters are supported:
- `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` - The time a user was created. 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