Skip to content

Commit

Permalink
Use server-side paging in aips page. (#209)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mcantelon committed Sep 29, 2023
1 parent 40b01a3 commit 0a6cc89
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
10 changes: 10 additions & 0 deletions AIPscan/Reporter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,13 @@ def get_premis_xml_lines(file_object):
premis_xml_lines = file_object.premis_object.split("\n")

return premis_xml_lines


def calculate_paging_window(page, pagination):
first_item = ((page - 1) * pagination.per_page) + 1
last_item = page * pagination.per_page

Check warning on line 152 in AIPscan/Reporter/helpers.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/Reporter/helpers.py#L151-L152

Added lines #L151 - L152 were not covered by tests

if last_item > pagination.total:
last_item = pagination.total

Check warning on line 155 in AIPscan/Reporter/helpers.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/Reporter/helpers.py#L155

Added line #L155 was not covered by tests

return first_item, last_item

Check warning on line 157 in AIPscan/Reporter/helpers.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/Reporter/helpers.py#L157

Added line #L157 was not covered by tests
31 changes: 27 additions & 4 deletions AIPscan/Reporter/templates/aips.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block content %}

<div class="alert alert-header"><strong>AIPs ({{ aips|length }})</strong></div>
<div class="alert alert-header"><strong>AIPs ({{ pagination.items|length }})</strong></div>

{% if storage_services %}

Expand Down Expand Up @@ -42,9 +42,9 @@
</div>
</div>

{% if aips %}
{% if pagination.items %}

<table id="aiptable" class="table table-striped table-bordered">
<table id="aipstable" class="table table-striped table-bordered">
<thead>
<tr>
<th><strong>Transfer name</strong></th>
Expand All @@ -56,7 +56,7 @@
</tr>
</thead>
{% if aips %}
{% for aip in aips %}
{% for aip in pagination.items %}
<tr>
<td>{{ aip.transfer_name }}</td>
<td>{{ aip.uuid }}</td>
Expand Down Expand Up @@ -85,6 +85,29 @@

{% endif %}

{% if pagination.total %}

<div class="dataTables_wrapper">
<div class="row">
<div class="col-sm-12 col-md-5">
<div class="dataTables_info" id="aiptable_info" role="status" aria-live="polite">Showing {{ first_item }} to {{ last_item }} of {{ pagination.total }} entries</div>
</div>
<div class="col-sm-12 col-md-7">
<div class="dataTables_paginate paging_full_numbers" id="aiptable_paginate">
<ul class="pagination">
<li class="paginate_button page-item first"><a href="{{ url_for('reporter.view_aips') }}?page=1" class="page-link">First</a></li>
<li class="paginate_button page-item previous"><a href="{{ url_for('reporter.view_aips') }}?page={{ pagination.prev_num }}" class="page-link {% if pagination.prev_num is none %}disabled{% endif %}">Previous</a></li>
<li class="paginate_button page-item active"><a href="#" class="page-link">{{ pagination.page }}</a></li>
<li class="paginate_button page-item next {% if pagination.next_num is none %}disabled{% endif %}"><a href="{{ url_for('reporter.view_aips') }}?page={{ pagination.next_num }}" class="page-link">Next</a></li>
<li class="paginate_button page-item last"><a href="{{ url_for('reporter.view_aips') }}?page={{ pagination.pages }}" class="page-link">Last</a></li>
</ul>
</div>
</div>
</div>
</div>

{% endif %}

<script>
$(document).ready(function(){
var storageServiceId = $('#ss').val();
Expand Down
19 changes: 15 additions & 4 deletions AIPscan/Reporter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
request_params,
sort_puids,
)
from AIPscan.Reporter.helpers import get_premis_xml_lines
from AIPscan.Reporter.helpers import calculate_paging_window, get_premis_xml_lines


def _get_storage_service(storage_service_id):
Expand Down Expand Up @@ -85,10 +85,18 @@ 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("page", default="1"))
per_page = 5

Check warning on line 89 in AIPscan/Reporter/views.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/Reporter/views.py#L88-L89

Added lines #L88 - L89 were not covered by tests

pagination = AIP.query.paginate(page=page, per_page=per_page, error_out=False)

Check warning on line 91 in AIPscan/Reporter/views.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/Reporter/views.py#L91

Added line #L91 was not covered by tests

if storage_location:
aips = aips.filter_by(storage_location_id=storage_location_id)
aips = aips.all()
pagination = AIP.query.filter_by(

Check warning on line 94 in AIPscan/Reporter/views.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/Reporter/views.py#L94

Added line #L94 was not covered by tests
storage_location_id=storage_location_id
).paginate(page=page, per_page=per_page, error_out=False)
aips = pagination.items

Check warning on line 97 in AIPscan/Reporter/views.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/Reporter/views.py#L97

Added line #L97 was not covered by tests

first_item, last_item = calculate_paging_window(page, pagination)

Check warning on line 99 in AIPscan/Reporter/views.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/Reporter/views.py#L99

Added line #L99 was not covered by tests

return render_template(
"aips.html",
Expand All @@ -99,6 +107,9 @@ def view_aips():
),
storage_location=storage_location,
aips=aips,
pagination=pagination,
first_item=first_item,
last_item=last_item,
)


Expand Down

0 comments on commit 0a6cc89

Please sign in to comment.