Skip to content

Commit

Permalink
scripts: Support pagination in rsync-active-users
Browse files Browse the repository at this point in the history
JupyterHub will otherwise silently only return the first
200 users, which is not very good.

Ref 2i2c-org#4238
  • Loading branch information
yuvipanda committed Jun 25, 2024
1 parent c03da1f commit bdf85b9
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions extra-scripts/rsync-active-users.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,28 @@ def escape(to_escape, safe=SAFE, escape_char=ESCAPE_CHAR, allow_collisions=False


def get_all_users(hub_url, token):
url = f"{hub_url}/hub/api/users"
resp = requests.get(url, headers={"Authorization": f"token {token}"})

users = resp.json()
for user in users:
if user["last_activity"]:
user["last_activity"] = parse(user.get("last_activity"))
return users
offset = 0
limit = 200 # The default max size for a page

while True:
url = f"{hub_url}/hub/api/users?offset={offset}&limit={limit}"
resp = requests.get(url, headers={
"Authorization": f"token {token}",
"Accept": "application/jupyterhub-pagination+json"
})

paginated_resp = resp.json()
users = paginated_resp['items']
pagination = paginated_resp.get('_pagination', {}).get('next', None)
if pagination is None:
break
else:
offset = pagination['offset']
limit = pagination['limit']
for user in users:
if user["last_activity"]:
user["last_activity"] = parse(user.get("last_activity"))
yield user


def rsync(user, src_basedir, dest_basedir, dry_run):
Expand Down

0 comments on commit bdf85b9

Please sign in to comment.