Skip to content

Commit

Permalink
#146: Add flag for ISIS endpoints in ICATQuery
Browse files Browse the repository at this point in the history
- ISIS endpoints require use of the DISTINCT aggregate, which is different to the DISTINCT filter in this API
  • Loading branch information
MRichards99 committed Oct 14, 2020
1 parent b6333e9 commit 10679bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def get_facility_cycles_for_instrument(client, instrument_id, filters):
"""
# TODO - Add logging

query = ICATQuery(client, "FacilityCycle")
query = ICATQuery(client, "FacilityCycle", aggregate="DISTINCT", isis_endpoint=True)

instrument_id_check = PythonICATWhereFilter(
"facility.instruments.id", instrument_id, "eq"
Expand Down
20 changes: 16 additions & 4 deletions common/icat/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@

class ICATQuery:
def __init__(
self, client, entity_name, conditions=None, aggregate=None, includes=None
self,
client,
entity_name,
conditions=None,
aggregate=None,
includes=None,
isis_endpoint=False,
):
"""
Create a Query object within Python ICAT
Expand Down Expand Up @@ -53,6 +59,8 @@ def __init__(
" suggesting an invalid argument"
)

self.isis_endpoint = isis_endpoint

def execute_query(self, client, return_json_formattable=False):
"""
Execute the ICAT Query object and return in the format specified by the
Expand All @@ -71,7 +79,7 @@ def execute_query(self, client, return_json_formattable=False):
"""

try:
log.debug("Executing ICAT query")
log.debug("Executing ICAT query: %s", self.query)
query_result = client.search(self.query)
except (ICATValidationError, ICATInternalError) as e:
raise PythonICATError(e)
Expand All @@ -86,7 +94,11 @@ def execute_query(self, client, return_json_formattable=False):
if "COUNT" in self.query.aggregate:
count_query = True

if self.query.aggregate == "DISTINCT" and not count_query:
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()
Expand Down Expand Up @@ -334,4 +346,4 @@ def flatten_query_included_fields(self, includes):
ICAT query
"""

return [m for n in (field.split(".") for field in includes) for m in n]
return [m for n in (field.split(".") for field in includes) for m in n]

0 comments on commit 10679bb

Please sign in to comment.