4 dbdir = os.path.dirname(__file__)
5 dbdef = os.path.join(dbdir, "coffee_db.sql")
6 dbfile = os.path.join(dbdir, "coffee.db")
10 conn = sqlite3.connect(dbfile)
22 with open(dbdef, "r") as f:
23 c.executescript(f.read())
29 c.execute("insert or ignore into users (id) values (?)", (uid,))
33 def add_user_identifier(uid, iid, name):
34 # Check if this identifier is not currently associated with different account
38 # Try to remove old relation of identifier
39 # As 'delete or ignore' does not exist, workaround is used
40 res = c.execute("select * from identifiers where id = ? and active = 0", (iid, ))
42 # This is True when some rows were found before; delete old relation
44 print("Deleting uid:%s id:%s" % (uid, iid))
45 res = c.execute("delete from identifiers where id = ? and active = 0", (iid, ))
48 res = c.execute("insert into identifiers (userid, id, name) values (?, ?, ?)", (uid, iid, name, ))
53 def disable_user_identifier(uid, iid):
55 c.execute("update identifiers set active = 0 where userid = ? and id = ?", (uid, iid, ))
61 for name, in c.execute("select name from users where id = ?", (uid,)):
70 res = list(c.execute("""
71 select userid from identifiers where id = ? and active
75 return res[0][0] if len(res) > 0 else None
78 def name_user(uid, name):
80 c.execute("update users set name = ? where id = ?", (name, uid))
84 def rename_user_identifier(uid, iid, name):
86 c.execute("update identifiers set name = ? where userid = ? and id = ?", (name, uid, iid, ))
92 for row in c.execute("select * from users"):
97 def list_user_identifiers(uid):
99 res = list(c.execute("""
100 select * from identifiers where userid = ? and active
106 def add_coffee(uid, flavor, time=None):
109 c.execute("insert into coffees (id, flavor) values (?,?)", (uid, flavor))
111 c.execute("insert or ignore into coffees (id, flavor, time) values (?,?,?)", (uid, flavor, time))
115 def add_event(uid, event_name, time):
117 c.execute("""insert into events (user_id, event_type, time)
118 values (?, (SELECT id FROM event_types WHERE name = ?), ?)""",
119 (uid, event_name, time))
125 res = list(c.execute("select distinct name, ord from flavors"))
130 def coffee_flavors(uid=None, days=0, start=0):
131 """Returns flavor statistics for team/user during selected
132 period/since beginning.
134 days -- number of days for computation
135 start -- shift size from the current time
137 When 'days' is not given or 0 return statistics since the beginning.
139 e.g. (7, 7) returns statistics for 7 days, 7 days ago.
147 if days is not None and days != 0:
148 query += " where date(time) between date('now', 'localtime', '-" + str(days+start-1) + " days') and date('now', 'localtime', '-" + str(start) + " days')"
151 query += " and ids.userid = ? and ids.active"
152 variables.append(uid)
153 elif uid is not None:
154 query += " where ids.userid = ? and ids.active"
155 variables.append(uid)
157 res = list(c.execute("""
158 select f.name, count(c.flavor) from flavors f
159 left join (select * from coffees co left join identifiers ids on co.id=ids.id
161 on f.name=c.flavor group by f.name
169 def coffee_history(uid=None):
173 res = list(c.execute("""
174 select strftime('%s', ds.d),count(c.flavor),c.flavor from
175 (select num,date('now', 'localtime', -num || ' days') as d from days) ds
177 (select time,flavor from coffees) c
178 on d = date(c.time) group by d, c.flavor
181 res = list(c.execute(
183 select strftime('%s', ds.d),count(c.flavor),c.flavor from
184 (select num,date('now', 'localtime', -num || ' days') as d from days) ds
186 (select date(time, 'localtime') as time,flavor from coffees co left join identifiers ids on co.id = ids.id where ids.userid = ? and ids.active) c
187 on d = date(c.time) group by d, c.flavor
195 def drink_count(uid=None, start=None, stop=None):
196 """Return a list of tuples ('<drink type>', <count>).
198 >>> drink_count(stop=0)
199 [('coffee', 7066), ('Club-Mate', 497), ('tea', 1)]
207 clauses.append("ids.userid = ? and ids.active")
210 if start is not None:
211 clauses.append("date(time, 'localtime') >= date('now', 'localtime', '-%d days')" % int(start))
214 clauses.append("date(time, 'localtime') <= date('now', 'localtime', '-%d days')" % int(stop))
216 return list(c.execute("select fl.type, count(*) from coffees co "
217 "left join flavors fl on co.flavor = fl.name "
218 "left join identifiers ids on co.id = ids.id where "
219 + " and ".join(clauses) + " group by fl.type "
220 "order by fl.ord asc", args))
224 """Return mapping with event names as keys and SQLite time string of
225 the last event as values.
228 res = dict(c.execute("""select name, MAX(time)
229 from events as e left join event_types as et on e.event_type = et.id