took changes from hub

This commit is contained in:
Ruben Sanchez 2023-11-22 12:09:28 -05:00
parent e9811ddffe
commit 6c703cbb64
5 changed files with 66 additions and 11 deletions

View File

@ -50,14 +50,14 @@ class DBSetup:
name = 'AdminTool' name = 'AdminTool'
description = 'Admin tool to control city persistence and to test the API v1.4' description = 'Admin tool to control city persistence and to test the API v1.4'
logging.info('Creating default admin tool application...') logging.info('Creating default admin tool application...')
application_id = application_repo.insert(name, description, application_uuid) application = application_repo.insert(name, description, application_uuid)
if isinstance(application_id, dict): if isinstance(application, dict):
logging.info(application_id) logging.info(application)
else: else:
msg = f'Created Admin tool with application_uuid: {application_uuid}' msg = f'Created Admin tool with application_uuid: {application_uuid}'
logging.info(msg) logging.info(msg)
return application_id return application.id
@staticmethod @staticmethod
def _create_admin_user(user_repo, admin_password, application_id): def _create_admin_user(user_repo, admin_password, application_id):

View File

@ -33,4 +33,4 @@ class City(Models):
self.scenario = scenario self.scenario = scenario
self.application_id = application_id self.application_id = application_id
self.user_id = user_id self.user_id = user_id
self.hub_release = hub_release self.hub_release = hub_release

View File

@ -50,8 +50,7 @@ class CityObject(Models):
self.volume = building.volume self.volume = building.volume
self.area = building.floor_area self.area = building.floor_area
self.roof_area = sum(roof.solid_polygon.area for roof in building.roofs) self.roof_area = sum(roof.solid_polygon.area for roof in building.roofs)
self.total_pv_area = sum( self.total_pv_area = sum(roof.solid_polygon.area * roof.solar_collectors_area_reduction_factor for roof in building.roofs)
roof.solid_polygon.area * roof.solar_collectors_area_reduction_factor for roof in building.roofs)
storeys = building.storeys_above_ground storeys = building.storeys_above_ground
wall_area = 0 wall_area = 0
window_ratio = 0 window_ratio = 0

View File

@ -90,7 +90,7 @@ class CityObject(Repository):
session.query(Model).filter(Model.city_id == city_id, Model.name == name).delete() session.query(Model).filter(Model.city_id == city_id, Model.name == name).delete()
session.commit() session.commit()
except SQLAlchemyError as err: except SQLAlchemyError as err:
logging.error('Error while deleting city_object %s', err) logging.error('Error while deleting application %s', err)
raise SQLAlchemyError from err raise SQLAlchemyError from err
def get_by_name_or_alias_and_city(self, name, city_id) -> Model: def get_by_name_or_alias_and_city(self, name, city_id) -> Model:
@ -124,3 +124,35 @@ class CityObject(Repository):
except IndexError as err: except IndexError as err:
logging.error('Error while fetching city object by name and city, empty result %s', err) logging.error('Error while fetching city object by name and city, empty result %s', err)
raise IndexError from err raise IndexError from err
def get_by_name_or_alias_in_cities(self, name, city_ids) -> Model:
"""
Fetch a city object based on name and city ids
:param name: city object name
:param city_ids: a list of city identifiers
:return: [CityObject] with the provided name or alias belonging to the city with id city_id
"""
try:
# search by name first
with Session(self.engine) as session:
city_object = session.execute(select(Model).where(Model.name == name, Model.city_id.in_(tuple(city_ids)))).first()
if city_object is not None:
return city_object[0]
# name not found, so search by alias instead
city_objects = session.execute(
select(Model).where(Model.aliases.contains(name), Model.city_id.in_(tuple(city_ids)))
).all()
for city_object in city_objects:
aliases = city_object[0].aliases.replace('{', '').replace('}', '').split(',')
for alias in aliases:
if alias == name:
# force the name as the alias
city_object[0].name = name
return city_object[0]
return None
except SQLAlchemyError as err:
logging.error('Error while fetching city object by name and city: %s', err)
raise SQLAlchemyError from err
except IndexError as err:
logging.error('Error while fetching city object by name and city, empty result %s', err)
raise IndexError from err

View File

@ -84,7 +84,7 @@ class SimulationResults(Repository):
else: else:
raise NotImplementedError('Missing either city_id or city_object_id') raise NotImplementedError('Missing either city_id or city_object_id')
except SQLAlchemyError as err: except SQLAlchemyError as err:
logging.error('Error while updating simulation results %s', err) logging.error('Error while updating city object %s', err)
raise SQLAlchemyError from err raise SQLAlchemyError from err
def delete(self, name: str, city_id=None, city_object_id=None): def delete(self, name: str, city_id=None, city_object_id=None):
@ -135,8 +135,8 @@ class SimulationResults(Repository):
logging.error('Error while fetching city by city_id: %s', err) logging.error('Error while fetching city by city_id: %s', err)
raise SQLAlchemyError from err raise SQLAlchemyError from err
def get_simulation_results_by_city_id_city_object_id_and_names(self, city_id, city_object_id, def get_simulation_results_by_city_id_city_object_id_and_names(self, city_id, city_object_id, result_names=None) -> [
result_names=None) -> [Model]: Model]:
""" """
Fetch the simulation results based in the city_id or city_object_id with the given names or all 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_id: the city id
@ -161,3 +161,27 @@ class SimulationResults(Repository):
except SQLAlchemyError as err: except SQLAlchemyError as err:
logging.error('Error while fetching city by city_id: %s', err) logging.error('Error while fetching city by city_id: %s', err)
raise SQLAlchemyError from err raise SQLAlchemyError from err
def get_simulation_results_by_city_object_id_and_names(self, city_object_id, result_names=None) -> [Model]:
"""
Fetch the simulation results based in the city_object_id with the given names or all
:param city_object_id: the city object id
:param result_names: if given filter the results
:return: [SimulationResult]
"""
try:
with Session(self.engine) as session:
result_set = session.execute(select(Model).where(
Model.city_object_id == city_object_id
))
results = [r[0] for r in result_set]
if not result_names:
return results
filtered_results = []
for result in results:
if result.name in result_names:
filtered_results.append(result)
return filtered_results
except SQLAlchemyError as err:
logging.error('Error while fetching city by city_id: %s', err)
raise SQLAlchemyError from err