Petites améliorations du graphe + préparation d'autre amélioration

Ajout téléchargement des données en JSON
This commit is contained in:
Pierrick C 2017-10-02 00:33:03 +02:00
parent b5c32cfc15
commit 9aa4f8084d
3 changed files with 48 additions and 28 deletions

View File

@ -41,9 +41,9 @@ db = SQLAlchemy(app)
class SensorModel(db.Model): class SensorModel(db.Model):
"""SensorModel : classe des modèles de capteur""" """SensorModel : classe des modèles de capteur"""
__tablename__= 'sensor_model' __tablename__= 'sensor_model'
name=db.Column(db.String(20), primary_key=True) name=db.Column(db.String(20), primary_key=True)
brand=db.Column(db.String(20), nullable=False) brand=db.Column(db.String(20), nullable=False)
description=db.Column(db.String(1000)) description=db.Column(db.String(1000))
@ -56,30 +56,33 @@ class SensorModel(db.Model):
class Sensor(db.Model): class Sensor(db.Model):
"""Sensor : classe des capteurs""" """Sensor : classe des capteurs"""
__tablename__= 'sensors' __tablename__= 'sensors'
name = db.Column(db.String(20), primary_key=True) name = db.Column(db.String(20), primary_key=True)
model_id = db.Column(db.String(20), nullable=False) model_id = db.Column(db.String(20), nullable=False)
description = db.Column(db.String(1000)) description = db.Column(db.String(1000))
def __repr__(self): def __repr__(self):
return "<Sensor(name='%s', model='%s', description='%s')>" % (self.name, self.model, self.description) return "<Sensor(name='%s', model='%s', description='%s')>" % (self.name, self.model, self.description)
def __str__(self): def __str__(self):
return """%s : return """%s :
- Modèle : \t%s - Modèle : \t%s
- Description: \t%s""" % (self.name, self.model, self.description) - Description: \t%s""" % (self.name, self.model, self.description)
class DataType(db.Model): class DataType(db.Model):
"""DataType : classe des types de données""" """DataType : classe des types de données"""
__tablename__ = 'data_type' __tablename__ = 'data_type'
type_id = db.Column(db.String(4), primary_key=True) type_id = db.Column(db.String(4), primary_key=True)
name = db.Column(db.String(20), nullable=False) name = db.Column(db.String(20), nullable=False) #Short name
description = db.Column(db.String(1000)) plot_mini = db.Column(db.Float, nullable=True) #y minimum for plotting
plot_maxi = db.Column(db.Float, nullable=True) #y maximum for plotting
unit_si = db.Column(db.String(10), nullable=True) #Unit in SI
description = db.Column(db.String(1000)) #Long description
def __repr__(self): def __repr__(self):
return "<DataType(name='%s', ID='%s', description='%s')>" % (self.name, self.type_id, self.description) return "<DataType(name='%s', ID='%s', description='%s')>" % (self.name, self.type_id, self.description)
@ -88,9 +91,9 @@ class DataType(db.Model):
class Data(db.Model): class Data(db.Model):
"""Data : classe des données""" """Data : classe des données"""
__tablename__ = 'data' __tablename__ = 'data'
data_id = db.Column(db.BigInteger, primary_key=True, autoincrement="auto") data_id = db.Column(db.BigInteger, primary_key=True, autoincrement="auto")
dbdate = db.Column(db.DateTime, nullable=False) dbdate = db.Column(db.DateTime, nullable=False)
valdate = db.Column(db.DateTime, nullable=True) valdate = db.Column(db.DateTime, nullable=True)
@ -98,29 +101,29 @@ class Data(db.Model):
unit = db.Column(db.String(20), nullable=True) unit = db.Column(db.String(20), nullable=True)
type_id = db.Column(db.String(4), nullable=True) type_id = db.Column(db.String(4), nullable=True)
sensor_id = db.Column(db.String(20), nullable=True) sensor_id = db.Column(db.String(20), nullable=True)
def __repr__(self): def __repr__(self):
return "<Data(value='%0.2f', type='%s', date='%s', sensor ID='%s', id='%s')>" % (self.value, self.type_id, self.valdate.utc, self.sensor_id, self.data_id) return "<Data(value='%0.2f', type='%s', date='%s', sensor ID='%s', id='%s')>" % (self.value, self.type_id, self.valdate.utc, self.sensor_id, self.data_id)
def __str__(self): def __str__(self):
try: try:
val = "{:8.2f} {:8s}".format(self.value, self.unit) val = "{:8.2f} {:8s}".format(self.value, self.unit)
except: except:
val = " 'NULL' " val = " 'NULL' "
return "{} : {:4s}= {} (capteur: {:s})".format(self.valdate, self.type_id, val, self.sensor_id) return "{} : {:4s}= {} (capteur: {:s})".format(self.valdate, self.type_id, val, self.sensor_id)
class Photo(db.Model): class Photo(db.Model):
"""DataType : classe des types de données""" """DataType : classe des types de données"""
__tablename__ = 'photos' __tablename__ = 'photos'
photo_id = db.Column(db.BigInteger, primary_key=True, autoincrement="auto") photo_id = db.Column(db.BigInteger, primary_key=True, autoincrement="auto")
file_name = db.Column(db.String(32), unique = True, index = True, nullable=False) file_name = db.Column(db.String(32), unique = True, index = True, nullable=False)
file_date = db.Column(db.DateTime, nullable=False) file_date = db.Column(db.DateTime, nullable=False)
image_type = db.Column(db.String(4), nullable=False) image_type = db.Column(db.String(4), nullable=False)
def __repr__(self): def __repr__(self):
return "<Photo(name='%s', date='%s', ID='%s')>" % (self.file_name, self.photo_id, self.description) return "<Photo(name='%s', date='%s', ID='%s')>" % (self.file_name, self.photo_id, self.description)
def __str__(self): def __str__(self):
return "%s" % (self.file_name) return "%s" % (self.file_name)

