Commit c0df94bcbfe6377d38071df0c728dbac38d5ee77

Authored by Goutte
1 parent 57f42bd7

Adding more logs.

Showing 1 changed file with 24 additions and 14 deletions   Show diff stats
web/run.py
... ... @@ -41,6 +41,8 @@ with open(get_path('../VERSION'), 'r') as version_file:
41 41 with open(get_path('../config.yml'), 'r') as config_file:
42 42 config = yaml_load(config_file.read())
43 43  
  44 +FILE_DATE_FMT = "%Y-%m-%dT%H:%M:%S"
  45 +
44 46  
45 47 # LOGGING #####################################################################
46 48  
... ... @@ -309,6 +311,23 @@ def generate_csv_contents(source_config, started_at, stopped_at):
309 311 return si.getvalue()
310 312  
311 313  
  314 +def generate_csv_file_if_needed(target_config, started_at, stopped_at):
  315 + filename = "%s_%s_%s.csv" % (target_config.slug,
  316 + started_at.strftime(FILE_DATE_FMT),
  317 + stopped_at.strftime(FILE_DATE_FMT))
  318 + local_csv_file = get_path("../cache/%s" % filename)
  319 + if not isfile(local_csv_file):
  320 + log.info("Generating CSV '%s'..." % local_csv_file)
  321 + try:
  322 + with open(local_csv_file, mode="w+") as f:
  323 + f.write(generate_csv_contents(target_config,
  324 + started_at=started_at,
  325 + stopped_at=stopped_at))
  326 + log.info("Generation of '%s' done." % filename)
  327 + except Exception as e:
  328 + abort(500, "Failed creating CSV '%s' : %s" % (filename, e.message))
  329 +
  330 +
312 331 def increment_hit_counter():
313 332 hit_count_path = get_path("../VISITS")
314 333  
... ... @@ -355,30 +374,21 @@ def get_target_csv(source, started_at, stopped_at):
355 374 rearrange it and return it as a CSV file.
356 375 `started_at` and `stopped_at` should be UTC.
357 376 """
358   - # http://cdpp1.cesr.fr/BASE/DDService/getDataUrl.php?dataSet=tao_ros_sw&StartTime=2014-02-23T10:00&StopTime=2016-02-24T23:59
359   - # Process input parameters
360 377 source_config = get_source_config(source)
361   - date_fmt = "%Y-%m-%dT%H:%M:%S"
362 378 try:
363   - started_at = datetime.datetime.strptime(started_at, date_fmt)
  379 + started_at = datetime.datetime.strptime(started_at, FILE_DATE_FMT)
364 380 except:
365 381 abort(400, "Invalid started_at parameter : '%s'." % started_at)
366 382 try:
367   - stopped_at = datetime.datetime.strptime(stopped_at, date_fmt)
  383 + stopped_at = datetime.datetime.strptime(stopped_at, FILE_DATE_FMT)
368 384 except:
369 385 abort(400, "Invalid stopped_at parameter : '%s'." % stopped_at)
370 386  
371 387 filename = "%s_%s_%s.csv" % (source,
372   - started_at.strftime(date_fmt),
373   - stopped_at.strftime(date_fmt))
374   -
  388 + started_at.strftime(FILE_DATE_FMT),
  389 + stopped_at.strftime(FILE_DATE_FMT))
375 390 local_csv_file = get_path("../cache/%s" % filename)
376   - if not isfile(local_csv_file):
377   - with open(local_csv_file, mode="w+") as f:
378   - f.write(generate_csv_contents(source_config,
379   - started_at=started_at,
380   - stopped_at=stopped_at))
381   -
  391 + generate_csv_file_if_needed(source_config, started_at, stopped_at)
382 392 if not isfile(local_csv_file):
383 393 abort(500, "Could not cache CSV file at '%s'." % local_csv_file)
384 394  
... ...