]> rtime.felk.cvut.cz Git - hubacji1/coffee-getter.git/commitdiff
Add get top drinks from db
authorJiri Vlasak <hubacji1@fel.cvut.cz>
Thu, 2 Jan 2020 10:22:05 +0000 (11:22 +0100)
committerJiri Vlasak <hubacji1@fel.cvut.cz>
Thu, 2 Jan 2020 10:59:40 +0000 (11:59 +0100)
cbdb.py

diff --git a/cbdb.py b/cbdb.py
index 22e00581ae4326a79e7724c32764a61239fe5fc8..2c53f41aa016cb94c060e33fd3455f1fc18558b9 100644 (file)
--- a/cbdb.py
+++ b/cbdb.py
@@ -22,6 +22,24 @@ class Db:
         if self.con:
             self.con.close()
 
+    def get_top_drinks(self):
+        """Return list of pairs of drink name and count."""
+        q = """
+
+        SELECT count(*), flavor
+        FROM coffees
+        WHERE time BETWEEN
+            datetime('now', 'localtime', '-7 days')
+            AND datetime('now', 'localtime')
+        GROUP BY flavor
+
+        """
+        top = []
+        for (cnt, dn) in self.cur.execute(q):
+            top.append((dn, cnt))
+        top.sort(key=lambda x: (x[1], x[0]), reverse=True)
+        return top
+
     def getTopMateDrinkers(self):
         """Return list of pairs of name, count for Mate drinkers."""
         users = {}