From d8d065b3a76eb976c8d45c0eccc4c436cc97902a Mon Sep 17 00:00:00 2001
From: thomaswoehlke <thomas.woehlke@gmail.com>
Date: Sun, 17 Jan 2021 00:19:51 +0100
Subject: [PATCH] ### 0.0.7 Release * Issue #8
 WhoServiceUpdate.update_db_short() * Issue #9 URL: /who/update/short * Issue
 #10 async who_update_short_task * Issue #11
 WhoServiceUpdate.__update_who_global_data_short() * Fixed #12 better layout
 for flash messages

---
 README.md                                     | 115 ++++++++++++++++++
 etc/src_sql/view02.sql                        |  14 +++
 etc/src_sql/view03.sql                        |   3 +
 org/woehlke/covid19/who/who_model.py          |  46 ++++---
 org/woehlke/covid19/who/who_service_update.py |  12 +-
 5 files changed, 166 insertions(+), 24 deletions(-)
 create mode 100644 etc/src_sql/view02.sql
 create mode 100644 etc/src_sql/view03.sql

diff --git a/README.md b/README.md
index 80bdbd5f..ac5a39c8 100644
--- a/README.md
+++ b/README.md
@@ -29,6 +29,121 @@
 * http://www.leeladharan.com/sqlalchemy-query-with-or-and-like-common-filters
 * https://riptutorial.com/flask/example/22201/pagination-route-example-with-flask-sqlalchemy-paginate
 
+## Database
+
+### WHO
+
+#### who_date_reported
+````postgresql
+CREATE TABLE public.who_date_reported (
+    id integer NOT NULL,
+    date_reported character varying(255) NOT NULL
+);
+````
+````python
+class WhoDateReported(db.Model):
+    __tablename__ = 'who_date_reported'
+
+    id = db.Column(db.Integer, primary_key=True)
+    date_reported = db.Column(db.String(255), unique=True, nullable=False)
+````
+
+#### who_region
+````postgresql
+CREATE TABLE public.who_region (
+    id integer NOT NULL,
+    region character varying(255) NOT NULL
+);
+````
+````python
+class WhoRegion(db.Model):
+    __tablename__ = 'who_region'
+
+    id = db.Column(db.Integer, primary_key=True)
+    region = db.Column(db.String(255), unique=True, nullable=False)
+````
+
+#### who_country
+````postgresql
+CREATE TABLE public.who_country (
+    id integer NOT NULL,
+    country_code character varying(255) NOT NULL,
+    country character varying(255) NOT NULL,
+    region_id integer NOT NULL
+);
+````
+````python
+class WhoCountry(db.Model):
+    __tablename__ = 'who_country'
+
+    id = db.Column(db.Integer, primary_key=True)
+    country_code = db.Column(db.String(255), unique=True, nullable=False)
+    country = db.Column(db.String(255), unique=False, nullable=False)
+    region_id = db.Column(db.Integer, db.ForeignKey('who_region.id'), nullable=False)
+    region = db.relationship('WhoRegion', lazy='joined') 
+````
+
+#### who_global_data
+````postgresql
+CREATE TABLE public.who_global_data (
+    id integer NOT NULL,
+    cases_new integer NOT NULL,
+    cases_cumulative integer NOT NULL,
+    deaths_new integer NOT NULL,
+    deaths_cumulative integer NOT NULL,
+    date_reported_id integer NOT NULL,
+    country_id integer NOT NULL
+);
+````
+````python
+class WhoGlobalData(db.Model):
+    __tablename__ = 'who_global_data'
+
+    id = db.Column(db.Integer, primary_key=True)
+    cases_new = db.Column(db.Integer, nullable=False)
+    cases_cumulative = db.Column(db.Integer, nullable=False)
+    deaths_new = db.Column(db.Integer, nullable=False)
+    deaths_cumulative = db.Column(db.Integer, nullable=False)
+
+    date_reported_id = db.Column(db.Integer, db.ForeignKey('who_date_reported.id'), nullable=False)
+    date_reported = db.relationship('WhoDateReported', lazy='joined')
+
+    country_id = db.Column(db.Integer, db.ForeignKey('who_country.id'), nullable=False)
+    country = db.relationship('WhoCountry', lazy='joined')
+````
+
+#### who_global_data_import
+````postgresql
+CREATE TABLE public.who_global_data_import (
+    id integer NOT NULL,
+    date_reported character varying(255) NOT NULL,
+    country_code character varying(255) NOT NULL,
+    country character varying(255) NOT NULL,
+    who_region character varying(255) NOT NULL,
+    new_cases character varying(255) NOT NULL,
+    cumulative_cases character varying(255) NOT NULL,
+    new_deaths character varying(255) NOT NULL,
+    cumulative_deaths character varying(255) NOT NULL,
+    row_imported boolean NOT NULL
+);
+````
+````python
+class WhoGlobalDataImportTable(db.Model):
+    __tablename__ = 'who_global_data_import'
+
+    id = db.Column(db.Integer, primary_key=True)
+    date_reported = db.Column(db.String(255), nullable=False)
+    country_code = db.Column(db.String(255), nullable=False)
+    country = db.Column(db.String(255), nullable=False)
+    who_region = db.Column(db.String(255), nullable=False)
+    new_cases = db.Column(db.String(255), nullable=False)
+    cumulative_cases = db.Column(db.String(255), nullable=False)
+    new_deaths = db.Column(db.String(255), nullable=False)
+    cumulative_deaths = db.Column(db.String(255), nullable=False)
+    row_imported = db.Column(db.Boolean, nullable=False)
+````
+
+
 ## Milestones
 
 ### 0.0.1 Release
