From ff0db8c532e86b813785b15c5921be9f774c16bb Mon Sep 17 00:00:00 2001
From: root <root@onni.utu.fi>
Date: Thu, 14 May 2020 12:16:27 +0300
Subject: [PATCH] Production setup

---
 .../templates/experiment_statistics.html      |  6 +++++
 app/experiment/views.py                       |  2 +-
 app/routes.py                                 | 22 ++++++++++++++-----
 config.py                                     |  9 +++++++-
 embody_plot.py                                | 17 ++++++++++++--
 5 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/app/experiment/templates/experiment_statistics.html b/app/experiment/templates/experiment_statistics.html
index 9c3bbd8..817470e 100644
--- a/app/experiment/templates/experiment_statistics.html
+++ b/app/experiment/templates/experiment_statistics.html
@@ -130,6 +130,12 @@
                   <iframe class="embed-responsive-item thumbnail" src="/{{ s.media }}" allowFullScreen></iframe>
               </div>
             </td>
+          {% elif s.type == 'audio' %}
+            <td>
+              <div class="embed-responsive embed-responsive-16by9 ">
+                  <iframe class="embed-responsive-item thumbnail" src="/{{ s.media }}" allowFullScreen></iframe>
+              </div>
+            </td>
           {% else %}
             <td>{{ s.text }}</td>
           {% endif %}
diff --git a/app/experiment/views.py b/app/experiment/views.py
index 0d40d63..6b02e51 100644
--- a/app/experiment/views.py
+++ b/app/experiment/views.py
@@ -986,7 +986,7 @@ def create_embody(page_id):
     embody = page_id["embody"]
 
     img_path = embody_plot.get_coordinates(page, embody)
-    print(img_path)
+    app.logger.info(img_path)
     emit('end', {'path':img_path})
 
 @socketio.on('end', namespace="/create_embody")
diff --git a/app/routes.py b/app/routes.py
index ada9a29..342fcb1 100644
--- a/app/routes.py
+++ b/app/routes.py
@@ -409,12 +409,22 @@ def download_csv():
                 embody_answers = embody_answer.query.filter_by(answer_set_idanswer_set=participant.idanswer_set).all()     
                 answers_list = []
                 for embody_answer_data in embody_answers:
-                    embody_answer_data = json.loads(embody_answer_data.coordinates)
-                    coordinates_to_bitmap = [[0 for x in range(embody_answer_data['height'] + 2)] for y in range(embody_answer_data['width'] + 2)] 
-                    for point in list(zip( embody_answer_data['x'], embody_answer_data['y'])):
-                        coordinates_to_bitmap[point[0]][point[1]] += 0.1
 
-                    answers_list.append(json.dumps(coordinates_to_bitmap))
+                    try:
+                        embody_answer_data = json.loads(embody_answer_data.coordinates)
+                        coordinates_to_bitmap = [[0 for x in range(embody_answer_data['height'] + 2)] for y in range(embody_answer_data['width'] + 2)] 
+
+                        for point in list(zip( embody_answer_data['x'], embody_answer_data['y'] )):
+
+                            try:
+                                 coordinates_to_bitmap[point[0]][point[1]] += 0.1
+                            except IndexError:
+                                 continue
+
+                        answers_list.append(json.dumps(coordinates_to_bitmap))
+  
+                    except ValueError as err:
+                        app.logger.info(err)
 
                 # old way to save only visited points:
                 # answers_list = [ json.dumps(list(zip( json.loads(a.coordinates)['x'], json.loads(a.coordinates)['y']))) for a in embody_answers]    
@@ -444,4 +454,4 @@ def researcher_info():
     return render_template('researcher_info.html')
 
 
-# EOF
\ No newline at end of file
+# EOF
diff --git a/config.py b/config.py
index b945f2f..26d0769 100644
--- a/config.py
+++ b/config.py
@@ -27,6 +27,13 @@ class Config(object):
     SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://'+MYSQL_USER+':'+MYSQL_PASSWORD+'@'+MYSQL_SERVER+'/'+MYSQL_DB+'?charset=utf8mb4'
    
     SQLALCHEMY_TRACK_MODIFICATIONS = False
+
+    SQLALCHEMY_ENGINE_OPTIONS = {
+	"pool_pre_ping": True,
+	"pool_recycle": 300,
+	"max_overflow": 30,
+	"pool_size": 20
+    }
     
     TEMPLATES_AUTO_RELOAD = True
-    DEBUG = True
+    DEBUG = False
diff --git a/embody_plot.py b/embody_plot.py
index 76bafdf..58f629a 100644
--- a/embody_plot.py
+++ b/embody_plot.py
@@ -36,7 +36,7 @@ from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
 from matplotlib.figure import Figure
 
 from flask_socketio import emit
-from app import socketio
+from app import socketio, app
 from config import Config
 
 
@@ -134,6 +134,7 @@ def get_coordinates(idpage, idembody=None, select_clause=SELECT_BY_PAGE_AND_PICT
         image_path = db._db_cur.fetchone()[0]
         image_path = './app' + image_path
 
+
         # Draw image
         plt = plot_coordinates(coordinates, image_path)
     else:
@@ -167,6 +168,9 @@ def format_coordinates(cursor):
             standard_radiuses = np.full((1, len(coordinates['x'])), standard_radius).tolist()[0]
             r.extend(standard_radiuses)
             continue
+        except ValueError as err:
+            app.logger.info(err)
+            continue
 
     return {
         "x":x,
@@ -196,9 +200,15 @@ def plot_coordinates(coordinates, image_path=DEFAULT_IMAGE_PATH):
     frame = np.zeros((image_data[0] + 10,image_data[1] + 10))
 
     if image_path == DEFAULT_IMAGE_PATH:
+        #app.logger.info(coordinates)
 
         for idx, point in enumerate(coordinates["coordinates"]):
-            frame[int(point[1]), int(point[0])] = 1
+
+            try:
+            	frame[int(point[1]), int(point[0])] = 1
+            except IndexError as err:
+            	app.logger.info(err)
+
             point = ndimage.gaussian_filter(frame, sigma=5)
             ax2.imshow(point, cmap='hot', interpolation='none')
 
@@ -212,6 +222,7 @@ def plot_coordinates(coordinates, image_path=DEFAULT_IMAGE_PATH):
 
         image_mask = mpimg.imread(IMAGE_PATH_MASK)
         ax2.imshow(image_mask)
+
     else:
         # TODO: gaussian disk appearing only on empty spaces in the pictures
         # -> at the moment this implementation works only for the default image
@@ -223,6 +234,8 @@ def plot_coordinates(coordinates, image_path=DEFAULT_IMAGE_PATH):
     ax1.plot(coordinates["x"],coordinates["y"], 'ro', alpha=0.2)
     ax1.imshow(image, alpha=0.6)
 
+    app.logger.info("iamge plotted")
+
     # return figure for saving/etc...
     return fig
 
-- 
GitLab