[Coffee] [PATCH coffee-flask 1/2] Add graphs with total coffee consumption on login screen
Jaroslav Klapalek
klapajar at fel.cvut.cz
Fri Sep 14 14:35:33 CEST 2018
Fixed version that takes into account timezone related issue, where
coffees made close to midnight are counted for wrong day.
---
app.py | 37 +++++++++++++++++++++++++++++++++++--
coffee_db.py | 25 ++++++++++++++++++++++++-
templates/user.html | 5 ++++-
3 files changed, 63 insertions(+), 4 deletions(-)
diff --git a/app.py b/app.py
index 807f614..a597ad6 100644
--- a/app.py
+++ b/app.py
@@ -78,7 +78,35 @@ def coffee_graph_flavors():
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("Team's taste")
+
+ fig.savefig(b, format="svg", bbox_inches="tight")
+ b.seek(0)
+ return send_file(b, mimetype="image/svg+xml")
+
+ at app.route("/coffee/graph_flavors_history")
+def coffee_graph_flavors_history():
+ b = BytesIO()
+ if "uid" in session:
+ uid = session["uid"]
+ flavors, counts = zip(*db.coffee_flavors_history(uid))
+ else:
+ flavors, counts = zip(*db.coffee_flavors_history())
+ 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))
+
+ 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 +161,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..ee84c90 100644
--- a/coffee_db.py
+++ b/coffee_db.py
@@ -79,6 +79,28 @@ def coffee_flavors(uid=None):
close_db(conn)
return res
+def coffee_flavors_history(uid=None):
+ conn, c = open_db()
+
+ if uid is None:
+ res = list(c.execute("""
+ select f.name, count(c.flavor) from
+ (select num,date('now', 'localtime', -num || ' days') as d from days) ds
+ left join flavors f
+ left join (select * from coffees) c
+ on f.name=c.flavor and d = date(c.time) group by f.name
+ """))
+ else:
+ res = list(c.execute("""
+ select f.name, count(c.flavor) from
+ (select num,date('now', 'localtime', -num || ' days') as d from days) ds
+ left join flavors f
+ left join (select * from coffees where id = ?) c
+ on f.name=c.flavor and d = date(c.time) group by f.name
+ """, (uid,)))
+
+ close_db(conn)
+ return res
def coffee_history(uid=None):
conn, c = open_db()
@@ -87,7 +109,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 95ee95e..51a41f6 100644
--- a/templates/user.html
+++ b/templates/user.html
@@ -30,5 +30,8 @@
</form>
</p
{% else %}
- Use your card/token to log in...
+ <img src="{{ url_for('coffee_graph_history', _external=True, stamp=stamp) }}">
+ <img src="{{ url_for('coffee_graph_flavors_history', _external=True, stamp=stamp) }}">
+
+ <p>Use your card/token to log in...</p>
{% endif %}
--
2.7.4
More information about the Coffee
mailing list