From 2457c9a7645bd84d7f12822a683d648e1c844d4d Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 11 Sep 2018 11:53:18 -0400 Subject: [PATCH 1/5] Add new stored procedure to model. --- app/models/building.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/building.py b/app/models/building.py index d95071a..1a72e36 100644 --- a/app/models/building.py +++ b/app/models/building.py @@ -8,6 +8,7 @@ class Building(BaseModel): PROCS = { 'READ': 'building_search', + 'BALT': 'baltimore_building_get', } __table__ = ProcTable( -- GitLab From a2ce4a4e30b333f49769615f8730a6063c6f8714 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 11 Sep 2018 11:54:12 -0400 Subject: [PATCH 2/5] Use new stored procedure to find Baltimore buildings. --- app/controllers/building.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/controllers/building.py b/app/controllers/building.py index 84a197c..ed63c05 100644 --- a/app/controllers/building.py +++ b/app/controllers/building.py @@ -46,7 +46,6 @@ class BuildingController(RestController): Returns: dict: Building object """ - try: building_list = proc(self.Model, self.Model.PROCS['READ'], **{'building_id': id_}) except Exception as err: @@ -56,7 +55,16 @@ class BuildingController(RestController): ) if not building_list: - raise NotFound + # Hack to try using get by id for Baltimore by using new stored procedure aka postres function + try: + building_list = proc(self.Model, self.Model.PROCS['BALT'], **{'building_id': id_}) + except Exception as err: + raise ( + BadRequest(str(err)) if current_app.config['DEBUG'] else + BadRequest('Error while executing db call') + ) + if not building_list: + raise NotFound address_list = [] for building in building_list: address_list.append(building.street_address) -- GitLab From d5389bd1e4e28c9d8789e136d383258d80a98f12 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 11 Sep 2018 17:23:52 -0400 Subject: [PATCH 3/5] Retrieving bgroup buildings and their addresses when building not in materialized view. --- app/controllers/bgroup.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/controllers/bgroup.py b/app/controllers/bgroup.py index e9d112a..c423a49 100644 --- a/app/controllers/bgroup.py +++ b/app/controllers/bgroup.py @@ -78,5 +78,23 @@ class BuildingBGroupController(RestController): GROUP BY building_bgroup.building_id, id '''.format(bgroup_id) + # Query for buildings that are not in the materialized view aka Baltimore right now + alt_query = ''' + SELECT + bg.id as id, + bg.building_id, + string_agg(concat(a.house_number, ' ', a.street_name) || ' ' || zipcode, ', ') AS address_list + FROM groups.building_bgroup bg + JOIN public.building b on bg.building_id = b.id + join public.building_address ba on b.id = ba.building_id + join public.address a on ba.address_id = a.id + WHERE bgroup_id = {} + GROUP BY bg.building_id, bg.id; + '''.format(bgroup_id) + results = self.db.session.execute(query) + + # Use alternate query for Baltimore group + if not results: + results = self.db.session.execute(alt_query) return [dict(row) for row in results] -- GitLab From 74264b6154d99cb7f776385a2225e543ca70ddb5 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 11 Sep 2018 17:38:38 -0400 Subject: [PATCH 4/5] Modify if statement check. --- app/controllers/bgroup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/bgroup.py b/app/controllers/bgroup.py index c423a49..96b40bb 100644 --- a/app/controllers/bgroup.py +++ b/app/controllers/bgroup.py @@ -95,6 +95,6 @@ class BuildingBGroupController(RestController): results = self.db.session.execute(query) # Use alternate query for Baltimore group - if not results: + if len(results) == 0: results = self.db.session.execute(alt_query) return [dict(row) for row in results] -- GitLab From 4012af52bafed50eb27545d34e62c32ac0d5c0df Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 11 Sep 2018 17:55:26 -0400 Subject: [PATCH 5/5] Fetch all to check size. --- app/controllers/bgroup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/bgroup.py b/app/controllers/bgroup.py index 96b40bb..ec911ab 100644 --- a/app/controllers/bgroup.py +++ b/app/controllers/bgroup.py @@ -92,7 +92,7 @@ class BuildingBGroupController(RestController): GROUP BY bg.building_id, bg.id; '''.format(bgroup_id) - results = self.db.session.execute(query) + results = self.db.session.execute(query).fetchall() # Use alternate query for Baltimore group if len(results) == 0: -- GitLab