diff --git a/cal_tools/cal_tools/mdc_config.py b/cal_tools/cal_tools/mdc_config.py new file mode 100644 index 0000000000000000000000000000000000000000..1f5f9cf2dc383e76c5a1d6cd0c7a373b89e6ae83 --- /dev/null +++ b/cal_tools/cal_tools/mdc_config.py @@ -0,0 +1,11 @@ +class MDC_config: + + mdconf = {} + mdconf['user-id'] = '' + mdconf['user-secret'] = '' + mdconf['user-email'] = 'calibration@example.com' + mdconf['token-url'] = 'https://in.xfel.eu/metadata/oauth/token' + mdconf['refresh-url'] = 'https://in.xfel.eu/metadata/oauth/token' + mdconf['auth-url'] = 'https://in.xfel.eu/metadata/oauth/authorize' + mdconf['scope'] = '' + mdconf['base-api-url'] = 'https://in.xfel.eu/metadata/api/' diff --git a/cal_tools/cal_tools/tools.py b/cal_tools/cal_tools/tools.py index b1e05a4678de3a39a074cd518580af48a1135d62..c4a1886bdcad6b2981e4c774f8688d7dd496889b 100644 --- a/cal_tools/cal_tools/tools.py +++ b/cal_tools/cal_tools/tools.py @@ -13,14 +13,16 @@ from textwrap import dedent from time import sleep from urllib.parse import urljoin +import dateutil.parser import ipykernel +from metadata_client.metadata_client import MetadataClient from notebook.notebookapp import list_running_servers import numpy as np import requests import tabulate from jinja2 import Template - +from .mdc_config import MDC_config from xfel_calibrate import settings @@ -425,15 +427,55 @@ def get_notebook_name(): return environ.get("CAL_NOTEBOOK_NAME", "Unknown Notebook") -def get_dir_creation_date(directory, run, tsdir=False): +def get_run_info(proposal, run): + """ + Return information about run from the MDC + + :param proposal: proposal number + :param run: run number + :return: dictionary with run information + """ + + mdconf = MDC_config.mdconf + mdc = MetadataClient(client_id=mdconf['user-id'], + client_secret=mdconf['user-secret'], + user_email=mdconf['user-email'], + token_url=mdconf['token-url'], + refresh_url=mdconf['refresh-url'], + auth_url=mdconf['auth-url'], + scope=mdconf['scope'], + base_api_url=mdconf['base-api-url']) + + runs = mdc.get_proposal_runs(mdc, proposal_number=proposal, + run_number=run) + run_id = runs['data']['runs'][0]['id'] + + resp = mdc.get_run_by_id_api(run_id) + return resp.json() + + +def get_dir_creation_date(directory, run, tsdir=False, verbosity=0): """ - Return modification time of oldest file.h5 in [directory]/[run]04 + Return run starting time from the MDC. + If not succeeded, return modification time of oldest file.h5 + in [directory]/[run]04. :param directory: path to directory which contains runs :param run: run number :param tsdir: to get modification time of [directory]/[run]04. + :param verbosity: Level of verbosity (0 - silent) :return: (datetime) modification time """ + + try: + path_list = list(filter(None, directory.strip('/').split('/'))) + proposal = int(path_list[-2][1:]) + run_info = get_run_info(proposal, run) + return dateutil.parser.parse(run_info['begin_at']) + except Exception as e: + if verbosity > 0: + print(e) + ntries = 100 while ntries > 0: try: