Skip to content

Commit d5e4631

Browse files
committed
Cap pageCount to agg bucket limit and close response before retry
1 parent 38f6e1e commit d5e4631

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

scan_explorer_service/utils/search_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ def serialize_os_agg_collection_bucket(bucket: dict):
183183
volume = id[5:9]
184184
return {'id': id, 'journal': journal, 'volume': volume, 'pages': bucket['doc_count']}
185185

186-
def serialize_os_collection_result(result: dict, page: int, limit: int, contentQuery):
186+
def serialize_os_collection_result(result: dict, page: int, limit: int, contentQuery, agg_bucket_limit: int = 10000):
187187
total_count = result['aggregations']['total_count']['value']
188-
page_count = int(math.ceil(total_count / limit))
188+
page_count = int(math.ceil(min(total_count, agg_bucket_limit) / limit))
189189
es_buckets = result['aggregations']['ids']['buckets']
190190

191191
return {'page': page, 'pageCount': page_count, 'limit': limit, 'total': total_count, 'query': contentQuery,
@@ -195,9 +195,9 @@ def serialize_os_agg_article_bucket(bucket: dict):
195195
id = bucket['key']
196196
return {'id': id, 'bibcode': id, 'pages': bucket['doc_count']}
197197

198-
def serialize_os_article_result(result: dict, page: int, limit: int, contentQuery = '', extra_col_count = 0, extra_page_count = 0):
198+
def serialize_os_article_result(result: dict, page: int, limit: int, contentQuery = '', extra_col_count = 0, extra_page_count = 0, agg_bucket_limit: int = 10000):
199199
total_count = result['aggregations']['total_count']['value']
200-
page_count = int(math.ceil(total_count / limit))
200+
page_count = int(math.ceil(min(total_count, agg_bucket_limit) / limit))
201201
es_buckets = result['aggregations']['ids']['buckets']
202202

203203
return {'page': page, 'pageCount': page_count, 'limit': limit, 'total': total_count, 'query': contentQuery,

scan_explorer_service/views/image_proxy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def image_proxy(path):
4242
f"Upstream image request failed (status {r.status_code}), "
4343
f"retrying in {retry_delay}s (attempt {attempt + 1}/{retries})")
4444
time.sleep(retry_delay)
45+
r.close()
4546
r = requests.request(request.method, encoded_url, params=request.args, stream=True,
4647
headers=req_headers, allow_redirects=False, data=request.form)
4748

scan_explorer_service/views/metadata.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ def article_search():
179179
if article_count == 0:
180180
collection_count = aggregate_search(qs, EsFields.volume_id, page, limit, sort)['aggregations']['total_count']['value']
181181
page_count = page_os_search(qs, page, limit, sort)['hits']['total']['value']
182-
return jsonify(serialize_os_article_result(result, page, limit, text_query, collection_count, page_count))
182+
agg_limit = current_app.config.get("OPEN_SEARCH_AGG_BUCKET_LIMIT", 10000)
183+
return jsonify(serialize_os_article_result(result, page, limit, text_query, collection_count, page_count, agg_limit))
183184
except Exception as e:
184185
current_app.logger.exception(f"An exception has occurred: {e}")
185186
return jsonify(message=str(e), type=ApiErrors.SearchError.value), 400
@@ -195,7 +196,8 @@ def collection_search():
195196
text_query = ''
196197
if SearchOptions.FullText.value in qs_dict.keys():
197198
text_query = qs_dict[SearchOptions.FullText.value]
198-
return jsonify(serialize_os_collection_result(result, page, limit, text_query))
199+
agg_limit = current_app.config.get("OPEN_SEARCH_AGG_BUCKET_LIMIT", 10000)
200+
return jsonify(serialize_os_collection_result(result, page, limit, text_query, agg_limit))
199201
except Exception as e:
200202
return jsonify(message=str(e), type=ApiErrors.SearchError.value), 400
201203

0 commit comments

Comments
 (0)