View File

@ -1,5 +1,5 @@
{% extends "base.html.j2" %} {% extends "base.html.j2" %}
{% block title %}Camétéo{% endblock %} {% block title %}Camétéo - {{ data_type }}{% endblock %}
{% block styles %} {% block styles %}
{{super()}} {{super()}}
@ -13,6 +13,10 @@
{{ plot_div|safe }} {{ plot_div|safe }}
<div id="json">
<a href="/json_type_id={{ data_type }}">Télécharger les données (JSON)</a>
</div>
{{ render_pagination(dat) }} {{ render_pagination(dat) }}
<div class="table-responsive"> <div class="table-responsive">

View File

@ -81,9 +81,10 @@ def by_data_type(dt):
date_deb = datetime.now()-timedelta(hours=1) date_deb = datetime.now()-timedelta(hours=1)
#Récupération des données à afficher #Récupération des données à afficher
q = Data.query.filter(Data.type_id == dt).filter(Data.dbdate >= date_deb).order_by(Data.valdate) q = Data.query.filter(Data.type_id == dt).filter(Data.dbdate >= date_deb).order_by(Data.valdate)
#q = Data.query.filter(Data.type_id == dt).order_by(Data.valdate)
res = pd.read_sql(q.selectable, db.engine) res = pd.read_sql(q.selectable, db.engine)
plot_data = ColumnDataSource(data=dict(x = res['data_valdate'], y = res['data_value'])) plot_data = ColumnDataSource(data=dict(dates = res['data_valdate'], values = res['data_value']))
#Préparation du graphique #Préparation du graphique
data_plot = figure(tools = "hover,pan,reset,save,wheel_zoom", data_plot = figure(tools = "hover,pan,reset,save,wheel_zoom",
@ -94,15 +95,17 @@ def by_data_type(dt):
plot_height = 400, plot_height = 400,
plot_width = 1140, plot_width = 1140,
) )
data_plot.line('x', 'y', source=plot_data, line_width=2) data_plot.line('dates', 'values', source=plot_data, line_width=2)
script, div = components(data_plot) script, div = components(data_plot)
return render_template('data_graph.html.j2', dat=q.order_by(Data.dbdate.desc()).paginate(per_page=15), return render_template('data_graph.html.j2',
plot_script=script, data_type = dt,
plot_div=div, dat=q.order_by(Data.dbdate.desc()).paginate(per_page=15),
js_resources=js_resources, plot_script=script,
css_resources=css_resources, plot_div=div,
js_resources=js_resources,
css_resources=css_resources,
) )
@app.route('/sensor_id=<sens>') @app.route('/sensor_id=<sens>')
@ -121,5 +124,15 @@ def config_page():
return render_template('config_page.html.j2') return render_template('config_page.html.j2')
@app.route('/json_type_id=<dt>')
def json_by_data_type(dt):
date_deb = datetime.now()-timedelta(hours=1)
#Récupération des données à afficher
q = Data.query.filter(Data.type_id == dt).filter(Data.dbdate >= date_deb).order_by(Data.valdate)
#q = Data.query.filter(Data.type_id == dt).order_by(Data.valdate)
res = pd.read_sql(q.selectable, db.engine)
return res.to_json()
if __name__=="__main__": if __name__=="__main__":
app.run(host="0.0.0.0") app.run(host="0.0.0.0")