heartbeatserver/heartbeat/export.py
2018-10-30 15:55:16 +01:00

30 lines
871 B
Python

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