Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/threshold in response #68

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion bento_beacon/utils/beacon_response.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import current_app, g, request
from .katsu_utils import search_summary_statistics, overview_statistics
from .censorship import get_censorship_threshold, censored_count
from .censorship import get_censorship_threshold, censored_count, MESSAGE_FOR_CENSORED_QUERY_WITH_NO_RESULTS
from .exceptions import InvalidQuery, APIException
from ..constants import GRANULARITY_BOOLEAN, GRANULARITY_COUNT, GRANULARITY_RECORD

Expand All @@ -10,6 +10,7 @@ def init_response_data():
g.response_data = {}
g.response_info = {}


def add_info_to_response(info):
add_message({"description": info, "level": "info"})

Expand All @@ -20,6 +21,11 @@ def add_message(message_obj):
g.response_info["messages"] = messages


def add_no_results_censorship_message_to_response():
add_info_to_response(MESSAGE_FOR_CENSORED_QUERY_WITH_NO_RESULTS)
add_info_to_response(f"censorship threshold: {current_app.config['COUNT_THRESHOLD']}")


def add_stats_to_response(ids):
if ids is not None and len(ids) <= get_censorship_threshold():
return
Expand Down Expand Up @@ -107,6 +113,8 @@ def build_query_response(ids=None, numTotalResults=None, full_record_handler=Non
granularity = response_granularity()
count = len(ids) if numTotalResults is None else numTotalResults
returned_count = censored_count(count)
if returned_count == 0 and get_censorship_threshold() > 0:
add_no_results_censorship_message_to_response()
if granularity == GRANULARITY_BOOLEAN:
return beacon_boolean_response(returned_count)
if granularity == GRANULARITY_COUNT:
Expand Down
3 changes: 3 additions & 0 deletions bento_beacon/utils/censorship.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from .exceptions import APIException, InvalidQuery


MESSAGE_FOR_CENSORED_QUERY_WITH_NO_RESULTS = "No results. Either none were found, or the query produced results numbering at or below the threshold for censorship."


def get_censorship_threshold():
if g.permission_query_data:
return 0
Expand Down