Produce PDF with real data.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 28 Jan 2019 21:41:54 +0000 (22:41 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 28 Jan 2019 21:41:54 +0000 (22:41 +0100)
web/seepark_web.py

index acec9c5..131489b 100644 (file)
@@ -4,11 +4,32 @@ import configparser
 import os
 import sys
 from collections import defaultdict
+import io
+import numpy as np
+import matplotlib
+matplotlib.use('pdf')
+import matplotlib.pyplot as plt
+from matplotlib.backends.backend_pdf import PdfPages
+
 from flask import Flask, render_template, jsonify, request, abort, Response, make_response
 import flask.json
 from flask_sqlalchemy import SQLAlchemy, inspect
 from sqlalchemy import func
 
+MONTH_DE = [
+    'Jänner',
+    'Februar',
+    'März',
+    'April',
+    'Mai',
+    'Juni',
+    'Juli',
+    'August',
+    'September',
+    'Oktober',
+    'November',
+    'Dezember']
+
 
 # https://stackoverflow.com/a/37350445
 def sqlalchemy_model_to_dict(model):
@@ -214,6 +235,10 @@ def currentwatertemperature(sensorid):
     return result.value, result.timestamp
 
 
+def add_month(date):
+    return (date + datetime.timedelta(days=42)).replace(day=1)
+
+
 @app.route('/api/<version>/sensors/')
 def sensors(version):
     """List all sensors found in the database"""
@@ -277,19 +302,42 @@ def currentwater(version):
 
 @app.route('/report/<int:year>-<int:month>')
 def report(year, month):
-    import io
-    import numpy as np
-    import matplotlib.pyplot as plt
-    from matplotlib.backends.backend_pdf import PdfPages
+
+    begin = datetime.datetime(year, month, 1)
+    end = add_month(begin)
+    data = list(select_sensordata_grouped(mainsensor, 'Wassertemperatur', begin, end))
+    x = [d.timestamp for d in data]
+    y = [d.value for d in data]
+
+    days_datetime = []
+    d = begin
+    while d < end:
+        days_datetime.append(d)
+        d = d + datetime.timedelta(1)
+    days_str = [d.strftime('%d') for d in days_datetime]
 
     binary_pdf = io.BytesIO()
     with PdfPages(binary_pdf) as pdf:
         a4 = (21./2.54, 29.7/2.54)
         plt.figure(figsize=a4)
-        x = np.arange(100)
-        y = np.sin(x/4)
         plt.plot(x, y)
+        plt.xticks(days_datetime, days_str, rotation='vertical')
+        plt.xlabel('Tag')
+        plt.ylabel('Temparatur in °C')
+        plt.axis(xmin=begin, xmax=end)
+        plt.grid()
+        title = 'Seepark Wassertemperatur {} {}'.format(MONTH_DE[begin.month-1], begin.year)
+        plt.title(title)
         pdf.savefig()
+
+        pdf_info = pdf.infodict()
+        pdf_info['Title'] = title
+        pdf_info['Author'] = 'Chrisu Jähnl'
+        pdf_info['Subject'] = 'Wassertemperatur'
+        pdf_info['Keywords'] = 'Seepark Wassertemperatur'
+        pdf_info['CreationDate'] = datetime.datetime.now()
+        pdf_info['ModDate'] = datetime.datetime.today()
+
     response = make_response(binary_pdf.getvalue())
     response.headers['Content-Type'] = 'application/pdf'
     response.headers['Content-Disposition'] = 'attachment; filename=seepark_{:04d}-{:02d}.pdf'.format(year, month)