From fc25c12149928693edb228070b1c7101d85c7bda Mon Sep 17 00:00:00 2001 From: guille Date: Mon, 13 Mar 2023 14:18:04 -0400 Subject: [PATCH 1/4] Partial correction of persistence business logic --- hub/exports/db_factory.py | 38 +++++++++++--- hub/persistence/repositories/city.py | 19 ++++++- .../repositories/simulation_results.py | 49 ++++++++++--------- hub/unittests/test_geometry_factory.py | 2 +- 4 files changed, 77 insertions(+), 31 deletions(-) diff --git a/hub/exports/db_factory.py b/hub/exports/db_factory.py index a0ddcda5..d8981af7 100644 --- a/hub/exports/db_factory.py +++ b/hub/exports/db_factory.py @@ -4,6 +4,8 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Concordia CERC group Project CoderPeter Yefi peteryefi@gmail.com """ +import json + from hub.persistence import City from hub.persistence import Application from hub.persistence import User @@ -30,6 +32,15 @@ class DBFactory: """ 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 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 @@ -39,6 +50,9 @@ class DBFactory: """ return self._user.get_by_name_application_uuid_and_password(name, password, application_uuid) + def cities_by_user_and_application(self, user_id, application_id) -> [City]: + return self._city.get_by_user_id_and_application_id(user_id, application_id) + def results(self, user_id, application_id, cities, result_names=[]): """ Retrieve the simulation results for the given cities @@ -47,15 +61,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]: + 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/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/unittests/test_geometry_factory.py b/hub/unittests/test_geometry_factory.py index 990097fd..4ed0eaab 100644 --- a/hub/unittests/test_geometry_factory.py +++ b/hub/unittests/test_geometry_factory.py @@ -154,7 +154,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)) From a3a382dae992eb01171bef7c607df043acbe67d2 Mon Sep 17 00:00:00 2001 From: guille Date: Mon, 13 Mar 2023 14:41:25 -0400 Subject: [PATCH 2/4] Reintroduce function in business logic --- hub/exports/db_factory.py | 28 +++++++++++++++++---- hub/persistence/repositories/city_object.py | 2 +- hub/persistence/repositories/user.py | 16 ++++++------ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/hub/exports/db_factory.py b/hub/exports/db_factory.py index d8981af7..5f4fde9e 100644 --- a/hub/exports/db_factory.py +++ b/hub/exports/db_factory.py @@ -5,6 +5,7 @@ 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 @@ -25,10 +26,11 @@ 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) @@ -38,6 +40,7 @@ class DBFactory: :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) @@ -47,13 +50,29 @@ class DBFactory: :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 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 results(self, user_id, application_id, cities, result_names=[]): + 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 @@ -61,6 +80,8 @@ 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 """ + if result_names is None: + result_names = [] results = {} for city in cities['cities']: city_name = next(iter(city)) @@ -83,6 +104,3 @@ class DBFactory: values["building"] = building_name results[city_name].append(values) return results - - - 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/user.py b/hub/persistence/repositories/user.py index 87005a4a..791a4c02 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,7 +86,7 @@ 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) -> Model: """ Fetch user based on the email address :param name: User name @@ -96,18 +96,17 @@ class User(Repository): try: return self.session.execute( select(Model).where(Model.name == name, Model.application_id == application_id) - ).first() + ).first()[0] 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 +118,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( From ecb09edcfbf20a9d37a641dca0eae6b78c92586a Mon Sep 17 00:00:00 2001 From: guille Date: Mon, 13 Mar 2023 15:01:55 -0400 Subject: [PATCH 3/4] Reintroduce function in business logic --- hub/persistence/repositories/user.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/hub/persistence/repositories/user.py b/hub/persistence/repositories/user.py index 791a4c02..cd6037ac 100644 --- a/hub/persistence/repositories/user.py +++ b/hub/persistence/repositories/user.py @@ -86,17 +86,20 @@ 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()[0] + ).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}') From 658c44389ecbbed323b37ab6429cbc15b319cc1b Mon Sep 17 00:00:00 2001 From: guille Date: Mon, 13 Mar 2023 15:02:28 -0400 Subject: [PATCH 4/4] Version 0.1.7.9 --- hub/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hub/version.py b/hub/version.py index c3913bcd..95b32a5c 100644 --- a/hub/version.py +++ b/hub/version.py @@ -1 +1 @@ -__version__ = '0.1.7.8' +__version__ = '0.1.7.9'