]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Add graphs with total coffee consumption on login screen
authorJaroslav Klapalek <klapajar@fel.cvut.cz>
Thu, 11 Oct 2018 11:42:38 +0000 (13:42 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Fri, 12 Oct 2018 07:10:29 +0000 (09:10 +0200)
app.py
coffee_db.py
templates/user.html

diff --git a/app.py b/app.py
index 807f614540346279580a34e9add88712d4eddfa2..c5fdc3c07f6d50b8eb16716eff6c596c53706935 100644 (file)
--- a/app.py
+++ b/app.py
@@ -53,7 +53,7 @@ def user():
                                count=db.coffee_count(uid, 0),
                                stamp=time.time()
                                )
-    return render_template('user.html')
+    return render_template('user.html', stamp=time.time())
 
 
 @app.route('/user/rename')
@@ -67,18 +67,26 @@ def user_rename():
 
 @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)
+
     b = BytesIO()
     if "uid" in session:
         uid = session["uid"]
-        flavors, counts = zip(*db.coffee_flavors(uid))
+        flavors, counts = zip(*db.coffee_flavors(uid, days, start))
     else:
-        flavors, counts = zip(*db.coffee_flavors())
+        flavors, counts = zip(*db.coffee_flavors(None, days, start))
     fig = plt.figure(figsize=(3, 3))
     ax = fig.add_subplot(111)
     ax.set_aspect(1)
     ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '')
     ax.legend(flavors, bbox_to_anchor=(1.0, 1.0))
-    ax.set_title("Your taste")
+
+    if "uid" in session:
+        ax.set_title("Your taste")
+    else:
+        ax.set_title("This week taste")
+
     fig.savefig(b, format="svg", bbox_inches="tight")
     b.seek(0)
     return send_file(b, mimetype="image/svg+xml")
@@ -133,7 +141,12 @@ def coffee_graph_history():
     xdays[-2] = "YDA"
     ax.set_xticks(range(len(unix_days)))
     ax.set_xticklabels(xdays)
-    ax.set_title("Your week")
+
+    if "uid" in session:
+        ax.set_title("Your week")
+    else:
+        ax.set_title("This week total")
+
     ax.yaxis.set_major_locator(MaxNLocator(integer=True))
     fig.savefig(b, format="svg", bbox_inches="tight")
     b.seek(0)
index b708d031b08ed718589caa891e5de9aa3ae45fe4..8653f98fbb274377c1b7b17b5c5f33fba7dd922b 100644 (file)
@@ -60,26 +60,42 @@ def flavors():
     close_db(conn)
     return res
 
-def coffee_flavors(uid=None):
+def coffee_flavors(uid=None, days=0, start=0):
+    """Returns flavor statistics for team/user during selected period/since beginning.
+
+    days -- number of days for computation
+    start -- shift size from the current time
+
+    When 'days' is not given or 0 return statistics since the beginning.
+
+    e.g. (7, 7) returns statistics for 7 days, 7 days ago.
+    """
+
     conn, c = open_db()
 
-    if uid is None:
-        res = list(c.execute("""
-            select f.name, count(c.flavor) from flavors f left join
-            (select * from coffees) c
-            on f.name=c.flavor group by f.name
-            """))
-    else:
-        res = list(c.execute("""
-            select f.name, count(c.flavor) from flavors f left join
-            (select * from coffees where id = ?) c
-            on f.name=c.flavor group by f.name
-            """, (uid,)))
+    query = ""
+    variables = list()
+
+    if days is not None and days != 0:
+        query += " where date(time) between date('now', 'localtime', '-"+ str(days) +" days') and date('now', 'localtime', '-"+ str(start) +" days')"
+
+        if uid is not None:
+            query += " and id = ?"
+            variables.append(uid)
+    elif uid is not None:
+        query += " where id = ?"
+        variables.append(uid)
+
+    res = list(c.execute("""
+        select f.name, count(c.flavor) from flavors f
+        left join (select * from coffees
+        """+query+""") c
+        on f.name=c.flavor group by f.name
+        """, variables))
 
     close_db(conn)
     return res
 
-
 def coffee_history(uid=None):
     conn, c = open_db()
 
@@ -87,7 +103,8 @@ def coffee_history(uid=None):
         res = list(c.execute("""
             select strftime('%s', ds.d),count(c.flavor),c.flavor from
             (select num,date('now', 'localtime', -num || ' days') as d from days) ds
-            left join coffees c
+            left join
+            (select time,flavor from coffees) c
             on d = date(c.time) group by d, c.flavor
             """))
     else:
index 0277d09aeb15dcccf7b3b245906d5b8645f62050..7e549b91f0197610a7fc7f2586bd064d287e8143 100644 (file)
@@ -30,5 +30,8 @@
         </form>
     </p>
 {% else %}
-    Use your card/token to log in...
+    <p>Use your card/token to log in...</p>
+
+    <img src="{{ url_for('coffee_graph_history', _external=True, stamp=stamp) }}">
+    <img src="{{ url_for('coffee_graph_flavors', _external=True, stamp=stamp, days=7) }}">
 {% endif %}