From: Jaroslav Klapalek Date: Thu, 11 Oct 2018 11:42:38 +0000 (+0200) Subject: Add graphs with total coffee consumption on login screen X-Git-Url: https://rtime.felk.cvut.cz/gitweb/coffee/coffee-flask.git/commitdiff_plain/d3fb15fdc416813d27cb266d5bd486a2e061e8b3 Add graphs with total coffee consumption on login screen --- diff --git a/app.py b/app.py index 807f614..c5fdc3c 100644 --- 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) diff --git a/coffee_db.py b/coffee_db.py index b708d03..8653f98 100644 --- a/coffee_db.py +++ b/coffee_db.py @@ -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: diff --git a/templates/user.html b/templates/user.html index 0277d09..7e549b9 100644 --- a/templates/user.html +++ b/templates/user.html @@ -30,5 +30,8 @@

{% else %} - Use your card/token to log in... +

Use your card/token to log in...

+ + + {% endif %}