Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fillDatabase/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
146 changes: 73 additions & 73 deletions flaskAPIwDocker/hello/app.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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?
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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']
Expand All @@ -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():
Expand All @@ -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 )
Expand All @@ -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 )
Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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')
Expand All @@ -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')

Expand All @@ -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

Expand All @@ -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()

Expand All @@ -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')
Expand All @@ -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
2 changes: 1 addition & 1 deletion flaskAPIwDocker/hello/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<html><head>
<html><head>app
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script type="text/javascript">
Expand Down