diff --git a/src/flask_covid19/blueprints/app_all/all_model.py b/src/flask_covid19/blueprints/app_all/all_model.py index 8f885b35866a7188c9ba1a5ecd70632a6a95b284..2cabd10981dc01cfe848dfa20341b071b8ed3568 100644 --- a/src/flask_covid19/blueprints/app_all/all_model.py +++ b/src/flask_covid19/blueprints/app_all/all_model.py @@ -1,5 +1,5 @@ from datetime import date -from database import db, ITEMS_PER_PAGE +from database import db, ITEMS_PER_PAGE, cache from sqlalchemy.orm import subqueryload from sqlalchemy import not_, and_ @@ -28,15 +28,18 @@ class BlueprintEntity(db.Model): return None @classmethod + @cache.memoize(50) def get_all_as_page(cls, page: int): return db.session.query(cls)\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_all(cls, page: int): return db.session.query(cls).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_all(cls): return db.session.query(cls).all() @@ -45,14 +48,17 @@ class BlueprintEntity(db.Model): pass @classmethod + @cache.memoize(50) def get_by_id(cls, other_id): return db.session.query(cls).filter(cls.id == other_id).one() @classmethod + @cache.memoize(50) def find_by_id(cls, other_id): return db.session.query(cls).filter(cls.id == other_id).one_or_none() @classmethod + @cache.memoize(50) def set_all_processed_full_update(cls): for o in cls.find_by_not_processed_full_update(): o.set_processed_full_update() @@ -76,10 +82,12 @@ class BlueprintEntity(db.Model): return self @classmethod + @cache.memoize(50) def find_by_not_processed_update(cls): return db.session.query(cls).filter(not_(cls.processed_update)).all() @classmethod + @cache.memoize(50) def find_by_not_processed_full_update(cls): return db.session.query(cls).filter(not_(cls.processed_full_update)).all() @@ -120,6 +128,7 @@ class BlueprintDateReported(BlueprintEntity): return self.get_names_for_weekday()[self.day_of_week] @classmethod + @cache.memoize(50) def get_names_for_weekday(cls): return {1: "Montag", 2: "Dienstag", 3: "Mittwoch", 4: "Donnerstag", 5: "Freitag", 6: "Samstag", 7: "Sonntag"} @@ -133,34 +142,40 @@ class BlueprintDateReported(BlueprintEntity): 7: "Juli", 8: "August", 9: "September", 10: "Oktober", 11: "November", 12: "Dezember"} @classmethod + @cache.memoize(50) def get_by_datum(cls, datum: date): return db.session.query(cls).filter(cls.datum == datum).one() @classmethod + @cache.memoize(50) def get_by_date_reported(cls, date_reported_import_str: str): return db.session.query(cls)\ .filter(cls.date_reported_import_str == date_reported_import_str)\ .one() @classmethod + @cache.memoize(50) def find_by_date_reported(cls, date_reported_import_str: str): return db.session.query(cls)\ .filter(cls.date_reported_import_str == date_reported_import_str)\ .one_or_none() @classmethod + @cache.memoize(50) def find_by_year_week(cls, year_week: str): return db.session.query(cls)\ .filter(cls.year_week == year_week)\ .all() @classmethod + @cache.memoize(50) def get_joungest_datum(cls): return db.session.query(cls)\ .order_by(cls.datum.desc())\ .first() @classmethod + @cache.memoize(50) def set_all_processed_update(cls): for o in cls.find_by_not_processed_update(): o.set_processed_update() @@ -168,10 +183,12 @@ class BlueprintDateReported(BlueprintEntity): return None @classmethod + @cache.memoize(50) def find_all(cls): return db.session.query(cls).order_by(cls.datum.desc()).all() @classmethod + @cache.memoize(50) def find_all_as_dict(cls): dates_reported = {} for my_date_reported in cls.find_all(): @@ -196,32 +213,38 @@ class BlueprintLocationGroup(BlueprintEntity): location_group = db.Column(db.String(255), nullable=False, unique=True) @classmethod + @cache.memoize(50) def get_last(cls): return db.session.query(cls).order_by(cls.location_group).all().pop() @classmethod + @cache.memoize(50) def get_by_location_group(cls, location_group: str): return db.session.query(cls)\ .filter(cls.location_group == location_group)\ .one() @classmethod + @cache.memoize(50) def find_by_location_group(cls, location_group: str): return db.session.query(cls) \ .filter(cls.location_group == location_group) \ .one_or_none() @classmethod + @cache.memoize(50) def get_all_as_page(cls, page: int): return db.session.query(cls)\ .order_by(cls.location_group)\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_all(cls): return db.session.query(cls).order_by(cls.location_group).all() @classmethod + @cache.memoize(50) def find_all_as_dict(cls): dates_reported = {} for my_location_group in cls.find_all(): @@ -252,38 +275,45 @@ class BlueprintLocation(BlueprintEntity): order_by='BlueprintLocationGroup.location_group') @classmethod + @cache.memoize(50) def find_by_location_code(cls, location_code: str): return db.session.query(cls).filter(cls.location_code == location_code)\ .one_or_none() @classmethod + @cache.memoize(50) def get_by_location_code(cls, location_code: str): return db.session.query(cls).filter(cls.location_code == location_code)\ .one() @classmethod + @cache.memoize(50) def find_by_location(cls, location: str): return db.session.query(cls).filter(cls.location == location)\ .one_or_none() @classmethod + @cache.memoize(50) def get_by_location(cls, location: str): return db.session.query(cls).filter(cls.location == location)\ .one() @classmethod + @cache.memoize(50) def find_by_location_group(cls, location_group: BlueprintLocationGroup): return db.session.query(cls).filter(cls.location_group == location_group)\ .order_by(cls.location)\ .all() @classmethod + @cache.memoize(50) def get_by_location_group(cls, location_group: BlueprintLocationGroup, page: int): return db.session.query(cls).filter(cls.location_group == location_group)\ .order_by(cls.location)\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_location_code_and_location_and_location_group(cls, location_code: str, location: str, location_group: BlueprintLocationGroup): return db.session.query(cls).filter( @@ -295,6 +325,7 @@ class BlueprintLocation(BlueprintEntity): ).one_or_none() @classmethod + @cache.memoize(50) def get_by_location_code_and_location_and_location_group(cls, location_code: str, location: str, location_group: BlueprintLocationGroup): return db.session.query(cls).filter( @@ -306,6 +337,7 @@ class BlueprintLocation(BlueprintEntity): ).one() @classmethod + @cache.memoize(50) def find_by_location_code_and_location(cls, location_code: str, location: str): return db.session.query(cls)\ .filter(and_(cls.location_code == location_code, cls.location == location))\ @@ -313,15 +345,18 @@ class BlueprintLocation(BlueprintEntity): .one_or_none() @classmethod + @cache.memoize(50) def get_by_location_code_and_location(cls, location_code: str, location: str): return db.session.query(cls).filter(and_(cls.location_code == location_code, cls.location == location)) \ .one() @classmethod + @cache.memoize(50) def find_all(cls): return db.session.query(cls).order_by(cls.location).all() @classmethod + @cache.memoize(50) def find_all_as_dict(cls): dates_reported = {} for my_location in cls.find_all(): @@ -329,6 +364,7 @@ class BlueprintLocation(BlueprintEntity): return dates_reported @classmethod + @cache.memoize(50) def get_all_as_page(cls, page: int): return db.session.query(cls)\ .order_by(cls.location)\ @@ -356,6 +392,7 @@ class BlueprintFactTableTimeSeries(BlueprintEntity): order_by='desc(BlueprintDateReported.datum)') @classmethod + @cache.memoize(50) def get_date_reported_list(cls): date_reported_list = [] for data in db.session.query(cls).options(subqueryload("date_reported").load_only("datum")): @@ -366,6 +403,7 @@ class BlueprintFactTableTimeSeries(BlueprintEntity): return date_reported_list @classmethod + @cache.memoize(50) def get_joungest_date_reported(cls): data = cls.get_date_reported_list() if len(data) > 0: @@ -446,12 +484,14 @@ class AllImport(BlueprintEntity): datum = db.Column(db.Date, nullable=False) @classmethod + @cache.memoize(50) def find_by_datum(cls, datum: date): return db.session.query(cls) \ .filter(cls.datum == datum) \ .all() @classmethod + @cache.memoize(50) def get_datum_list(cls): return db.session.query(cls.datum) \ .order_by(cls.datum.desc()) \ diff --git a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py index 696e401df43654c5c02252592cd429c8cd29008b..01dad0b59d57374fcd700a86cec697f5d351e050 100644 --- a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py +++ b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py @@ -1,6 +1,6 @@ from sqlalchemy import and_ from sqlalchemy.orm import joinedload, subqueryload -from database import db, ITEMS_PER_PAGE +from database import db, ITEMS_PER_PAGE, cache from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, BlueprintLocationGroup from flask_covid19.blueprints.app_all.all_model import BlueprintLocation, BlueprintFactTable @@ -17,6 +17,9 @@ class EcdcDateReported(BlueprintDateReported): ), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) # date_reported_import_str = db.Column(db.String(255), nullable=False, unique=True) @@ -36,6 +39,7 @@ class EcdcDateReported(BlueprintDateReported): processed_full_update = db.Column(db.Boolean, nullable=False) @classmethod + @cache.memoize(50) def get_all_as_page(cls, page: int): return db.session.query(cls)\ .order_by(cls.datum.desc())\ @@ -49,6 +53,9 @@ class EcdcContinent(BlueprintLocationGroup): db.UniqueConstraint('location_group', name="uix_ecdc_country_continent"), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) location_group = db.Column(db.String(255), nullable=False, unique=True) processed_update = db.Column(db.Boolean, nullable=False) @@ -67,6 +74,9 @@ class EcdcCountry(BlueprintLocation): ), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + def __str__(self): return " " + self.location_group.location_group \ + " : " + self.location_code \ @@ -91,6 +101,7 @@ class EcdcCountry(BlueprintLocation): geo_id = db.Column(db.String(255), nullable=False) @classmethod + @cache.memoize(50) def get_by(cls, location: str = '', geo_id: str = '', location_code: str = ''): return db.session.query(cls).filter(and_( (cls.location == location), @@ -99,6 +110,7 @@ class EcdcCountry(BlueprintLocation): )).one() @classmethod + @cache.memoize(50) def find_by(cls, location: str = '', geo_id: str = '', location_code: str = ''): return db.session.query(cls).filter(and_( (cls.location == location), @@ -107,18 +119,21 @@ class EcdcCountry(BlueprintLocation): )).one_or_none() @classmethod + @cache.memoize(50) def find_by_location_group(cls, location_group: EcdcContinent): return db.session.query(cls)\ .filter(cls.location_group_id == location_group.id)\ .all() @classmethod + @cache.memoize(50) def get_by_location_group(cls, location_group: EcdcContinent, page: int): return db.session.query(cls)\ .filter(cls.location_group_id == location_group.id)\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_germany(cls): return db.session.query(cls) \ .filter(cls.location_code == 'DEU') \ @@ -128,6 +143,12 @@ class EcdcCountry(BlueprintLocation): class EcdcData(BlueprintFactTable): __tablename__ = 'ecdc' __mapper_args__ = {'concrete': True} + __table_args__ = ( + db.UniqueConstraint('date_reported_id', 'location_id', name="uix_ecdc"), + ) + + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) id = db.Column(db.Integer, primary_key=True) date_reported_id = db.Column(db.Integer, db.ForeignKey('ecdc_date_reported.id'), nullable=False) @@ -188,54 +209,66 @@ class EcdcData(BlueprintFactTable): .filter(and_((cls.location_id == location.id), (cls.date_reported_id == date_reported.id))) @classmethod + @cache.memoize(50) def find_by_date_reported(cls, date_reported: EcdcDateReported): return cls.__query_by_date_reported(date_reported).all() @classmethod + @cache.memoize(50) def get_by_date_reported(cls, date_reported: EcdcDateReported, page: int): return cls.__query_by_date_reported(date_reported)\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_date_reported_order_by_notification_rate(cls, date_reported: EcdcDateReported): return cls.__query_by_date_reported_order_by_notification_rate(date_reported).all() @classmethod + @cache.memoize(50) def get_by_date_reported_order_by_notification_rate(cls, date_reported: EcdcDateReported, page: int): return cls.__query_by_date_reported_order_by_notification_rate(date_reported)\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_date_reported_order_by_deaths(cls, date_reported: EcdcDateReported): return cls.__query_by_date_reported_order_by_deaths_weekly(date_reported).all() @classmethod + @cache.memoize(50) def get_by_date_reported_order_by_deaths(cls, date_reported: EcdcDateReported, page: int): return cls.__query_by_date_reported_order_by_deaths_weekly(date_reported)\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_date_reported_order_by_cases(cls, date_reported: EcdcDateReported): return cls.__query_by_date_reported_order_by_cases_weekly(date_reported).all() @classmethod + @cache.memoize(50) def get_by_date_reported_order_by_cases(cls, date_reported: EcdcDateReported, page: int): return cls.__query_by_date_reported_order_by_cases_weekly(date_reported)\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_location(cls, location: EcdcCountry): return cls.__query_by_location(location).all() @classmethod + @cache.memoize(50) def get_by_location(cls, location: EcdcCountry, page: int): return cls.__query_by_location(location).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_date_reported_and_location(cls, date_reported: EcdcDateReported, location: EcdcCountry): return cls.__query_by_date_reported_and_location(date_reported, location).one_or_none() @classmethod + @cache.memoize(50) def get_by_date_reported_and_location(cls, date_reported: EcdcDateReported, location: EcdcCountry, page: int): return cls.__query_by_date_reported_and_location(date_reported, location).one() diff --git a/src/flask_covid19/blueprints/data_ecdc/ecdc_model_import.py b/src/flask_covid19/blueprints/data_ecdc/ecdc_model_import.py index 5a49015b4b5aebb8de97d0bbdf22c553980f3cb5..1d651591d2aa8847560788c9c1dc3dac4a727349 100644 --- a/src/flask_covid19/blueprints/data_ecdc/ecdc_model_import.py +++ b/src/flask_covid19/blueprints/data_ecdc/ecdc_model_import.py @@ -1,4 +1,4 @@ -from database import db, ITEMS_PER_PAGE +from database import db, ITEMS_PER_PAGE, cache from flask_covid19.blueprints.app_all.all_model import AllImport, AllFlat @@ -6,6 +6,9 @@ class EcdcImport(AllImport): __tablename__ = 'ecdc_import' __mapper_args__ = {'concrete': True} + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) date_reported_import_str = db.Column(db.String(255), nullable=False) datum = db.Column(db.Date, nullable=False) @@ -29,6 +32,7 @@ class EcdcImport(AllImport): cumulative_number_for_14_days_of_covid19_cases_per_100000 = db.Column(db.String(255), nullable=False) @classmethod + @cache.memoize(50) def remove_all(cls): num_rows_deleted = 0 try: @@ -39,6 +43,7 @@ class EcdcImport(AllImport): return num_rows_deleted @classmethod + @cache.memoize(50) def get_all_as_page(cls, page: int): return db.session.query(cls).order_by( cls.year, @@ -48,6 +53,7 @@ class EcdcImport(AllImport): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_all(cls): return db.session.query(cls).order_by( cls.year, @@ -57,10 +63,12 @@ class EcdcImport(AllImport): ).all() @classmethod + @cache.memoize(50) def get_by_id(cls, other_id: int): return db.session.query(cls).filter(cls.id == other_id).one() @classmethod + @cache.memoize(50) def get_date_rep(cls): # sql = "select distinct date_rep, year_week from edcd_import order by year_week desc" #return db.session.execute(sql).fetchall() @@ -71,6 +79,7 @@ class EcdcImport(AllImport): .all() @classmethod + @cache.memoize(50) def get_continent(cls): # sql = "select distinct continent_exp from edcd_import order by continent_exp asc" #return db.session.execute(sql).fetchall() @@ -81,6 +90,7 @@ class EcdcImport(AllImport): .all() @classmethod + @cache.memoize(50) def get_countries_of_continent(cls, my_continent): my_continent_exp = my_continent.location_group my_params = {} @@ -124,6 +134,7 @@ class EcdcImport(AllImport): #return db.session.execute(sql, my_params).fetchall() @classmethod + @cache.memoize(50) def find_by_date_reported(cls, p_edcd_date_reported_str: str = ''): return db.session.query(cls)\ .filter(cls.date_rep == p_edcd_date_reported_str) \ @@ -134,6 +145,9 @@ class EcdcFlat(AllFlat): __tablename__ = 'ecdc_import_flat' __mapper_args__ = {'concrete': True} + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) processed_update = db.Column(db.Boolean, nullable=False) processed_full_update = db.Column(db.Boolean, nullable=False) diff --git a/src/flask_covid19/blueprints/data_owid/owid_model.py b/src/flask_covid19/blueprints/data_owid/owid_model.py index 217038ae090dbe9073d4f55912bb36bf916bb696..26f0dac68b7a256634bb259fa3fac8ab8775c55e 100644 --- a/src/flask_covid19/blueprints/data_owid/owid_model.py +++ b/src/flask_covid19/blueprints/data_owid/owid_model.py @@ -1,7 +1,7 @@ from sqlalchemy import and_ from sqlalchemy.orm import joinedload, subqueryload -from database import db, ITEMS_PER_PAGE +from database import db, ITEMS_PER_PAGE, cache from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, BlueprintLocationGroup from flask_covid19.blueprints.app_all.all_model import BlueprintLocation, BlueprintFactTable @@ -18,6 +18,9 @@ class OwidDateReported(BlueprintDateReported): ), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) # date_reported_import_str = db.Column(db.String(255), nullable=False, unique=True) @@ -44,6 +47,9 @@ class OwidContinent(BlueprintLocationGroup): db.UniqueConstraint('location_group', name="uix_owid_country_continent"), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + def __str__(self): result = "" + self.location_group + " " return result @@ -61,6 +67,9 @@ class OwidCountry(BlueprintLocation): db.UniqueConstraint('location_code', 'location', name="uix_owid_country"), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + def __str__(self): result = "" + self.location_group.__str__() + " : " + self.location_code + " | " + self.location + " " return result @@ -101,24 +110,29 @@ class OwidCountry(BlueprintLocation): return None @classmethod + @cache.memoize(50) def get_germany(cls): iso_code = 'DEU' location = 'Germany' return cls.find_by_location_code_and_location(location_code=iso_code, location=location) @classmethod + @cache.memoize(50) def get_countries_for_continent(cls, owid_continent_one: OwidContinent, page: int): return db.session.query(cls).filter(cls.location_group == owid_continent_one).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_all_countries_for_continent(cls, owid_continent_one: OwidContinent): return db.session.query(cls).filter(cls.location_group == owid_continent_one).all() @classmethod + @cache.memoize(50) def find_by_iso_code_and_location(cls, iso_code, location): return db.session.query(cls).filter(and_((cls.location_code == iso_code), (cls.location == location))).one_or_none() @classmethod + @cache.memoize(50) def get_by_iso_code_and_location(cls, iso_code, location): return db.session.query(cls).filter(and_((cls.location_code == iso_code), (cls.location == location))).one() @@ -126,6 +140,12 @@ class OwidCountry(BlueprintLocation): class OwidData(BlueprintFactTable): __tablename__ = 'owid' __mapper_args__ = {'concrete': True} + __table_args__ = ( + db.UniqueConstraint('date_reported_id', 'location_id', name="uix_owid"), + ) + + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) id = db.Column(db.Integer, primary_key=True) date_reported_id = db.Column(db.Integer, db.ForeignKey('owid_date_reported.id'), nullable=False) @@ -203,23 +223,27 @@ class OwidData(BlueprintFactTable): ) @classmethod + @cache.memoize(50) def find_by_location(cls, location: OwidCountry): return cls.__query_by_location(location)\ .order_by(cls.date_reported.datum.desc())\ .all() @classmethod + @cache.memoize(50) def get_by_location(cls, location: OwidCountry, page: int): return cls.__query_by_location(location) \ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def delete_by_location(cls, location: OwidCountry): cls.__query_by_location(location).delete() db.session.commit() return None @classmethod + @cache.memoize(50) def find_by_date_reported(cls, date_reported: OwidDateReported): return cls.__query_by_date_reported(date_reported).order_by( cls.new_deaths_per_million.desc(), @@ -229,6 +253,7 @@ class OwidData(BlueprintFactTable): ).all() @classmethod + @cache.memoize(50) def get_by_date_reported(cls, date_reported: OwidDateReported, page: int): return cls.__query_by_date_reported(date_reported).order_by( cls.new_deaths_per_million.desc(), @@ -238,6 +263,7 @@ class OwidData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_date_reported_order_by_deaths_new(cls, date_reported: OwidDateReported, page: int): return cls.__query_by_date_reported(date_reported).order_by( cls.new_deaths.desc(), @@ -247,6 +273,7 @@ class OwidData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_date_reported_order_by_deaths_cumulative(cls, date_reported: OwidDateReported, page: int): return cls.__query_by_date_reported(date_reported).order_by( cls.new_deaths_per_million.desc(), @@ -256,6 +283,7 @@ class OwidData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_date_reported_order_by_cases_new(cls, date_reported: OwidDateReported, page: int): return cls.__query_by_date_reported(date_reported).order_by( cls.new_cases.desc(), @@ -265,6 +293,7 @@ class OwidData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_date_reported_order_by_cases_cumulative(cls, date_reported: OwidDateReported, page: int): return cls.__query_by_date_reported(date_reported).order_by( cls.new_cases_per_million.desc(), @@ -274,6 +303,7 @@ class OwidData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def delete_data_for_one_day(cls, date_reported: OwidDateReported): for data in cls.find_by_date_reported(date_reported): db.session.delete(data) diff --git a/src/flask_covid19/blueprints/data_owid/owid_model_import.py b/src/flask_covid19/blueprints/data_owid/owid_model_import.py index f792fd1c0d97a3caf6eec549eec9241a6dc1a14e..edf4e431b1f4bd6ed77b667cd598ffa02d84c297 100644 --- a/src/flask_covid19/blueprints/data_owid/owid_model_import.py +++ b/src/flask_covid19/blueprints/data_owid/owid_model_import.py @@ -8,6 +8,9 @@ class OwidImport(AllImport): __tablename__ = 'owid_import' __mapper_args__ = {'concrete': True} + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + def __str__(self): return self.datum.isoformat() + " " + self.iso_code + " " + self.location + " " + str(self.continent) @@ -263,6 +266,9 @@ class OwidFlat(AllFlat): __tablename__ = 'owid_import_flat' __mapper_args__ = {'concrete': True} + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + def __str__(self): return self.datum.isoformat() + " " + self.location_code + " " + self.location + " " + str(self.location_group) diff --git a/src/flask_covid19/blueprints/data_rki/rki_model.py b/src/flask_covid19/blueprints/data_rki/rki_model.py index 925d1a4f6f5e79fe484c3a6e0df4f11708505da9..4658a2cea276f0e933088c4bd786c428164c48f9 100644 --- a/src/flask_covid19/blueprints/data_rki/rki_model.py +++ b/src/flask_covid19/blueprints/data_rki/rki_model.py @@ -1,7 +1,7 @@ from sqlalchemy import and_ from sqlalchemy.orm import joinedload -from database import db, ITEMS_PER_PAGE +from database import db, ITEMS_PER_PAGE, cache from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, BlueprintLocationGroup from flask_covid19.blueprints.app_all.all_model import BlueprintLocation, BlueprintFactTable, BlueprintEntity @@ -18,6 +18,9 @@ class RkiMeldedatum(BlueprintDateReported): ), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) # date_reported_import_str = db.Column(db.String(255), nullable=False, unique=True) @@ -59,6 +62,9 @@ class RkiDatenstand(BlueprintDateReported): ), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) # date_reported_import_str = db.Column(db.String(255), nullable=False, unique=True) @@ -90,6 +96,9 @@ class RkiRefDatum(BlueprintDateReported): ), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) # date_reported_import_str = db.Column(db.String(255), nullable=False, unique=True) @@ -116,6 +125,9 @@ class RkiBundesland(BlueprintLocationGroup): db.UniqueConstraint('id_bundesland', 'location_group', name="uix_rki_landkreis_bundesland"), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + def __str__(self): return " " + self.location_group + " " @@ -133,6 +145,9 @@ class RkiLandkreis(BlueprintLocation): db.UniqueConstraint('location', 'id_landkreis', name="uix_rki_landkreis"), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + def __str__(self): return " " + str(self.location_group) + " : " + self.location + " ( " + self.id_landkreis + " ) " @@ -150,28 +165,33 @@ class RkiLandkreis(BlueprintLocation): location_type = db.Column(db.String(255), nullable=False) @classmethod + @cache.memoize(50) def get_bochum(cls): return db.session.query(cls)\ .filter(cls.location == 'SK Bochum')\ .one() @classmethod + @cache.memoize(50) def find_all(cls): return db.session.query(cls).order_by(cls.location).all() @classmethod + @cache.memoize(50) def find_by_location_group(cls, location_group: RkiBundesland): return db.session.query(cls).filter( cls.location_group == location_group ).order_by(cls.location).all() @classmethod + @cache.memoize(50) def get_by_location_group(cls, location_group: RkiBundesland, page: int): return db.session.query(cls).filter( cls.location_group == location_group ).order_by(cls.location).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_all_as_dict(cls): rki_landkreis_dict = {} for my_rki_landkreis in cls.find_all(): @@ -184,6 +204,9 @@ class RkiAltersgruppe(BlueprintEntity): __tablename__ = 'rki_altersgruppe' __mapper_args__ = {'concrete': True} + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + def __str__(self): return " " + self.altersgruppe + " " @@ -193,20 +216,24 @@ class RkiAltersgruppe(BlueprintEntity): altersgruppe = db.Column(db.String(255), nullable=False) @classmethod + @cache.memoize(50) def get_all_as_page(cls, page: int): return db.session.query(cls) \ .order_by(cls.altersgruppe)\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_all(cls, page: int): return db.session.query(cls).order_by(cls.altersgruppe).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_all(cls): return db.session.query(cls).order_by(cls.altersgruppe).all() @classmethod + @cache.memoize(50) def find_all_as_dict(cls): altersgruppe_dict = {} for my_altersgruppe in cls.find_all(): @@ -222,6 +249,9 @@ class RkiData(BlueprintFactTable): 'ref_datum_id', 'altersgruppe_id', name="uix_rki"), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) processed_update = db.Column(db.Boolean, nullable=False) processed_full_update = db.Column(db.Boolean, nullable=False) @@ -298,20 +328,24 @@ class RkiData(BlueprintFactTable): ) @classmethod + @cache.memoize(50) def get_by_location(cls, location: RkiLandkreis, page: int): return cls.__query_by_location(location).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_location(cls, location: RkiLandkreis): return cls.__query_by_location(location).all() @classmethod + @cache.memoize(50) def find_by_date_reported_and_location(cls, date_reported: RkiMeldedatum, location: RkiLandkreis): return db.session.query(cls)\ .filter(and_((cls.date_reported_id == date_reported.id), (cls.location_id == location.id)))\ .all() @classmethod + @cache.memoize(50) def get_by_date_reported_and_location(cls, date_reported: RkiMeldedatum, location: RkiLandkreis, page: int): return db.session.query(cls)\ .filter(and_((cls.date_reported_id == date_reported.id), (cls.location_id == location.id)))\ diff --git a/src/flask_covid19/blueprints/data_rki/rki_model_import.py b/src/flask_covid19/blueprints/data_rki/rki_model_import.py index 1ae35a13cc3fe66fef5b4f4a94367eb817c365e7..76c62836597d1d9e315495268896adb1e315d510 100644 --- a/src/flask_covid19/blueprints/data_rki/rki_model_import.py +++ b/src/flask_covid19/blueprints/data_rki/rki_model_import.py @@ -1,7 +1,7 @@ from datetime import date from sqlalchemy.orm import Bundle from sqlalchemy import and_ -from database import db, ITEMS_PER_PAGE +from database import db, ITEMS_PER_PAGE, cache from flask_covid19.blueprints.app_all.all_model import AllImport, AllFlat @@ -9,6 +9,9 @@ class RkiImport(AllImport): __tablename__ = 'rki_import' __mapper_args__ = {'concrete': True} + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) date_reported_import_str = db.Column(db.String(255), nullable=False) datum = db.Column(db.Date, nullable=False) @@ -45,26 +48,32 @@ class RkiImport(AllImport): return num_rows_deleted @classmethod + @cache.memoize(50) def get_all_as_page(cls, page: int): return db.session.query(cls).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_all(cls): return db.session.query(cls).all() @classmethod + @cache.memoize(50) def get_aktualisierungen_as_array(cls): return [] @classmethod + @cache.memoize(50) def find_by_aktualisierung(cls, aktualisierung_from_import: str): return [] @classmethod + @cache.memoize(50) def get_new_aktualisierungen_as_array(cls): return [] @classmethod + @cache.memoize(50) def get_date_datenstand_of_all_import(cls): dates_reported = [] bu = Bundle('datenstand', cls.datenstand) @@ -75,6 +84,7 @@ class RkiImport(AllImport): return dates_reported @classmethod + @cache.memoize(50) def get_date_ref_datum_of_all_import(cls): dates_reported = [] bu = Bundle('ref_datum', cls.ref_datum) @@ -85,6 +95,7 @@ class RkiImport(AllImport): return dates_reported @classmethod + @cache.memoize(50) def get_datum_of_all_import(cls): dates_reported = [] bu = Bundle('meldedatum', cls.meldedatum) @@ -95,6 +106,7 @@ class RkiImport(AllImport): return dates_reported @classmethod + @cache.memoize(50) def get_meldedatum_list(cls): return db.session.query(cls.meldedatum)\ .distinct()\ @@ -103,6 +115,7 @@ class RkiImport(AllImport): .all() @classmethod + @cache.memoize(50) def get_bundesland_list(cls): bundesland_list = [] bu = Bundle('bundesland', cls.bundesland, cls.id_bundesland) @@ -113,6 +126,7 @@ class RkiImport(AllImport): return bundesland_list @classmethod + @cache.memoize(50) def get_altersgruppe_list(cls): altersgruppe_list = [] bu = Bundle('altersgruppe', cls.altersgruppe) @@ -123,6 +137,7 @@ class RkiImport(AllImport): return altersgruppe_list @classmethod + @cache.memoize(50) def find_by_datun(cls, datum: date): return db.session.query(cls)\ .filter(cls.datum == datum) \ @@ -130,6 +145,7 @@ class RkiImport(AllImport): .all() @classmethod + @cache.memoize(50) def get_landkreis_for_bundesland(cls, bundesland:str): return db.session.query(cls.landkreis, cls.id_landkreis) \ .filter(cls.bundesland == bundesland) \ @@ -138,6 +154,7 @@ class RkiImport(AllImport): .all() @classmethod + @cache.memoize(50) def find_by_datum(cls, my_datum: date): return db.session.query(cls) \ .filter(cls.datum == my_datum) \ @@ -145,6 +162,7 @@ class RkiImport(AllImport): .all() @classmethod + @cache.memoize(50) def find_by_meldedatum_and_landkreis(cls, my_datum: str, my_landkreis: str): return db.session.query(cls) \ .filter(and_(cls.date_reported_import_str == my_datum), (cls.landkreis.id == my_landkreis.id)) \ @@ -156,6 +174,9 @@ class RkiFlat(AllFlat): __tablename__ = 'rki_import_flat' __mapper_args__ = {'concrete': True} + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) # date_reported_import_str = db.Column(db.String(255), nullable=False) diff --git a/src/flask_covid19/blueprints/data_vaccination/vaccination_model.py b/src/flask_covid19/blueprints/data_vaccination/vaccination_model.py index eb6ede6f4f88b721346c87d003da29528318e2ca..5ade371785697069f8c1a51512ba645a2c535d92 100644 --- a/src/flask_covid19/blueprints/data_vaccination/vaccination_model.py +++ b/src/flask_covid19/blueprints/data_vaccination/vaccination_model.py @@ -17,6 +17,9 @@ class VaccinationDateReported(BlueprintDateReported): ), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) # date_reported_import_str = db.Column(db.String(255), nullable=False, unique=True) @@ -39,6 +42,12 @@ class VaccinationDateReported(BlueprintDateReported): class VaccinationData(BlueprintFactTableTimeSeries): __tablename__ = 'vaccination' __mapper_args__ = {'concrete': True} + __table_args__ = ( + db.UniqueConstraint('date_reported_id', name="uix_vaccination"), + ) + + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) id = db.Column(db.Integer, primary_key=True) date_reported_id = db.Column(db.Integer, db.ForeignKey('vaccination_date_reported.id'), nullable=False) diff --git a/src/flask_covid19/blueprints/data_vaccination/vaccination_model_import.py b/src/flask_covid19/blueprints/data_vaccination/vaccination_model_import.py index 0551eaacd75e9f4309349d4e17d7e514f39cba2f..9e6d23e31d4b131138755a48e92f98e95ad4b302 100644 --- a/src/flask_covid19/blueprints/data_vaccination/vaccination_model_import.py +++ b/src/flask_covid19/blueprints/data_vaccination/vaccination_model_import.py @@ -1,11 +1,14 @@ -from database import db, ITEMS_PER_PAGE -from flask_covid19.blueprints.app_all.all_model import AllImport, AllFlat +from database import db, ITEMS_PER_PAGE, cache +from flask_covid19.blueprints.app_all.all_model import AllImport class VaccinationImport(AllImport): __tablename__ = 'vaccination_import' __mapper_args__ = {'concrete': True} + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) processed_update = db.Column(db.Boolean, nullable=False) processed_full_update = db.Column(db.Boolean, nullable=False) @@ -40,36 +43,42 @@ class VaccinationImport(AllImport): return None @classmethod + @cache.memoize(50) def get_all_as_page(cls, page: int): return db.session.query(cls)\ .order_by(cls.datum.desc())\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_all(cls): return db.session.query(cls)\ .order_by(cls.datum)\ .all() @classmethod + @cache.memoize(50) def get_by_id(cls, other_id: int): return db.session.query(cls)\ .filter(cls.id == other_id)\ .one() @classmethod + @cache.memoize(50) def find_by_id(cls, other_id: int): return db.session.query(cls) \ .filter(cls.id == other_id) \ .one_or_none() @classmethod + @cache.memoize(50) def find_by_datum(cls, other_datum: str): return db.session.query(cls) \ .filter(cls.datum == other_datum) \ .all() @classmethod + @cache.memoize(50) def get_date_rep(cls): return db.session.query(cls.datum)\ .group_by(cls.datum)\ @@ -78,6 +87,7 @@ class VaccinationImport(AllImport): .all() @classmethod + @cache.memoize(50) def get_date_reported_as_array(cls): resultarray = [] resultset = db.session.query(cls.datum)\ @@ -130,6 +140,9 @@ class VaccinationFlat(AllImport): __tablename__ = 'vaccination_import_flat' __mapper_args__ = {'concrete': True} + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + id = db.Column(db.Integer, primary_key=True) processed_update = db.Column(db.Boolean, nullable=False) processed_full_update = db.Column(db.Boolean, nullable=False) diff --git a/src/flask_covid19/blueprints/data_who/who_model.py b/src/flask_covid19/blueprints/data_who/who_model.py index 039cf5f60128e5b9e75974c982d99d049bee30ce..1ba6f127caa359ec1c6292095d45d9efdb167903 100644 --- a/src/flask_covid19/blueprints/data_who/who_model.py +++ b/src/flask_covid19/blueprints/data_who/who_model.py @@ -1,6 +1,6 @@ from sqlalchemy import and_ from sqlalchemy.orm import joinedload -from database import db, ITEMS_PER_PAGE +from database import db, ITEMS_PER_PAGE, cache from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, BlueprintFactTable, AllImport from flask_covid19.blueprints.app_all.all_model import BlueprintLocationGroup, BlueprintLocation, AllFlat @@ -17,6 +17,9 @@ class WhoDateReported(BlueprintDateReported): ), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + def __str__(self): return self.datum.isoformat() @@ -43,14 +46,17 @@ class WhoCountryRegion(BlueprintLocationGroup): db.UniqueConstraint('location_group', name="uix_who_country_region"), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + + def __str__(self): + return " " + self.location_group + " " + id = db.Column(db.Integer, primary_key=True) location_group = db.Column(db.String(255), nullable=False, unique=True) processed_update = db.Column(db.Boolean, nullable=False) processed_full_update = db.Column(db.Boolean, nullable=False) - def __str__(self): - return " " + self.location_group + " " - class WhoCountry(BlueprintLocation): __tablename__ = 'who_country' @@ -59,6 +65,12 @@ class WhoCountry(BlueprintLocation): db.UniqueConstraint('location_code', 'location', name="uix_who_country"), ) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + + def __str__(self): + return self.location_group.__str__() + " : "+ self.location_code + " | " + self.location + id = db.Column(db.Integer, primary_key=True) location_code = db.Column(db.String(255), unique=True, nullable=False) location = db.Column(db.String(255), unique=True, nullable=False) @@ -71,10 +83,8 @@ class WhoCountry(BlueprintLocation): processed_update = db.Column(db.Boolean, nullable=False) processed_full_update = db.Column(db.Boolean, nullable=False) - def __str__(self): - return self.location_group.__str__() + " : "+ self.location_code + " | " + self.location - @classmethod + @cache.memoize(50) def find_by_location_code_and_location_and_location_group(cls, location_code: str, location: str, location_group: WhoCountryRegion): return db.session.query(cls).filter( and_( @@ -85,6 +95,7 @@ class WhoCountry(BlueprintLocation): ).one_or_none() @classmethod + @cache.memoize(50) def get_by_location_code_and_location_and_location_group(cls, location_code: str, location: str, location_group: WhoCountryRegion): return db.session.query(cls).filter( and_( @@ -95,6 +106,7 @@ class WhoCountry(BlueprintLocation): ).one() @classmethod + @cache.memoize(50) def find_by_location_code_and_location(cls, location_code: str, location: str): return db.session.query(cls).filter( and_( @@ -104,6 +116,7 @@ class WhoCountry(BlueprintLocation): ).one_or_none() @classmethod + @cache.memoize(50) def get_by_location_code_and_location(cls, location_code: str, location: str): return db.session.query(cls).filter( and_( @@ -113,12 +126,14 @@ class WhoCountry(BlueprintLocation): ).one() @classmethod + @cache.memoize(50) def find_by_location_group(cls, location_group: WhoCountryRegion): return db.session.query(cls).filter( cls.location_group == location_group ).order_by(cls.location).all() @classmethod + @cache.memoize(50) def get_by_location_group(cls, location_group: WhoCountryRegion, page: int): return db.session.query(cls).filter( cls.location_group == location_group @@ -128,6 +143,12 @@ class WhoCountry(BlueprintLocation): class WhoData(BlueprintFactTable): __tablename__ = 'who' __mapper_args__ = {'concrete': True} + __table_args__ = ( + db.UniqueConstraint('date_reported_id', 'location_id', name="uix_who"), + ) + + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) id = db.Column(db.Integer, primary_key=True) date_reported_id = db.Column(db.Integer, db.ForeignKey('who_date_reported.id'), nullable=False) @@ -158,6 +179,7 @@ class WhoData(BlueprintFactTable): return None @classmethod + @cache.memoize(50) def __query_by_date_reported(cls, date_reported: WhoDateReported): return db.session.query(cls).filter( cls.date_reported_id == date_reported.id @@ -172,6 +194,7 @@ class WhoData(BlueprintFactTable): ) @classmethod + @cache.memoize(50) def __query_by_location(cls, location: WhoCountry): return db.session.query(cls).filter( cls.location_id == location.id @@ -181,6 +204,7 @@ class WhoData(BlueprintFactTable): ) @classmethod + @cache.memoize(50) def __query_by_date_reported_and_location(cls, date_reported: WhoDateReported, location: WhoCountry): return db.session.query(cls).filter( and_( @@ -190,30 +214,37 @@ class WhoData(BlueprintFactTable): ) @classmethod + @cache.memoize(50) def find_by_date_reported(cls, date_reported: WhoDateReported): return cls.__query_by_date_reported(date_reported).all() @classmethod + @cache.memoize(50) def get_by_date_reported(cls, date_reported: WhoDateReported, page: int): return cls.__query_by_date_reported(date_reported).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_by_location(cls, location: WhoCountry, page: int): return cls.__query_by_location(location).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_location(cls, location: WhoCountry): return cls.__query_by_location(location).all() @classmethod + @cache.memoize(50) def find_by_date_reported_and_location(cls, date_reported: WhoDateReported, location: WhoCountry): return cls.__query_by_date_reported_and_location(date_reported, location).one_or_none() @classmethod + @cache.memoize(50) def get_by_date_reported_and_location(cls, date_reported: WhoDateReported, location: WhoCountry, page: int): return cls.__query_by_date_reported_and_location(date_reported, location).one() @classmethod + @cache.memoize(50) def get_by_date_reported_order_by_cases_new(cls, date_reported: WhoDateReported, page): return db.session.query(cls).filter( cls.date_reported_id == date_reported.id @@ -225,6 +256,7 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_by_date_reported_order_by_cases_cumulative(cls, date_reported: WhoDateReported, page): return db.session.query(cls).filter( cls.date_reported_id == date_reported.id @@ -236,6 +268,7 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_by_date_reported_order_by_deaths_new(cls, date_reported: WhoDateReported, page): return db.session.query(cls).filter( cls.date_reported_id == date_reported.id @@ -247,6 +280,7 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_by_date_reported_order_by_deaths_cumulative(cls, date_reported: WhoDateReported, page): return db.session.query(cls).filter( cls.date_reported_id == date_reported.id @@ -258,6 +292,7 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_by_location_order_by_cases_new(cls, location: WhoCountry, page): return db.session.query(cls).filter( cls.location_id == location.id @@ -269,6 +304,7 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def get_by_location_order_by_cases_cumulative(cls, location: WhoDateReported, page): return db.session.query(cls).filter( cls.location_id == location.id @@ -302,10 +338,12 @@ class WhoData(BlueprintFactTable): ) @classmethod + @cache.memoize(50) def get_by_location_order_by_deaths_cumulative(cls, location: WhoDateReported, page): return cls.__query_by_location_order_by_deaths_cumulative(location).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod + @cache.memoize(50) def find_by_location_order_by_deaths_cumulative(cls, location: WhoDateReported): return cls.__query_by_location_order_by_deaths_cumulative(location).all() diff --git a/src/flask_covid19/blueprints/data_who/who_model_import.py b/src/flask_covid19/blueprints/data_who/who_model_import.py index 89437cda0940dcd7cce1d769d12e6fafe0c09d6f..ffdfe6b0572311ebe00b82ecae30c07aaf682320 100644 --- a/src/flask_covid19/blueprints/data_who/who_model_import.py +++ b/src/flask_covid19/blueprints/data_who/who_model_import.py @@ -1,5 +1,5 @@ from sqlalchemy.orm import Bundle -from database import db, ITEMS_PER_PAGE +from database import db, ITEMS_PER_PAGE, cache from flask_covid19.blueprints.app_all.all_model import AllImport, AllFlat @@ -7,6 +7,9 @@ class WhoImport(AllImport): __tablename__ = 'who_import' __mapper_args__ = {'concrete': True} + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + def __str__(self): return self.datum.isoformat() + " " + self.country_code + " " + self.country + " " + str(self.row_imported) @@ -25,18 +28,21 @@ class WhoImport(AllImport): date_reported = db.Column(db.String(255), nullable=False) @classmethod + @cache.memoize(50) def get_regions(cls): return db.session.query(cls.who_region)\ .order_by(cls.who_region)\ .distinct().all() @classmethod + @cache.memoize(50) def get_dates_reported(cls): return db.session.query(cls.date_reported)\ .order_by(cls.date_reported.desc())\ .distinct().all() @classmethod + @cache.memoize(50) def get_for_one_day(cls, day): return db.session.query(cls)\ .filter(cls.date_reported == day)\ @@ -44,6 +50,7 @@ class WhoImport(AllImport): .all() @classmethod + @cache.memoize(50) def get_dates_reported_as_string_array(cls): myresultarray = [] myresultset = db.session.query(cls.datum)\ @@ -128,16 +135,19 @@ class WhoImport(AllImport): return new_dates @classmethod + @cache.memoize(50) def get_new_dates_as_array(cls): #return cls.__get_new_dates_as_array_sql() return cls.__get_new_dates_as_array_orm() @classmethod + @cache.memoize(50) def countries(cls): bu = Bundle('countries', cls.country_code, cls.country, cls.who_region) return db.session.query(bu).distinct() @classmethod + @cache.memoize(50) def get_datum_of_all_who_import(cls): dates_reported = [] bu = Bundle('dates_reported', cls.date_reported) @@ -152,6 +162,9 @@ class WhoFlat(AllFlat): __tablename__ = 'who_import_flat' __mapper_args__ = {'concrete': True} + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, self.id) + def __str__(self): return self.datum.isoformat() + " " + self.location_code + " " + self.location + " " + str(self.location_group) diff --git a/src/flask_covid19/blueprints/data_who/who_views.py b/src/flask_covid19/blueprints/data_who/who_views.py index 54c19e12379c528ad7ca9634f2f1ed037e8bc47c..a31409378c4c00acb838110f57d728cc24ffca07 100644 --- a/src/flask_covid19/blueprints/data_who/who_views.py +++ b/src/flask_covid19/blueprints/data_who/who_views.py @@ -5,7 +5,7 @@ from celery.utils.log import get_task_logger from flask_admin.contrib.sqla import ModelView from flask_login import login_required -from database import app, admin, db, celery +from database import app, admin, db, celery, cache from flask_covid19.blueprints.app_web.web_dispachter_matrix_service import who_service from flask_covid19.blueprints.app_web.web_model_transient import WebPageContent @@ -31,6 +31,7 @@ admin.add_view(ModelView(WhoData, db.session, category="WHO")) @app_who.route('/info') +@cache.cached(timeout=50) def url_who_info(): page_info = WebPageContent('WHO', "Info") return render_template( @@ -39,6 +40,7 @@ def url_who_info(): @app_who.route('/tasks') +@cache.cached(timeout=50) @login_required def url_who_tasks(): page_info = WebPageContent('WHO', "Tasks") @@ -49,6 +51,7 @@ def url_who_tasks(): @app_who.route('/imported/page/<int:page>') @app_who.route('/imported') +@cache.memoize(50) @login_required def url_who_imported(page=1): page_info = WebPageContent('WHO', "Last Import") @@ -65,6 +68,7 @@ def url_who_imported(page=1): @app_who.route('/flat/page/<int:page>') @app_who.route('/flat') +@cache.memoize(50) @login_required def url_who_flat(page=1): page_info = WebPageContent('WHO', "Flat") @@ -95,6 +99,7 @@ def url_who_date_reported_all(page: int = 1): @app_who.route('/date_reported/<int:date_reported_id>/page/<int:page>') @app_who.route('/date_reported/<int:date_reported_id>') +@cache.memoize(50) def url_who_date_reported(date_reported_id: int, page: int = 1): date_reported = WhoDateReported.get_by_id(date_reported_id) page_info = WebPageContent( @@ -116,6 +121,7 @@ def url_who_date_reported(date_reported_id: int, page: int = 1): @app_who.route('/date_reported/<int:date_reported_id>/cases_new/page/<int:page>') @app_who.route('/date_reported/<int:date_reported_id>/cases_new') +@cache.memoize(50) def url_who_date_reported_cases_new(date_reported_id: int, page: int = 1): date_reported = WhoDateReported.get_by_id(date_reported_id) page_info = WebPageContent( @@ -137,6 +143,7 @@ def url_who_date_reported_cases_new(date_reported_id: int, page: int = 1): @app_who.route('/date_reported/<int:date_reported_id>/cases_cumulative/page/<int:page>') @app_who.route('/date_reported/<int:date_reported_id>/cases_cumulative') +@cache.memoize(50) def url_who_date_reported_cases_cumulative(date_reported_id: int, page: int = 1): date_reported = WhoDateReported.get_by_id(date_reported_id) page_info = WebPageContent( @@ -158,6 +165,7 @@ def url_who_date_reported_cases_cumulative(date_reported_id: int, page: int = 1) @app_who.route('/date_reported/<int:date_reported_id>/deaths_new/page/<int:page>') @app_who.route('/date_reported/<int:date_reported_id>/deaths_new') +@cache.memoize(50) def url_who_date_reported_deaths_new(date_reported_id: int, page: int = 1): date_reported = WhoDateReported.get_by_id(date_reported_id) page_info = WebPageContent( @@ -179,6 +187,7 @@ def url_who_date_reported_deaths_new(date_reported_id: int, page: int = 1): @app_who.route('/date_reported/<int:date_reported_id>/deaths_cumulative/page/<int:page>') @app_who.route('/date_reported/<int:date_reported_id>/deaths_cumulative') +@cache.memoize(50) def url_who_date_reported_deaths_cumulative(date_reported_id: int, page: int = 1): date_reported = WhoDateReported.get_by_id(date_reported_id) page_info = WebPageContent( @@ -200,6 +209,7 @@ def url_who_date_reported_deaths_cumulative(date_reported_id: int, page: int = 1 @app_who.route('/region/all/page/<int:page>') @app_who.route('/region/all') +@cache.memoize(50) def url_who_region_all(page: int = 1): page_info = WebPageContent('WHO', "Region", "All") try: @@ -236,6 +246,7 @@ def url_who_region(region_id: int, page: int = 1): @app_who.route('/country/all/page/<int:page>') @app_who.route('/country/all') +@cache.memoize(50) def url_who_country_all(page: int = 1): page_info = WebPageContent('WHO', "Countries", "All") try: @@ -251,6 +262,7 @@ def url_who_country_all(page: int = 1): @app_who.route('/country/<int:country_id>/page/<int:page>') @app_who.route('/country/<int:country_id>') +@cache.memoize(50) def url_who_country(country_id: int, page: int = 1): who_country = WhoCountry.get_by_id(country_id) page_data = WhoData.get_by_location(who_country, page) @@ -266,6 +278,7 @@ def url_who_country(country_id: int, page: int = 1): @app_who.route('/country/<int:country_id>/cases_new/page/<int:page>') @app_who.route('/country/<int:country_id>/cases_new') +@cache.memoize(50) def url_who_country_cases_new(country_id: int, page: int = 1): who_country = WhoCountry.get_by_id(country_id) page_data = WhoData.get_by_location_order_by_cases_new(who_country, page) @@ -281,6 +294,7 @@ def url_who_country_cases_new(country_id: int, page: int = 1): @app_who.route('/country/<int:country_id>/cases_cumulative/page/<int:page>') @app_who.route('/country/<int:country_id>/cases_cumulative') +@cache.memoize(50) def url_who_country_cases_cumulative(country_id: int, page: int = 1): who_country = WhoCountry.get_by_id(country_id) page_data = WhoData.get_by_location_order_by_cases_cumulative(who_country, page) @@ -296,6 +310,7 @@ def url_who_country_cases_cumulative(country_id: int, page: int = 1): @app_who.route('/country/<int:country_id>/deaths_new/page/<int:page>') @app_who.route('/country/<int:country_id>/deaths_new') +@cache.memoize(50) def url_who_country_deaths_new(country_id: int, page: int = 1): who_country = WhoCountry.get_by_id(country_id) page_data = WhoData.get_by_location_order_by_deaths_new(who_country, page) @@ -311,6 +326,7 @@ def url_who_country_deaths_new(country_id: int, page: int = 1): @app_who.route('/country/<int:country_id>/deaths_cumulative/page/<int:page>') @app_who.route('/country/<int:country_id>/deaths_cumulative') +@cache.memoize(50) def url_who_country_deaths_cumulative(country_id: int, page: int = 1): who_country = WhoCountry.get_by_id(country_id) page_data = WhoData.get_by_location_order_by_deaths_cumulative(who_country, page) @@ -326,6 +342,7 @@ def url_who_country_deaths_cumulative(country_id: int, page: int = 1): @app_who.route('/germany/page/<int:page>') @app_who.route('/germany') +@cache.memoize(50) def url_who_germany(page: int = 1): page_info = WebPageContent('WHO', "Germany") who_country_germany = WhoCountry.find_by_location_code("DE")