From 72b6cddea49e0f9724778a6992f45fe4d0a4e1ac Mon Sep 17 00:00:00 2001 From: guille Date: Fri, 24 Nov 2023 08:02:01 +0100 Subject: [PATCH 1/7] change result structure (concept proof) --- tests/test_db_factory.py | 60 ++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/tests/test_db_factory.py b/tests/test_db_factory.py index 4c216ad2..fe5b329e 100644 --- a/tests/test_db_factory.py +++ b/tests/test_db_factory.py @@ -248,36 +248,36 @@ TestDBFactory for x in building.onsite_electrical_production[cte.MONTH]] yearly_on_site_electrical_production = [x * cte.WATTS_HOUR_TO_JULES for x in building.onsite_electrical_production[cte.YEAR]] - results = json.dumps({cte.INSEL_MEB: [ - {'monthly_cooling_peak_load': monthly_cooling_peak_load}, - {'yearly_cooling_peak_load': yearly_cooling_peak_load}, - {'monthly_heating_peak_load': monthly_heating_peak_load}, - {'yearly_heating_peak_load': yearly_heating_peak_load}, - {'monthly_lighting_peak_load': monthly_lighting_peak_load}, - {'yearly_lighting_peak_load': yearly_lighting_peak_load}, - {'monthly_appliances_peak_load': monthly_appliances_peak_load}, - {'yearly_appliances_peak_load': yearly_appliances_peak_load}, - {'monthly_cooling_demand': monthly_cooling_demand}, - {'yearly_cooling_demand': yearly_cooling_demand}, - {'monthly_heating_demand': monthly_heating_demand}, - {'yearly_heating_demand': yearly_heating_demand}, - {'monthly_lighting_electrical_demand': monthly_lighting_electrical_demand}, - {'yearly_lighting_electrical_demand': yearly_lighting_electrical_demand}, - {'monthly_appliances_electrical_demand': monthly_appliances_electrical_demand}, - {'yearly_appliances_electrical_demand': yearly_appliances_electrical_demand}, - {'monthly_domestic_hot_water_heat_demand': monthly_domestic_hot_water_heat_demand}, - {'yearly_domestic_hot_water_heat_demand': yearly_domestic_hot_water_heat_demand}, - {'monthly_heating_consumption': monthly_heating_consumption}, - {'yearly_heating_consumption': yearly_heating_consumption}, - {'monthly_cooling_consumption': monthly_cooling_consumption}, - {'yearly_cooling_consumption': yearly_cooling_consumption}, - {'monthly_domestic_hot_water_consumption': monthly_domestic_hot_water_consumption}, - {'yearly_domestic_hot_water_consumption': yearly_domestic_hot_water_consumption}, - {'monthly_distribution_systems_electrical_consumption': monthly_distribution_systems_electrical_consumption}, - {'yearly_distribution_systems_electrical_consumption': yearly_distribution_systems_electrical_consumption}, - {'monthly_on_site_electrical_production': monthly_on_site_electrical_production}, - {'yearly_on_site_electrical_production': yearly_on_site_electrical_production} - ]}) + results = {cte.INSEL_MEB: { + 'monthly_cooling_peak_load': monthly_cooling_peak_load, + 'yearly_cooling_peak_load': yearly_cooling_peak_load, + 'monthly_heating_peak_load': monthly_heating_peak_load, + 'yearly_heating_peak_load': yearly_heating_peak_load, + 'monthly_lighting_peak_load': monthly_lighting_peak_load, + 'yearly_lighting_peak_load': yearly_lighting_peak_load, + 'monthly_appliances_peak_load': monthly_appliances_peak_load, + 'yearly_appliances_peak_load': yearly_appliances_peak_load, + 'monthly_cooling_demand': monthly_cooling_demand, + 'yearly_cooling_demand': yearly_cooling_demand, + 'monthly_heating_demand': monthly_heating_demand, + 'yearly_heating_demand': yearly_heating_demand, + 'monthly_lighting_electrical_demand': monthly_lighting_electrical_demand, + 'yearly_lighting_electrical_demand': yearly_lighting_electrical_demand, + 'monthly_appliances_electrical_demand': monthly_appliances_electrical_demand, + 'yearly_appliances_electrical_demand': yearly_appliances_electrical_demand, + 'monthly_domestic_hot_water_heat_demand': monthly_domestic_hot_water_heat_demand, + 'yearly_domestic_hot_water_heat_demand': yearly_domestic_hot_water_heat_demand, + 'monthly_heating_consumption': monthly_heating_consumption, + 'yearly_heating_consumption': yearly_heating_consumption, + 'monthly_cooling_consumption': monthly_cooling_consumption, + 'yearly_cooling_consumption': yearly_cooling_consumption, + 'monthly_domestic_hot_water_consumption': monthly_domestic_hot_water_consumption, + 'yearly_domestic_hot_water_consumption': yearly_domestic_hot_water_consumption, + 'monthly_distribution_systems_electrical_consumption': monthly_distribution_systems_electrical_consumption, + 'yearly_distribution_systems_electrical_consumption': yearly_distribution_systems_electrical_consumption, + 'monthly_on_site_electrical_production': monthly_on_site_electrical_production, + 'yearly_on_site_electrical_production': yearly_on_site_electrical_production + }} db_building_id = _building.id city_objects_id.append(db_building_id) From 914375171a871ecffc919d83568cbb5d9dd1c54d Mon Sep 17 00:00:00 2001 From: guille Date: Fri, 24 Nov 2023 08:03:55 +0100 Subject: [PATCH 2/7] remove useless files --- .gitignore | 2 +- cerc_hub.egg-info/.gitignore | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 cerc_hub.egg-info/.gitignore diff --git a/.gitignore b/.gitignore index e23b0042..a456a415 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ **/hub/logs/ **/__pycache__/ **/.idea/ - +cerc_hub.egg-info diff --git a/cerc_hub.egg-info/.gitignore b/cerc_hub.egg-info/.gitignore deleted file mode 100644 index e0a497ab..00000000 --- a/cerc_hub.egg-info/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Except this file -* -!.gitignore \ No newline at end of file From 802641b4a39cb4f9b6d766d1b2e1f2f52a71de89 Mon Sep 17 00:00:00 2001 From: Guille Gutierrez Date: Fri, 24 Nov 2023 02:04:37 -0500 Subject: [PATCH 3/7] Update hub/version.py --- hub/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hub/version.py b/hub/version.py index c6339f58..c2c410c1 100644 --- a/hub/version.py +++ b/hub/version.py @@ -1,4 +1,4 @@ """ Hub version number """ -__version__ = '0.1.8.31' +__version__ = '0.1.8.32' From b51d49d598811df1479472059317f4d93727185d Mon Sep 17 00:00:00 2001 From: guille Date: Fri, 24 Nov 2023 09:01:49 +0100 Subject: [PATCH 4/7] correct persistence json read --- hub/persistence/db_control.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hub/persistence/db_control.py b/hub/persistence/db_control.py index 259b9dcf..4ac5f95a 100644 --- a/hub/persistence/db_control.py +++ b/hub/persistence/db_control.py @@ -144,7 +144,7 @@ class DBControl: result_names) for value in _: - values = json.loads(value.values) + values = value.values values["building"] = building_name results[scenario_name].append(values) return results From 03da727d297f34369c624bda0cd6c7910e9acd50 Mon Sep 17 00:00:00 2001 From: Guille Gutierrez Date: Fri, 24 Nov 2023 03:03:20 -0500 Subject: [PATCH 5/7] Update hub/version.py --- hub/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hub/version.py b/hub/version.py index c2c410c1..d4ce5d34 100644 --- a/hub/version.py +++ b/hub/version.py @@ -1,4 +1,4 @@ """ Hub version number """ -__version__ = '0.1.8.32' +__version__ = '0.1.8.33' From 953b6796e9c3fc6e2bf9f8dc3b387ac66b6cbaed Mon Sep 17 00:00:00 2001 From: guille Date: Mon, 27 Nov 2023 07:00:39 +0100 Subject: [PATCH 6/7] correct persistence json read --- hub/persistence/db_control.py | 7 ++-- hub/persistence/repositories/city_object.py | 41 +++++++++++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/hub/persistence/db_control.py b/hub/persistence/db_control.py index 4ac5f95a..e060559b 100644 --- a/hub/persistence/db_control.py +++ b/hub/persistence/db_control.py @@ -76,10 +76,9 @@ class DBControl: """ cities = self._city.get_by_user_id_application_id_and_scenario(user_id, application_id, scenario) c = [c[0].id for c in cities] - for city in cities: - result = self.building_info(name, city[0].id) - if result is not None: - return result + result = self._city_object.building_in_cities_info(name, c) + if result is not None: + return result return None def building_info(self, name, city_id) -> CityObject: diff --git a/hub/persistence/repositories/city_object.py b/hub/persistence/repositories/city_object.py index 3fd5c62c..bba01ef5 100644 --- a/hub/persistence/repositories/city_object.py +++ b/hub/persistence/repositories/city_object.py @@ -6,14 +6,15 @@ Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca """ import datetime import logging +from typing import Union -from sqlalchemy import select, or_ +from sqlalchemy import select from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Session from hub.city_model_structure.building import Building -from hub.persistence.repository import Repository from hub.persistence.models import CityObject as Model +from hub.persistence.repository import Repository class CityObject(Repository): @@ -100,7 +101,41 @@ class CityObject(Repository): logging.error('Error while deleting application %s', err) raise SQLAlchemyError from err - def get_by_name_or_alias_and_city(self, name, city_id) -> Model: + def building_in_cities_info(self, name, cities): + """ + Fetch a city object based on name and city id + :param name: city object name + :param cities: city identifiers + :return: [CityObject] with the provided name or alias belonging to the city with id city_id + """ + try: + # search by name first + with Session(self.engine) as session: + city_object = session.execute(select(Model).where( + Model.name == name, Model.city_id.in_(cities)) + ).first() + if city_object is not None: + return city_object[0] + # name not found, so search by alias instead + city_objects = session.execute( + select(Model).where(Model.aliases.contains(name), Model.city_id.in_(cities)) + ).all() + for city_object in city_objects: + aliases = city_object[0].aliases.replace('{', '').replace('}', '').split(',') + for alias in aliases: + if alias == name: + # force the name as the alias + city_object[0].name = name + return city_object[0] + return None + except SQLAlchemyError as err: + logging.error('Error while fetching city object by name and city: %s', err) + raise SQLAlchemyError from err + except IndexError as err: + logging.error('Error while fetching city object by name and city, empty result %s', err) + raise IndexError from err + + def get_by_name_or_alias_and_city(self, name, city_id) -> Union[Model, None]: """ Fetch a city object based on name and city id :param name: city object name From 716465f1ebb0f9f53b92348a64c99ba0f994c543 Mon Sep 17 00:00:00 2001 From: Guille Gutierrez Date: Mon, 27 Nov 2023 01:01:59 -0500 Subject: [PATCH 7/7] Update hub/version.py --- hub/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hub/version.py b/hub/version.py index d4ce5d34..3021db4a 100644 --- a/hub/version.py +++ b/hub/version.py @@ -1,4 +1,4 @@ """ Hub version number """ -__version__ = '0.1.8.33' +__version__ = '0.1.8.34'