Merge remote-tracking branch 'origin/main' into main

This commit is contained in:
Pilar 2023-03-10 14:07:24 -05:00
commit 2ac1fd045b
4 changed files with 45 additions and 103 deletions

View File

@ -23,44 +23,40 @@ class DBFactory:
self._city_object = CityObject(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) 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) 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): 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) 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): 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) return self._user.get_by_name_application_uuid_and_password(name, password, application_uuid)
def building_info(self, name, city_id): def results(self, user_id, application_id, cities, result_names=[]):
return self._city_object.get_by_name_and_city(name, city_id) """
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

View File

@ -65,8 +65,8 @@ class GeometryHelper:
@staticmethod @staticmethod
def city_mapping(city, building_names=None, plot=False): def city_mapping(city, building_names=None, plot=False):
""" """
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}] "building_name" : [{line: 0 coordinate_1: [x,y,z], coordinate_2:[x, y, z], points: 0}]
} }

View File

@ -109,51 +109,19 @@ class City(Repository):
except SQLAlchemyError as err: except SQLAlchemyError as err:
logger.error(f'Error while fetching city: {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
"""
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]
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 Fetch city based on the user who created it
:param user_id: the id of the user :param user_id: the user id
:return: [ModelCity] with the provided name :param application_id: the application id
:param city_name: the city name
:return: ModelCity
""" """
try: try:
result_set = self.session.execute(select(Model).where(Model.user_id == user_id)) result_set = self.session.execute(select(Model).where(Model.user_id == user_id,
return [building[0] for building in result_set] Model.application_id == application_id,
Model.name == city_name
)).first()[0]
return result_set
except SQLAlchemyError as err: except SQLAlchemyError as err:
logger.error(f'Error while fetching city by name: {err}') logger.error(f'Error while fetching city by name: {err}')

View File

@ -44,11 +44,11 @@ class SimulationResults(Repository):
:return SimulationResults or Dictionary :return SimulationResults or Dictionary
""" """
if city_id is not None: if city_id is not None:
city = self.get_city(city_id) city = self._get_city(city_id)
if city is None: if city is None:
return {'message': f'City does not exists'} return {'message': f'City does not exists'}
else: else:
city_object = self.get_city_object(city_object_id) city_object = self._get_city_object(city_object_id)
if city_object is None: if city_object is None:
return {'message': f'City object does not exists'} return {'message': f'City object does not exists'}
try: try:
@ -114,7 +114,7 @@ class SimulationResults(Repository):
except SQLAlchemyError as err: except SQLAlchemyError as err:
logger.error(f'Error while deleting application: {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 Fetch a city object based city id
:param city_id: a city identifier :param city_id: a city identifier
@ -125,28 +125,6 @@ class SimulationResults(Repository):
except SQLAlchemyError as err: except SQLAlchemyError as err:
logger.error(f'Error while fetching city by city_id: {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]: def get_simulation_results_by_city_id(self, city_id) -> [Model]:
""" """
Fetch simulation results by name Fetch simulation results by name
@ -169,7 +147,7 @@ class SimulationResults(Repository):
except SQLAlchemyError as err: except SQLAlchemyError as err:
logger.error(f'Error while fetching simulation results by name: {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 Fetch a city object based city id
:param city_object_id: a city object identifier :param city_object_id: a city object identifier