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
Project CoderPeter Yefi peteryefi@gmail.com
"""
from hub.persistence import CityRepo
from hub.persistence import HeatPumpSimulationRepo
from hub.persistence import City
from hub.persistence import Application
class DBFactory:
@ -14,40 +14,30 @@ class DBFactory:
"""
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._hp_simulation_repo = HeatPumpSimulationRepo(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._application = Application(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
def get_city(self, city_id):
"""
Retrieve a single city from postgres
: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):
"""
Retrieve a single city from postgres
: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):
"""
Retrieve cities created by 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):
"""
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 application_info(self, application_uuid):
return self._application.get_by_uuid(application_uuid)
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
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.persistence import City as CityRepository
class DBFactory:
@ -16,14 +14,13 @@ class DBFactory:
"""
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._hp_simulation_repo = HeatPumpSimulationRepo(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)
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
"""
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):
"""
@ -31,27 +28,11 @@ class DBFactory:
:param city_id: the id of the city to update
: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):
"""
Deletes a single city from postgres
:param city_id: the id of the city to get
"""
self._city_repo.delete_city(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)
self._city_repository.delete(city_id)

View File

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

View File

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

View File

@ -30,15 +30,15 @@ class DBSetup:
# Create the tables using the models
Application.__table__.create(bind=repository.engine, checkfirst=True)
User.__table__.create(bind=repository.engine, checkfirst=True)
City.__table__.create(bind=repository.engine, checkfirst=True)
CityObject.__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._application_repo = ApplicationRepository(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
self._create_admin_user(self._user_repo, admin_password)
self._create_admin_app(self._application_repo, application_uuid)
application_id = self._create_admin_app(self._application_repo, application_uuid)
self._create_admin_user(self._user_repo, admin_password, application_id)
@staticmethod
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'
print('Creating default admin tool application...')
application = application_repo.insert(name, description, application_uuid)
if type(application) is dict:
logger.info(application)
else:
msg = f'Created Admin tool with application_uuid: {application_uuid}'
print(msg)
logger.info(msg)
return application.id
@staticmethod
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 import Column, Integer, String, Sequence
from sqlalchemy import DateTime
from hub.persistence.configuration import Models
class Application:
class Application(Models):
"""
A model representation of an application
"""
__table__ = "application"
__tablename__ = 'application'
id = Column(Integer, Sequence('application_id_seq'), primary_key=True)
name = 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 DateTime, PickleType
from hub.persistence.configuration import Models
class City:
class City(Models):
"""A model representation of a city
"""
__table__ = "city"
__tablename__ = 'city'
id = Column(Integer, Sequence('city_id_seq'), primary_key=True)
city = Column(PickleType, 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 DateTime
from hub.persistence.configuration import Models
class CityObject:
class CityObject(Models):
"""
A model representation of an application
"""
__table__ = "city_object"
__tablename__ = 'city_object'
id = Column(Integer, Sequence('application_id_seq'), primary_key=True)
city_id = Column(Integer, ForeignKey('city.id'), 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 DateTime
from sqlalchemy.dialects.postgresql import JSONB
from hub.persistence.configuration import Models
class SimulationResults:
class SimulationResults(Models):
"""
A model representation of an application
"""
__table__ = "simulation_results"
__tablename__ = 'simulation_results'
id = Column(Integer, Sequence('application_id_seq'), primary_key=True)
city_id = Column(Integer, ForeignKey('city.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 DateTime, Enum
from hub.persistence.configuration import Models
class UserRoles(enum.Enum):
Admin = 'Admin'
Hub_Reader = 'Hub_Reader'
class User:
class User(Models):
"""
A model representation of a city
"""
__table__ = "user"
__tablename__ = 'user'
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
name = Column(String, nullable=False)
password = Column(String, nullable=False)

View File

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