Added /export route

This commit is contained in:
l_samenv
2024-08-26 13:25:41 +02:00
parent c706b78d07
commit f469208c0d
2 changed files with 48 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import time
import logging import logging
from colors import assign_colors_to_curves from colors import assign_colors_to_curves
import json import json
import io
class InfluxGraph: class InfluxGraph:
""" """
@ -162,6 +163,27 @@ class InfluxGraph:
self.livemode = self.LIVE self.livemode = self.LIVE
return dict(type='accept-graph', live=True) return dict(type='accept-graph', live=True)
def w_export(self, variables, time, interval):
"""
Returns the bytes of a dataframe with the curves given by variables in the time range "time"
Called when the route /export is reached.
Parameters :
variables (str) : a comma separataed value string of variable names (influx names) to retrieve
time (str) : a commma separated value string (range) of seconds.
interval (str) : the interval (resolution) of the values to get (string in seconds)
Returns :
io.BytesIO : an BytesIO object containing the dataframe to retrieve
"""
df = self.influx_data_getter.get_curves_data_frame(variables, time, interval)
mem = io.BytesIO()
mem.write(bytes(df.to_csv(sep="\t", index=False), "utf-8"))
mem.seek(0)
return mem
def graphpoll(self): def graphpoll(self):
""" """
Polls the last known values for all the available variables, and returns only those whose polled value is more recent than the nost recent displayed one. Polls the last known values for all the available variables, and returns only those whose polled value is more recent than the nost recent displayed one.

View File

@ -120,6 +120,32 @@ def show_clients():
result += c.remote_info + " " + "; ".join(c.info()) + "<br>" result += c.remote_info + " " + "; ".join(c.info()) + "<br>"
return result return result
@app.route('/export')
def export():
args = flask.request.args
kwargs = dict((k, args.get(k)) for k in args)
path = flask.request.path
logging.info('GET %s %s', path, repr(kwargs))
try:
id = kwargs.pop('id')
client = instrument.clients[id]
bytes = client.w_export(**kwargs)
return flask.send_file(
bytes,
as_attachment=True,
download_name='export.tsv',
mimetype='text/tab-separated-values'
)
except Exception as e:
logging.error('%s', traceback.format_exc())
circularlog.log()
msg = dict(type='error', request=path[1:], error=repr(e))
resp = flask.Response(json.dumps(msg), mimetype='application/json')
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/getblock') @app.route('/getblock')
@app.route('/updateblock') @app.route('/updateblock')
@app.route('/sendcommand') @app.route('/sendcommand')