]> ToastFreeware Gitweb - chrisu/seepark.git/blobdiff - web/seepark_web.py
Add a sqlalchemy group by/having example
[chrisu/seepark.git] / web / seepark_web.py
index bb597324065e5ae81cd3ed65fbf75e8c297aeacb..9871b040d87bbca510124decb3f2b42e9fd15c8e 100644 (file)
@@ -9,12 +9,6 @@ import flask.json
 from flask_sqlalchemy import SQLAlchemy, inspect
 
 
-app_path = os.path.dirname(os.path.realpath(__file__))
-lib_path = os.path.join(app_path, '..')
-sys.path.append(lib_path)
-from seeparklib.openweathermap import openweathermap_json, OpenWeatherMapError
-
-
 # https://stackoverflow.com/a/37350445
 def sqlalchemy_model_to_dict(model):
     return {c.key: getattr(model, c.key)
@@ -46,7 +40,7 @@ config = configparser.ConfigParser()
 config.read(os.environ['SEEPARKINI'])
 apikey = config.get('openweathermap', 'apikey')
 cityid = config.get('openweathermap', 'cityid')
-mainsensor = config.get('temperature', 'mainsensor')
+mainsensor = config.get('webapp', 'mainsensor')
 
 app = Flask(__name__)
 app.json_encoder = JSONEncoder
@@ -85,17 +79,18 @@ def select_sensordata(sensor_id, sensor_type, begin, end, mode):
         )
         duration = (end - begin).total_seconds()
         day = 60 * 60 * 24
-        if duration < day:
+        if duration <= day:
             resolution = resolutions['day']
-        elif duration < 7 * day:
+        elif duration <= 7 * day:
             resolution = resolutions['week']
-        elif duration < 31 * day:
+        elif duration <= 31 * day:
             resolution = resolutions['month']
         else:
             resolution = resolutions['year']
         # TODO: filter out samples from 'result'
-        # something like 
+        # something like
         # select to_seconds(datetime) DIV (60*60*24) as interval_id, min(datetime), max(datetime), min(temp), avg(temp), max(temp), count(temp) from openweathermap group by interval_id order by interval_id;
+        # seepark_web.db.session.query(func.to_seconds(Sensors.timestamp).op('div')(60*60*24).label('g'), func.min(Sensors.timestamp), func.min(Sensors.value)).group_by('g').all()
     return query.all()