]> ToastFreeware Gitweb - chrisu/seepark.git/blobdiff - web/seepark_web.py
Now the generated PDF contains only 1 page and in A4 landscape mode.
[chrisu/seepark.git] / web / seepark_web.py
index 131489bd1dbc5a4dfd4b9372de8ccc41307bf783..b899a1730ebdfaea506fd652c7993f0d3b2d2779 100644 (file)
@@ -30,6 +30,15 @@ MONTH_DE = [
     'November',
     'Dezember']
 
+DAY_OF_WEEK_DE = [
+    'Montag',
+    'Dienstag',
+    'Mittwoch',
+    'Donnerstag',
+    'Freitag',
+    'Samstag',
+    'Sonntag']
+
 
 # https://stackoverflow.com/a/37350445
 def sqlalchemy_model_to_dict(model):
@@ -305,29 +314,56 @@ def report(year, month):
 
     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]
+    data = list(select_sensordata(mainsensor, 'Wassertemperatur', begin, end))
+    x = np.array([d.timestamp for d in data])
+    y = np.array([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)
+        a4 = (29.7/2.54, 21./2.54)
+        title = 'Seepark Wassertemperatur {} {}'.format(MONTH_DE[begin.month-1], begin.year)
+        report_times = [datetime.time(10), datetime.time(15)]
+
+        # graphic
         plt.figure(figsize=a4)
         plt.plot(x, y)
-        plt.xticks(days_datetime, days_str, rotation='vertical')
-        plt.xlabel('Tag')
-        plt.ylabel('Temparatur in °C')
+        plt.xticks(days_datetime, [''] * len(days_datetime))
+        plt.ylabel('Temperatur in °C')
         plt.axis(xmin=begin, xmax=end)
         plt.grid()
-        title = 'Seepark Wassertemperatur {} {}'.format(MONTH_DE[begin.month-1], begin.year)
         plt.title(title)
+
+        # table
+        columns = []
+        for d in days_datetime:
+            columns.append('{}.'.format(d.day))
+        rows = []
+        for t in report_times:
+            rows.append('Wasser {:02d}:{:02d} °C'.format(t.hour, t.minute))
+        cells = []
+        for t in report_times:
+            columns.append('{}.'.format(d.day))
+            row_cells = []
+            for d in days_datetime:
+                report_datetime = datetime.datetime.combine(d.date(), t)
+                closest_index = np.argmin(np.abs(x - report_datetime))
+                if abs(x[closest_index] - report_datetime) > datetime.timedelta(hours=1):
+                    cell = 'N/A'
+                else:
+                    value = y[closest_index]
+                    cell = '{:.1f}'.format(value)
+                row_cells.append(cell)
+            cells.append(row_cells)
+        table = plt.table(cellText=cells, colLabels=columns, rowLabels=rows, loc='bottom')
+        table.scale(xscale=1, yscale=2)
+        plt.title(title)
+        plt.subplots_adjust(left=0.15, right=0.97, bottom=0.3)  # do not cut row labels
         pdf.savefig()
 
         pdf_info = pdf.infodict()