Commit 89f61bb226e079d459098473dee3e486d58c0a3f

Authored by hitier
1 parent a1a624db
Exists in rhitier-dev

New get_default_sta_sto()

Showing 3 changed files with 51 additions and 24 deletions   Show diff stats
config.yml
... ... @@ -52,6 +52,8 @@ amda: "http://amda.irap.omp.eu/DDService/getDataUrl.php?dataSet={dataSet}&StartT
52 52  
53 53 defaults:
54 54 input_slug: 'om'
  55 + relative_before: 1
  56 + relative_after: 1
55 57  
56 58 # Data files are in data/catalog/
57 59 # Constraints are joined by an outer OR and an inner AND
... ...
web/hp_cmd.py
1 1 import sys
2 2  
3   -from run import generate_csv_file_if_needed, init_console_logger, config
  3 +from run import generate_csv_file_if_needed, init_console_logger, config, get_default_sta_sto
4 4 from datetime import datetime
5 5 import argparse
6 6  
... ... @@ -27,9 +27,9 @@ if __name__ == "__main__":
27 27 _logger = init_console_logger()
28 28  
29 29 if args.hpcmd_name == 'csv_gen':
30   - # todo: build default dates in not -b or -e
31   - start_date = datetime.strptime(args.begin, '%Y%m%d')
32   - stop_date = datetime.strptime(args.end, '%Y%m%d')
  30 + # build default dates in not -b or not -e
  31 + sta, sto = get_default_sta_sto( args.begin, args.end)
  32 +
33 33 if args.all:
34 34 # build the {target_slug: [input list]} dictionnary from config.yaml
35 35 targets = {t['slug']: list(t['models'].keys()) for t in config['targets']}
... ... @@ -43,4 +43,4 @@ if __name__ == "__main__":
43 43 print(f"generating csv for {_t}/ {_i}")
44 44 if args.dry_run:
45 45 continue
46   - generate_csv_file_if_needed(_t, _i, start_date, stop_date)
  46 + generate_csv_file_if_needed(_t, _i, sta, sto)
... ...
web/run.py
... ... @@ -1082,26 +1082,51 @@ def get_input_slug_from_query(inp=None):
1082 1082 return input_slug
1083 1083  
1084 1084  
1085   -def get_interval_from_query():
  1085 +def get_default_sta_sto(sta=None, sto=None):
  1086 +
  1087 + # TODO : maybe get from configuration
  1088 + relative_after = config['defaults']['relative_after']
  1089 + relative_before = config['defaults']['relative_before']
  1090 + before = relativedelta(months=relative_before)
  1091 + after = relativedelta(months=relative_after)
  1092 +
  1093 + if sta is None and sto is None:
  1094 + today = datetime.datetime.now().replace(hour=0, minute=0, second=0)
  1095 + sta_date = today - before
  1096 + sto_date = today + after
  1097 +
  1098 + if sta is None and sto is not None:
  1099 + sto_date = dateparser.isoparse(sto)
  1100 + sta_date = sto_date - before
  1101 +
  1102 + if sta is not None and sto is None:
  1103 + sta_date = dateparser.isoparse(sta)
  1104 + sto_date = sta_date + after
  1105 +
  1106 + return sta_date, sto_date
  1107 +
  1108 +
  1109 +def get_interval_from_query(_req=None):
1086 1110 """
1087   - Get the interval from the query, or from defaults.
  1111 + Get the interval from the query as flask.request, or from defaults.
1088 1112 Returns ISO date strings.
1089 1113 """
1090   - before = relativedelta(months=1)
1091   - after = relativedelta(months=1)
1092   - today = datetime.datetime.now().replace(hour=0, minute=0, second=0)
1093   - started_at = today - before
1094   - stopped_at = today + after
1095   - default_started_at = started_at.strftime(FILE_DATE_FMT)
1096   - default_stopped_at = stopped_at.strftime(FILE_DATE_FMT)
1097   -
1098   - started_at = request.args.get('started_at', default_started_at)
1099   - stopped_at = request.args.get('stopped_at', default_stopped_at)
1100   -
1101   - d_started_at = dateparser.isoparse(started_at)
1102   - d_stopped_at = dateparser.isoparse(stopped_at)
1103   - started_at = d_started_at.strftime(FILE_DATE_FMT)
1104   - stopped_at = d_stopped_at.strftime(FILE_DATE_FMT)
  1114 +
  1115 + sta_default, sto_default = get_default_sta_sto()
  1116 +
  1117 + # convert to string
  1118 + started_at_default = sta_default.strftime(FILE_DATE_FMT)
  1119 + stopped_at_default = sto_default.strftime(FILE_DATE_FMT)
  1120 +
  1121 + if _req is not None and 'Request' == _req.__class__.__name__:
  1122 + started_at_str = _req.args.get('started_at', started_at_default)
  1123 + stopped_at_str = _req.args.get('stopped_at', stopped_at_default)
  1124 +
  1125 + # convert back to proper format
  1126 + started_at_date = dateparser.isoparse(started_at_str)
  1127 + stopped_at_date = dateparser.isoparse(stopped_at_str)
  1128 + started_at = started_at_date.strftime(FILE_DATE_FMT)
  1129 + stopped_at = stopped_at_date.strftime(FILE_DATE_FMT)
1105 1130  
1106 1131 return started_at, stopped_at
1107 1132  
... ... @@ -1266,7 +1291,7 @@ def home():
1266 1291 parameters = sorted(parameters, key=lambda x: x['position'])
1267 1292 input_slug = get_input_slug_from_query()
1268 1293 targets = [t for t in config['targets'] if not t['locked']]
1269   - started_at, stopped_at = get_interval_from_query()
  1294 + started_at, stopped_at = get_interval_from_query(request)
1270 1295 for i, target in enumerate(targets):
1271 1296 targets[i]['catalog_layers'] = get_catalog_layers(
1272 1297 input_slug, target['slug'], started_at, stopped_at
... ... @@ -1771,7 +1796,7 @@ def cache_warmup():
1771 1796 Warms up the cache for the current day.
1772 1797 """
1773 1798 warmup_started_at = datetime.datetime.now()
1774   - sta, sto = get_interval_from_query()
  1799 + sta, sto = get_interval_from_query(request)
1775 1800 inp = config['defaults']['input_slug'] # default input, maybe warm them all up ?
1776 1801  
1777 1802 targets = get_active_targets()
... ...