]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blobdiff - app.py
Fix "ValueError: cannot convert float NaN to integer"
[coffee/coffee-flask.git] / app.py
diff --git a/app.py b/app.py
index 14a26842d4faccf78c975f5e327bb3485f21f565..473da298079a73722f062943ed2634e71be1f581 100644 (file)
--- a/app.py
+++ b/app.py
@@ -1,9 +1,7 @@
 from flask import Flask, render_template, send_file, request, session, redirect, url_for, make_response
 from flask_cors import CORS
 
-import numpy as np
 import matplotlib
-matplotlib.use('Agg')
 import matplotlib.pyplot as plt
 from matplotlib.ticker import MaxNLocator
 from io import BytesIO
@@ -16,32 +14,43 @@ from datetime import date, timedelta, datetime, timezone
 from json import loads
 from requests import post
 import jinja2
+import threading
+
+matplotlib.use('Agg')
 
 db.init_db()
 app = Flask(__name__)
 CORS(app, supports_credentials=True)
 app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
 
+# Workaround for
+# https://github.com/matplotlib/matplotlib/issues/13723. Despite the
+# issue is closed, the problem is still there. See
+# https://github.com/matplotlib/matplotlib/issues/13723#issuecomment-761302131.
+plot_lock = threading.Lock()
 
 # Inspired by https://shubhamjain.co/til/how-to-render-human-readable-time-in-jinja/,
 # updated to our needs
-def humanize_ts(time):
-    """
-    Convert date in ISO format to relative, human readable string
-    like 'an hour ago', 'Yesterday', '3 months ago',
-    'just now', etc
+def humanize_ts(time, max_interval="years"):
+    """Convert date/time in ISO format to relative, human readable string.
+
+    Example return values: 'an hour ago', 'Yesterday', '3 months ago',
+    'just now', etc.
+
+    When optional max_interval is set to "days", the return value will
+    report at most the number of days ago, not week, months or years.
     """
     if jinja2.is_undefined(time):
         return time
-    now = datetime.now(timezone.utc)
-    if time[-1] == 'Z':     # Convert Zulu time zone to datetime compatible format
-        time = time[0:-1] + '+00:00'
-    diff = now - datetime.fromisoformat(time)
+    if max_interval not in ["years", "days"]:
+        raise ValueError
+
+    diff = datetime.now(timezone.utc) - time
     second_diff = diff.seconds
     day_diff = diff.days
 
     if day_diff < 0:
-        return ''
+        return 'in the future'
 
     if day_diff == 0:
         if second_diff < 10:
@@ -58,7 +67,7 @@ def humanize_ts(time):
             return str(int(second_diff / 3600)) + " hours ago"
     if day_diff == 1:
         return "Yesterday"
-    if day_diff < 7:
+    if day_diff < 7 or max_interval == "days":
         return str(day_diff) + " days ago"
     if day_diff < 31:
         return str(int(day_diff / 7)) + " weeks ago"
@@ -70,14 +79,31 @@ def humanize_ts(time):
 app.jinja_env.filters['humanize'] = humanize_ts
 
 
+def days_filter(time):
+    """Return the number of days elapsed since time."""
+    if jinja2.is_undefined(time):
+        return time
+    diff = datetime.now(timezone.utc) - time
+    return diff.days
+
+
+app.jinja_env.filters['days'] = days_filter
+
+
 @app.route('/')
 def hello():
-    if "uid" in session:
-        uid = session["uid"]
-        return render_template('hello.html', name=db.get_name(uid))
     return render_template('hello.html')
 
 
+@app.route('/home')
+def home():
+    # TODO: Replace stamp parameter with proper cache control HTTP
+    # headers in response
+    return render_template('home.html',
+                           stamp=time.time(),
+                           last_events=db.last_events())
+
+
 @app.route('/login', methods=["POST"])
 @app.route('/login/<iid>')
 def login(iid=None):
@@ -101,7 +127,7 @@ def login(iid=None):
 def logout():
     session.pop('uid', None)
     session.pop('iid', None)
-    return redirect(url_for('user'))
+    return redirect(url_for('home'))
 
 
 @app.route('/user')
@@ -118,11 +144,7 @@ def user():
                                stamp=time.time(),
                                last_events=db.last_events()
                                )
-    # TODO: Replace stamp parameter with proper cache control HTTP
-    # headers in response
-    return render_template('user.html', stamp=time.time(),
-                           last_events=db.last_events(),
-                           )
+    return redirect(url_for('home'))
 
 
 @app.route('/user/rename')
@@ -158,13 +180,13 @@ def user_disable_identifier():
         json = request.json
         if "uid" in session and "id" in json:
             db.disable_user_identifier(session["uid"], json["id"])
-    return logout() # force logout
+    return logout()  # force logout
 
 
 @app.route("/coffee/graph_flavors")
 def coffee_graph_flavors():
-    days = request.args.get('days', default = 0, type = int)
-    start = request.args.get('start', default = 0, type = int)
+    days = request.args.get('days', default=0, type=int)
+    start = request.args.get('start', default=0, type=int)
 
     b = BytesIO()
     if "uid" in session:
@@ -178,7 +200,7 @@ def coffee_graph_flavors():
     if "normalize" in matplotlib.pyplot.pie.__code__.co_varnames:
         # Matplotlib >= 3.3.0
         ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '',
-               normalize=True)
+               normalize=sum(counts) != 0)
     else:
         # Matplotlib < 3.3.0
         ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '')
@@ -190,7 +212,9 @@ def coffee_graph_flavors():
     else:
         ax.set_title("This week taste")
 
-    fig.savefig(b, format="svg", bbox_inches="tight")
+    with plot_lock:
+        fig.savefig(b, format="svg", bbox_inches="tight")
+        plt.close(fig)
     b.seek(0)
     return send_file(b, mimetype="image/svg+xml")
 
@@ -251,9 +275,10 @@ def coffee_graph_history():
         ax.set_title("This week total")
 
     ax.yaxis.set_major_locator(MaxNLocator(integer=True))
-    fig.savefig(b, format="svg", bbox_inches="tight")
+    with plot_lock:
+        fig.savefig(b, format="svg", bbox_inches="tight")
+        plt.close(fig)
     b.seek(0)
-    plt.close(fig)
     return send_file(b, mimetype="image/svg+xml")
 
 
@@ -276,14 +301,6 @@ def event_add():
     return redirect(url_for('user'))
 
 
-# TODO: Remove me - unused
-@app.route("/coffee/count")
-def coffee_count():
-    start = request.args.get("start")
-    stop = request.args.get("stop")
-    return str(dict(db.drink_count(session.get("uid"), start, stop)).get("coffee", 0))
-
-
 @app.route('/js')
 def js():
     response = make_response(render_template('main.js'))
@@ -299,6 +316,7 @@ def log():
         return data
     return "nope"
 
+
 @app.route("/tellCoffeebot", methods=["POST"])
 def tell_coffeebot():
     err = "Don't worry now! There is a NEW HOPE Tonda is buying NEW PACK!"