Commit ec60eb6f authored by Nelso Jost's avatar Nelso Jost

FIX: [in progress] better graph data serving

parent 2cc82468
......@@ -14,15 +14,17 @@ SECRET_KEY = os.environ.get('FLASK_SECRET_KEY') or \
'hard to guess string'
# [flask-sqlalchemy] -- for sqlite version
# SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
# [flask-sqlalchemy] -- for mysql version
# format: mysql://username:password@hostname/database
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://ctaemm:ctaemm@localhost/emmdb'
# SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://ctaemm:ctaemm@localhost/emmdb'
# [flask-sqlalchemy] -- assures a commit to prevent data loss
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
UPLOAD_FOLDER = 'upload'
class DATETIME:
INTERNAL_FORMAT = "%Y%m%d%H%M%S"
PRINT_FORMAT = "%Y-%m-%d %H:%M:%S"
......
......@@ -2,6 +2,8 @@ from .. import db
from .dbutils import DBUtils
from .rawsensordata import RawSensorData
import csv
class Sensor(db.Model, DBUtils):
id = db.Column(db.Integer, primary_key=True)
......@@ -76,3 +78,9 @@ class Sensor(db.Model, DBUtils):
if k in d:
d[k] = v
return d
def export_csv_file(self, filename, sep=','):
with open(filename, 'w') as f:
writer = csv.writer(f, delimiter=sep)
for d in self.rawdata.all():
writer.writerow([d.datetime_obj.strftime('%Y/%m/%d %H:%M:%S'), d.value])
......@@ -32,7 +32,7 @@
<div class="container">
<a href={{ url_for('.view_manage_board', id=board_id) }}>Gerenciar Estação</a> |
<!-- <a href={{ url_for('.view_get_meteorolog', id=board_id) }}>Obter Meteorolog</a> | -->
<a href="https://git.cta.if.ufrgs.br/meteorolog/arduino-meteorolog/repository/archive.zip">Obter Meteorolog</a> |
<a href='javascript:show_data_loss_confirm("{{ url_for('.view_clean_board', id=board_id) }}");'>Apagar todos os dados</a> |
<a href='javascript:show_data_loss_confirm("{{ url_for('.view_delete_board', id=board_id) }}");'>Excluir Estação</a>
</div>
......@@ -43,12 +43,12 @@
<!--
<li class="active"><a href="#sectionB">Log</a></li>
-->
{% for sensor in all_sensors %}
{% if sensor['nickname'] == graph_data['nickname'] %}
<li class="active"><a href="">{{ sensor['nickname'] }}</a></li>
{% for s in all_sensors %}
{% if s['nickname'] == sensor['nickname'] %}
<li class="active"><a href="">{{ s['nickname'] }}</a></li>
{% else %}
<li onClick='javascript:window.location.replace("{{ url_for('.view_board_sensor', id=board_id,
sensor_nickname=sensor['nickname']) }}");'><a href="">{{ sensor['nickname'] }}</a></li>
sensor_nickname=s['nickname']) }}");'><a href="">{{ s['nickname'] }}</a></li>
{% endif %}
{% endfor %}
</ul>
......@@ -56,21 +56,21 @@
http://dygraphs.com/tutorial.html
-->
<div class="tab-content">
<div id="section_graph_{{ graph_data['nickname'] }}" class="tab-pane fade in active">
<h3>{{ graph_data.quantity }} ({{ graph_data.unity }})</h3>
{% if graph_data['data_js'] %}
<div id="{{ graph_data['nickname'] + '_graphdiv' }}" style="height:300px;"></div>
<div id="section_graph_{{ sensor['nickname'] }}" class="tab-pane fade in active">
<h3>{{ sensor['quantity'] }} ({{ sensor['unity'] }})</h3>
<div id="{{ sensor['nickname'] + '_graphdiv' }}" style="height:300px;"></div>
<script type="text/javascript">
g_{{ graph_data['nickname' ]}} = new Dygraph(
document.getElementById("{{ graph_data['nickname'] + '_graphdiv' }}"), // containing div
{{ graph_data['data_js']|safe }}
g_{{ sensor['nickname' ]}} = new Dygraph(
document.getElementById("{{ sensor['nickname'] + '_graphdiv' }}"), // containing div
"{{ sensor['data_filename'] }}"
);
</script>
{% endif %}
</div>
<div>
<h4>Descrição</h4>
{{ current_sensor['description']|safe }}
{{ sensor['description']|safe }}
</div>
</div>
</div>
......
......@@ -29,7 +29,8 @@ from .forms import SelectBoardForm, ManageBoardForm, SensorForm
from . import config
from flask import Blueprint, url_for, render_template, jsonify, \
request, abort, redirect, Response, flash, session
request, abort, redirect, Response, flash, session,\
send_from_directory, after_this_request
import csv
import os
import gzip
......@@ -187,11 +188,17 @@ def view_board_sensor(id, sensor_nickname):
form.board_choices.data = id
update_need_refresh()
sensor = board.get_sensor(sensor_nickname)
print(sensor)
base_filename = 'board-{}-{}.csv'.format(id, sensor_nickname)
sensor.export_csv_file('app/static/' + base_filename)
sensor_json = sensor.to_json
sensor_json['data_filename'] = url_for('static', filename=base_filename)
return render_template('viewsensor.html', config=config, form=form,
board_id=id,
current_sensor=Sensor.get(sensor_nickname).to_json,
all_sensors=[s.to_json for s in board.sensors.all()],
graph_data=get_graph_data(id, sensor_nickname))
board_id=id, sensor=sensor_json,
all_sensors=[s.to_json for s in board.sensors.all()])
@main.route('/board/insert', methods=['GET', 'POST'])
def view_insert_board():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment