Skip to content
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

feat: add count endpoint for users, enabling better pagination #4848

Merged
merged 18 commits into from
Nov 8, 2022

Conversation

presleyp
Copy link
Contributor

@presleyp presleyp commented Nov 1, 2022

  • adds endpoint for getting number of users that pass a filter
  • hooks up the frontend to this endpoint, refreshing only when the filter changes

Here's the last page, showing that we know how many pages there are and that the "next page" button is disabled:
Screen Shot 2022-11-02 at 3 12 24 PM

@presleyp presleyp marked this pull request as ready for review November 3, 2022 21:39
@presleyp presleyp requested a review from a team as a code owner November 3, 2022 21:39
@presleyp presleyp requested review from Kira-Pilot, f0ssel and coadler and removed request for a team November 3, 2022 21:39
@presleyp
Copy link
Contributor Author

presleyp commented Nov 3, 2022

The active users test is failing on CI but passing locally (I remembered DB=ci), any idea why?

@@ -240,27 +240,35 @@ describe("UsersPage", () => {

describe("pagination", () => {
it("goes to next and previous page", async () => {
renderPage()
const { container } = renderPage()
Copy link
Member

Choose a reason for hiding this comment

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

I love that we have tests and I'm happy to keep these in there. I'm curious if we should just add tests for the pagination widget itself, rather than tests for each time it's implemented.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I added a bit to the widget in a previous PR but I think there's also value in making sure the page is using the widget properly

@@ -60,7 +61,7 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
// - users are loading or
// - the user can edit the users but the roles are loading
const isLoading =
usersState.matches("gettingUsers") ||
usersState.matches("users.gettingUsers") ||
Copy link
Member

Choose a reason for hiding this comment

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

Was this just not working before? How come we have to add users now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I put all the existing states inside a "users" state node (submachine) so that the count-related states can run in parallel to them

@@ -57,6 +57,8 @@ export interface UsersContext {
updateUserRolesError?: Error | unknown
paginationContext: PaginationContext
paginationRef: PaginationMachineRef
count: number
getCountError: Error | unknown
Copy link
Member

Choose a reason for hiding this comment

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

I see we aren't using this anywhere. To reduce complexity, shall we leave it out?
If we don't want to leave it out, should we at least console.error the count error so we can fix it if we come across it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thank you, I meant to make sure all the errors were handled! though it might be okay for this to fail silently...maybe a console error is the best option, what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I completely agree! No need to warn the user but a console.error for us devs might be nice.

Copy link
Member

@Kira-Pilot Kira-Pilot left a comment

Choose a reason for hiding this comment

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

FE ✅

@@ -245,6 +245,7 @@ func AGPLRoutes(a *AuthTester) (map[string]string, map[string]RouteCheck) {
// Endpoints that use the SQLQuery filter.
"GET:/api/v2/workspaces/": {StatusCode: http.StatusOK, NoAuthorize: true},
"GET:/api/v2/workspaces/count": {StatusCode: http.StatusOK, NoAuthorize: true},
"GET:/api/v2/users/count": {StatusCode: http.StatusOK, NoAuthorize: true},
Copy link
Contributor

Choose a reason for hiding this comment

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

not doing an auth check here, combined with the fact that you can query for username or email substrings means that you can easily dump all usernames and emails by repeatedly hitting this endpoint and doing a tree search.

Copy link
Member

Choose a reason for hiding this comment

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

To follow up, you can follow how we do this for workspaces.

type workspaceQuerier interface {
GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspacesParams, authorizedFilter rbac.AuthorizeFilter) ([]Workspace, error)
GetAuthorizedWorkspaceCount(ctx context.Context, arg GetWorkspaceCountParams, authorizedFilter rbac.AuthorizeFilter) (int64, error)
}

If you add api.Authorize(r, rbac.ActionRead, rbac.ResourceUser) it is technically ok as it checks if you can read all users. We should really do it proper though so we can mess with perms later and everything still works.

Add a new function called GetAuthorizedUsersCount

Copy link
Contributor

@f0ssel f0ssel left a comment

Choose a reason for hiding this comment

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

I liked that you seem to carbon copy from workspaces, the uniformity made this easy to review. Based on what we discussed this will be changed down the road but this is good and functional for the short term. 👍

@presleyp presleyp merged commit f496b14 into main Nov 8, 2022
@presleyp presleyp deleted the users-count/presleyp branch November 8, 2022 15:58
@github-actions github-actions bot locked and limited conversation to collaborators Nov 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants