heartbeatserver/heartbeat/export.py

31 lines
871 B
Python
Raw Normal View History

2018-10-23 22:07:28 +02:00
# 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)
2018-10-30 15:55:16 +01:00
c.execute("SELECT * FROM beats WHERE recordedAt > date('now', ?) ORDER BY recordedAt ASC", (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()