Merge branch 'main' into systems_catalog

This commit is contained in:
Pilar 2023-03-13 18:24:19 -04:00
commit 6f6f3253a5
7 changed files with 113 additions and 48 deletions

View File

@ -4,6 +4,9 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group Copyright © 2022 Concordia CERC group
Project CoderPeter Yefi peteryefi@gmail.com Project CoderPeter Yefi peteryefi@gmail.com
""" """
import json
from typing import Union, Dict
from hub.persistence import City from hub.persistence import City
from hub.persistence import Application from hub.persistence import Application
from hub.persistence import User 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._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 application_info(self, application_uuid): def application_info(self, application_uuid) -> Union[Application, None]:
""" """
Retrieve the application info for the given uuid Retrieve the application info for the given uuid
:param application_uuid: the uuid for the application :param application_uuid: the uuid for the application
:return: Application or None
""" """
return self._application.get_by_uuid(application_uuid) 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): def user_login(self, name, password, application_uuid):
""" """
Retrieve the user info Retrieve the user info
:param name: the user name :param name: the user name
:param password: the user password :param password: the user password
:param application_uuid: the application uuid :param application_uuid: the application uuid
:return: User or None
""" """
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 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 Retrieve the simulation results for the given cities
:param user_id: the user id owning the results :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 cities: dictionary containing the city and building names for the results
:param result_names: if given, filter the results to the selected names :param result_names: if given, filter the results to the selected names
""" """
results = [] if result_names is None:
for city in cities['cities'].keys(): result_names = []
city_id = self._city.get_by_user_id_application_id_and_name(user_id, application_id, city).id results = {}
for building_name in cities[city]: 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 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_id,
city_object_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 return results

View File

@ -121,7 +121,24 @@ class City(Repository):
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,
Model.application_id == application_id, Model.application_id == application_id,
Model.name == city_name Model.name == city_name
)).first()[0] )).first()
if result_set is not None:
result_set = result_set[0]
return result_set 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}')
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}')

View File

@ -105,7 +105,7 @@ class CityObject(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_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 Fetch a city object based on name and city id
:param name: city object name :param name: city object name

View File

@ -10,6 +10,7 @@ from typing import Union, Dict
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import or_
from hub.hub_logger import logger from hub.hub_logger import logger
from hub.persistence import Repository from hub.persistence import Repository
@ -98,7 +99,7 @@ class SimulationResults(Repository):
Deletes an application with the application_uuid Deletes an application with the application_uuid
:param name: The simulation results tool and workflow name :param name: The simulation results tool and workflow name
:param city_id: The id for the city owning the simulation results :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 :return: None
""" """
@ -125,28 +126,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_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]: def _get_city_object(self, city_object_id) -> [CityObject]:
""" """
Fetch a city object based city id 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() return self.session.execute(select(CityObject).where(CityObject.id == city_object_id)).first()
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_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}')

View File

@ -60,7 +60,7 @@ class User(Repository):
:param name: the name of the user :param name: the name of the user
:param password: the password of the user :param password: the password of the user
:param role: the role of the user :param role: the role of the user
:return: :return: None, Dictionary
""" """
try: try:
self.session.query(Model).filter(Model.id == user_id).update({ self.session.query(Model).filter(Model.id == user_id).update({
@ -86,28 +86,30 @@ class User(Repository):
except SQLAlchemyError as err: except SQLAlchemyError as err:
logger.error(f'Error while fetching user: {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 Fetch user based on the email address
:param name: User name :param name: User name
:param application_id: User application name :param application_id: User application name
:return: [User] matching the search criteria :return: User matching the search criteria or None
""" """
try: try:
return self.session.execute( user = self.session.execute(
select(Model).where(Model.name == name, Model.application_id == application_id) select(Model).where(Model.name == name, Model.application_id == application_id)
).first() ).first()
if user is not None:
user = user[0]
return user
except SQLAlchemyError as err: except SQLAlchemyError as err:
logger.error(f'Error while fetching user by name and application: {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 Fetch user based on the email and password
:param name: User name :param name: User name
:param password: User password :param password: User password
:param application_id: User password :param application_id: User password
:return: User
:return: [User]
""" """
try: try:
user = self.session.execute( user = self.session.execute(
@ -119,14 +121,13 @@ class User(Repository):
except SQLAlchemyError as err: except SQLAlchemyError as err:
logger.error(f'Error while fetching user by email: {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 Fetch user based on the email and password
:param name: User name :param name: User name
:param password: User password :param password: User password
:param application_uuid: Application uuid :param application_uuid: Application uuid
:return: User
:return: [User]
""" """
try: try:
application = self.session.execute( application = self.session.execute(

View File

@ -155,7 +155,7 @@ class TestGeometryFactory(TestCase):
height_field='citygml_me', height_field='citygml_me',
year_of_construction_field='ANNEE_CONS', year_of_construction_field='ANNEE_CONS',
function_field='LIBELLE_UT') function_field='LIBELLE_UT')
GeometryHelper.city_mapping(city) print(GeometryHelper.city_mapping(city))
for building in city.buildings: for building in city.buildings:
self.assertEqual(2, len(building.neighbours)) self.assertEqual(2, len(building.neighbours))

View File

@ -1 +1 @@
__version__ = '0.1.7.8' __version__ = '0.1.7.9'