heartbeatserver/heartbeat/export.py

44 lines
1.4 KiB
Python

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