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

Use server-side paging in aips page. (#209) #215

Merged
merged 1 commit into from
Oct 11, 2023

Conversation

mcantelon
Copy link
Member

To avoid slow page responses when there are a large amount of AIPs use server-side paging to limit the number of AIP HTML sent to the browser.

@mcantelon mcantelon force-pushed the dev/aips-page-pagination branch 25 times, most recently from 57f1273 to fc3fe8d Compare September 30, 2023 22:47
Copy link
Member

@sevein sevein left a comment

Choose a reason for hiding this comment

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

Hi @mcantelon! This looks good to me. I haven't tested but I can see that what you're doing is going to bring a big improvement. I've made some small suggestions, but nothing important functionally speaking. Thank you!

<li class="paginate_button page-item previous {% if pager.prev_num is none %}disabled{% endif %}"><a href="{{ url_for('reporter.view_aips') }}?page={{ pager.prev_num }}&{{ state_querystring }}" class="page-link">Previous</a></li>
<li class="paginate_button page-item active"><a href="#" class="page-link">{{ pager.page }}</a></li>
<li class="paginate_button page-item next {% if pager.next_num is none %}disabled{% endif %}"><a href="{{ url_for('reporter.view_aips') }}?page={{ pager.next_num }}&{{ state_querystring }}" class="page-link">Next</a></li>
<li class="paginate_button page-item last {% if pager.page == pager.pages %}disabled{% endif %}"><a href="{{ url_for('reporter.view_aips') }}?page={{ pager.pages }}&{{ state_querystring }}" class="page-link">Last</a></li>
Copy link
Member

Choose a reason for hiding this comment

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

Do you know if we can include query parameters directly in url_for, e.g. something like the following:

url_for('reporter.view_aips', page=pager.next_num, **state_querystring)

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice! Yeah, that's cleaner.

Comment on lines 134 to 158
def test_calculate_paging_window():
class MockPagination(object):
pass

pagination = MockPagination()
pagination.per_page = 5
pagination.total = 17

# Test paging window at start of results
first_item, last_item = helpers.calculate_paging_window(1, pagination)

assert first_item == 1
assert last_item == 5

# Test paging window on an arbitrary page of results
first_item, last_item = helpers.calculate_paging_window(3, pagination)

assert first_item == 11
assert last_item == 15

# Test paging window with last item being set to the total of items
first_item, last_item = helpers.calculate_paging_window(4, pagination)

assert first_item == 16
assert last_item == 17
Copy link
Member

Choose a reason for hiding this comment

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

You may want to use pytest.mark.parametrize, but I must say that sometimes I find pytest's parametrize not as legible as I wish. Yours is fine.

class MockPagination(object):
    pass

@pytest.mark.parametrize("page, per_page, total, expected_first_item, expected_last_item", [
    # Test paging window at start of results.
    (1, 5, 17, 1, 5),  
    # ...
    (3, 5, 17, 11, 15),  
    # ...
    (4, 5, 17, 16, 17),
    # add more scenarios as needed
])
def test_calculate_paging_window(page, per_page, total, expected_first_item, expected_last_item):
    pagination = MockPagination()
    pagination.per_page = per_page
    pagination.total = total

    first_item, last_item = helpers.calculate_paging_window(page, pagination)

    assert first_item == expected_first_item
    assert last_item == expected_last_item

Comment on lines 161 to 166
def urlencode_without_none_values(values):
for key in values:
if values[key] is None:
values[key] = ""

return urlencode(values)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a good use case for values.items(), e.g.:

def urlencode_without_none_values(values):
    """URL encode the values dict converting any None values to empty strings."""
    for key, value in values.items():
        if value is None:
            values[key] = ""
    return urlencode(values)

Here's an alternative using a dictionary comprenhension, I think this can be useful if you want to avoid mutating the input dictionary or doing it during iteration.

def urlencode_without_none_values(values):
    """URL encode the values dict converting any None values to empty strings."""
    return urlencode(
        {
            key: (value if value is not None else '')
            for key, value in values.items()
        }
    )

Comment on lines 153 to 156
last_item = page * pagination.per_page

if last_item > pagination.total:
last_item = pagination.total
Copy link
Member

Choose a reason for hiding this comment

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

An alternative to calculate last_item that is more concise by using min():

Suggested change
last_item = page * pagination.per_page
if last_item > pagination.total:
last_item = pagination.total
last_item = min(page * pagination.per_page, pagination.total)

Copy link
Member Author

Choose a reason for hiding this comment

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

Very handy!

@@ -85,10 +89,24 @@ def view_aips():
except Exception as e:
print(e)

aips = AIP.query.filter_by(storage_service_id=storage_service.id)
page = int(request.args.get(request_params.PAGE, default="1"))
Copy link
Member

Choose a reason for hiding this comment

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

Should we handle ValueError when int doesn't receive a valid base 10 literal?

@mcantelon
Copy link
Member Author

Thanks @sevein !

@mcantelon
Copy link
Member Author

I've updated the PR @sevein .... thanks for the tips!

Copy link
Member

@sevein sevein left a comment

Choose a reason for hiding this comment

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

LGTM

To avoid slow page responses when there are a large amount of AIPs use
server-side paging to limit the number of AIP HTML sent to the browser.
@mcantelon mcantelon force-pushed the dev/aips-page-pagination branch from 6cd0ce5 to 6df7478 Compare October 11, 2023 07:32
@mcantelon mcantelon merged commit a64364e into main Oct 11, 2023
5 checks passed
@mcantelon mcantelon deleted the dev/aips-page-pagination branch October 11, 2023 07:32
mcantelon added a commit that referenced this pull request Jan 5, 2024
To avoid slow page responses when there are a large amount of AIPs use
server-side paging to limit the number of AIP HTML sent to the browser.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants