diff --git a/hub/city_model_structure/city.py b/hub/city_model_structure/city.py index 1aebd170..5f4da399 100644 --- a/hub/city_model_structure/city.py +++ b/hub/city_model_structure/city.py @@ -459,6 +459,7 @@ class City: for surface in city_object.surfaces: radiation = surface.global_irradiance if 'year' not in radiation and 'month' not in radiation: + continue elif "year" in radiation: building_radiation += radiation["year"].iloc[0] @@ -468,11 +469,11 @@ class City: if building_radiation < min_radiation: min_radiation = building_radiation selected_city_object = city_object - # merge the city object with the minimum radiation - if selected_city_object is not None: - _merge_city.add_city_object(selected_city_object) - else: - _merge_city.add_city_object(building) + # merge the city object with the minimum radiation + if selected_city_object is not None: + _merge_city.add_city_object(selected_city_object) + else: + _merge_city.add_city_object(building) return _merge_city @property diff --git a/hub/exports/db_factory.py b/hub/exports/db_factory.py index a0ddcda5..5f4fde9e 100644 --- a/hub/exports/db_factory.py +++ b/hub/exports/db_factory.py @@ -4,6 +4,9 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Concordia CERC group Project CoderPeter Yefi peteryefi@gmail.com """ +import json +from typing import Union, Dict + from hub.persistence import City from hub.persistence import Application from hub.persistence import User @@ -23,23 +26,53 @@ class DBFactory: self._city_object = CityObject(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) self._simulation_results = SimulationResults(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env) - def application_info(self, application_uuid): + def application_info(self, application_uuid) -> Union[Application, None]: """ Retrieve the application info for the given uuid :param application_uuid: the uuid for the application + :return: Application or None """ return self._application.get_by_uuid(application_uuid) + def user_info(self, name, password, application_id): + """ + Retrieve the user info for the given name and password and application_id + :param name: the user name + :param password: the user password + :param application_id: the application id + :return: User or None + """ + return self._user.get_by_name_application_id_and_password(name, password, application_id) + def user_login(self, name, password, application_uuid): """ Retrieve the user info :param name: the user name :param password: the user password :param application_uuid: the application uuid + :return: User or None """ return self._user.get_by_name_application_uuid_and_password(name, password, application_uuid) - def results(self, user_id, application_id, cities, result_names=[]): + def cities_by_user_and_application(self, user_id, application_id) -> [City]: + """ + Retrieve the cities belonging to the user and the application + :param user_id: User id + :param application_id: Application id + :return: [City] + """ + return self._city.get_by_user_id_and_application_id(user_id, application_id) + + def building_info(self, name, city_id) -> Union[CityObject, None]: + """ + Retrieve the building info + :param name: Building name + :param city_id: City Id + :return: CityObject or None + """ + return self._city_object.get_by_name_and_city(name, city_id) + + def results(self, user_id, application_id, cities, result_names=None) -> Dict: """ Retrieve the simulation results for the given cities :param user_id: the user id owning the results @@ -47,16 +80,27 @@ class DBFactory: :param cities: dictionary containing the city and building names for the results :param result_names: if given, filter the results to the selected names """ - results = [] - for city in cities['cities'].keys(): - city_id = self._city.get_by_user_id_application_id_and_name(user_id, application_id, city).id - for building_name in cities[city]: + if result_names is None: + result_names = [] + results = {} + for city in cities['cities']: + city_name = next(iter(city)) + result_set = self._city.get_by_user_id_application_id_and_name(user_id, application_id, city_name) + if result_set is None: + continue + city_id = result_set.id + results[city_name] = [] + for building_name in city[city_name]: + if self._city_object.get_by_name_and_city(building_name, city_id) is None: + continue city_object_id = self._city_object.get_by_name_and_city(building_name, city_id).id - results.append(self._simulation_results.get_simulation_results_by_city_id_city_object_id_and_names( + _ = self._simulation_results.get_simulation_results_by_city_id_city_object_id_and_names( city_id, city_object_id, - result_names)) + result_names) + + for value in _: + values = json.loads(value.values) + values["building"] = building_name + results[city_name].append(values) return results - - - diff --git a/hub/persistence/repositories/city.py b/hub/persistence/repositories/city.py index 52e59204..a242286a 100644 --- a/hub/persistence/repositories/city.py +++ b/hub/persistence/repositories/city.py @@ -121,7 +121,24 @@ class City(Repository): result_set = self.session.execute(select(Model).where(Model.user_id == user_id, Model.application_id == application_id, Model.name == city_name - )).first()[0] + )).first() + if result_set is not None: + result_set = result_set[0] return result_set except SQLAlchemyError as err: logger.error(f'Error while fetching city by name: {err}') + + def get_by_user_id_and_application_id(self, user_id, application_id) -> [Model]: + """ + Fetch city based on the user who created it + :param user_id: the user id + :param application_id: the application id + :return: ModelCity + """ + try: + result_set = self.session.execute( + select(Model).where(Model.user_id == user_id, Model.application_id == application_id) + ) + return [r[0] for r in result_set] + except SQLAlchemyError as err: + logger.error(f'Error while fetching city by name: {err}') diff --git a/hub/persistence/repositories/city_object.py b/hub/persistence/repositories/city_object.py index ded9a1ad..7ca4d8e6 100644 --- a/hub/persistence/repositories/city_object.py +++ b/hub/persistence/repositories/city_object.py @@ -105,7 +105,7 @@ class CityObject(Repository): except SQLAlchemyError as err: logger.error(f'Error while deleting application: {err}') - def get_by_name_and_city(self, name, city_id) -> [Model]: + def get_by_name_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/persistence/repositories/simulation_results.py b/hub/persistence/repositories/simulation_results.py index 53524bba..f02f48b9 100644 --- a/hub/persistence/repositories/simulation_results.py +++ b/hub/persistence/repositories/simulation_results.py @@ -10,6 +10,7 @@ from typing import Union, Dict from sqlalchemy import select from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy import or_ from hub.hub_logger import logger from hub.persistence import Repository @@ -98,7 +99,7 @@ class SimulationResults(Repository): Deletes an application with the application_uuid :param name: The simulation results tool and workflow name :param city_id: The id for the city owning the simulation results - :param city_object_id: the id for the city_object ownning these simulation results + :param city_object_id: the id for the city_object owning these simulation results :return: None """ @@ -125,28 +126,6 @@ class SimulationResults(Repository): except SQLAlchemyError as err: logger.error(f'Error while fetching city by city_id: {err}') - def get_simulation_results_by_city_id(self, city_id) -> [Model]: - """ - Fetch simulation results by name - :param city_id: the id of the city - :return: [Model] with the provided city id - """ - try: - return self.session.execute(select(Model).where(Model.city_id == city_id)) - except SQLAlchemyError as err: - logger.error(f'Error while fetching simulation results by name: {err}') - - def get_simulation_results_by_city_object_id(self, city_object_id) -> [Model]: - """ - Fetch simulation results by name - :param city_object_id: the id of the city object - :return: [Model] with the provided city object id - """ - try: - return self.session.execute(select(Model).where(Model.city_object_id == city_object_id)) - except SQLAlchemyError as err: - logger.error(f'Error while fetching simulation results by name: {err}') - def _get_city_object(self, city_object_id) -> [CityObject]: """ Fetch a city object based city id @@ -157,3 +136,27 @@ class SimulationResults(Repository): return self.session.execute(select(CityObject).where(CityObject.id == city_object_id)).first() except SQLAlchemyError as err: logger.error(f'Error while fetching city by city_id: {err}') + + def get_simulation_results_by_city_id_city_object_id_and_names(self, city_id, city_object_id, result_names=[]): + """ + Fetch the simulation results based in the city_id or city_object_id with the given names or all + :param city_id: the city id + :param city_object_id: the city object id + :param result_names: if given filter the results + :return: [SimulationResult] + """ + try: + result_set = self.session.execute(select(Model).where(or_( + Model.city_id == city_id, + Model.city_object_id == city_object_id + ))) + results = [r[0] for r in result_set] + if not result_names: + return results + _ = [] + for result in results: + if result.name in result_names: + _.append(result) + return _ + except SQLAlchemyError as err: + logger.error(f'Error while fetching city by city_id: {err}') diff --git a/hub/persistence/repositories/user.py b/hub/persistence/repositories/user.py index 87005a4a..cd6037ac 100644 --- a/hub/persistence/repositories/user.py +++ b/hub/persistence/repositories/user.py @@ -60,7 +60,7 @@ class User(Repository): :param name: the name of the user :param password: the password of the user :param role: the role of the user - :return: + :return: None, Dictionary """ try: self.session.query(Model).filter(Model.id == user_id).update({ @@ -86,28 +86,30 @@ class User(Repository): except SQLAlchemyError as err: logger.error(f'Error while fetching user: {err}') - def get_by_name_and_application(self, name: str, application_id: int) -> [Model]: + def get_by_name_and_application(self, name: str, application_id: int) -> Union[Model, None]: """ Fetch user based on the email address :param name: User name :param application_id: User application name - :return: [User] matching the search criteria + :return: User matching the search criteria or None """ try: - return self.session.execute( + user = self.session.execute( select(Model).where(Model.name == name, Model.application_id == application_id) ).first() + if user is not None: + user = user[0] + return user except SQLAlchemyError as err: logger.error(f'Error while fetching user by name and application: {err}') - def get_by_name_application_id_and_password(self, name: str, password: str, application_id: int) -> [Model]: + def get_by_name_application_id_and_password(self, name: str, password: str, application_id: int) -> Union[Model, None]: """ Fetch user based on the email and password :param name: User name :param password: User password :param application_id: User password - - :return: [User] + :return: User """ try: user = self.session.execute( @@ -119,14 +121,13 @@ class User(Repository): except SQLAlchemyError as err: logger.error(f'Error while fetching user by email: {err}') - def get_by_name_application_uuid_and_password(self, name: str, password: str, application_uuid: str) -> [Model]: + def get_by_name_application_uuid_and_password(self, name: str, password: str, application_uuid: str) -> Union[Model, None]: """ Fetch user based on the email and password :param name: User name :param password: User password :param application_uuid: Application uuid - - :return: [User] + :return: User """ try: application = self.session.execute( diff --git a/hub/unittests/test_geometry_factory.py b/hub/unittests/test_geometry_factory.py index e98e2a78..c6a19d39 100644 --- a/hub/unittests/test_geometry_factory.py +++ b/hub/unittests/test_geometry_factory.py @@ -155,7 +155,7 @@ class TestGeometryFactory(TestCase): height_field='citygml_me', year_of_construction_field='ANNEE_CONS', function_field='LIBELLE_UT') - GeometryHelper.city_mapping(city) + print(GeometryHelper.city_mapping(city)) for building in city.buildings: self.assertEqual(2, len(building.neighbours)) diff --git a/hub/version.py b/hub/version.py index c3913bcd..5fa8186c 100644 --- a/hub/version.py +++ b/hub/version.py @@ -1 +1 @@ -__version__ = '0.1.7.8' +__version__ = '0.1.7.10'