]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Fix coffee graph for start of the month
authorJaroslav Klapalek <klapajar@fel.cvut.cz>
Mon, 3 Sep 2018 16:03:43 +0000 (18:03 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Mon, 3 Sep 2018 16:18:39 +0000 (18:18 +0200)
An error was found during the start of new month. To fix this I have
changed the SQL query to use '%s' (unix time) instead of '%d' (day
number).

How does this work?
The SQL query is using relation between two tables based on the date.
This relation is created using these parts of query:
 > "select num,date('now',-num || ' days')" as d from days
 > d = date(c.time)

First line creates timestampes of last 7 days -- time is set to
UTC midnight.
Second line converts data in 'c.time' to date struct - BUT! Even
here the time is set to midnight. This behaviour makes the relation
possible.

How does it behave in app.py?
Result of the query contains data that are ordered by time.
Unfortunately this order is lost during recasting the data to
list/dict (which are unordered). To solve this sorting is used.
Sorting data with unix time is safe, because it is always increasing.

To highlight this change, 'days' variable is refactored to 'unix_days'.

app.py
coffee_db.py

diff --git a/app.py b/app.py
index 54816242b4640af21592d5c153f659a4ccadd3f1..807f614540346279580a34e9add88712d4eddfa2 100644 (file)
--- a/app.py
+++ b/app.py
@@ -93,21 +93,21 @@ def coffee_graph_history():
     else:
         hist = db.coffee_history()
     if hist == []:
-        days = tuple()
+        unix_days = tuple()
         counts = tuple()
         flavors = tuple()
     else:
-        days, counts, flavors = zip(*hist)
+        unix_days, counts, flavors = zip(*hist)
     fig = plt.figure(figsize=(4, 3))
     ax = fig.add_subplot(111)
 
     list_flavor = sorted(db.flavors())
     l = [{} for i in range(len(list_flavor))]
     for ll in l:
-        for d in days:
+        for d in unix_days:
             ll[d] = 0
 
-    for(d, c, f) in zip(days, counts, flavors):
+    for(d, c, f) in zip(unix_days, counts, flavors):
         if f is None:
             continue
         what_f = 0
@@ -125,13 +125,13 @@ def coffee_graph_history():
         ax.bar(range(len(x)), y, bottom=z)
         z = [sum(i) for i in zip(y, z)]
 
-    days = set(days)
+    unix_days = set(unix_days)
     xdays = [i.strftime("%a") for i in [
         date.today() - timedelta(j - 1) for j in
-        range(len(days), 0, -1)]]
+        range(len(unix_days), 0, -1)]]
     xdays[-1] = "TDY"
     xdays[-2] = "YDA"
-    ax.set_xticks(range(len(days)))
+    ax.set_xticks(range(len(unix_days)))
     ax.set_xticklabels(xdays)
     ax.set_title("Your week")
     ax.yaxis.set_major_locator(MaxNLocator(integer=True))
index 0abb5a4fe68a0010ea5cb7d12ad97cc3062bf434..8c18e63982bacb3e95fed035af47073d4fe4cd97 100644 (file)
@@ -95,7 +95,7 @@ def coffee_history(uid=None):
 
     if uid is None:
         res = list(c.execute("""
-            select strftime('%d', ds.d),count(c.flavor),c.flavor from
+            select strftime('%s', ds.d),count(c.flavor),c.flavor from
             (select num,date('now',-num || ' days') as d from days) ds
             left join coffees c
             on d = date(c.time) group by d, c.flavor
@@ -103,7 +103,7 @@ def coffee_history(uid=None):
     else:
         res = list(c.execute(
             """
-            select strftime('%d', ds.d),count(c.flavor),c.flavor from
+            select strftime('%s', ds.d),count(c.flavor),c.flavor from
             (select num,date('now',-num || ' days') as d from days) ds
             left join
             (select time,flavor from coffees where id = ?) c