From f99b8497ff41928f39a9f7d28ba8aea84e0ecb19 Mon Sep 17 00:00:00 2001 From: guille Date: Thu, 2 Feb 2023 06:12:24 -0500 Subject: [PATCH] Persistence up and running with basic app applicartion --- hub/exports/db_factory.py | 28 ++++++------------ hub/imports/db_factory.py | 31 ++++---------------- hub/persistence/__init__.py | 1 + hub/persistence/configuration.py | 3 +- hub/persistence/db_setup.py | 10 ++++--- hub/persistence/models/application.py | 5 ++-- hub/persistence/models/city.py | 5 ++-- hub/persistence/models/city_object.py | 5 ++-- hub/persistence/models/simulation_results.py | 5 ++-- hub/persistence/models/user.py | 6 ++-- hub/persistence/repositories/application.py | 7 +++-- 11 files changed, 43 insertions(+), 63 deletions(-) diff --git a/hub/exports/db_factory.py b/hub/exports/db_factory.py index 482ed481..406818c3 100644 --- a/hub/exports/db_factory.py +++ b/hub/exports/db_factory.py @@ -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) diff --git a/hub/imports/db_factory.py b/hub/imports/db_factory.py index f4d75ba8..d23f2428 100644 --- a/hub/imports/db_factory.py +++ b/hub/imports/db_factory.py @@ -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) diff --git a/hub/persistence/__init__.py b/hub/persistence/__init__.py index 2a7eff84..264a5ee7 100644 --- a/hub/persistence/__init__.py +++ b/hub/persistence/__init__.py @@ -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 diff --git a/hub/persistence/configuration.py b/hub/persistence/configuration.py index 9c01ebe1..c3afdc77 100644 --- a/hub/persistence/configuration.py +++ b/hub/persistence/configuration.py @@ -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: """ diff --git a/hub/persistence/db_setup.py b/hub/persistence/db_setup.py index e0acfd29..cf3786f1 100644 --- a/hub/persistence/db_setup.py +++ b/hub/persistence/db_setup.py @@ -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): @@ -62,4 +64,4 @@ class DBSetup: logger.info(user) else: print(f'Created Admin user') - logger.info(f'Created Admin user') \ No newline at end of file + logger.info(f'Created Admin user') diff --git a/hub/persistence/models/application.py b/hub/persistence/models/application.py index 37f4d76d..3da947f4 100644 --- a/hub/persistence/models/application.py +++ b/hub/persistence/models/application.py @@ -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) diff --git a/hub/persistence/models/city.py b/hub/persistence/models/city.py index 45dfeaab..dfec2b63 100644 --- a/hub/persistence/models/city.py +++ b/hub/persistence/models/city.py @@ -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) diff --git a/hub/persistence/models/city_object.py b/hub/persistence/models/city_object.py index 86ee798c..0d2a47e7 100644 --- a/hub/persistence/models/city_object.py +++ b/hub/persistence/models/city_object.py @@ -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) diff --git a/hub/persistence/models/simulation_results.py b/hub/persistence/models/simulation_results.py index a985148c..256cbea5 100644 --- a/hub/persistence/models/simulation_results.py +++ b/hub/persistence/models/simulation_results.py @@ -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) diff --git a/hub/persistence/models/user.py b/hub/persistence/models/user.py index b53132ae..a40ccece 100644 --- a/hub/persistence/models/user.py +++ b/hub/persistence/models/user.py @@ -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) diff --git a/hub/persistence/repositories/application.py b/hub/persistence/repositories/application.py index 408221bd..7df3cc9d 100644 --- a/hub/persistence/repositories/application.py +++ b/hub/persistence/repositories/application.py @@ -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}') +