Skip to content

Commit

Permalink
Merge pull request #68 from bento-platform/features/threshold-in-resp…
Browse files Browse the repository at this point in the history
…onse

Features/threshold in response
  • Loading branch information
gsfk authored Jan 23, 2024
2 parents 6385ada + 3d39e7a commit 76dde0c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
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

0 comments on commit 76dde0c

Please sign in to comment.