From 0ed6c2d9356e677468e96d033c9ac55a48ac12b7 Mon Sep 17 00:00:00 2001 From: guille Date: Fri, 10 Mar 2023 14:04:00 -0500 Subject: [PATCH] Partial correction of persistence business logic --- hub/exports/db_factory.py | 62 +++++++++---------- hub/helpers/geometry_helper.py | 2 +- hub/persistence/repositories/city.py | 54 ++++------------ .../repositories/simulation_results.py | 30 ++------- 4 files changed, 45 insertions(+), 103 deletions(-) diff --git a/hub/exports/db_factory.py b/hub/exports/db_factory.py index c003510f..a0ddcda5 100644 --- a/hub/exports/db_factory.py +++ b/hub/exports/db_factory.py @@ -23,44 +23,40 @@ 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 city_by_id(self, city_id): - """ - Retrieve a single city using the id - :param city_id: the id of the city to get - """ - return self._city.get_by_id(city_id) - - def cities_by_user(self, user_id): - """ - Retrieve cities created by user - :param user_id: the id of the user - """ - return self._city.get_by_user(user_id) - - def simulation_results_by_city_id(self, city_id): - """ - Retrieve all simulation results for the given city - :param city_id: the city id of the simulation results to get - """ - return self._simulation_results.get_simulation_results_by_city_id(city_id) - - def simulation_results_by_city_object_id(self, city_object_id): - """ - Retrieve all simulation results for the given object_id - :param city_object_id: the city object id of the simulation results to get - """ - return self._simulation_results.get_simulation_results_by_city_object_id(city_object_id) - def application_info(self, application_uuid): + """ + Retrieve the application info for the given uuid + :param application_uuid: the uuid for the application + """ return self._application.get_by_uuid(application_uuid) - def user_info(self, name, password, 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 + :param name: the user name + :param password: the user password + :param application_uuid: the application uuid + """ return self._user.get_by_name_application_uuid_and_password(name, password, application_uuid) - def building_info(self, name, city_id): - return self._city_object.get_by_name_and_city(name, city_id) + def results(self, user_id, application_id, cities, result_names=[]): + """ + Retrieve the simulation results for the given cities + :param user_id: the user id owning the results + :param application_id: the application id owning the results + :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]: + 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( + city_id, + city_object_id, + result_names)) + return results + diff --git a/hub/helpers/geometry_helper.py b/hub/helpers/geometry_helper.py index eb66ee1f..d46eb71b 100644 --- a/hub/helpers/geometry_helper.py +++ b/hub/helpers/geometry_helper.py @@ -65,8 +65,8 @@ class GeometryHelper: @staticmethod def city_mapping(city, building_names=None): """ - Returns a shared_information dictionary like + Returns a shared_information dictionary like { "building_name" : [{line: 0 coordinate_1: [x,y,z], coordinate_2:[x, y, z], points: 0}] } diff --git a/hub/persistence/repositories/city.py b/hub/persistence/repositories/city.py index ebfccaad..52e59204 100644 --- a/hub/persistence/repositories/city.py +++ b/hub/persistence/repositories/city.py @@ -109,51 +109,19 @@ class City(Repository): except SQLAlchemyError as err: logger.error(f'Error while fetching city: {err}') - def get_by_id(self, city_id: int) -> Model: + def get_by_user_id_application_id_and_name(self, user_id, application_id, city_name): """ - Fetch a City based on the id - :param city_id: the city id - :return: a city + Fetch city based on the user who created it + :param user_id: the user id + :param application_id: the application id + :param city_name: the city name + :return: ModelCity """ try: - return self.session.execute(select(Model).where(Model.id == city_id)).first()[0] - except SQLAlchemyError as err: - logger.error(f'Error while fetching city: {err}') - - def _get_by_hub_version_and_name(self, hub_release: str, city_name: str) -> Model: - """ - Fetch a City based on the name and hub project - :param hub_release: the hub release - :param city_name: the name of the city - :return: a city - """ - try: - return self.session.execute(select(Model) - .where(Model.hub_release == hub_release, Model.name == city_name) - ).first() - except SQLAlchemyError as err: - logger.error(f'Error while fetching city: {err}') - - def get_by_name(self, city_name: str) -> [Model]: - """ - Fetch city based on the name - :param city_name: the name of the building - :return: [ModelCity] with the provided name - """ - try: - result_set = self.session.execute(select(Model).where(Model.name == city_name)) - return [building[0] for building in result_set] + result_set = self.session.execute(select(Model).where(Model.user_id == user_id, + Model.application_id == application_id, + Model.name == city_name + )).first()[0] + return result_set except SQLAlchemyError as err: logger.error(f'Error while fetching city by name: {err}') - - def get_by_user(self, user_id: int) -> [Model]: - """ - Fetch city based on the user who created it - :param user_id: the id of the user - :return: [ModelCity] with the provided name - """ - try: - result_set = self.session.execute(select(Model).where(Model.user_id == user_id)) - return [building[0] for building 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 1b5027a2..53524bba 100644 --- a/hub/persistence/repositories/simulation_results.py +++ b/hub/persistence/repositories/simulation_results.py @@ -44,11 +44,11 @@ class SimulationResults(Repository): :return SimulationResults or Dictionary """ if city_id is not None: - city = self.get_city(city_id) + city = self._get_city(city_id) if city is None: return {'message': f'City does not exists'} else: - city_object = self.get_city_object(city_object_id) + city_object = self._get_city_object(city_object_id) if city_object is None: return {'message': f'City object does not exists'} try: @@ -114,7 +114,7 @@ class SimulationResults(Repository): except SQLAlchemyError as err: logger.error(f'Error while deleting application: {err}') - def get_city(self, city_id) -> [City]: + def _get_city(self, city_id) -> [City]: """ Fetch a city object based city id :param city_id: a city identifier @@ -125,28 +125,6 @@ class SimulationResults(Repository): except SQLAlchemyError as err: logger.error(f'Error while fetching city by city_id: {err}') - def get_simulation_results_by_id(self, sim_id) -> [Model]: - """ - Fetch simulation results by id - :param sim_id: a simulation result identifier - :return: [Model] with the provided sim_id - """ - try: - return self.session.execute(select(Model).where(Model.id == sim_id)).first() - except SQLAlchemyError as err: - logger.error(f'Error while fetching simulation results by sim_id: {err}') - - def get_simulation_results_by_name(self, name) -> [Model]: - """ - Fetch simulation results by name - :param name: the name of the simulation results - :return: [Model] with the provided name - """ - try: - return self.session.execute(select(Model).where(Model.name == name)) - except SQLAlchemyError as err: - logger.error(f'Error while fetching simulation results by name: {err}') - def get_simulation_results_by_city_id(self, city_id) -> [Model]: """ Fetch simulation results by name @@ -169,7 +147,7 @@ class SimulationResults(Repository): except SQLAlchemyError as err: logger.error(f'Error while fetching simulation results by name: {err}') - def get_city_object(self, city_object_id) -> [CityObject]: + def _get_city_object(self, city_object_id) -> [CityObject]: """ Fetch a city object based city id :param city_object_id: a city object identifier