From c0df94bcbfe6377d38071df0c728dbac38d5ee77 Mon Sep 17 00:00:00 2001
From: Goutte <antoine.goutenoir@gmail.com>
Date: Thu, 20 Jul 2017 10:57:06 +0200
Subject: [PATCH] Adding more logs.

---
 web/run.py | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/web/run.py b/web/run.py
index 8737e34..5352d9d 100755
--- a/web/run.py
+++ b/web/run.py
@@ -41,6 +41,8 @@ with open(get_path('../VERSION'), 'r') as version_file:
 with open(get_path('../config.yml'), 'r') as config_file:
     config = yaml_load(config_file.read())
 
+FILE_DATE_FMT = "%Y-%m-%dT%H:%M:%S"
+
 
 # LOGGING #####################################################################
 
@@ -309,6 +311,23 @@ def generate_csv_contents(source_config, started_at, stopped_at):
     return si.getvalue()
 
 
+def generate_csv_file_if_needed(target_config, started_at, stopped_at):
+    filename = "%s_%s_%s.csv" % (target_config.slug,
+                                 started_at.strftime(FILE_DATE_FMT),
+                                 stopped_at.strftime(FILE_DATE_FMT))
+    local_csv_file = get_path("../cache/%s" % filename)
+    if not isfile(local_csv_file):
+        log.info("Generating CSV '%s'..." % local_csv_file)
+        try:
+            with open(local_csv_file, mode="w+") as f:
+                f.write(generate_csv_contents(target_config,
+                                              started_at=started_at,
+                                              stopped_at=stopped_at))
+            log.info("Generation of '%s' done." % filename)
+        except Exception as e:
+            abort(500, "Failed creating CSV '%s' : %s" % (filename, e.message))
+
+
 def increment_hit_counter():
     hit_count_path = get_path("../VISITS")
 
@@ -355,30 +374,21 @@ def get_target_csv(source, started_at, stopped_at):
     rearrange it and return it as a CSV file.
     `started_at` and `stopped_at` should be UTC.
     """
-    # http://cdpp1.cesr.fr/BASE/DDService/getDataUrl.php?dataSet=tao_ros_sw&StartTime=2014-02-23T10:00&StopTime=2016-02-24T23:59
-    # Process input parameters
     source_config = get_source_config(source)
-    date_fmt = "%Y-%m-%dT%H:%M:%S"
     try:
-        started_at = datetime.datetime.strptime(started_at, date_fmt)
+        started_at = datetime.datetime.strptime(started_at, FILE_DATE_FMT)
     except:
         abort(400, "Invalid started_at parameter : '%s'." % started_at)
     try:
-        stopped_at = datetime.datetime.strptime(stopped_at, date_fmt)
+        stopped_at = datetime.datetime.strptime(stopped_at, FILE_DATE_FMT)
     except:
         abort(400, "Invalid stopped_at parameter : '%s'." % stopped_at)
 
     filename = "%s_%s_%s.csv" % (source,
-                                 started_at.strftime(date_fmt),
-                                 stopped_at.strftime(date_fmt))
-
+                                 started_at.strftime(FILE_DATE_FMT),
+                                 stopped_at.strftime(FILE_DATE_FMT))
     local_csv_file = get_path("../cache/%s" % filename)
-    if not isfile(local_csv_file):
-        with open(local_csv_file, mode="w+") as f:
-            f.write(generate_csv_contents(source_config,
-                                          started_at=started_at,
-                                          stopped_at=stopped_at))
-
+    generate_csv_file_if_needed(source_config, started_at, stopped_at)
     if not isfile(local_csv_file):
         abort(500, "Could not cache CSV file at '%s'." % local_csv_file)
 
--
libgit2 0.21.2