diff --git a/etc/src_sql/view02.sql b/etc/src_sql/view02.sql
new file mode 100644
index 00000000..db0a22f9
--- /dev/null
+++ b/etc/src_sql/view02.sql
@@ -0,0 +1,14 @@
+select date_reported as my_date from who_global_data_import group by date_reported order by date_reported desc ;
+
+select distinct d.date_reported as my_date from who_global_data as i left join who_date_reported as d on i.date_reported_id=d.id order by d.date_reported desc ;
+
+select * from who_global_data where date_reported_id = 226 order by country_id;
+
+delete from who_global_data where date_reported_id = 226;
+delete from who_global_data where date_reported_id = 284;
+
+
+
+
+
+
diff --git a/etc/src_sql/view03.sql b/etc/src_sql/view03.sql
new file mode 100644
index 00000000..7b18f69e
--- /dev/null
+++ b/etc/src_sql/view03.sql
@@ -0,0 +1,3 @@
+select date_reported as my_date from who_global_data_import group by date_reported not in (
+select distinct d.date_reported as my_date from who_global_data as i left join who_date_reported as d on i.date_reported_id=d.id
+) order by date_reported desc;
diff --git a/org/woehlke/covid19/who/who_model.py b/org/woehlke/covid19/who/who_model.py
index 15321834..85da919d 100644
--- a/org/woehlke/covid19/who/who_model.py
+++ b/org/woehlke/covid19/who/who_model.py
@@ -262,23 +262,35 @@ class WhoGlobalDataImportTable(db.Model):
     def get_dates_reported(cls):
         return db.session.query(cls.date_reported).distinct()
 
-    @classmethod
-    def get_jounger_dates(cls, joungest_date_old):
-        return db.session.query(cls).filter(cls.date_reported > joungest_date_old).all()
-
-    @classmethod
-    def get_new_dates_reported_as_dict(cls):
-        new_dates_reported = []
-        joungest_date_old = WhoDateReported.get_joungest_date()
-        jounger_dates = None
-        if joungest_date_old is None:
-            jounger_dates = cls.get_dates_reported()
-        else:
-            jounger_dates = cls.get_jounger_dates(joungest_date_old)
-        for jounger_date in jounger_dates:
-            new_dates_reported.append(jounger_date.date_reported)
-        return new_dates_reported
-
     @classmethod
     def get_for_one_day(cls, day):
         return db.session.query(cls).filter(cls.date_reported == day).all()
+
+    @classmethod
+    def get_new_dates_as_array(cls):
+        sql_query = """
+            select
+                date_reported
+            from
+                who_global_data_import
+            where
+                date_reported
+            not in (
+            select
+                distinct
+                    who_date_reported.date_reported
+                from
+                    who_global_data
+                left join
+                    who_date_reported
+                on
+                    who_global_data.date_reported_id=who_date_reported.id
+            )
+            group by
+                who_global_data_import.date_reported
+            order by date_reported desc
+            """
+        new_dates = []
+        for item in db.session.execute(sql_query):
+            new_dates.append(item['date_reported'])
+        return new_dates
diff --git a/org/woehlke/covid19/who/who_service_update.py b/org/woehlke/covid19/who/who_service_update.py
index d718032a..a455a96b 100644
--- a/org/woehlke/covid19/who/who_service_update.py
+++ b/org/woehlke/covid19/who/who_service_update.py
@@ -74,12 +74,10 @@ class WhoServiceUpdate:
         """
         result = db.session.execute(sql_text).fetchall()
         for result_item in result:
-            #print(result_item)
             i_country_code = result_item.country_code
             i_country = result_item.country
             i_who_region = result_item.who_region
             output = i_country_code + " " + i_country + " " + i_who_region
-            #logging.debug(output)
             my_region = WhoRegion.find_by_region(i_who_region)
             my_country = WhoCountry.find_by_country_code_and_country_and_who_region_id(
                 i_country_code, i_country, my_region
@@ -111,6 +109,9 @@ class WhoServiceUpdate:
         dates_reported = WhoDateReported.get_all_as_dict()
         regions = WhoRegion.get_all_as_dict()
         countries = WhoCountry.get_all_as_dict()
+
+        #
+        #
         i = 0
         result = WhoGlobalDataImportTable.get_all()
         for result_item in result:
@@ -143,14 +144,11 @@ class WhoServiceUpdate:
     def __update_who_global_data_short(self):
         app.logger.info(" update WHO short [begin]")
         app.logger.info("------------------------------------------------------------")
-        #dates_reported = WhoDateReported.get_all_as_dict()
-        #regions = WhoRegion.get_all_as_dict()
+        new_dates_reported_from_import = WhoGlobalDataImportTable.get_new_dates_as_array()
         countries = WhoCountry.get_all_as_dict()
         i = 0
-        new_dates_reported_from_import = WhoGlobalDataImportTable.get_new_dates_reported_as_dict()
         for my_date_reported in new_dates_reported_from_import:
-            result = WhoGlobalDataImportTable.get_for_one_day(my_date_reported)
-            for result_item in result:
+            for result_item in WhoGlobalDataImportTable.get_for_one_day(my_date_reported):
                 my_country = countries[result_item.country_code]
                 result_who_global_data = WhoGlobalData.find_one_or_none_by_date_and_country(
                     my_date_reported,
-- 
GitLab