]> ToastFreeware Gitweb - chrisu/seepark.git/blobdiff - web/seepark_web.py
format x tick values (datetime) differently according to interval
[chrisu/seepark.git] / web / seepark_web.py
index 1b1285aebe41459e4b2cb789387cb28b8a45c5d9..9ad59f124d3a78f18b81f49bf43f17885cd958ef 100644 (file)
@@ -35,29 +35,11 @@ def open_engine(config):
     return engine
 
 
     return engine
 
 
-@app.route('/api/<version>/sensors/')
-def sensors(version):
-    """List all sensors found in the database"""
-    engine = open_engine(config)
-    with engine.connect() as conn:
-        cursor = conn.execute('select distinct sensor_id, sensor_name, value_type from sensors')
-        result = [dict(row) for row in cursor]
-        return jsonify(result)
-
-
-@app.route('/api/<version>/sensor/<sensor_id>')
-def sensor(version, sensor_id):
-    """Return all sensor data
-
-    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)
-    """
+def select_sensordata(initial_where, initial_sql_args):
     engine = open_engine(config)
     with engine.connect() as conn:
     engine = open_engine(config)
     with engine.connect() as conn:
-        where = ['sensor_id=%s']
-        sql_args = [sensor_id]
+        where = [initial_where]
+        sql_args = [initial_sql_args]
         begin = None
         end = None
         if 'begin' in request.args:
         begin = None
         end = None
         if 'begin' in request.args:
@@ -72,34 +54,84 @@ def sensor(version, sensor_id):
         cursor = conn.execute(sql, *sql_args)
         result = [dict(row) for row in cursor]
 
         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
+    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:
             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
+                resolution = resolutions['year']
+            # TODO: filter out samples from 'result'
+            # like loop over results and skip if timestamp(n+1)-timestamp(n)<resolution
+
+    format = request.args.get('format', 'default')
+    if format == 'c3':
+        c3result = dict()
+        for row in result:
+            if not row['sensor_id'] in c3result:
+                c3result[row['sensor_id']] = list()
+            c3result[row['sensor_id']].append(row['value'])
+            if not row['sensor_id'] + '_x' in c3result:
+                c3result[row['sensor_id'] + '_x'] = list()
+            dt = row['timestamp'].strftime('%Y-%m-%d %H:%M:%S')
+            c3result[row['sensor_id'] + '_x'].append(dt)
+        result = c3result
+    return result
+
 
 
+@app.route('/api/<version>/sensors/')
+def sensors(version):
+    """List all sensors found in the database"""
+    engine = open_engine(config)
+    with engine.connect() as conn:
+        cursor = conn.execute('select distinct sensor_id, sensor_name, value_type from sensors')
+        result = [dict(row) for row in cursor]
         return jsonify(result)
 
 
         return jsonify(result)
 
 
+@app.route('/api/<version>/sensor/id/<sensor_id>')
+def sensorid(version, sensor_id):
+    """Return all data for a specific sensor
+
+    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)
+    format=<default|c3>, optional. return result as returned by sqlalchemy (default) or formatted for c3.js
+    """
+    result = select_sensordata('sensor_id=%s', sensor_id)
+    return jsonify(result)
+
+
+@app.route('/api/<version>/sensor/type/<sensor_type>')
+def sensortype(version, sensor_type):
+    """Return all data for a specific sensor type
+
+    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)
+    format=<default|c3>, optional. return result as returned by sqlalchemy (default) or formatted for c3.js
+    """
+    result = select_sensordata('value_type=%s', sensor_type)
+    return jsonify(result)
+
+
 @app.route('/data/', defaults={'timespan': 1})
 @app.route("/data/<int:timespan>", methods=['GET'])
 def data(timespan):
 @app.route('/data/', defaults={'timespan': 1})
 @app.route("/data/<int:timespan>", methods=['GET'])
 def data(timespan):