Skip to content

Commit

Permalink
#143: Improve structure of execute_query()
Browse files Browse the repository at this point in the history
- This commit will also add some info logging to track what happens to the query during its lifecycle
  • Loading branch information
MRichards99 committed Sep 10, 2020
1 parent 2210fdb commit 26f303a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
7 changes: 2 additions & 5 deletions common/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def icat_set_limit(query, skip_number, limit_number):
class PythonICATIncludeFilter(IncludeFilter):
def __init__(self, included_filters):
self.included_filters = []
log.info("Extracting fields for include filter")
self._extract_filter_fields(included_filters["include"])

def _extract_filter_fields(self, field):
Expand Down Expand Up @@ -219,13 +220,9 @@ def _extract_filter_fields(self, field):
)

def apply_filter(self, query):
log.debug(
f"Included filters: {self.included_filters},"
f" Type: {type(self.included_filters)}"
)
log.info("Applying include filter, adding fields: %s", self.included_filters)

try:
pass
query.addIncludes(self.included_filters)
except ValueError as e:
raise FilterError(e)
23 changes: 6 additions & 17 deletions common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def __init__(
"""

try:
log.info("Creating ICATQuery for entity: %s", entity_name)
self.query = Query(
client,
entity_name,
Expand Down Expand Up @@ -152,17 +153,16 @@ def execute_query(self, client, return_json_formattable=False):
"""

try:
# Going to Python ICAT to perform the query
log.debug("Executing ICAT query")
query_result = client.search(self.query)
log.debug("Query Result: %s", query_result)
except ICATValidationError as e:
raise PythonICATError(e)

if self.query.aggregate == "DISTINCT":
log.info("Extracting the distinct fields from query's conditions")
distinct_filter_flag = True
# Check query's conditions for the ones created by the distinct filter
self.attribute_names = []
log.debug("Query conditions: %s", self.query.conditions)

for key, value in self.query.conditions.items():
# Value can be a list if there's multiple WHERE filters for the same
Expand All @@ -181,21 +181,12 @@ def execute_query(self, client, return_json_formattable=False):
distinct_filter_flag = False

if return_json_formattable:
log.info("Query results will be returned in a JSON format")
data = []

# TODO - Test this all works without include filter
for result in query_result:
# TODO - Put this in the docstring
# Converting each row/result into its dictionary form if include filter
# not used - `______` will do this if include filter(s) are used in the
# request - Python ICAT's `as_dict()` doesn't do included fields but is
# a simpler function so only used when needed
dict_result = {} if self.query.includes else result.as_dict()
# Creating dictionary to store distinct fields for use later on
distinct_result = {}

if self.query.includes:
dict_result = self.entity_to_dict(result, self.query.includes)
dict_result = self.entity_to_dict(result, self.query.includes)

for key, value in dict_result.items():
if distinct_filter_flag:
Expand All @@ -204,15 +195,13 @@ def execute_query(self, client, return_json_formattable=False):
if key in self.attribute_names:
distinct_result[key] = dict_result[key]

# Add to the response's data depending on whether request has a distinct
# filter
if distinct_filter_flag:
data.append(distinct_result)
else:
data.append(dict_result)
return data
else:
# Return data exactly as Python ICAT returned the query
log.info("Query results will be returned as ICAT entities")
return query_result

def check_attribute_name_for_distinct(self, key, value):
Expand Down

0 comments on commit 26f303a

Please sign in to comment.