diff --git a/influxgraph.py b/influxgraph.py index 91705eb..248130b 100644 --- a/influxgraph.py +++ b/influxgraph.py @@ -3,6 +3,7 @@ import time import logging from colors import assign_colors_to_curves import json +import io class InfluxGraph: """ @@ -162,6 +163,27 @@ class InfluxGraph: self.livemode = self.LIVE 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): """ 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. diff --git a/seaweb.py b/seaweb.py index ae44b17..e24c1e6 100755 --- a/seaweb.py +++ b/seaweb.py @@ -120,6 +120,32 @@ def show_clients(): result += c.remote_info + " " + "; ".join(c.info()) + "
" 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('/updateblock') @app.route('/sendcommand')