heartbeatserver/heartbeat/export.py

44 lines
1.4 KiB
Python
Raw Normal View History

2018-10-23 22:07:28 +02:00
# TODO: web frontend
import os
import tornado.web
2018-11-04 07:24:55 +01:00
from .ranges import yamiIchiCodes
2018-10-23 22:07:28 +02:00
class ExportHandler(tornado.web.RequestHandler):
def initialize(self, conn):
"""
conn: sqlite3 connection
"""
self.conn = conn
def get(self, range):
ranges = {
2018-11-04 07:24:55 +01:00
'1h': ('-1 hour',),
'24h': ('-1 day',),
'week': ('-7 days',),
2018-10-23 22:07:28 +02:00
}
2018-11-04 07:24:55 +01:00
q = "SELECT * FROM beats WHERE recordedAt > date('now', ?) ORDER BY recordedAt ASC"
if range in yamiIchiCodes:
range = yamiIchiCodes[range]
# range = 1d, 3d, 1w
ranges = {
2018-11-04 08:09:17 +01:00
"24h": ("2018-11-04 10:00:00","2018-11-05 12:00:00"),
2018-11-04 07:24:55 +01:00
"3d": ("2018-11-04 10:00:00","2018-11-07 12:00:00"),
"1w": ("2018-11-04 10:00:00","2018-11-11 12:00:00")
}
q = "SELECT * FROM beats WHERE recordedAt > ? AND recordedAt < ? ORDER BY recordedAt ASC"
2018-11-04 08:09:17 +01:00
print(range, ranges)
2018-11-04 07:24:55 +01:00
# TODO: userid + start time
2018-10-23 22:07:28 +02:00
if range not in ranges:
# Not found
raise tornado.web.HTTPError(404)
2018-11-04 07:24:55 +01:00
c = self.conn.cursor()
self.set_header("Content-Type", 'text/csv')
c.execute(q, ranges[range])
2018-10-23 22:07:28 +02:00
self.write("id,bpm,timestamp\n")
for row in c:
self.write("{},{},{}\n".format(row['id'], row['bpm'], row['createdAt']))
self.flush()