Merge branch 'main' into Transys

This commit is contained in:
Guille Gutierrez 2023-11-29 07:54:21 +01:00
commit 42b1d363a6
6 changed files with 74 additions and 43 deletions

2
.gitignore vendored
View File

@ -9,4 +9,4 @@
**/hub/logs/ **/hub/logs/
**/__pycache__/ **/__pycache__/
**/.idea/ **/.idea/
cerc_hub.egg-info

View File

@ -1,3 +0,0 @@
# Except this file
*
!.gitignore

View File

@ -76,8 +76,7 @@ class DBControl:
""" """
cities = self._city.get_by_user_id_application_id_and_scenario(user_id, application_id, scenario) cities = self._city.get_by_user_id_application_id_and_scenario(user_id, application_id, scenario)
c = [c[0].id for c in cities] c = [c[0].id for c in cities]
for city in cities: result = self._city_object.building_in_cities_info(name, c)
result = self.building_info(name, city[0].id)
if result is not None: if result is not None:
return result return result
return None return None
@ -144,7 +143,7 @@ class DBControl:
result_names) result_names)
for value in _: for value in _:
values = json.loads(value.values) values = value.values
values["building"] = building_name values["building"] = building_name
results[scenario_name].append(values) results[scenario_name].append(values)
return results return results

View File

@ -6,14 +6,15 @@ Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca
""" """
import datetime import datetime
import logging import logging
from typing import Union
from sqlalchemy import select, or_ from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from hub.city_model_structure.building import Building from hub.city_model_structure.building import Building
from hub.persistence.repository import Repository
from hub.persistence.models import CityObject as Model from hub.persistence.models import CityObject as Model
from hub.persistence.repository import Repository
class CityObject(Repository): class CityObject(Repository):
@ -100,7 +101,41 @@ class CityObject(Repository):
logging.error('Error while deleting application %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 building_in_cities_info(self, name, cities):
"""
Fetch a city object based on name and city id
:param name: city object name
:param cities: 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_(cities))
).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_(cities))
).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
def get_by_name_or_alias_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

@ -1,4 +1,4 @@
""" """
Hub version number Hub version number
""" """
__version__ = '0.1.8.31' __version__ = '0.1.8.34'

View File

@ -248,36 +248,36 @@ TestDBFactory
for x in building.onsite_electrical_production[cte.MONTH]] for x in building.onsite_electrical_production[cte.MONTH]]
yearly_on_site_electrical_production = [x * cte.WATTS_HOUR_TO_JULES yearly_on_site_electrical_production = [x * cte.WATTS_HOUR_TO_JULES
for x in building.onsite_electrical_production[cte.YEAR]] for x in building.onsite_electrical_production[cte.YEAR]]
results = json.dumps({cte.INSEL_MEB: [ results = {cte.INSEL_MEB: {
{'monthly_cooling_peak_load': monthly_cooling_peak_load}, 'monthly_cooling_peak_load': monthly_cooling_peak_load,
{'yearly_cooling_peak_load': yearly_cooling_peak_load}, 'yearly_cooling_peak_load': yearly_cooling_peak_load,
{'monthly_heating_peak_load': monthly_heating_peak_load}, 'monthly_heating_peak_load': monthly_heating_peak_load,
{'yearly_heating_peak_load': yearly_heating_peak_load}, 'yearly_heating_peak_load': yearly_heating_peak_load,
{'monthly_lighting_peak_load': monthly_lighting_peak_load}, 'monthly_lighting_peak_load': monthly_lighting_peak_load,
{'yearly_lighting_peak_load': yearly_lighting_peak_load}, 'yearly_lighting_peak_load': yearly_lighting_peak_load,
{'monthly_appliances_peak_load': monthly_appliances_peak_load}, 'monthly_appliances_peak_load': monthly_appliances_peak_load,
{'yearly_appliances_peak_load': yearly_appliances_peak_load}, 'yearly_appliances_peak_load': yearly_appliances_peak_load,
{'monthly_cooling_demand': monthly_cooling_demand}, 'monthly_cooling_demand': monthly_cooling_demand,
{'yearly_cooling_demand': yearly_cooling_demand}, 'yearly_cooling_demand': yearly_cooling_demand,
{'monthly_heating_demand': monthly_heating_demand}, 'monthly_heating_demand': monthly_heating_demand,
{'yearly_heating_demand': yearly_heating_demand}, 'yearly_heating_demand': yearly_heating_demand,
{'monthly_lighting_electrical_demand': monthly_lighting_electrical_demand}, 'monthly_lighting_electrical_demand': monthly_lighting_electrical_demand,
{'yearly_lighting_electrical_demand': yearly_lighting_electrical_demand}, 'yearly_lighting_electrical_demand': yearly_lighting_electrical_demand,
{'monthly_appliances_electrical_demand': monthly_appliances_electrical_demand}, 'monthly_appliances_electrical_demand': monthly_appliances_electrical_demand,
{'yearly_appliances_electrical_demand': yearly_appliances_electrical_demand}, 'yearly_appliances_electrical_demand': yearly_appliances_electrical_demand,
{'monthly_domestic_hot_water_heat_demand': monthly_domestic_hot_water_heat_demand}, 'monthly_domestic_hot_water_heat_demand': monthly_domestic_hot_water_heat_demand,
{'yearly_domestic_hot_water_heat_demand': yearly_domestic_hot_water_heat_demand}, 'yearly_domestic_hot_water_heat_demand': yearly_domestic_hot_water_heat_demand,
{'monthly_heating_consumption': monthly_heating_consumption}, 'monthly_heating_consumption': monthly_heating_consumption,
{'yearly_heating_consumption': yearly_heating_consumption}, 'yearly_heating_consumption': yearly_heating_consumption,
{'monthly_cooling_consumption': monthly_cooling_consumption}, 'monthly_cooling_consumption': monthly_cooling_consumption,
{'yearly_cooling_consumption': yearly_cooling_consumption}, 'yearly_cooling_consumption': yearly_cooling_consumption,
{'monthly_domestic_hot_water_consumption': monthly_domestic_hot_water_consumption}, 'monthly_domestic_hot_water_consumption': monthly_domestic_hot_water_consumption,
{'yearly_domestic_hot_water_consumption': yearly_domestic_hot_water_consumption}, 'yearly_domestic_hot_water_consumption': yearly_domestic_hot_water_consumption,
{'monthly_distribution_systems_electrical_consumption': monthly_distribution_systems_electrical_consumption}, 'monthly_distribution_systems_electrical_consumption': monthly_distribution_systems_electrical_consumption,
{'yearly_distribution_systems_electrical_consumption': yearly_distribution_systems_electrical_consumption}, 'yearly_distribution_systems_electrical_consumption': yearly_distribution_systems_electrical_consumption,
{'monthly_on_site_electrical_production': monthly_on_site_electrical_production}, 'monthly_on_site_electrical_production': monthly_on_site_electrical_production,
{'yearly_on_site_electrical_production': yearly_on_site_electrical_production} 'yearly_on_site_electrical_production': yearly_on_site_electrical_production
]}) }}
db_building_id = _building.id db_building_id = _building.id
city_objects_id.append(db_building_id) city_objects_id.append(db_building_id)