-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
73 lines (63 loc) · 2.36 KB
/
database.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
import sqlite3
from datetime import datetime
formats = {
"day": "%Y-%m-%dT00:00:00Z",
"hour": "%Y-%m-%dT%H:00:00Z",
"minute": "%Y-%m-%dT%H:%M:00Z",
}
rollup_tables = {
"day": "moisture_day",
"hour": "moisture_hour",
"minute": "moisture_minute",
}
class Database:
def __init__(self):
self.conn = sqlite3.connect("database.db")
def setup(self):
c = self.conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS moisture
(datetime TEXT UNIQUE, moisture INT, state TEXT);
''')
c.execute('''
CREATE TABLE IF NOT EXISTS moisture_day
(datetime TEXT UNIQUE, moisture REAL, samples INT);
''')
c.execute('''
CREATE TABLE IF NOT EXISTS moisture_hour
(datetime TEXT UNIQUE, moisture REAL, samples INT);
''')
c.execute('''
CREATE TABLE IF NOT EXISTS moisture_minute
(datetime TEXT UNIQUE, moisture REAL, samples INT);
''')
self.conn.commit()
def insert_to_rollup_table(self, c, rollup, timestamp, moisture):
c.execute(f'''
INSERT INTO {rollup_tables[rollup]} (datetime, moisture, samples) VALUES (strftime(?, ?), ?, 1)
ON CONFLICT(datetime) DO UPDATE SET
moisture = CAST ((moisture * samples + excluded.moisture) as REAL) / (samples + 1),
samples = samples + 1
''', (formats[rollup], timestamp, moisture))
def write_moisture(self, moisture, state, timestamp=None):
if timestamp == None:
timestamp = datetime.now()
c = self.conn.cursor()
c.execute('INSERT INTO moisture (datetime, moisture, state) VALUES (?, ?, ?)',
(timestamp.isoformat(), moisture, state))
self.insert_to_rollup_table(c, "day", timestamp, moisture)
self.insert_to_rollup_table(c, "hour", timestamp, moisture)
self.insert_to_rollup_table(c, "minute", timestamp, moisture)
self.conn.commit()
def get_moisture(self):
c = self.conn.cursor()
c.execute('SELECT * FROM moisture')
return c.fetchall()
def get_moisture_stats(self, start, end, resolution):
c = self.conn.cursor()
c.execute(f'''
select datetime, moisture from {rollup_tables[resolution]}
where datetime >= ? and datetime < ?
order by datetime asc
''', (start, end))
return c.fetchall()