- engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db), echo=False)
- return engine
-
-
-def select_sensordata(initial_where, initial_sql_args):
- engine = open_engine(config)
- with engine.connect() as conn:
- where = [initial_where]
- sql_args = [initial_sql_args]
- begin = None
- end = None
- if 'begin' in request.args:
- where.append('timestamp>=%s')
- begin = request.args.get('begin', None, parse_datetime)
- sql_args.append(begin)
- if 'end' in request.args:
- where.append('timestamp<=%s')
- 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
+ return 'mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db)
+
+
+config = configparser.ConfigParser()
+config.read(os.environ['SEEPARKINI'])
+apikey = config.get('openweathermap', 'apikey')
+cityid = config.get('openweathermap', 'cityid')
+mainsensor = config.get('temperature', 'mainsensor')
+
+app = Flask(__name__)
+app.json_encoder = JSONEncoder
+app.config['SQLALCHEMY_DATABASE_URI'] = get_sqlalchemy_database_uri(config)
+app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
+db = SQLAlchemy(app)
+db.reflect(app=app)
+
+
+class Sensors(db.Model):
+ __tablename__ = 'sensors'
+
+ # https://stackoverflow.com/a/37350445
+ def to_dict(self):
+ return {c.key: getattr(self, c.key)
+ for c in inspect(self).mapper.column_attrs}
+
+def select_sensordata(initial_where):
+ query = Sensors.query.filter(initial_where)
+ begin = request.args.get('begin', None, parse_datetime)
+ end = request.args.get('end', None, parse_datetime)
+ if begin is not None:
+ query = query.filter(Sensors.timestamp >= begin)
+ if end is not None:
+ query = query.filter(Sensors.timestamp <= end)
+ result = query.all()
+
+ 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']