Skip to content

Implement pagination for Admin API raw queries to be consistent with built-in result set pagination #8

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

Closed
s1monj opened this issue Nov 22, 2024 · 5 comments
Assignees
Labels
backend Python and SQL code

Comments

@s1monj
Copy link
Collaborator

s1monj commented Nov 22, 2024

While we're on the subject of pagination

  • Listing observations and patients from the web console (Admin API) could be large and needs to use pagination
  • Verify that the default pagination system for the Admin API endpoints uses DB limit/offset and not in-memory pagination
  • If it does not use DB pagination update the default paginators for all Admin API endpoints (except for Organizations that uses a custom response) accordingly
@s1monj s1monj added the enhancement New feature or request label Nov 22, 2024
@s1monj s1monj added the backend Python and SQL code label Nov 22, 2024
@s1monj s1monj changed the title Use DB pagination rather than Django in-memory Check pagination is DB not in-memory fro Admin API Mar 28, 2025
@s1monj s1monj removed the enhancement New feature or request label Mar 28, 2025
@s1monj s1monj changed the title Check pagination is DB not in-memory fro Admin API Check pagination is DB not in-memory for Admin API Mar 28, 2025
@s1monj s1monj moved this from Backlog to Ready in jupyterhealth-exchange Mar 31, 2025
@travis-sauer-oltech travis-sauer-oltech moved this from Ready to In Progress in jupyterhealth-exchange Mar 31, 2025
@travis-sauer-oltech
Copy link
Collaborator

@s1monj, we can close this ticket. After thorough research, I found some resources I will include for future reference.

We're using GenericViewSets and mostly ModelViewSets.

Both classes use QuerySet slicing. Fetch to the DB happens, and then Django's ORM uses lazy QuerySets, which slice at the database level, effectively translating into SQL LIMIT and OFFSET clauses.

  1. ListModelMixin:

GitHub

  1. QuerySet methods:

Django Docs

and

Django Docs

@s1monj s1monj changed the title Check pagination is DB not in-memory for Admin API Implement pagination for raw queries to be consistent with built-in result set pagination Apr 2, 2025
@s1monj s1monj changed the title Implement pagination for raw queries to be consistent with built-in result set pagination Implement pagination for Admin API raw queries to be consistent with built-in result set pagination Apr 2, 2025
@s1monj
Copy link
Collaborator Author

s1monj commented Apr 2, 2025

RE: https://stackoverflow.com/questions/5152984/django-pagination-and-rawqueryset#:~:text=Unfortunately%2C%20RawQuerySet,will%20hit%20the%20DB%20twice

  • We will need to implement our own pagination for raw queries
  • Params should be the same as default result set pagination params
  • Similar to the pagination for FHIR Observations - it's up to you if you just want to pass along the params to the model function or implement a custom paginator - whatever makes most sense

@s1monj
Copy link
Collaborator Author

s1monj commented Apr 2, 2025

Also we will need to consider how to return the total - so probably best to run 2 queries again as per slack discussion

query1 = FROM core_patients WHERE…
query1_total = SELECT COUNT(*) ${query1}
query1_page = SELECT * ${query1} LIMIT ${limit} OFFSET ${offset}

so then when the request is made we execute query1_total and query1_page as two separate calls to the DB

@s1monj
Copy link
Collaborator Author

s1monj commented Apr 3, 2025

@travis-sauer-oltech I needed to revert DEFAULT_PAGINATION_CLASS in 8dd5e10 extended default pagination class for all admin apis because I think it's broken the UI and I'm not sure exactly why

Image

@travis-sauer-oltech travis-sauer-oltech moved this from In Progress to Done in jupyterhealth-exchange Apr 19, 2025
@travis-sauer-oltech
Copy link
Collaborator

@s1monj

Refactored PRs: #57, #64, #65, and #66

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Python and SQL code
Projects
Status: Done
Development

No branches or pull requests

2 participants