Persistence up and running with basic app applicartion

This commit is contained in:
Guille Gutierrez 2023-02-02 06:12:24 -05:00
parent d6c3f3ef67
commit f99b8497ff
11 changed files with 43 additions and 63 deletions

View File

@ -4,8 +4,8 @@ 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
""" """
from hub.persistence import CityRepo from hub.persistence import City
from hub.persistence import HeatPumpSimulationRepo from hub.persistence import Application
class DBFactory: class DBFactory:
@ -14,40 +14,30 @@ class DBFactory:
""" """
def __init__(self, db_name, app_env, dotenv_path): def __init__(self, db_name, app_env, dotenv_path):
self._city_repo = CityRepo(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) self._city = City(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
self._hp_simulation_repo = HeatPumpSimulationRepo(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) self._application = Application(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
def get_city(self, city_id): def get_city(self, city_id):
""" """
Retrieve a single city from postgres Retrieve a single city from postgres
:param city_id: the id of the city to get :param city_id: the id of the city to get
""" """
return self._city_repo.get_by_id(city_id) return self._city.get_by_id(city_id)
def get_city_by_name(self, city_name): def get_city_by_name(self, city_name):
""" """
Retrieve a single city from postgres Retrieve a single city from postgres
:param city_name: the name of the city to get :param city_name: the name of the city to get
""" """
return self._city_repo.get_by_name(city_name) return self._city.get_by_name(city_name)
def get_city_by_user(self, user_id): def get_city_by_user(self, user_id):
""" """
Retrieve cities created by user Retrieve cities created by user
:param user_id: the id of the user :param user_id: the id of the user
""" """
return self._city_repo.get_by_user(user_id) return self._city.get_by_user(user_id)
def get_hp_simulation(self, hp_sim_id: int): def application_info(self, application_uuid):
""" return self._application.get_by_uuid(application_uuid)
Retrieve a single heat pump simulation from postgres
:param hp_sim_id: the id of the heat pump to get
"""
return self._hp_simulation_repo.get_by_id(hp_sim_id)
def get_hp_simulation_by_city(self, city_id: int):
"""
Retrieve a single city from postgres
:param city_id: the id of the city
"""
return self._hp_simulation_repo.get_by_city(city_id)

View File

@ -4,10 +4,8 @@ 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
""" """
from hub.persistence import CityRepo
from hub.persistence import HeatPumpSimulationRepo
from typing import Dict
from hub.city_model_structure.city import City from hub.city_model_structure.city import City
from hub.persistence import City as CityRepository
class DBFactory: class DBFactory:
@ -16,14 +14,13 @@ class DBFactory:
""" """
def __init__(self, db_name, dotenv_path, app_env): def __init__(self, db_name, dotenv_path, app_env):
self._city_repo = CityRepo(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env) self._city_repository = CityRepository(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
self._hp_simulation_repo = HeatPumpSimulationRepo(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
def persist_city(self, user_id: int, city: City): def persist_city(self, application_id: int, user_id: int, city: City):
""" """
Persist city into postgres database Persist city into postgres database
""" """
return self._city_repo.insert(city, user_id) return self._city_repository.insert(city, application_id, user_id)
def update_city(self, city_id, city): def update_city(self, city_id, city):
""" """
@ -31,27 +28,11 @@ class DBFactory:
:param city_id: the id of the city to update :param city_id: the id of the city to update
:param city: the updated city object :param city: the updated city object
""" """
return self._city_repo.update(city_id, city) return self._city_repository.update(city_id, city)
def delete_city(self, city_id): def delete_city(self, city_id):
""" """
Deletes a single city from postgres Deletes a single city from postgres
:param city_id: the id of the city to get :param city_id: the id of the city to get
""" """
self._city_repo.delete_city(city_id) self._city_repository.delete(city_id)
def persist_hp_simulation(self, hp_simulation_data: Dict, city_id: int):
"""
Persist heat pump simulation results
:param hp_simulation_data: the simulation results
:param city_id: the city object used in running the simulation
:return: HeatPumpSimulation object
"""
return self._hp_simulation_repo.insert(hp_simulation_data, city_id)
def delete_hp_simulation(self, hp_sim_id):
"""
Deletes a single heat pump simulation from postgres
:param hp_sim_id: the id of the heat pump simulation to get
"""
self._hp_simulation_repo.delete_hp_simulation(hp_sim_id)

View File

@ -1,5 +1,6 @@
from .repository import Repository from .repository import Repository
from .repositories.city import City from .repositories.city import City
from .repositories.application import Application
from .db_setup import DBSetup from .db_setup import DBSetup
from .repositories.user import User from .repositories.user import User
from .models.user import UserRoles from .models.user import UserRoles

View File

@ -10,8 +10,7 @@ from dotenv import load_dotenv
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from hub.hub_logger import logger from hub.hub_logger import logger
Base = declarative_base() Models = declarative_base()
class Configuration: class Configuration:
""" """

View File

@ -30,15 +30,15 @@ class DBSetup:
# Create the tables using the models # Create the tables using the models
Application.__table__.create(bind=repository.engine, checkfirst=True) Application.__table__.create(bind=repository.engine, checkfirst=True)
User.__table__.create(bind=repository.engine, checkfirst=True)
City.__table__.create(bind=repository.engine, checkfirst=True) City.__table__.create(bind=repository.engine, checkfirst=True)
CityObject.__table__.create(bind=repository.engine, checkfirst=True) CityObject.__table__.create(bind=repository.engine, checkfirst=True)
SimulationResults.__table__.create(bind=repository.engine, checkfirst=True) SimulationResults.__table__.create(bind=repository.engine, checkfirst=True)
User.__table__.create(bind=repository.engine, checkfirst=True)
self._user_repo = UserRepository(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) self._user_repo = UserRepository(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
self._application_repo = ApplicationRepository(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) self._application_repo = ApplicationRepository(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
self._create_admin_user(self._user_repo, admin_password) application_id = self._create_admin_app(self._application_repo, application_uuid)
self._create_admin_app(self._application_repo, application_uuid) self._create_admin_user(self._user_repo, admin_password, application_id)
@staticmethod @staticmethod
def _create_admin_app(application_repo, application_uuid): def _create_admin_app(application_repo, application_uuid):
@ -46,12 +46,14 @@ class DBSetup:
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'
print('Creating default admin tool application...') print('Creating default admin tool application...')
application = application_repo.insert(name, description, application_uuid) application = application_repo.insert(name, description, application_uuid)
if type(application) is dict: if type(application) is dict:
logger.info(application) logger.info(application)
else: else:
msg = f'Created Admin tool with application_uuid: {application_uuid}' msg = f'Created Admin tool with application_uuid: {application_uuid}'
print(msg) print(msg)
logger.info(msg) logger.info(msg)
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

@ -10,13 +10,14 @@ import datetime
from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy import Column, Integer, String, Sequence from sqlalchemy import Column, Integer, String, Sequence
from sqlalchemy import DateTime from sqlalchemy import DateTime
from hub.persistence.configuration import Models
class Application: class Application(Models):
""" """
A model representation of an application A model representation of an application
""" """
__table__ = "application" __tablename__ = 'application'
id = Column(Integer, Sequence('application_id_seq'), primary_key=True) id = Column(Integer, Sequence('application_id_seq'), primary_key=True)
name = Column(String, nullable=False) name = Column(String, nullable=False)
description = Column(String, nullable=False) description = Column(String, nullable=False)

View File

@ -9,12 +9,13 @@ import datetime
from sqlalchemy import Column, Integer, String, Sequence, ForeignKey from sqlalchemy import Column, Integer, String, Sequence, ForeignKey
from sqlalchemy import DateTime, PickleType from sqlalchemy import DateTime, PickleType
from hub.persistence.configuration import Models
class City: class City(Models):
"""A model representation of a city """A model representation of a city
""" """
__table__ = "city" __tablename__ = 'city'
id = Column(Integer, Sequence('city_id_seq'), primary_key=True) id = Column(Integer, Sequence('city_id_seq'), primary_key=True)
city = Column(PickleType, nullable=False) city = Column(PickleType, nullable=False)
name = Column(String, nullable=False) name = Column(String, nullable=False)

View File

@ -9,12 +9,13 @@ import datetime
from sqlalchemy import Column, Integer, String, Sequence, ForeignKey, Float from sqlalchemy import Column, Integer, String, Sequence, ForeignKey, Float
from sqlalchemy import DateTime from sqlalchemy import DateTime
from hub.persistence.configuration import Models
class CityObject: class CityObject(Models):
""" """
A model representation of an application A model representation of an application
""" """
__table__ = "city_object" __tablename__ = 'city_object'
id = Column(Integer, Sequence('application_id_seq'), primary_key=True) id = Column(Integer, Sequence('application_id_seq'), primary_key=True)
city_id = Column(Integer, ForeignKey('city.id'), nullable=False) city_id = Column(Integer, ForeignKey('city.id'), nullable=False)
name = Column(String, nullable=False) name = Column(String, nullable=False)

View File

@ -10,12 +10,13 @@ import datetime
from sqlalchemy import Column, Integer, String, Sequence, ForeignKey from sqlalchemy import Column, Integer, String, Sequence, ForeignKey
from sqlalchemy import DateTime from sqlalchemy import DateTime
from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.dialects.postgresql import JSONB
from hub.persistence.configuration import Models
class SimulationResults: class SimulationResults(Models):
""" """
A model representation of an application A model representation of an application
""" """
__table__ = "simulation_results" __tablename__ = 'simulation_results'
id = Column(Integer, Sequence('application_id_seq'), primary_key=True) id = Column(Integer, Sequence('application_id_seq'), primary_key=True)
city_id = Column(Integer, ForeignKey('city.id'), nullable=True) city_id = Column(Integer, ForeignKey('city.id'), nullable=True)
city_object_id = Column(Integer, ForeignKey('city_object.id'), nullable=True) city_object_id = Column(Integer, ForeignKey('city_object.id'), nullable=True)

View File

@ -11,17 +11,19 @@ import enum
from sqlalchemy import Column, Integer, String, Sequence from sqlalchemy import Column, Integer, String, Sequence
from sqlalchemy import DateTime, Enum from sqlalchemy import DateTime, Enum
from hub.persistence.configuration import Models
class UserRoles(enum.Enum): class UserRoles(enum.Enum):
Admin = 'Admin' Admin = 'Admin'
Hub_Reader = 'Hub_Reader' Hub_Reader = 'Hub_Reader'
class User: class User(Models):
""" """
A model representation of a city A model representation of a city
""" """
__table__ = "user" __tablename__ = 'user'
id = Column(Integer, Sequence('user_id_seq'), primary_key=True) id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
name = Column(String, nullable=False) name = Column(String, nullable=False)
password = Column(String, nullable=False) password = Column(String, nullable=False)

View File

@ -80,15 +80,16 @@ class Application(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_uuid(self, application_uuid: str) -> [Model]: def get_by_uuid(self, application_uuid: str) -> Model:
""" """
Fetch Application based on the application uuid Fetch Application based on the application uuid
:param application_uuid: the application uuid :param application_uuid: the application uuid
:return: [Application] with the provided application_uuid :return: Application with the provided application_uuid
""" """
try: try:
return self.session.execute(select(Model).where( return self.session.execute(select(Model).where(
Model.application_uuid == application_uuid) Model.application_uuid == application_uuid)
).first() ).first()[0]
except SQLAlchemyError as err: except SQLAlchemyError as err:
logger.error(f'Error while fetching application by application_uuid: {err}') logger.error(f'Error while fetching application by application_uuid: {err}')