Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.

Implement pagination for the Users page #541

Merged
merged 3 commits into from
Sep 14, 2020

Conversation

whaught
Copy link
Contributor

@whaught whaught commented Sep 14, 2020

Fixes #538

Proposed Changes

  • Adds pagination when there is a large number of users per realm
    • Still probably not ideal for order ~thousands

pages

Release Note

Pagination for Users page

@googlebot googlebot added the cla: yes Auto: added by CLA bot when all committers have signed a CLA. label Sep 14, 2020
@mikehelmick
Copy link
Contributor

/lgtm
/approve

Based on screenshot , couple of future improvements

  • would be good to have a "return to first" and "go to last" type link
  • Different sorts (name or email)
  • Search

@google-oss-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mikehelmick, whaught

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [mikehelmick,whaught]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@google-oss-robot google-oss-robot merged commit b8e3d8d into google:main Sep 14, 2020
@@ -308,6 +308,7 @@ func realMain(ctx context.Context) error {

userController := user.New(ctx, cacher, config, db, h)
userSub.Handle("", userController.HandleIndex()).Methods("GET")
userSub.Handle("", userController.HandleIndex()).Queries("offset", "{[0-9]*?}").Methods("GET")
Copy link
Member

Choose a reason for hiding this comment

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

I think this might be better as:

userSub.Handle("page/{offset:[0-9]+}", usersController.HandleIndex()).Methods("GET")

And then pull the pagination from mux.Vars(). That will give us nicer urls like /users/page/2

func (c *Controller) HandleIndex() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
qvar := r.FormValue("offset")
offset, _ := strconv.Atoi(qvar)
Copy link
Member

Choose a reason for hiding this comment

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

Need to handle this error, probably an internal server error.


realm := controller.RealmFromContext(ctx)
if realm == nil {
controller.MissingRealm(w, r, c.h)
return
}

users, err := realm.ListUsers(c.db)
count, err := realm.CountUsers(c.db)
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, this is pretty expensive (count is a full table lock). I feel like we should have a perPage constant and then just pipe the current count into a SQL OFFSET

// ListUsers returns the list of users on this realm.
func (r *Realm) ListUsers(db *Database) ([]*User, error) {
func (r *Realm) ListUsers(db *Database, offset, limit int) ([]*User, error) {
if limit > 100 {
Copy link
Member

Choose a reason for hiding this comment

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

100 feels arbitrary

@sethvargo
Copy link
Member

Oh looks like @mikehelmick was reviewing at the same time. I left some comments @whaught

@google google locked and limited conversation to collaborators Oct 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes Auto: added by CLA bot when all committers have signed a CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

User pagination
5 participants