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 limit and offset parameters to routes that fetch lists from db #277

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

blenzi
Copy link
Contributor

@blenzi blenzi commented Aug 7, 2023

This PR adds limit and offset parameters with descriptions, to all routes that fetch lists from db (fix #243, fix #250). Also:

  • use crud.fetch_all in all the routes above, to limit the number of items returned for non-admin users (fix Fetch routes do not limit number of items for non-admin users #276)
  • forbid /installation/site-devices to access site outside of group, instead of filtering devices
  • improve test_installations.py (test_get_active_devices_on_site)
  • Dockerfile-dev: update FROM image name

- add limit and offset parameters with descriptions, to all routes that fetch lists from db (fix #243, #250)
- use crud.fetch_all in all the routes above, to limit the number of items returned for non-admin users (fix #276)
- forbid /installation/site-devices to access site outside of group, instead of filtering devices
- improve test_installations.py (test_get_active_devices_on_site)
- Dockerfile-dev: update FROM image name
@ghost
Copy link

ghost commented Aug 7, 2023

Nice of you to open a PR 🙏
When you're ready and want to get it reviewed, post a comment in this Pull Request with this message: /quack review

@codecov
Copy link

codecov bot commented Aug 7, 2023

Codecov Report

Merging #277 (a94ceb9) into main (fa2624d) will increase coverage by 0.16%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #277      +/-   ##
==========================================
+ Coverage   95.12%   95.28%   +0.16%     
==========================================
  Files          63       66       +3     
  Lines        1497     1549      +52     
==========================================
+ Hits         1424     1476      +52     
  Misses         73       73              
Flag Coverage Δ
client 100.00% <ø> (?)
unittests 95.05% <100.00%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
src/app/api/crud/base.py 100.00% <100.00%> (ø)
src/app/api/endpoints/accesses.py 100.00% <100.00%> (ø)
src/app/api/endpoints/alerts.py 98.50% <100.00%> (-0.13%) ⬇️
src/app/api/endpoints/devices.py 98.70% <100.00%> (-0.05%) ⬇️
src/app/api/endpoints/events.py 100.00% <100.00%> (ø)
src/app/api/endpoints/groups.py 100.00% <100.00%> (ø)
src/app/api/endpoints/installations.py 100.00% <100.00%> (ø)
src/app/api/endpoints/media.py 85.33% <100.00%> (-0.57%) ⬇️
src/app/api/endpoints/notifications.py 96.42% <100.00%> (+0.13%) ⬆️
src/app/api/endpoints/recipients.py 100.00% <100.00%> (ø)
... and 3 more

... and 3 files with indirect coverage changes

@blenzi
Copy link
Contributor Author

blenzi commented Aug 7, 2023

/quack review

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Thanks for the PR 🙏

Other sections
  • [string-addition] detected at src/tests/routes/test_groups.py:L121
  • For a better experience, it's recommended to check the review comments in the tab Files Changed.


    Feedback is a gift! Quack will adjust your review style when you add reactions to a review comment

    src/tests/routes/test_groups.py Show resolved Hide resolved
    Copy link
    Member

    @frgfm frgfm left a comment

    Choose a reason for hiding this comment

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

    Thanks for the PR! I added a few comments, but the overall one is: do we want to paginate query results or allow users to specify limits? (in the last case, if a large value is selected, that can blow up the RAM. even though we might not have a lot of malicious users around)

    I saw something a few months ago to paginate easily in fastapi: https://uriyyo-fastapi-pagination.netlify.app/

    What do you think?

    Comment on lines +46 to +61
    offset: Optional[int] = None,
    query: Optional[Select] = None,
    ) -> List[Mapping[str, Any]]:
    query = table.select().order_by(table.c.id.desc())
    if query is None:
    query = table.select()
    if isinstance(query_filters, dict):
    for key, value in query_filters.items():
    query = query.where(getattr(table.c, key) == value)

    if isinstance(exclusions, dict):
    for key, value in exclusions.items():
    query = query.where(getattr(table.c, key) != value)
    return (await database.fetch_all(query=query.limit(limit)))[::-1]
    query = query.order_by(table.c.id.desc()).limit(limit).offset(offset)
    if isinstance(query, Query):
    return [item.__dict__ for item in query[::-1]]
    return (await database.fetch_all(query=query))[::-1]
    Copy link
    Member

    Choose a reason for hiding this comment

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

    Good idea. Two questions though:

    • I think I understand this is an equivalent of the offset syntax in SQL. But here we seem to be making an arbitrary choice on order direction. Should we make that a boolean param?
    • regarding the query arg, we expect it to already have a .select()?

    If we only want to paginate the queries, there are a few plugin options for this I think

    Copy link
    Member

    Choose a reason for hiding this comment

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

    (paginate will prevent the case where we have a huge table and someone send limit=100000 which might blow up the RAM)

    Comment on lines -28 to +33
    async def fetch_accesses(_=Security(get_current_access, scopes=[AccessType.admin])):
    async def fetch_accesses(
    limit: Annotated[int, Query(description="maximum number of items", ge=1, le=1000)] = 50,
    offset: Annotated[Optional[int], Query(description="number of items to skip", ge=0)] = None,
    _=Security(get_current_access, scopes=[AccessType.admin]),
    ):
    Copy link
    Member

    Choose a reason for hiding this comment

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

    Just a reminder for me: how do we change those values when sending a request?

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment