mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2024-11-22 06:15:02 +01:00
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.
This commit is contained in:
parent
a02211fd60
commit
4f8cd1097b
@ -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
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user