From 9aa4f8084d2e88c733d1f537d97257c15990c03f Mon Sep 17 00:00:00 2001 From: Pierrick C Date: Mon, 2 Oct 2017 00:33:03 +0200 Subject: [PATCH] =?UTF-8?q?Petites=20am=C3=A9liorations=20du=20graphe=20+?= =?UTF-8?q?=20pr=C3=A9paration=20d'autre=20am=C3=A9lioration=20Ajout=20t?= =?UTF-8?q?=C3=A9l=C3=A9chargement=20des=20donn=C3=A9es=20en=20JSON?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- raspberry/python/cameteo.py | 43 ++++++++++--------- raspberry/python/templates/data_graph.html.j2 | 6 ++- raspberry/python/web_app.py | 27 +++++++++--- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/raspberry/python/cameteo.py b/raspberry/python/cameteo.py index 3f0f843..6c06c2b 100644 --- a/raspberry/python/cameteo.py +++ b/raspberry/python/cameteo.py @@ -41,9 +41,9 @@ db = SQLAlchemy(app) class SensorModel(db.Model): """SensorModel : classe des modèles de capteur""" - + __tablename__= 'sensor_model' - + name=db.Column(db.String(20), primary_key=True) brand=db.Column(db.String(20), nullable=False) description=db.Column(db.String(1000)) @@ -56,30 +56,33 @@ class SensorModel(db.Model): class Sensor(db.Model): """Sensor : classe des capteurs""" - + __tablename__= 'sensors' - + name = db.Column(db.String(20), primary_key=True) model_id = db.Column(db.String(20), nullable=False) description = db.Column(db.String(1000)) - + def __repr__(self): return "" % (self.name, self.model, self.description) def __str__(self): - return """%s : + return """%s : - Modèle : \t%s - Description: \t%s""" % (self.name, self.model, self.description) class DataType(db.Model): """DataType : classe des types de données""" - + __tablename__ = 'data_type' - + type_id = db.Column(db.String(4), primary_key=True) - name = db.Column(db.String(20), nullable=False) - description = db.Column(db.String(1000)) - + name = db.Column(db.String(20), nullable=False) #Short name + 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): return "" % (self.name, self.type_id, self.description) @@ -88,9 +91,9 @@ class DataType(db.Model): class Data(db.Model): """Data : classe des données""" - + __tablename__ = 'data' - + data_id = db.Column(db.BigInteger, primary_key=True, autoincrement="auto") dbdate = db.Column(db.DateTime, nullable=False) valdate = db.Column(db.DateTime, nullable=True) @@ -98,29 +101,29 @@ class Data(db.Model): unit = db.Column(db.String(20), nullable=True) type_id = db.Column(db.String(4), nullable=True) sensor_id = db.Column(db.String(20), nullable=True) - + def __repr__(self): return "" % (self.value, self.type_id, self.valdate.utc, self.sensor_id, self.data_id) def __str__(self): - try: + try: val = "{:8.2f} {:8s}".format(self.value, self.unit) except: val = " 'NULL' " return "{} : {:4s}= {} (capteur: {:s})".format(self.valdate, self.type_id, val, self.sensor_id) - + class Photo(db.Model): """DataType : classe des types de données""" - + __tablename__ = 'photos' - + 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_date = db.Column(db.DateTime, nullable=False) image_type = db.Column(db.String(4), nullable=False) - + def __repr__(self): return "" % (self.file_name, self.photo_id, self.description) def __str__(self): - return "%s" % (self.file_name) \ No newline at end of file + return "%s" % (self.file_name) diff --git a/raspberry/python/templates/data_graph.html.j2 b/raspberry/python/templates/data_graph.html.j2 index d16b98b..42a3abc 100644 --- a/raspberry/python/templates/data_graph.html.j2 +++ b/raspberry/python/templates/data_graph.html.j2 @@ -1,5 +1,5 @@ {% extends "base.html.j2" %} -{% block title %}Camétéo{% endblock %} +{% block title %}Camétéo - {{ data_type }}{% endblock %} {% block styles %} {{super()}} @@ -13,6 +13,10 @@ {{ plot_div|safe }} + + {{ render_pagination(dat) }}
diff --git a/raspberry/python/web_app.py b/raspberry/python/web_app.py index 2b97c7b..6164d84 100755 --- a/raspberry/python/web_app.py +++ b/raspberry/python/web_app.py @@ -81,9 +81,10 @@ def 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) - 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 data_plot = figure(tools = "hover,pan,reset,save,wheel_zoom", @@ -94,15 +95,17 @@ def by_data_type(dt): plot_height = 400, 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) - return render_template('data_graph.html.j2', dat=q.order_by(Data.dbdate.desc()).paginate(per_page=15), - plot_script=script, - plot_div=div, - js_resources=js_resources, - css_resources=css_resources, + return render_template('data_graph.html.j2', + data_type = dt, + dat=q.order_by(Data.dbdate.desc()).paginate(per_page=15), + plot_script=script, + plot_div=div, + js_resources=js_resources, + css_resources=css_resources, ) @app.route('/sensor_id=') @@ -121,5 +124,15 @@ def config_page(): return render_template('config_page.html.j2') +@app.route('/json_type_id=
') +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__": app.run(host="0.0.0.0")