From d5f8f21308d68d6fc2d51acbecbfc84099db46c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gi=C3=B2=20Diani?= Date: Sat, 26 Oct 2024 18:02:53 +0200 Subject: [PATCH] some more queries from consultancy 1 --- src/data/database.py | 67 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/src/data/database.py b/src/data/database.py index 63e1af7..e788613 100644 --- a/src/data/database.py +++ b/src/data/database.py @@ -9,11 +9,72 @@ class Database: return self.connection.sql("DESCRIBE;").show() def seeds(self): - return self.connection.sql("SELECT regions.name, seeds.uri FROM consultancy_d.regions LEFT JOIN consultancy_d.seeds ON regions.id = seeds.region_id;").show() + return self.connection.sql(""" + SELECT + regions.name, + seeds.uri + FROM + consultancy_d.regions + LEFT JOIN + consultancy_d.seeds ON regions.id = seeds.region_id; + """).show() def properties_growth(self): - return self.connection.sql("SELECT strftime(created_at, '%Y-%m-%d') AS date, COUNT(*) as properties_count FROM consultancy_d.properties GROUP BY date;").show() + return self.connection.sql(""" + SELECT + strftime(created_at, '%Y-%m-%d') AS date, + COUNT(*) as properties_count + FROM + consultancy_d.properties + GROUP BY + date; + """).show() def properties_per_region(self): - return self.connection.sql("SELECT regions.name, COUNT(*) AS count_properties FROM consultancy_d.properties LEFT JOIN consultancy_d.seeds ON seeds.id = properties.seed_id LEFT JOIN consultancy_d.regions ON regions.id = seeds.region_id GROUP BY properties.seed_id, regions.name").show() + return self.connection.sql(""" + SELECT + regions.name, + COUNT(*) AS count_properties + FROM + consultancy_d.properties + LEFT JOIN + consultancy_d.seeds ON seeds.id = properties.seed_id + LEFT JOIN + consultancy_d.regions ON regions.id = seeds.region_id + GROUP BY + properties.seed_id, + regions.name + """).show() + + def properties_unreachable(self): + return self.connection.sql(""" + SELECT + entity_id, + strftime(properties.created_at, '%Y-%m-%d') AS first_found, + strftime(properties.last_found, '%Y-%m-%d') AS last_found + FROM + consultancy_d.exceptions + LEFT JOIN + consultancy_d.properties ON properties.id = exceptions.entity_id + WHERE + JSON_VALID(exception) = true AND + JSON_EXTRACT(exception, '$.status') = '404' + GROUP BY ALL + ORDER BY + last_found + """).show() + + def properties_not_found(self): + return self.connection.sql(""" + SELECT + COUNT(entity_id) as count_props, + strftime(created_at, '%Y-%m-%d') as date + FROM + consultancy_d.exceptions + WHERE + JSON_VALID(exception) = true AND + JSON_EXTRACT(exception, '$.status') > 400 + GROUP BY + date + """).show()