Skip to content

Commit

Permalink
#141: Fix manual count flag
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Apr 12, 2021
1 parent 42dea33 commit f5c3bbe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions datagateway_api/common/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def apply_filter(self, query):
# perform `len()` on the list, using `manual_count` as a flag to
# recognise this situation
query.setAggregate("DISTINCT")
log.debug("Manual count flag enabled")
query.manual_count = True
else:
query.setAggregate("DISTINCT")
Expand Down
25 changes: 21 additions & 4 deletions datagateway_api/common/icat/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ def __init__(
:raises PythonICATError: If a ValueError is raised when creating a Query(), 500
will be returned as a response
"""
# Flag for a count request that uses a distinct filter
self.manual_count = False

try:
log.info("Creating ICATQuery for entity: %s", entity_name)
Expand All @@ -49,6 +47,8 @@ def __init__(
aggregate=aggregate,
includes=includes,
)
# Initialising flag for distinct filter on count endpoints
self.query.manual_count = False
except ValueError:
raise PythonICATError(
"An issue has occurred while creating a Python ICAT Query object,"
Expand Down Expand Up @@ -89,7 +89,13 @@ def execute_query(self, client, return_json_formattable=False):
count_query = True
log.debug("This ICATQuery is used for COUNT purposes")

if self.query.aggregate == "DISTINCT" and not count_query:
distinct_query = False
if (
self.query.aggregate == "DISTINCT"
and not count_query
and not self.query.manual_count
):
distinct_query = True
log.info("Extracting the distinct fields from query's conditions")
# Check query's conditions for the ones created by the distinct filter
distinct_attributes = self.get_distinct_attributes()
Expand All @@ -108,7 +114,18 @@ def execute_query(self, client, return_json_formattable=False):
data = []

for result in query_result:
if not count_query:
if self.query.manual_count:
# Manually count the number of results
data.append(len(query_result))
break
elif distinct_query:
# Map distinct attributes and result
data.append(
self.map_distinct_attributes_to_results(
distinct_attributes, result,
),
)
elif not count_query:
dict_result = self.entity_to_dict(
result, flat_query_includes, mapped_distinct_fields,
)
Expand Down

0 comments on commit f5c3bbe

Please sign in to comment.