from flask import Flask, render_template, jsonify, request, abort, Response, make_response
import flask.json
-from flask_sqlalchemy import SQLAlchemy, inspect
-from sqlalchemy import func
+from flask_sqlalchemy import SQLAlchemy
+from sqlalchemy import func, inspect
MONTH_DE = [
'Jänner',
app.config['SQLALCHEMY_DATABASE_URI'] = get_sqlalchemy_database_uri(config)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
-db.reflect(app=app)
+with app.app_context():
+ db.reflect()
class Sensors(db.Model):
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:
return jsonify({"value": value, "timestamp": timestamp})
-@app.route('/report/<int:year>/<int:month>')
+@app.route('/report/<int(fixed_digits=4):year>/<int(fixed_digits=2):month>')
def report(year, month):
- """Report for given year and month
+ """Report for given year (4 digits) and month (2 digits)
"""
paper_size = (29.7 / 2.54, 21. / 2.54) # A4
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}
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:
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)
row_colors = list(ntimes(report_colors + ['w'], len(report_times)))
table = plt.table(cellText=cells, colLabels=columns, rowLabels=rows, rowColours=row_colors, loc='bottom')
table.scale(xscale=1, yscale=2)
def index():
airvalue, airtime = currentairtemperature(cityid)
watervalue, watertime = currentwatertemperature(mainsensor)
+ this_month = first_of_month(datetime.date.today(), 0)
+ last_month = first_of_month(this_month, -1)
return render_template(
'seepark_web.html',
watertime=watertime,
airvalue=airvalue,
airtime=airtime,
+ this_month=this_month,
+ last_month=last_month
)