Remove last and total keys from pagination links

These don't appear to be used anywhere in the admin app and this
route is only used by the admin app. Therefore it is safe to remove
them.

We remove them because the calculate the total number of notifications
or the final page number of results can be particularly slow for
services with many many notifications, for example 100 seconds
for a service with 500k notifications sent in the past 7 days.

Given neither are being used, this will give us the potential in
the next commit to reduce the number of slow queries and improve
page load times.

Note, I've kept the scope small by only introducing the new
pagination function for this one endpoint but there could be scope
in future to get all pagination using the next function if
appropriate.
This commit is contained in:
David McDonald
2021-12-02 09:44:43 +00:00
parent a62e63fcef
commit 989ef9c21a
2 changed files with 14 additions and 11 deletions

View File

@@ -1,7 +1,7 @@
import itertools
from datetime import datetime
from flask import Blueprint, current_app, jsonify, request
from flask import Blueprint, current_app, jsonify, request, url_for
from notifications_utils.letter_timings import (
letter_can_be_cancelled,
too_late_to_cancel_letter,
@@ -441,11 +441,22 @@ def get_all_notifications_for_service(service_id):
notifications = [notification.serialize_for_csv() for notification in pagination.items]
else:
notifications = notification_with_template_schema.dump(pagination.items, many=True).data
def get_prev_next_pagination_links(pagination, endpoint, **kwargs):
if 'page' in kwargs:
kwargs.pop('page', None)
links = {}
if pagination.has_prev:
links['prev'] = url_for(endpoint, page=pagination.prev_num, **kwargs)
if pagination.has_next:
links['next'] = url_for(endpoint, page=pagination.next_num, **kwargs)
return links
return jsonify(
notifications=notifications,
page_size=page_size,
total=pagination.total,
links=pagination_links(
links=get_prev_next_pagination_links(
pagination,
'.get_all_notifications_for_service',
**kwargs