-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
80 lines (73 loc) · 2.6 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import sqlite3
import time
import os
class BabyDB:
filename = 'bt.db'
def __init__(self):
create = not os.path.isfile(self.filename)
self.conn = sqlite3.connect(self.filename)
self.c = self.conn.cursor()
if create:
self.create()
def create(self):
self.c.execute("CREATE TABLE IF NOT EXISTS babytracker (timestamp int,action text);")
self.commit()
def commit(self):
return self.conn.commit()
def close(self):
return self.conn.close()
def add(self,action,ts=None):
if ts is None:
ts=time.time()
sql = 'INSERT INTO babytracker (timestamp,action) VALUES (%i,"%s");' % (int(ts),action)
self.c.execute(sql)
self.commit()
return int(ts)
def getlast(self,action):
sql ='Select timestamp FROM babytracker WHERE action="%s" ORDER BY timestamp DESC LIMIT 1;' % action
for row in self.c.execute(sql):
return row[0]
return 0
def preparePlot(self, start=0,step=60):
actions=dict()
sql = "SELECT DISTINCT action FROM babytracker ORDER BY timestamp ASC;"
for row in self.c.execute(sql):
actions[row[0]]=list()
sql = "SELECT timestamp FROM babytracker ORDER BY timestamp ASC LIMIT 1;"
for row in self.c.execute(sql):
start=max(row[0],start)
start = start - start % step
sql = "SELECT timestamp FROM babytracker WHERE timestamp>=%i ORDER BY timestamp ASC;" % start
xaxis=set()
for row in self.c.execute(sql):
xaxis.add(row[0]-1)
xaxis.add(row[0])
xaxis.add(int(time.time()))
xaxis=list(xaxis)
xaxis.sort()
xaxis.pop(0)
xl=len(xaxis)
for action in actions.keys():
sql='SELECT timestamp FROM babytracker WHERE action="%s" AND timestamp>=%i ORDER BY timestamp ASC;' % (action,start)
runner = 0
last=xaxis[0]
for row in self.c.execute(sql):
ts=row[0]
if runner >= xl:
break
cur=xaxis[runner]
while(cur<ts):
actions[action].append(cur-last)
runner+=1
cur=xaxis[runner]
last=ts
actions[action].append(cur-last)
runner+=1
while(len(actions[action]) < xl):
cur=xaxis[runner]
actions[action].append(cur-last)
runner+=1
return xaxis, actions
if __name__ == '__main__':
db = BabyDB()
db.close()