Skip to content

Commit

Permalink
Actually optimise instrument list iteration (#663)
Browse files Browse the repository at this point in the history
* Actually optimise instrument list iteration

* timer
  • Loading branch information
dsavchenko authored Feb 28, 2024
1 parent 2b9cf7e commit 6171102
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 42 deletions.
16 changes: 11 additions & 5 deletions cdci_data_analysis/flask_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from oda_api.api import DispatcherAPI

from cdci_data_analysis.configurer import ConfigEnv
from cdci_data_analysis.timer import block_timer

logger = app_logging.getLogger('flask_app')

Expand Down Expand Up @@ -204,11 +205,16 @@ def common_exception_payload():
payload['cdci_data_analysis_version'] = __version__
payload['cdci_data_analysis_version_details'] = os.getenv('DISPATCHER_VERSION_DETAILS', 'unknown')
payload['oda_api_version'] = oda_api.__version__

_l = []

for instrument_factory in importer.instrument_factory_iter:
_l.append(str(getattr(instrument_factory, 'instr_name', instrument_factory().name)))

with block_timer(logger=logger,
message_template="Instrument factory iteration took {:.1f} seconds"):
_l = []
for instrument_factory in importer.instrument_factory_iter:
if hasattr(instrument_factory, 'instr_name'):
iname = instrument_factory.instr_name
else:
iname = instrument_factory().name
_l.append(str(iname))

payload['installed_instruments'] = _l

Expand Down
88 changes: 51 additions & 37 deletions cdci_data_analysis/flask_app/dispatcher_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

from oda_api.data_products import NumpyDataProduct
import oda_api
from cdci_data_analysis.timer import block_timer

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -445,19 +446,24 @@ def get_user_specific_instrument_list(app):
roles = tokenHelper.get_token_roles(decoded_token)
email = tokenHelper.get_token_user_email_address(decoded_token)

out_instrument_list = []
for instrument_factory in importer.instrument_factory_iter:
if hasattr(instrument_factory, 'instrument_query'):
instrument_query = instrument_factory.instrument_query
instr_name = getattr(instrument_factory, 'instr_name', instrument_factory().name)
else:
instrument = instrument_factory()
instrument_query = instrument.instrumet_query
instr_name = instrument.name

with block_timer(logger=logger,
message_template="Instrument factory iteration took {:.1f} seconds"):
out_instrument_list = []
for instrument_factory in importer.instrument_factory_iter:
if hasattr(instrument_factory, 'instrument_query'):
instrument_query = instrument_factory.instrument_query
if hasattr(instrument_factory, 'instr_name'):
instr_name = instrument_factory.instr_name
else:
instr_name = instrument_factory().name
else:
instrument = instrument_factory()
instrument_query = instrument.instrumet_query
instr_name = instrument.name


if instrument_query.check_instrument_access(roles, email):
out_instrument_list.append(instr_name)
if instrument_query.check_instrument_access(roles, email):
out_instrument_list.append(instr_name)

return jsonify(out_instrument_list)

Expand Down Expand Up @@ -1111,9 +1117,15 @@ def get_paramters_dict(self):
return jsonify(self.par_dic)

def get_instr_list(self, name=None):
_l = []
for instrument_factory in importer.instrument_factory_iter:
_l.append(getattr(instrument_factory, 'instr_name', instrument_factory().name))
with block_timer(logger=logger,
message_template="Instrument factory iteration took {:.1f} seconds"):
_l = []
for instrument_factory in importer.instrument_factory_iter:
if hasattr(instrument_factory, 'instr_name'):
iname = instrument_factory.instr_name
else:
iname = instrument_factory().name
_l.append(iname)

return jsonify(_l)

Expand Down Expand Up @@ -1497,30 +1509,32 @@ def set_instrument(self, instrument_name, roles, email):
if instrument_name == 'mock':
new_instrument = 'mock'
else:
for instrument_factory in importer.instrument_factory_iter:
_instrument = None
if hasattr(instrument_factory, 'instr_name'):
instr_name = instrument_factory.instr_name
else:
_instrument = instrument_factory()
instr_name = _instrument.name

if instr_name == instrument_name:
if _instrument is None and hasattr(instrument_factory, 'instrument_query'):
instr_query = instrument_factory.instrument_query
with block_timer(logger=logger,
message_template="Instrument factory iteration took {:.1f} seconds"):
for instrument_factory in importer.instrument_factory_iter:
_instrument = None
if hasattr(instrument_factory, 'instr_name'):
instr_name = instrument_factory.instr_name
else:
if _instrument is None:
_instrument = instrument_factory()
instr_query = _instrument.instrumet_query

if instr_query.check_instrument_access(roles, email):
if _instrument is None:
_instrument = instrument_factory()
new_instrument = _instrument # multiple assignment? TODO
else:
no_access = True
_instrument = instrument_factory()
instr_name = _instrument.name

if instr_name == instrument_name:
if _instrument is None and hasattr(instrument_factory, 'instrument_query'):
instr_query = instrument_factory.instrument_query
else:
if _instrument is None:
_instrument = instrument_factory()
instr_query = _instrument.instrumet_query

if instr_query.check_instrument_access(roles, email):
if _instrument is None:
_instrument = instrument_factory()
new_instrument = _instrument # multiple assignment? TODO
else:
no_access = True

known_instruments.append(instr_name)
known_instruments.append(instr_name)
if new_instrument is None:
if no_access:
raise RequestNotAuthorized(f"Unfortunately, your priviledges are not sufficient "
Expand Down
19 changes: 19 additions & 0 deletions cdci_data_analysis/timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from time import perf_counter
from contextlib import contextmanager
from cdci_data_analysis.flask_app.sentry import sentry

@contextmanager
def block_timer(logger=None,
sentry=sentry,
sentry_threshold=30,
message_template="Execution took {:.2f} seconds"):
t1 = t2 = perf_counter()
try:
yield lambda: t2 - t1
finally:
t2 = perf_counter()
message = message_template.format(t2-t1)
if logger is not None:
logger.info(message)
if sentry is not None and t2-t1 > sentry_threshold:
sentry.capture_message(message)

0 comments on commit 6171102

Please sign in to comment.