Partial correction of persistence business logic

This commit is contained in:
Guille Gutierrez 2023-03-13 14:18:04 -04:00
parent 77743fea1c
commit fc25c12149
4 changed files with 77 additions and 31 deletions

View File

@ -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

View File

@ -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}')

View File

@ -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}')

View File

@ -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))