Add swimmer row with dummy data.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 5 Feb 2019 21:16:23 +0000 (22:16 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 5 Feb 2019 21:43:15 +0000 (22:43 +0100)
web/seepark_web.py

index 0ba8f57..c510655 100644 (file)
@@ -1,3 +1,4 @@
+import collections
 import datetime
 import time
 import configparser
@@ -185,6 +186,32 @@ def select_openweatherdata_grouped(cityid, begin, end):
     return query.all()
 
 
+def estimate_swimmer_count(date):
+    return date.day
+
+
+def select_swimmerdata(begin, end):
+    def report_times(begin, end):
+        d = begin
+        while d < end:
+            for t in [10, 15]:
+                a = datetime.datetime.combine(d.date(), datetime.time(t))
+                if a >= d:
+                    yield a
+            d += datetime.timedelta(days=1)
+    SwimmerData = collections.namedtuple('SwimmerData', ['datetime', 'count'])
+    for d in report_times(begin, end):
+        count = estimate_swimmer_count(d)
+        yield SwimmerData(d, count)
+
+
+def swimmerdata_to_xy(swimmerdata):
+    swimmerdata = list(swimmerdata)
+    x = np.array([d.datetime for d in swimmerdata])
+    y = np.array([d.count for d in swimmerdata])
+    return x, y
+
+
 def convert_to_c3(result, id, field_x, field_y):
     c3result = defaultdict(list)
     for row in result:
@@ -332,6 +359,7 @@ def report(year, month):
 
     water_data = sensordata_to_xy(select_sensordata(mainsensor, 'Wassertemperatur', begin, end))
     air_data = openweatherdata_to_xy(select_openweatherdata(cityid, begin, end))
+    swimmer_data = swimmerdata_to_xy(select_swimmerdata(begin, end))
 
     report_times = [datetime.time(10), datetime.time(15)]
     report_data = {'Wasser': water_data, 'Luft': air_data}
@@ -366,6 +394,8 @@ def report(year, month):
         for label in sorted(report_data.keys(), reverse=True):
             for t in report_times:
                 rows.append('{:02d}:{:02d} {} °C'.format(t.hour, t.minute, label))
+        for t in report_times:
+            rows.append('{:02d}:{:02d} Badende'.format(t.hour, t.minute))
         cells = []
         for label, data in sorted(report_data.items(), reverse=True):
             for t in report_times:
@@ -381,6 +411,18 @@ def report(year, month):
                         cell = '{:.1f}'.format(value)
                     row_cells.append(cell)
                 cells.append(row_cells)
+        for t in report_times:
+            row_cells = []
+            x, y = swimmer_data
+            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:
+                    cell = y[closest_index]
+                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)