Skip to content

Commit

Permalink
Do config check before ReaderQueryHandler creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinphippsstfc authored and MRichards99 committed Oct 2, 2024
1 parent 7b4d5af commit dca658f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
21 changes: 21 additions & 0 deletions datagateway_api/src/datagateway_api/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from cachetools import cached
from dateutil.tz import tzlocal
from datagateway_api.src.common.config import Config
from icat.exception import (
ICATInternalError,
ICATNoObjectError,
Expand Down Expand Up @@ -334,6 +335,12 @@ def get_count_with_filters(client, entity_type, filters, client_pool=None):
def get_data_with_filters(
client, entity_type, filters, aggregate=None, client_pool=None,
):
if not is_use_reader_for_performance_enabled():
# just execute the query as normal
return execute_entity_query(client, entity_type, filters, aggregate=aggregate)

# otherwise see if this query is eligible to benefit from running
# faster using the reader account
reader_query = ReaderQueryHandler(entity_type, filters)
if reader_query.is_query_eligible_for_reader_performance():
log.info("Query is eligible to be passed as reader acount")
Expand Down Expand Up @@ -377,6 +384,20 @@ def execute_entity_query(client, entity_type, filters, aggregate=None):
return query.execute_query(client, True)


def is_use_reader_for_performance_enabled():
"""
Returns true is the 'use_reader_for_performance' section is present in the
config file and 'enabled' in that section is set to true
"""
reader_config = Config.config.datagateway_api.use_reader_for_performance
if not reader_config:
return False
if not reader_config.enabled:
return False

return True


def get_first_result_with_filters(client, entity_type, filters):
"""
Using filters in the request, get results of the given entity, but only show the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ def create_reader_client(self):
return ReaderQueryHandler.reader_client

def check_eligibility(self):
reader_config = Config.config.datagateway_api.use_reader_for_performance
if not reader_config:
return False
if not reader_config.enabled:
return False

log.info("Checking whether query is eligible to go via reader account")
if self.entity_type in ReaderQueryHandler.entity_filter_check.keys():
if self.get_where_filter_for_entity_id_check():
Expand Down

0 comments on commit dca658f

Please sign in to comment.