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')
@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")
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)
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()
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:
</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 %}