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

Make Distinct Field Filters Apply Correctly on ISIS Endpoints #176

Merged
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
4 changes: 2 additions & 2 deletions common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def get_facility_cycles_for_instrument(

query_aggregate = "COUNT:DISTINCT" if count_query else "DISTINCT"
query = ICATQuery(
client, "FacilityCycle", aggregate=query_aggregate, isis_endpoint=True
client, "FacilityCycle", aggregate=query_aggregate
)

instrument_id_check = PythonICATWhereFilter(
Expand Down Expand Up @@ -573,7 +573,7 @@ def get_investigations_for_instrument_in_facility_cycle(

query_aggregate = "COUNT:DISTINCT" if count_query else "DISTINCT"
query = ICATQuery(
client, "Investigation", aggregate=query_aggregate, isis_endpoint=True
client, "Investigation", aggregate=query_aggregate
)

instrument_id_check = PythonICATWhereFilter(
Expand Down
29 changes: 10 additions & 19 deletions common/icat/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def __init__(
conditions=None,
aggregate=None,
includes=None,
isis_endpoint=False,
):
"""
Create a Query object within Python ICAT
Expand All @@ -39,12 +38,6 @@ def __init__(
:param includes: List of related entity names to add to the query so related
entities (and their data) can be returned with the query result
:type includes: :class:`str` or iterable of :class:`str`
:param isis_endpoint: Flag to determine if the instance will be used for an ISIS
specific endpoint. These endpoints require the use of the DISTINCT aggregate
which is different to the distinct field filter implemented in this API, so
this flag prevents code related to the filter from executing (because it
doesn't need to be on a DISTINCT aggregate)
:type isis_endpoint: :class:`bool`
:return: Query object from Python ICAT
:raises PythonICATError: If a ValueError is raised when creating a Query(), 500
will be returned as a response
Expand All @@ -65,7 +58,6 @@ def __init__(
" suggesting an invalid argument"
)

self.isis_endpoint = isis_endpoint

def execute_query(self, client, return_json_formattable=False):
"""
Expand Down Expand Up @@ -104,19 +96,19 @@ def execute_query(self, client, return_json_formattable=False):
if (
self.query.aggregate == "DISTINCT"
and not count_query
and not self.isis_endpoint
):
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.iterate_query_conditions_for_distinctiveness()
mapped_distinct_fields = self.map_distinct_attributes_to_entity_names(
distinct_attributes, flat_query_includes
)
log.debug(
"Attribute names used in the distinct filter, mapped to the entity they"
" are a part of: %s",
mapped_distinct_fields,
)
if distinct_attributes != []:
mapped_distinct_fields = self.map_distinct_attributes_to_entity_names(
distinct_attributes, flat_query_includes
)
log.debug(
"Attribute names used in the distinct filter, mapped to the entity they"
" are a part of: %s",
mapped_distinct_fields,
)

if return_json_formattable:
log.info("Query results will be returned in a JSON format")
Expand Down Expand Up @@ -276,8 +268,7 @@ def map_distinct_attributes_to_entity_names(self, distinct_fields, included_fiel
"""

# Mapping which entities have distinct fields
distinct_field_dict = {}
distinct_field_dict["base"] = []
distinct_field_dict = {"base": []}

for field in distinct_fields:
split_fields = field.split(".")
Expand Down