-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatalogger.py
157 lines (146 loc) · 5.13 KB
/
datalogger.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import sqlite3
import datetime
sql_live = """SELECT
strftime('%Y-%m-%d %H:%M GMT', logdate) AS logdate,
avg(value) AS value
FROM log
WHERE logdate >= datetime('now', '-180 minutes')
GROUP BY strftime('%Y-%m-%d %H:%M', logdate)
ORDER BY 1;"""
sql_pastday = """SELECT
strftime('%Y-%m-%d %H:%M GMT', min(logdate)) AS logdate,
avg(value) AS value
FROM log
WHERE logdate >= datetime('now', '-24 hours')
GROUP BY strftime('%Y-%m-%d %H', logdate), strftime('%M', logdate) / 10
ORDER BY 1;"""
sql_pastweek = """SELECT
strftime('%Y-%m-%d %H:%M GMT', logdate) AS logdate,
value
FROM log_byhour
WHERE logdate >= datetime('now', '-7 days')
ORDER BY 1;"""
sql_pastyear = """SELECT
strftime('%Y-%m-%d 00:00:00 GMT', logdate) AS logdate,
avg(value) AS value
FROM log_byhour
GROUP BY strftime('%Y-%m-%d', logdate)
HAVING logdate < date('now')
ORDER BY 1;"""
band_pastyear = """SELECT
min(value) AS min,
max(value) AS max
FROM log_byhour
GROUP BY strftime('%Y-%m-%d', logdate)
HAVING logdate < date('now')
ORDER BY logdate;"""
sql_update = """INSERT INTO log_byhour
(logdate, value)
SELECT
strftime('%Y-%m-%d %H:00:00', logdate) AS logdate,
avg(value) AS value
FROM log
WHERE logdate >= ? AND logdate < ?
GROUP BY strftime('%Y-%m-%d %H', logdate)
ORDER BY 1;"""
def log(value):
logdate = datetime.datetime.utcnow()
try:
con = sqlite3.connect('/var/log/datalog.db')
with con:
cur = con.cursor()
cur.execute('INSERT INTO log (logdate, value) VALUES(?, ?)', (logdate, value))
except Exception, e:
print '## datalogger.log ## {0}'.format(e)
def getlog(period):
import json
data = {}
if period == 'pastyear':
sql = sql_pastyear
band = band_pastyear
elif period == 'pastweek':
sql = sql_pastweek
band = ''
elif period == 'pastday':
sql = sql_pastday
band = ''
else:
sql = sql_live
band = ''
con = sqlite3.connect('/var/log/datalog.db', detect_types=sqlite3.PARSE_DECLTYPES)
with con:
c = con.cursor()
c.execute(sql)
rr = c.fetchall()
data['series'] = rr
if band != '':
c.execute(band)
rr = c.fetchall()
data['band'] = rr
return json.dumps(data)
def update_byhour():
con = sqlite3.connect('/var/log/datalog.db', detect_types=sqlite3.PARSE_DECLTYPES)
with con:
c = con.cursor()
# Get startDate for incremental update
c.execute('SELECT MAX(logdate) FROM log_byhour;')
startDate = c.fetchone()[0]
if startDate == None:
# Rascal24 arrival in UK
startDate = datetime.datetime(2011, 12, 30)
else:
startDate = datetime.datetime.strptime(startDate, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(hours = 1)
# Get endDate
c.execute("SELECT strftime('%Y-%m-%d %H:00:00','now');")
endDate = c.fetchone()[0]
# Update log_byhour
c.execute(sql_update, (startDate, endDate))
rows = c.rowcount
if rows > 0:
c.execute("DELETE FROM log WHERE logdate < datetime('now', '-36 hours');")
# print '## update_byhour ## deleted ' + str(c.rowcount)
return rows
def init(confirm):
if confirm:
logdate = datetime.datetime.utcnow()
value = 21.2
con = sqlite3.connect('/var/log/datalog.db')
with con:
cur = con.cursor()
cur.execute('DROP TABLE IF EXISTS log;')
cur.execute('CREATE TABLE log(logdate TIMESTAMP, value REAL);')
cur.execute('INSERT INTO log (logdate, value) VALUES(?, ?)', (logdate, value))
# Read back row to check, then delete
con = sqlite3.connect('/var/log/datalog.db', detect_types=sqlite3.PARSE_DECLTYPES)
con.row_factory = sqlite3.Row
with con:
c = con.cursor()
c.execute('select * from log;')
r = c.fetchone()
print 'Logdate: ' + r['logdate'].strftime('%a, %d %b %Y %H:%M %Z')
print 'Value: {0}'.format(r['value'])
c.execute('delete from log;')
else:
print 'Set arg to True to initialise (deletes all data)'
def init_byhour(confirm):
if confirm:
logdate = datetime.datetime.utcnow()
value = 21.2
con = sqlite3.connect('/var/log/datalog.db')
with con:
cur = con.cursor()
cur.execute('DROP TABLE IF EXISTS log_byhour;')
cur.execute('CREATE TABLE log_byhour(logdate TIMESTAMP, value REAL);')
cur.execute('INSERT INTO log_byhour (logdate, value) VALUES(?, ?)', (logdate, value))
# Read back row to check, then delete
con = sqlite3.connect('/var/log/datalog.db', detect_types=sqlite3.PARSE_DECLTYPES)
con.row_factory = sqlite3.Row
with con:
c = con.cursor()
c.execute('select * from log_byhour;')
r = c.fetchone()
print 'Logdate: ' + r['logdate'].strftime('%a, %d %b %Y %H:%M %Z')
print 'Value: {0}'.format(r['value'])
c.execute('delete from log_byhour;')
else:
print 'Set arg to True to initialise (deletes all data)'