diff --git a/config.yml b/config.yml index ebb0a57..bc57036 100644 --- a/config.yml +++ b/config.yml @@ -52,6 +52,8 @@ amda: "http://amda.irap.omp.eu/DDService/getDataUrl.php?dataSet={dataSet}&StartT defaults: input_slug: 'om' + relative_before: 1 + relative_after: 1 # Data files are in data/catalog/ # Constraints are joined by an outer OR and an inner AND diff --git a/web/hp_cmd.py b/web/hp_cmd.py index be038d0..88ce106 100644 --- a/web/hp_cmd.py +++ b/web/hp_cmd.py @@ -1,6 +1,6 @@ import sys -from run import generate_csv_file_if_needed, init_console_logger, config +from run import generate_csv_file_if_needed, init_console_logger, config, get_default_sta_sto from datetime import datetime import argparse @@ -27,9 +27,9 @@ if __name__ == "__main__": _logger = init_console_logger() if args.hpcmd_name == 'csv_gen': - # todo: build default dates in not -b or -e - start_date = datetime.strptime(args.begin, '%Y%m%d') - stop_date = datetime.strptime(args.end, '%Y%m%d') + # build default dates in not -b or not -e + sta, sto = get_default_sta_sto( args.begin, args.end) + if args.all: # build the {target_slug: [input list]} dictionnary from config.yaml targets = {t['slug']: list(t['models'].keys()) for t in config['targets']} @@ -43,4 +43,4 @@ if __name__ == "__main__": print(f"generating csv for {_t}/ {_i}") if args.dry_run: continue - generate_csv_file_if_needed(_t, _i, start_date, stop_date) + generate_csv_file_if_needed(_t, _i, sta, sto) diff --git a/web/run.py b/web/run.py index 61a5167..6092549 100755 --- a/web/run.py +++ b/web/run.py @@ -1082,26 +1082,51 @@ def get_input_slug_from_query(inp=None): return input_slug -def get_interval_from_query(): +def get_default_sta_sto(sta=None, sto=None): + + # TODO : maybe get from configuration + relative_after = config['defaults']['relative_after'] + relative_before = config['defaults']['relative_before'] + before = relativedelta(months=relative_before) + after = relativedelta(months=relative_after) + + if sta is None and sto is None: + today = datetime.datetime.now().replace(hour=0, minute=0, second=0) + sta_date = today - before + sto_date = today + after + + if sta is None and sto is not None: + sto_date = dateparser.isoparse(sto) + sta_date = sto_date - before + + if sta is not None and sto is None: + sta_date = dateparser.isoparse(sta) + sto_date = sta_date + after + + return sta_date, sto_date + + +def get_interval_from_query(_req=None): """ - Get the interval from the query, or from defaults. + Get the interval from the query as flask.request, or from defaults. Returns ISO date strings. """ - before = relativedelta(months=1) - after = relativedelta(months=1) - today = datetime.datetime.now().replace(hour=0, minute=0, second=0) - started_at = today - before - stopped_at = today + after - default_started_at = started_at.strftime(FILE_DATE_FMT) - default_stopped_at = stopped_at.strftime(FILE_DATE_FMT) - - started_at = request.args.get('started_at', default_started_at) - stopped_at = request.args.get('stopped_at', default_stopped_at) - - d_started_at = dateparser.isoparse(started_at) - d_stopped_at = dateparser.isoparse(stopped_at) - started_at = d_started_at.strftime(FILE_DATE_FMT) - stopped_at = d_stopped_at.strftime(FILE_DATE_FMT) + + sta_default, sto_default = get_default_sta_sto() + + # convert to string + started_at_default = sta_default.strftime(FILE_DATE_FMT) + stopped_at_default = sto_default.strftime(FILE_DATE_FMT) + + if _req is not None and 'Request' == _req.__class__.__name__: + started_at_str = _req.args.get('started_at', started_at_default) + stopped_at_str = _req.args.get('stopped_at', stopped_at_default) + + # convert back to proper format + started_at_date = dateparser.isoparse(started_at_str) + stopped_at_date = dateparser.isoparse(stopped_at_str) + started_at = started_at_date.strftime(FILE_DATE_FMT) + stopped_at = stopped_at_date.strftime(FILE_DATE_FMT) return started_at, stopped_at @@ -1266,7 +1291,7 @@ def home(): parameters = sorted(parameters, key=lambda x: x['position']) input_slug = get_input_slug_from_query() targets = [t for t in config['targets'] if not t['locked']] - started_at, stopped_at = get_interval_from_query() + started_at, stopped_at = get_interval_from_query(request) for i, target in enumerate(targets): targets[i]['catalog_layers'] = get_catalog_layers( input_slug, target['slug'], started_at, stopped_at @@ -1771,7 +1796,7 @@ def cache_warmup(): Warms up the cache for the current day. """ warmup_started_at = datetime.datetime.now() - sta, sto = get_interval_from_query() + sta, sto = get_interval_from_query(request) inp = config['defaults']['input_slug'] # default input, maybe warm them all up ? targets = get_active_targets() -- libgit2 0.21.2