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 diff --git a/hub/persistence/db_control.py b/hub/persistence/db_control.py index 259b9dcf..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: @@ -144,7 +143,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 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 diff --git a/hub/version.py b/hub/version.py index c6339f58..3021db4a 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.34' 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)