Update current persistence code base to the hub version

This commit is contained in:
Ruben Sanchez 2023-11-28 13:50:06 -05:00
parent ba2a9a76fd
commit cbca0e7637
13 changed files with 147 additions and 79 deletions

View File

@ -16,7 +16,7 @@ Models = declarative_base()
class Configuration:
"""
Configuration class to hold common persistence configuration
Configuration class to hold common cerc_persistence configuration
"""
def __init__(self, db_name: str, dotenv_path: str, app_env='TEST'):

View File

@ -7,6 +7,7 @@ Project CoderPeter Yefi peteryefi@gmail.com
import json
from typing import Dict
from cerc_persistence.repositories.application import Application
from cerc_persistence.repositories.city import City
from cerc_persistence.repositories.city_object import CityObject
@ -74,10 +75,10 @@ class DBControl:
:
"""
cities = self._city.get_by_user_id_application_id_and_scenario(user_id, application_id, scenario)
for city in cities:
result = self.building_info(name, city[0].id)
if result is not None:
return result
c = [c[0].id for c in cities]
result = self._city_object.building_in_cities_info(name, c)
if result is not None:
return result
return None
def building_info(self, name, city_id) -> CityObject:
@ -89,6 +90,15 @@ class DBControl:
"""
return self._city_object.get_by_name_or_alias_and_city(name, city_id)
def building_info_in_cities(self, name, cities) -> CityObject:
"""
Retrieve the building info from the database
:param name: Building name
:param cities: [City ID]
:return: CityObject
"""
return self._city_object.get_by_name_or_alias_in_cities(name, cities)
def buildings_info(self, request_values, city_id) -> [CityObject]:
"""
Retrieve the buildings info from the database
@ -121,24 +131,21 @@ class DBControl:
)
if result_sets is None:
continue
for result_set in result_sets:
city_id = result_set[0].id
results[scenario_name] = []
city_ids = [r[0].id for r in result_sets]
for building_name in scenario[scenario_name]:
_building = self._city_object.get_by_name_or_alias_in_cities(building_name, city_ids)
if _building is None:
continue
city_object_id = _building.id
_ = self._simulation_results.get_simulation_results_by_city_object_id_and_names(
city_object_id,
result_names)
results[scenario_name] = []
for building_name in scenario[scenario_name]:
_building = self._city_object.get_by_name_or_alias_and_city(building_name, city_id)
if _building is None:
continue
city_object_id = _building.id
_ = self._simulation_results.get_simulation_results_by_city_id_city_object_id_and_names(
city_id,
city_object_id,
result_names)
for value in _:
values = json.loads(value.values)
values["building"] = building_name
results[scenario_name].append(values)
for value in _:
values = value.values
values["building"] = building_name
results[scenario_name].append(values)
return results
def persist_city(self, city: City, pickle_path, scenario, application_id: int, user_id: int):
@ -151,7 +158,7 @@ class DBControl:
:param user_id: User who create the city
return identity_id
"""
return self._city.insert(city, pickle_path, scenario, application_id, user_id)
return self._city.insert(city, pickle_path, scenario, application_id, user_id)
def update_city(self, city_id, city):
"""

View File

@ -48,7 +48,7 @@ class DBSetup:
@staticmethod
def _create_admin_app(application_repo, application_uuid):
name = 'AdminTool'
description = 'Admin tool to control city persistence and to test the API v1.4'
description = 'Admin tool to control city cerc_persistence and to test the API v1.4'
logging.info('Creating default admin tool application...')
application = application_repo.insert(name, description, application_uuid)

View File

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

View File

@ -1,10 +1,3 @@
"""
Repositories Package
"""
import datetime
import logging
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from cerc_persistence.repository import Repository

View File

@ -4,8 +4,16 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca
"""
import datetime
import logging
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm.session import Session
from cerc_persistence.repository import Repository
from cerc_persistence.models import Application as Model
from . import datetime, logging, select, SQLAlchemyError, Session, Repository
class Application(Repository):

View File

@ -4,11 +4,18 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
import datetime
import logging
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from hub.city_model_structure.city import City as CityHub
from hub.version import __version__
from cerc_persistence.repository import Repository
from cerc_persistence.models import City as Model
from cerc_persistence.models import CityObject
from . import datetime, logging, select, SQLAlchemyError, Session, Repository
from hub.version import __version__
class City(Repository):

View File

@ -4,9 +4,17 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca
"""
import datetime
import logging
from typing import Union
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from hub.city_model_structure.building import Building
from cerc_persistence.models import CityObject as Model
from . import datetime, logging, select, SQLAlchemyError, Session, Repository
from cerc_persistence.repository import Repository
class CityObject(Repository):
@ -93,7 +101,41 @@ class CityObject(Repository):
logging.error('Error while deleting application %s', 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
:param name: city object name

View File

@ -4,11 +4,18 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca
"""
import datetime
import logging
from sqlalchemy import or_
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from cerc_persistence.repository import Repository
from cerc_persistence.models import City
from cerc_persistence.models import CityObject
from cerc_persistence.models import SimulationResults as Model
from . import datetime, logging, select, SQLAlchemyError, Session, Repository
class SimulationResults(Repository):
@ -69,10 +76,10 @@ class SimulationResults(Repository):
with Session(self.engine) as session:
if city_id is not None:
session.query(Model).filter(Model.name == name, Model.city_id == city_id).update(
{
'values': values,
'updated': datetime.datetime.utcnow()
})
{
'values': values,
'updated': datetime.datetime.utcnow()
})
session.commit()
elif city_object_id is not None:
session.query(Model).filter(Model.name == name, Model.city_object_id == city_object_id).update(
@ -135,8 +142,7 @@ class SimulationResults(Repository):
logging.error('Error while fetching city by city_id: %s', err)
raise SQLAlchemyError from err
def get_simulation_results_by_city_id_city_object_id_and_names(self, city_id, city_object_id, result_names=None) -> [
Model]:
def get_simulation_results_by_city_id_city_object_id_and_names(self, city_id, city_object_id, result_names=None) -> [Model]:
"""
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
@ -184,4 +190,4 @@ class SimulationResults(Repository):
return filtered_results
except SQLAlchemyError as err:
logging.error('Error while fetching city by city_id: %s', err)
raise SQLAlchemyError from err
raise SQLAlchemyError from err

View File

@ -4,12 +4,16 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
import datetime
import logging
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from hub.helpers.auth import Auth
from cerc_persistence.repository import Repository
from cerc_persistence.models import User as Model, Application as ApplicationModel, UserRoles
from . import datetime, logging, select, SQLAlchemyError, Session, Repository
class User(Repository):

View File

@ -18,7 +18,7 @@ with open(version) as f:
exec(f.read(), main_ns)
setup(
name='cerc-persistence',
name='cerc-cerc_persistence',
version=main_ns['__version__'],
description="",
long_description="",

View File

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

View File

@ -1,8 +1,8 @@
"""
Test db retrieve
Test db factory
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Ruben Sanchez ruben.sanchez@mail.concordia.ca
Project Coder Peter Yefi peteryefi@gmail.com
"""
import distutils.spawn
import logging
@ -14,6 +14,7 @@ from unittest import TestCase
import sqlalchemy.exc
from sqlalchemy import create_engine
from sqlalchemy.exc import ProgrammingError
from cerc_persistence.db_control import DBControl
from cerc_persistence.repository import Repository