Merge branch 'feature/global_incident_rates' into 'master'

Global incident rates

Closes #279

See merge request cara/caimira!405
This commit is contained in:
Andre Henriques 2022-12-12 10:28:11 +01:00
commit d3a7fa5f2f
4 changed files with 72 additions and 2 deletions

View file

@ -8,6 +8,8 @@ import base64
import functools import functools
import html import html
import json import json
import pandas as pd
from io import StringIO
import os import os
from pathlib import Path from pathlib import Path
import traceback import traceback
@ -298,6 +300,44 @@ class ArveData(BaseRequestHandler):
self.set_header("Content-Type", 'application/json') self.set_header("Content-Type", 'application/json')
return self.finish(response.body) return self.finish(response.body)
class CasesData(BaseRequestHandler):
async def get(self, country):
http_client = AsyncHTTPClient()
# First we need the country to fetch the data
URL = f'https://restcountries.com/v3.1/alpha/{country}?fields=name'
try:
response = await http_client.fetch(HTTPRequest(
url=URL,
method='GET',
),
raise_error=True)
except Exception as e:
print("Something went wrong: %s" % e)
country_name = json.loads(response.body)['name']['common']
# Get global incident rates
URL = 'https://covid19.who.int/WHO-COVID-19-global-data.csv'
try:
response = await http_client.fetch(HTTPRequest(
url=URL,
method='GET',
),
raise_error=True)
except Exception as e:
print("Something went wrong: %s" % e)
df = pd.read_csv(StringIO(response.body.decode('utf-8')), index_col=False)
cases = df.loc[df['Country'] == country_name]
# 7-day rolling average
current_date = str(datetime.datetime.now()).split(' ')[0]
eight_days_ago = str(datetime.datetime.now() - datetime.timedelta(days=7)).split(' ')[0]
cases = cases.set_index(['Date_reported'])
# If any of the 'New_cases' is 0, it means the data is not updated.
if (cases.loc[eight_days_ago:current_date]['New_cases'] == 0).any(): return self.finish('')
return self.finish(str(round(cases.loc[eight_days_ago:current_date]['New_cases'].mean())))
def make_app( def make_app(
debug: bool = False, debug: bool = False,
@ -317,6 +357,7 @@ def make_app(
(calculator_prefix + r'/baseline-model/result', StaticModel), (calculator_prefix + r'/baseline-model/result', StaticModel),
(calculator_prefix + r'/user-guide', ReadmeHandler), (calculator_prefix + r'/user-guide', ReadmeHandler),
(calculator_prefix + r'/api/arve/v1/(.*)/(.*)', ArveData), (calculator_prefix + r'/api/arve/v1/(.*)/(.*)', ArveData),
(calculator_prefix + r'/cases/(.*)', CasesData),
(calculator_prefix + r'/static/(.*)', StaticFileHandler, {'path': calculator_static_dir}), (calculator_prefix + r'/static/(.*)', StaticFileHandler, {'path': calculator_static_dir}),
] ]

View file

@ -418,6 +418,20 @@ function show_sensors_data(url) {
} }
}; };
function geographic_cases(location_country_name) {
$.ajax({
url: `${$('#url_prefix').data().calculator_prefix}/cases/${location_country_name}`,
type: 'GET',
success: function (result) {
$('#geographic_cases').val(result);
result != '' ? $('#source_geographic_cases').show() : $('#source_geographic_cases').hide();
},
error: function(_, _, errorThrown) {
console.log(errorThrown);
}
});
}
$("#sensors").change(function (el) { $("#sensors").change(function (el) {
sensor_id = DATA_FROM_SENSORS.findIndex(function(sensor) { sensor_id = DATA_FROM_SENSORS.findIndex(function(sensor) {
return sensor.RoomId == el.target.value return sensor.RoomId == el.target.value
@ -930,6 +944,12 @@ $(document).ready(function () {
} }
} }
// Update geographic_cases
geographic_cases('CHE');
// Handle WHO source message if geographic_cases pre-defined value is modified by user
$('#geographic_cases').change(() => $('#source_geographic_cases').hide());
// When the document is ready, deal with the fact that we may be here // When the document is ready, deal with the fact that we may be here
// as a result of a forward/back browser action. If that is the case, update // as a result of a forward/back browser action. If that is the case, update
// the visibility of some of our inputs. // the visibility of some of our inputs.
@ -1120,9 +1140,12 @@ $(document).ready(function () {
success: function (locations) { success: function (locations) {
// If there isn't precisely one result something is very wrong. // If there isn't precisely one result something is very wrong.
geocoded_loc = locations.candidates[0]; geocoded_loc = locations.candidates[0];
$('input[name="location_name"]').val(selectedSuggestion.text); $('input[name="location_name"]').val(selectedSuggestion.text);
$('input[name="location_latitude"]').val(geocoded_loc.location.y.toPrecision(7)); $('input[name="location_latitude"]').val(geocoded_loc.location.y.toPrecision(7));
$('input[name="location_longitude"]').val(geocoded_loc.location.x.toPrecision(7)); $('input[name="location_longitude"]').val(geocoded_loc.location.x.toPrecision(7));
// Update geographic_cases
geographic_cases(geocoded_loc.attributes['country']);
} }
}); });

View file

@ -386,8 +386,13 @@
<div class="col-sm-6 pl-0 align-self-center"><input type="number" step="any" id="geographic_population" class="non_zero form-control" name="geographic_population" placeholder="Inhabitants (#)" min="0"></div> <div class="col-sm-6 pl-0 align-self-center"><input type="number" step="any" id="geographic_population" class="non_zero form-control" name="geographic_population" placeholder="Inhabitants (#)" min="0"></div>
</div> </div>
<div class="form-group row"> <div class="form-group row">
<div class="col-sm-4"><label class="col-form-label">New confirmed cases (weekly):</label></div> <div class="col-sm-4"><label class="col-form-label pt-0">New confirmed cases (weekly):</label></div>
<div class="col-sm-6 pl-0 align-self-center"><input type="number" step="any" id="geographic_cases" class="non_zero form-control" name="geographic_cases" placeholder="Cases (#7-day rolling avg)" min="0"></div> <div class="col-sm-6 pl-0 align-self-center">
<input type="number" step="any" id="geographic_cases" class="non_zero form-control" name="geographic_cases" placeholder="Cases (#7-day rolling avg)" min="0">
<small id="source_geographic_cases" class="form-text text-muted" style="display: none">
Source: World Health Organization.
</small>
</div>
</div> </div>
<div class="form-group row"> <div class="form-group row">
<div class="col-sm-4"><label class="col-form-label">Confidence level:</label></div> <div class="col-sm-4"><label class="col-form-label">Confidence level:</label></div>

View file

@ -28,6 +28,7 @@ REQUIREMENTS: dict = {
'memoization', 'memoization',
'mistune', 'mistune',
'numpy', 'numpy',
'pandas',
'psutil', 'psutil',
'python-dateutil', 'python-dateutil',
'retry', 'retry',