[Coffee] [PATCH coffee-flask] Fix coffee graph for start of the month

Jaroslav Klapalek klapajar at fel.cvut.cz
Mon Sep 3 18:03:43 CEST 2018


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       | 14 +++++++-------
 coffee_db.py |  4 ++--
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/app.py b/app.py
index 5481624..807f614 100644
--- 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))
diff --git a/coffee_db.py b/coffee_db.py
index 0abb5a4..8c18e63 100644
--- a/coffee_db.py
+++ b/coffee_db.py
@@ -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
-- 
2.7.4




More information about the Coffee mailing list