diff --git a/caimira/apps/calculator/__init__.py b/caimira/apps/calculator/__init__.py index 91f2c5d1..3a61cd79 100644 --- a/caimira/apps/calculator/__init__.py +++ b/caimira/apps/calculator/__init__.py @@ -8,6 +8,8 @@ import base64 import functools import html import json +import pandas as pd +from io import StringIO import os from pathlib import Path import traceback @@ -298,6 +300,44 @@ class ArveData(BaseRequestHandler): self.set_header("Content-Type", 'application/json') 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( debug: bool = False, @@ -317,6 +357,7 @@ def make_app( (calculator_prefix + r'/baseline-model/result', StaticModel), (calculator_prefix + r'/user-guide', ReadmeHandler), (calculator_prefix + r'/api/arve/v1/(.*)/(.*)', ArveData), + (calculator_prefix + r'/cases/(.*)', CasesData), (calculator_prefix + r'/static/(.*)', StaticFileHandler, {'path': calculator_static_dir}), ] diff --git a/caimira/apps/calculator/static/js/form.js b/caimira/apps/calculator/static/js/form.js index 86c23880..a88c5ccc 100644 --- a/caimira/apps/calculator/static/js/form.js +++ b/caimira/apps/calculator/static/js/form.js @@ -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) { sensor_id = DATA_FROM_SENSORS.findIndex(function(sensor) { 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 // as a result of a forward/back browser action. If that is the case, update // the visibility of some of our inputs. @@ -1120,9 +1140,12 @@ $(document).ready(function () { success: function (locations) { // If there isn't precisely one result something is very wrong. geocoded_loc = locations.candidates[0]; + $('input[name="location_name"]').val(selectedSuggestion.text); $('input[name="location_latitude"]').val(geocoded_loc.location.y.toPrecision(7)); $('input[name="location_longitude"]').val(geocoded_loc.location.x.toPrecision(7)); + // Update geographic_cases + geographic_cases(geocoded_loc.attributes['country']); } }); diff --git a/caimira/apps/templates/base/calculator.form.html.j2 b/caimira/apps/templates/base/calculator.form.html.j2 index e6326a12..20160e4a 100644 --- a/caimira/apps/templates/base/calculator.form.html.j2 +++ b/caimira/apps/templates/base/calculator.form.html.j2 @@ -386,8 +386,13 @@