diff --git a/fillDatabase/aggregate.py b/fillDatabase/aggregate.py index 28f430b..df145a1 100644 --- a/fillDatabase/aggregate.py +++ b/fillDatabase/aggregate.py @@ -85,7 +85,8 @@ def write_to_table(content): for item in content: query = """ - INSERT INTO rki_data_germany (state_id, state, sex, province_id, province, object_id, notification_date, death_count, case_count, age_group_start, age_group_end, extraction_date) + INSERT INTO rki_data_germany (state_id, state, sex, province_id, province, object_id, notification_date, + death_count, case_count, age_group_start, age_group_end, extraction_date) VALUES ({}, '{}', '{}', {}, '{}', {}, date'{}', {}, {}, {}, {}, now()) """.format( diff --git a/flaskAPIwDocker/hello/app.py b/flaskAPIwDocker/hello/app.py index ac1873f..4c6a707 100644 --- a/flaskAPIwDocker/hello/app.py +++ b/flaskAPIwDocker/hello/app.py @@ -1,51 +1,49 @@ import os -from flask import Flask -from flask import jsonify -from flask import request -from flask import render_template +from flask import Flask, jsonify, request, render_template from datetime import datetime, timedelta import psycopg2 import psycopg2.extras +import logging app = Flask(__name__) + def create_app(): """ Create a Flask application using the app factory pattern. - :param settings_override: Override settings :return: Flask app """ - app = Flask(__name__) + application = Flask(__name__) cases = [ - { - "country": "Germany", - "date": "19.03.2020", - "lat": "51", - "long": "9", - "total_cases": 12000, - "active_cases": 11000, - "died_cases": 50 - }, - { - "country": "Germany", - "date": "18.03.2020", - "lat": "51", - "long": "9", - "total_cases": 11500, - "active_cases": 11000, - "died_cases": 30 - }, - { - "country": "France", - "date": "19.03.2020", - "lat": "46.2276", - "long": "2.2137", - "total_cases": 15000, - "active_cases": 13000, - "died_cases": 60 - } - ] + { + "country": "Germany", + "date": "19.03.2020", + "lat": "51", + "long": "9", + "total_cases": 12000, + "active_cases": 11000, + "died_cases": 50 + }, + { + "country": "Germany", + "date": "18.03.2020", + "lat": "51", + "long": "9", + "total_cases": 11500, + "active_cases": 11000, + "died_cases": 30 + }, + { + "country": "France", + "date": "19.03.2020", + "lat": "46.2276", + "long": "2.2137", + "total_cases": 15000, + "active_cases": 13000, + "died_cases": 60 + } + ] """ mögliche Endpunkte der technischen Schnittstelle (API): @@ -66,8 +64,7 @@ def create_app(): --> im Kontext Ibuprofen eingeschränkt, in die Zukunft: impstoff gefunden, etc. """ - - @app.route('/') + @application.route('/') def index(): """ Render our API documentation and some graphics here? @@ -76,7 +73,7 @@ def index(): # GET /get_total_infected # (parameter = zeitraum, nation, bundesland, city, gender, agegroup ... ) - @app.route("/get_totals") + @application.route("/get_totals") def get_totals(): state = request.args.get('state') province = request.args.get('province') @@ -85,17 +82,19 @@ def get_totals(): try: age_group_start = int(age_group_start) - except: + except Exception as e: + logging.exception(e) pass age_group_end = request.args.get('age_group_end') try: age_group_end = int(age_group_end) - except: + except Exception as e: + logging.exception(e) pass extraction_date = request.args.get('extraction_date') - + extraction_date = handle_extraction_date(extraction_date) date_range = request.args.get('date_range') @@ -111,7 +110,8 @@ def get_totals(): else: date_range_start = date_range[0] date_range_end = date_range[0] - except: + except Exception as e: + logging.exception(e) pass dbname = os.environ['DB_DATABASE'] @@ -122,10 +122,10 @@ def get_totals(): print(dbname, user, password, host) conn = psycopg2.connect("dbname='{}' user='{}' host='{}'".format(dbname, user, host)) - cur = conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) + cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) - selection = {"extraction_date": extraction_date,"state":state, "province":province, "sex": sex, - "age_group_start":age_group_start, "age_group_end": age_group_end} + selection = {"extraction_date": extraction_date, "state": state, "province": province, "sex": sex, + "age_group_start": age_group_start, "age_group_end": age_group_end} columns = [] for key, value in selection.items(): @@ -134,11 +134,10 @@ def get_totals(): columns = ", ".join(columns) print(columns) - selection["date_range_start"] = date_range_start selection["date_range_end"] = date_range_end - ### get the numbers for the end + # get the numbers for the end cur.execute(""" SELECT {} , sum(case_count) AS infected, sum(death_count) AS deceased FROM rki_data_germany WHERE (extraction_date = %(extraction_date)s OR %(extraction_date)s IS NULL ) @@ -148,12 +147,12 @@ def get_totals(): AND (age_group_start >= %(age_group_start)s OR %(age_group_start)s IS NULL) AND (age_group_end <= %(age_group_end)s OR %(age_group_end)s IS NULL) AND ((notification_date <= %(date_range_end)s) OR %(date_range_end)s IS NULL) - GROUP BY {}""".format(columns, columns), - selection) + GROUP BY {}""".format(columns, columns), + selection) rows_end = cur.fetchall() - ### get the numbers for the start date + # get the numbers for the start date cur.execute(""" SELECT {} , sum(case_count) AS infected, sum(death_count) AS deceased FROM rki_data_germany WHERE (extraction_date = %(extraction_date)s OR %(extraction_date)s IS NULL ) @@ -163,8 +162,8 @@ def get_totals(): AND (age_group_start >= %(age_group_start)s OR %(age_group_start)s IS NULL) AND (age_group_end <= %(age_group_end)s OR %(age_group_end)s IS NULL) AND ((notification_date <= %(date_range_start)s) OR %(date_range_start)s IS NULL) - GROUP BY {}""".format(columns, columns), - selection) + GROUP BY {}""".format(columns, columns), + selection) rows_start = cur.fetchall() @@ -177,8 +176,8 @@ def get_totals(): AND (age_group_start >= %(age_group_start)s OR %(age_group_start)s IS NULL) AND (age_group_end <= %(age_group_end)s OR %(age_group_end)s IS NULL) AND ((notification_date BETWEEN %(date_range_start)s AND %(date_range_end)s) OR %(date_range_start)s IS NULL) - GROUP BY {}""".format(columns, columns), - selection) + GROUP BY {}""".format(columns, columns), + selection) rows = cur.fetchall() @@ -188,14 +187,14 @@ def get_totals(): if date_range_start: print(rows_end[0]["infected"]) print(rows_start[0]["infected"]) - rows[0]["diff_infected"] = rows_end[0]["infected"] - rows_start[0]["infected"] - rows[0]["diff_deceased"] = rows_end[0]["deceased"] - rows_start[0]["deceased"] + rows[0]["diff_infected"] = rows_end[0]["infected"] - rows_start[0]["infected"] + rows[0]["diff_deceased"] = rows_end[0]["deceased"] - rows_start[0]["deceased"] rows[0]["from"] = date_range_start rows[0]["to"] = date_range_end return jsonify(rows) # GET /get_data - @app.route("/get_data") + @application.route("/get_data") def get_data(): state = request.args.get('state') province = request.args.get('province') @@ -204,13 +203,15 @@ def get_data(): try: age_group_start = int(age_group_start) - except: + except Exception as e: + logging.exception(e) pass age_group_end = request.args.get('age_group_end') try: age_group_end = int(age_group_end) - except: + except Exception as e: + logging.exception(e) pass extraction_date = request.args.get('extraction_date') @@ -229,23 +230,22 @@ def get_data(): else: date_range_start = date_range[0] date_range_end = date_range[0] - except: + except Exception as e: + logging.exception(e) pass - - conn = psycopg2.connect("dbname='wirvsvirus' user='wirvsvirus' host='marc-book.de' password='[n2^3kKCyxUGgzuV'") - cur = conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) + cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) - selection = {"extraction_date": extraction_date,"state":state, "province":province, "sex": sex, - "age_group_start":age_group_start, "age_group_end": age_group_end} + selection = {"extraction_date": extraction_date, "state": state, "province": province, "sex": sex, + "age_group_start": age_group_start, "age_group_end": age_group_end} columns = [] for key, value in selection.items(): columns.append(key) columns = ", ".join(columns) print(columns) - + selection["date_range_start"] = date_range_start selection["date_range_end"] = date_range_end @@ -259,8 +259,8 @@ def get_data(): AND (age_group_start >= %(age_group_start)s OR %(age_group_start)s IS NULL) AND (age_group_end <= %(age_group_end)s OR %(age_group_end)s IS NULL) AND ((notification_date BETWEEN %(date_range_start)s AND %(date_range_end)s) OR %(date_range_start)s IS NULL) - """.format(columns, columns), - selection) + """.format(columns, columns), + selection) rows = cur.fetchall() @@ -274,7 +274,7 @@ def get_data(): return jsonify(rows) # GET /get_events - @app.route("/get_events") + @application.route("/get_events") def get_total_deceased(): state = request.args.get('state') province = request.args.get('province') @@ -294,21 +294,21 @@ def get_total_deceased(): else: date_range_start = date_range[0] date_range_end = date_range[0] - except: + except Exception as e: + logging.exception(e) pass return None - ############################# def handle_extraction_date(date): if date: return date else: - #extraction_date = (datetime.now() - timedelta(1)).strftime('%Y-%m-%d') + # extraction_date = (datetime.now() - timedelta(1)).strftime('%Y-%m-%d') date = "2020-03-21" print("set extraction date to yesterday: " + str(date)) return date - - return app + + return application diff --git a/flaskAPIwDocker/hello/templates/index.html b/flaskAPIwDocker/hello/templates/index.html index 8ac0978..bc70b18 100644 --- a/flaskAPIwDocker/hello/templates/index.html +++ b/flaskAPIwDocker/hello/templates/index.html @@ -1,4 +1,4 @@ - +app