kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Paginate the facility username endpoint and require a minimum search length

Open rtibbles opened this issue 5 months ago • 4 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Overview

The /api/auth/facilityusername/ endpoint returns every matching username in a facility with no pagination. On a 10,000-learner facility, a short search prefix can match the entire facility — measured at 48ms server-side per request (vs 12ms at 50 learners). It is called per keystroke from the sign-in page of facilities with passwordless learner login, so this cost lands on the most-trafficked page during sign-in bursts. The endpoint should paginate its response (non-optional) and require a minimum search length, and the sign-in page frontend should be updated to consume the paginated response.

Complexity: Medium Target branch: develop

Context

FacilityUsernameViewSet (kolibri/core/auth/api.py:820) is a ReadOnlyValuesViewset with no pagination: it returns every username matching the facility filter and optional ^username prefix search. Of the measured 48ms at 10k learners, only ~2ms is SQL — the cost is fetching and serializing the full row set in Python.

The sole consumer is the sign-in page (kolibri/plugins/user_auth/frontend/views/SignInPage/index.vue), which calls it in two ways for facilities with learner_can_login_with_no_password:

  • on facility selection with no search param (fetches the entire facility's usernames; drives the ≤16-user listing view via MAX_USERS_FOR_LISTING_VIEW)
  • per keystroke (debounced) with search as the typed prefix, for the username suggestions dropdown

The Change

Backend — FacilityUsernameViewSet responses become paginated, always:

  • Page size is controlled by a max_results query parameter, following the existing convention (page_size_query_param = "max_results", as in OptionalPagination in kolibri/core/content/api.py), defaulting to 10 and capped at 20 (requests for more are clamped, not rejected). There is no way to request the full unpaginated list.
  • A request with a search param shorter than 3 characters is rejected with a 400.
  • A request with no search param returns the first page.

Frontend — the sign-in page adapts to the new contract:

  • The username suggestions dropdown only issues requests once the typed prefix reaches 3 characters, and renders from a single page of results.
  • The facility-selection call no longer fetches every username: the ≤16-user listing view must still appear for small facilities and large facilities must not transfer their full username list. The response's pagination shape (rather than the array length of a full fetch) determines whether the facility exceeds MAX_USERS_FOR_LISTING_VIEW.

How to Get There

  1. On a device with a facility that has Allow learners to sign in with no password enabled, sign out and go to the sign-in page.
  2. Small facility (≤16 users): the clickable user list appears after selecting the facility — this is the no-search call.
  3. Larger facility: select the username field and start typing — each (debounced) keystroke issues the search call for the suggestions dropdown.

To reproduce the measured cost at scale, a 10k-learner facility can be provisioned with integration_testing/load_testing/loadtest.py setup --users 10000.

Out of Scope

  • /api/auth/facility/ — its num_users/num_classrooms COUNT annotations also scale with facility size (~2× at 10k learners) but are DB-side and cheap; separate issue if pursued.
  • The uncapped max_results on the content API pagination classes — this issue caps only the new pagination on facilityusername.
  • remotefacilityuser and the change-facility flow in user_profile — different endpoint, not a consumer of facilityusername.

Acceptance Criteria

General

  • [ ] GET /api/auth/facilityusername/ responses are always paginated; no request can retrieve the full username list
  • [ ] max_results controls page size, defaulting to 10 and clamped to 20
  • [ ] A search param shorter than 3 characters returns a 400 (generic validation error)
  • [ ] A request with no search param returns the first page
  • [ ] Sign-in page: username suggestions only request once the typed prefix is ≥3 characters
  • [ ] Sign-in page: the ≤16-user listing view still appears for small facilities, without fetching the facility's full username list on larger ones

Testing

  • [ ] Backend API tests cover: pagination shape, default page size, max_results clamping, short-search 400, and the no-search first page

References

  • FacilityUsernameViewSet: kolibri/core/auth/api.py (currently unpaginated ReadOnlyValuesViewset)
  • Sign-in page consumer: kolibri/plugins/user_auth/frontend/views/SignInPage/index.vue (both call sites)
  • Existing max_results pagination convention: OptionalPagination in kolibri/core/content/api.py

AI usage

I used Claude Code throughout. It ran the load-test and single-user profiling experiments that identified and measured this endpoint's scaling behaviour (under my direction), and drafted this issue section by section with me reviewing each one. The design decisions (always-on pagination, max_results default/cap, minimum search length, 400 behaviour) are mine.

rtibbles avatar Jun 06 '26 18:06 rtibbles

Plan: Add mandatory pagination (default 10, max 20) and a 3-character minimum search requirement to FacilityUsernameViewSet, then update the sign-in page frontend to consume the new paginated response.

  • [ ] Write failing backend tests
  • [ ] Implement backend pagination and search validation
  • [ ] Write failing frontend tests for SignInPage
  • [ ] Implement frontend changes to consume paginated response

status


@rtibblesbot's comments are generated by an LLM, and should be evaluated accordingly

How was this generated?
  • Implemented code following the pre-approved plan using test-driven development (red-green-refactor)
  • Ran the project's full test suite and linter before committing
  • For UI changes: captured live screenshots to verify rendering
  • For backend changes: verified with example API/CLI output

rtibblesbot avatar Jun 06 '26 19:06 rtibblesbot

The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.

rtibblesbot avatar Jun 06 '26 22:06 rtibblesbot

The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.

rtibblesbot avatar Jun 08 '26 00:06 rtibblesbot

The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.

rtibblesbot avatar Jun 08 '26 08:06 rtibblesbot