From 6ada5c266c422d8c8488dac6b9e8910649c952d9 Mon Sep 17 00:00:00 2001 From: thomaswoehlke <thomas.woehlke@gmail.com> Date: Sun, 14 Feb 2021 10:48:58 +0100 Subject: [PATCH] * Fixed #123 split RkiBundeslaenderService into two Services, one for bundeslaender and one for landkreise * Fixed #128 add fields from csv to RkiLandkreiseImport * Fixed #139 refactor RkiBundeslaenderServiceDownload to new method scheme introduced 07.02.2021 * Fixed #140 move WhoImport to RKI in: rk_service_import.py * Fixed #125 implement RkiLandkreise * Fixed #126 implement RkiBundeslaenderImport --- .../application/application_model.py | 26 ++--- src/covid19/blueprints/ecdc/ecdc_model.py | 61 ++++++++---- .../blueprints/ecdc/ecdc_service_update.py | 98 +++++++++++++------ 3 files changed, 127 insertions(+), 58 deletions(-) diff --git a/src/covid19/blueprints/application/application_model.py b/src/covid19/blueprints/application/application_model.py index e27101d0..811912e4 100644 --- a/src/covid19/blueprints/application/application_model.py +++ b/src/covid19/blueprints/application/application_model.py @@ -106,7 +106,7 @@ class ApplicationDateReported(db.Model): return None @classmethod - def get_all_as_page(cls, page): + def get_all_as_page(cls, page: int): return db.session.query(cls)\ .order_by(cls.date_reported.desc())\ .paginate(page, per_page=ITEMS_PER_PAGE) @@ -123,37 +123,37 @@ class ApplicationDateReported(db.Model): return dates_reported @classmethod - def get_by_id(cls, other_id): + def get_by_id(cls, other_id: int): return db.session.query(cls)\ .filter(cls.id == other_id)\ .one() @classmethod - def find_by_id(cls, other_id): + def find_by_id(cls, other_id: int): return db.session.query(cls)\ .filter(cls.id == other_id)\ .one_or_none() @classmethod - def get_by_date_reported(cls, i_date_reported): + def get_by_date_reported(cls, p_date_reported: str): return db.session.query(cls)\ - .filter(cls.date_reported == i_date_reported)\ + .filter(cls.date_reported == p_date_reported)\ .one() @classmethod - def find_by_date_reported(cls, i_date_reported): + def find_by_date_reported(cls, p_date_reported: str): return db.session.query(cls)\ - .filter(cls.date_reported == i_date_reported)\ + .filter(cls.date_reported == p_date_reported)\ .one_or_none() @classmethod - def get_by_year_week(cls, year_week): + def get_by_year_week(cls, year_week: str): return db.session.query(cls)\ .filter(cls.year_week == year_week)\ .one() @classmethod - def find_by_year_week(cls, year_week): + def find_by_year_week(cls, year_week: str): return db.session.query(cls)\ .filter(cls.year_week == year_week)\ .one_or_none() @@ -208,15 +208,15 @@ class ApplicationRegion(db.Model): .one_or_none() @classmethod - def get_by_region(cls, i_who_region): + def get_by_region(cls, i_region): return db.session.query(cls)\ - .filter(cls.region == i_who_region)\ + .filter(cls.region == i_region)\ .one() @classmethod - def find_by_region(cls, i_who_region): + def find_by_region(cls, i_region): return db.session.query(cls)\ - .filter(cls.region == i_who_region)\ + .filter(cls.region == i_region)\ .one_or_none() diff --git a/src/covid19/blueprints/ecdc/ecdc_model.py b/src/covid19/blueprints/ecdc/ecdc_model.py index 35c42654..e15b118b 100644 --- a/src/covid19/blueprints/ecdc/ecdc_model.py +++ b/src/covid19/blueprints/ecdc/ecdc_model.py @@ -23,7 +23,7 @@ class EcdcDateReported(ApplicationDateReported): day_of_week = db.Column(db.Integer, nullable=False) week_of_year = db.Column(db.Integer, nullable=False) - def get_date_rep_as_str(self): + def get_date_import_format_from_date_reported(self): my_date_parts = self.date_reported.split("-") my_year = my_date_parts[0] my_month = my_date_parts[1] @@ -31,8 +31,16 @@ class EcdcDateReported(ApplicationDateReported): return my_day + '/' + my_month + '/' + my_year @classmethod - def get_my_date_rep_as_str(cls, date_reported): - my_date_parts = date_reported.split("/") + def get_date_import_format_from_date_reported_str(cls, date_reported_str: str): + my_date_parts = date_reported_str.split("-") + my_year = my_date_parts[0] + my_month = my_date_parts[1] + my_day = my_date_parts[2] + return my_day + '/' + my_month + '/' + my_year + + @classmethod + def get_date_format_from_ecdc_import_fomat(cls, date_reported_ecdc_import_fomat: str): + my_date_parts = date_reported_ecdc_import_fomat.split("/") my_year = my_date_parts[2] my_month = my_date_parts[1] my_day = my_date_parts[0] @@ -92,14 +100,15 @@ class EcdcCountry(db.Model): continent_id = db.Column(db.Integer, db.ForeignKey('ecdc_continent.id'), nullable=False) continent = db.relationship( 'EcdcContinent', - lazy='subquery', - order_by='EcdcContinent.region', - cascade="all, delete" + lazy='subquery', cascade="all, delete", + order_by='asc(EcdcContinent.region)' ) def __str__(self): - result = " " + self.geo_id + " " + self.country_territory_code + " " + self.countries_and_territories + " " - return result + return " " + self.geo_id \ + + " " + self.country_territory_code \ + + " " + self.countries_and_territories \ + + " " @classmethod def remove_all(cls): @@ -110,19 +119,27 @@ class EcdcCountry(db.Model): @classmethod def get_all_as_page(cls, page): - return db.session.query(cls).paginate(page, per_page=ITEMS_PER_PAGE) + return db.session.query(cls)\ + .order_by(cls.countries_and_territories.asc())\ + .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod def get_all(cls): - return db.session.query(cls).all() + return db.session.query(cls)\ + .order_by(cls.countries_and_territories.asc())\ + .all() @classmethod def get_by_id(cls, other_id: int): - return db.session.query(cls).filter(cls.id == other_id).one() + return db.session.query(cls)\ + .filter(cls.id == other_id)\ + .one() @classmethod def find_by_id(cls, other_id: int): - return db.session.query(cls).filter(cls.id == other_id).one_or_none() + return db.session.query(cls)\ + .filter(cls.id == other_id)\ + .one_or_none() @classmethod def get_by(cls, countries_and_territories: str, geo_id: str, country_territory_code: str): @@ -141,13 +158,19 @@ class EcdcCountry(db.Model): )).one_or_none() @classmethod - def find_by_continent(cls, continent, page): + def find_by_continent(cls, continent: EcdcContinent, page: int): return db.session.query(cls)\ .filter(cls.continent_id == continent.id)\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod def get_germany(cls): + return db.session.query(cls) \ + .filter(cls.country_territory_code == 'DEU') \ + .one() + + @classmethod + def find_germany(cls): return db.session.query(cls) \ .filter(cls.country_territory_code == 'DEU') \ .one_or_none() @@ -162,14 +185,18 @@ class EcdcData(db.Model): notification_rate_per_100000_population_14days = db.Column(db.Float, nullable=False) ecdc_country_id = db.Column(db.Integer, db.ForeignKey('ecdc_country.id'), nullable=False) - ecdc_country = db.relationship('EcdcCountry', lazy='joined', cascade="all, delete") + ecdc_country = db.relationship( + 'EcdcCountry', + lazy='joined', cascade="all, delete", + order_by='asc(EcdcCountry.countries_and_territories)' + ) ecdc_date_reported_id = db.Column(db.Integer, db.ForeignKey('ecdc_date_reported.id'), nullable=False) ecdc_date_reported = db.relationship( 'EcdcDateReported', - lazy='joined', - cascade='all, delete', - order_by='desc(EcdcDateReported.date_reported)') + lazy='joined', cascade='all, delete', + order_by='desc(EcdcDateReported.date_reported)' + ) @classmethod def remove_all(cls): diff --git a/src/covid19/blueprints/ecdc/ecdc_service_update.py b/src/covid19/blueprints/ecdc/ecdc_service_update.py index dfd1f683..ff513af4 100644 --- a/src/covid19/blueprints/ecdc/ecdc_service_update.py +++ b/src/covid19/blueprints/ecdc/ecdc_service_update.py @@ -80,44 +80,86 @@ class EcdcServiceUpdate: app.logger.info("------------------------------------------------------------") return self + def __get_continent_from_import(self, ecdc_import: EcdcImport): + my_a = ecdc_import.continent_exp + ecdc_continent = EcdcContinent.find_by_region(my_a) + if ecdc_continent in None: + ecdc_continent = EcdcContinent(region=my_a) + db.session.add(ecdc_continent) + db.session.commit() + ecdc_continent = EcdcContinent.find_by_region(my_a) + return ecdc_continent + + def __get_country_from_import(self, ecdc_import : EcdcImport): + my_countries_and_territories = ecdc_import.countries_and_territories + my_geo_id = ecdc_import.geo_id + my_country_territory_code = ecdc_import.country_territory_code + my_pop_data_2019 = ecdc_import.pop_data_2019 + ecdc_country = EcdcCountry.find_by( + countries_and_territories=my_countries_and_territories, + geo_id=my_geo_id, + country_territory_code=my_country_territory_code + ) + if ecdc_country in None: + my_continent = self.__get_continent_from_import(ecdc_import) + ecdc_country = EcdcCountry( + countries_and_territories=my_countries_and_territories, + pop_data_2019=my_pop_data_2019, + geo_id=my_geo_id, + country_territory_code=my_country_territory_code, + continent=my_continent + ) + db.session.add(ecdc_country) + db.session.commit() + ecdc_country = EcdcCountry.get_by( + countries_and_territories=my_countries_and_territories, + geo_id=my_geo_id, + country_territory_code=my_country_territory_code + ) + return ecdc_country + + def __get_country_from_import(self, ecdc_import: EcdcImport): + ecdc_date_reported = None + return ecdc_date_reported + + def __get_date_reported_from_import(self): + dict_date_reported_from_import = {} + result_date_str_from_ecdc_import = EcdcImport.get_date_rep() + for item_date_str_from_ecdc_import in result_date_str_from_ecdc_import: + my_date_reported_str = item_date_str_from_ecdc_import[0] + my_date_reported_search = EcdcDateReported.get_date_format_from_ecdc_import_fomat(my_date_reported_str) + my_ecdc_date_reported = EcdcDateReported.find_by_date_reported(p_date_reported=my_date_reported_search) + if my_ecdc_date_reported is None: + my_ecdc_date_reported = EcdcDateReported.create_new_object_factory(my_date_reported_search) + db.session.add(my_ecdc_date_reported) + db.session.commit() + my_ecdc_date_reported = EcdcDateReported.get_by_date_reported(my_date_reported_search) + dict_date_reported_from_import[my_date_reported_str] = my_ecdc_date_reported + return dict_date_reported_from_import + def __update_data_initial(self): app.logger.info(" __update_data_initial [begin]") app.logger.info("------------------------------------------------------------") EcdcData.remove_all() - result_date_rep = EcdcImport.get_date_rep() i = 0 - for item_date_rep in result_date_rep: - my_date_reported = item_date_rep[0] - my_date_reported_search = EcdcDateReported.get_my_date_rep_as_str(my_date_reported) - ecdc_date_reported = EcdcDateReported.find_by_date_reported( - i_date_reported=my_date_reported_search - ) - if ecdc_date_reported is None: - ecdc_date_reported = EcdcDateReported.create_new_object_factory(my_date_reported) - db.session.add(ecdc_date_reported) - db.session.commit() - result_ecdc_data_import = EcdcImport.find_by_date_reported(ecdc_date_reported) + for (my_date_reported, my_ecdc_date_reported) in self.__get_date_reported_from_import(): + ecdc_country = self.__get_country_from_import(item_ecdc_data_import) + result_ecdc_data_import = EcdcImport.find_by_date_reported(my_ecdc_date_reported) for item_ecdc_data_import in result_ecdc_data_import: - my_a = item_ecdc_data_import.countries_and_territories - my_b = item_ecdc_data_import.geo_id - my_c = item_ecdc_data_import.country_territory_code - ecdc_country = EcdcCountry.find_by( - countries_and_territories=my_a, - geo_id=my_b, - country_territory_code=my_c - ) - my_d = int(item_ecdc_data_import.deaths_weekly) - my_e = int(item_ecdc_data_import.cases_weekly) + ecdc_country = self.__get_country_from_import(item_ecdc_data_import) + my_deaths_weekly = int(item_ecdc_data_import.deaths_weekly) + my_cases_weekly = int(item_ecdc_data_import.cases_weekly) if item_ecdc_data_import.notification_rate_per_100000_population_14days == '': - my_f = 0.0 + my_notification_rate_per_100000_population_14days = 0.0 else: - my_f = float(item_ecdc_data_import.notification_rate_per_100000_population_14days) + my_notification_rate_per_100000_population_14days = \ + float(item_ecdc_data_import.notification_rate_per_100000_population_14days) o = EcdcData( ecdc_country=ecdc_country, - ecdc_date_reported=ecdc_date_reported, - deaths_weekly=my_d, - cases_weekly=my_e, - notification_rate_per_100000_population_14days=my_f + ecdc_date_reported=my_ecdc_date_reported, + deaths_weekly=my_deaths_weekly, + cases_weekly=my_cases_weekly, + notification_rate_per_100000_population_14days=my_notification_rate_per_100000_population_14days ) db.session.add(o) i += 1 -- GitLab