add mode=<full|consolidated> request argument to sensor endpoint
authorgregor herrmann <gregor@toastfreeware.priv.at>
Tue, 12 Jun 2018 17:44:58 +0000 (19:44 +0200)
committergregor herrmann <gregor@toastfreeware.priv.at>
Tue, 12 Jun 2018 17:44:58 +0000 (19:44 +0200)
the idea is to calculate the resolution of the returned data, i.e. increase
the interval between measurements for creating charts for larger timespans

TODO: the actual filtering

web/seepark_web.py

index 596d4823410fbec7c435850ae7599f4d5da29dab..1b1285aebe41459e4b2cb789387cb28b8a45c5d9 100644 (file)
@@ -52,20 +52,51 @@ def sensor(version, sensor_id):
     URL parameters:
     begin=<datetime>, optional, format like "2018-05-19T21:07:53"
     end=<datetime>, optional, format like "2018-05-19T21:07:53"
+    mode=<full|consolidated>, optional. return all rows (default) or with lower resolution (for charts)
     """
     engine = open_engine(config)
     with engine.connect() as conn:
         where = ['sensor_id=%s']
         sql_args = [sensor_id]
+        begin = None
+        end = None
         if 'begin' in request.args:
             where.append('timestamp>=%s')
-            sql_args.append(request.args.get('begin', None, parse_datetime))
+            begin = request.args.get('begin', None, parse_datetime)
+            sql_args.append(begin)
         if 'end' in request.args:
             where.append('timestamp<=%s')
-            sql_args.append(request.args.get('end', None, parse_datetime))
+            end = request.args.get('end', None, parse_datetime)
+            sql_args.append(end)
         sql = 'select * from sensors where {} order by id'.format(' and '.join(where))
         cursor = conn.execute(sql, *sql_args)
         result = [dict(row) for row in cursor]
+
+        mode = request.args.get('mode', 'full')
+        if mode == 'consolidated':
+            if begin is None or end is None:
+                pass
+            else:
+                # copied from munin/master/_bin/munin-cgi-graph.in
+                resolutions = dict(
+                    day   =   300,
+                    week  =  1800,
+                    month =  7200,
+                    year  = 86400,
+                )
+                duration = (end - begin).total_seconds()
+                day = 60 * 60 * 24
+                if duration < day:
+                    resolution = resolutions['day']
+                elif duration < 7 * day:
+                    resolution = resolutions['week']
+                elif duration < 31 * day:
+                    resolution = resolutions['month']
+                else:
+                    resolution = resolutions['year']
+                # TODO: filter out samples from 'result'
+                # like loop over results and skip if timestamp(n+1)-timestamp(n)<resolution
+
         return jsonify(result)