From 4f8cd1097bd0b1384540518304cbfdb88f73d151 Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Sun, 27 Dec 2020 12:22:07 +0200 Subject: [PATCH] Rework paging The "totalPages" value in our response is calculated incorrectly. Instead of casting to int and rounding, we should rather round up to the next integer with math.ceil. This is a more correct way to get the value. Also update the indexer to use the same logic, although there the values are printed with +1 so they are more readable. --- dspace_statistics_api/app.py | 5 +++-- dspace_statistics_api/indexer.py | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/dspace_statistics_api/app.py b/dspace_statistics_api/app.py index c7e74ad..3c8cdbf 100644 --- a/dspace_statistics_api/app.py +++ b/dspace_statistics_api/app.py @@ -1,4 +1,5 @@ import json +import math import falcon import psycopg2.extras @@ -75,7 +76,7 @@ class AllStatisticsResource: with db.cursor() as cursor: # get total number of communities/collections/items so we can estimate the pages cursor.execute(f"SELECT COUNT(id) FROM {req.context.statistics_scope}") - pages = round(cursor.fetchone()[0] / limit) + pages = math.ceil(cursor.fetchone()[0] / limit) # get statistics and use limit and offset to page through results cursor.execute( @@ -128,7 +129,7 @@ class AllStatisticsResource: # Helper variables to make working with pages/items/results easier and # to make the code easier to understand number_of_elements: int = len(req.context.elements) - pages: int = int(number_of_elements / req.context.limit) + pages: int = math.ceil(number_of_elements / req.context.limit) first_element: int = req.context.page * req.context.limit last_element: int = first_element + req.context.limit # Get a subset of the POSTed items based on our limit. Note that Python diff --git a/dspace_statistics_api/indexer.py b/dspace_statistics_api/indexer.py index 04029fe..b82e197 100644 --- a/dspace_statistics_api/indexer.py +++ b/dspace_statistics_api/indexer.py @@ -28,6 +28,8 @@ # # See: https://wiki.duraspace.org/display/DSPACE/Solr +import math + import psycopg2.extras import requests @@ -75,9 +77,9 @@ def index_views(indexType: str, facetField: str): exit(0) - # divide results into "pages" (cast to int to effectively round down) + # divide results into "pages" and round up to next integer results_per_page = 100 - results_num_pages = int(results_totalNumFacets / results_per_page) + results_num_pages = math.ceil(results_totalNumFacets / results_per_page) results_current_page = 0 with DatabaseManager() as db: @@ -158,9 +160,8 @@ def index_downloads(indexType: str, facetField: str): exit(0) - # divide results into "pages" (cast to int to effectively round down) results_per_page = 100 - results_num_pages = int(results_totalNumFacets / results_per_page) + results_num_pages = math.ceil(results_totalNumFacets / results_per_page) results_current_page = 0 with DatabaseManager() as db: