2022-11-11 16:16:31 -05:00
|
|
|
"""
|
|
|
|
City repository with database CRUD operations
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project Coder Peter Yefi peteryefi@gmail.com
|
|
|
|
"""
|
2023-01-31 13:11:39 -05:00
|
|
|
import datetime
|
2023-05-18 16:15:57 -04:00
|
|
|
import logging
|
2023-01-31 13:11:39 -05:00
|
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2023-08-03 12:06:45 -04:00
|
|
|
from sqlalchemy.orm import Session
|
2023-01-31 13:11:39 -05:00
|
|
|
|
2023-02-01 06:05:12 -05:00
|
|
|
from hub.city_model_structure.city import City as CityHub
|
2023-05-19 13:15:40 -04:00
|
|
|
from hub.persistence.repository import Repository
|
2023-02-01 06:05:12 -05:00
|
|
|
from hub.persistence.models import City as Model
|
2023-02-02 13:00:58 -05:00
|
|
|
from hub.persistence.models import CityObject
|
2023-01-31 13:11:39 -05:00
|
|
|
from hub.version import __version__
|
2022-11-11 16:16:31 -05:00
|
|
|
|
|
|
|
|
2023-02-01 06:05:12 -05:00
|
|
|
class City(Repository):
|
2023-05-18 12:29:28 -04:00
|
|
|
"""
|
|
|
|
City repository
|
|
|
|
"""
|
2022-11-15 20:48:42 -05:00
|
|
|
_instance = None
|
|
|
|
|
2022-12-07 19:06:17 -05:00
|
|
|
def __init__(self, db_name: str, dotenv_path: str, app_env: str):
|
2022-12-14 17:49:29 -05:00
|
|
|
super().__init__(db_name, dotenv_path, app_env)
|
2022-11-11 16:16:31 -05:00
|
|
|
|
2022-12-07 19:06:17 -05:00
|
|
|
def __new__(cls, db_name, dotenv_path, app_env):
|
2022-11-15 20:48:42 -05:00
|
|
|
"""
|
|
|
|
Implemented for a singleton pattern
|
|
|
|
"""
|
|
|
|
if cls._instance is None:
|
2023-02-01 06:05:12 -05:00
|
|
|
cls._instance = super(City, cls).__new__(cls)
|
2022-11-15 20:48:42 -05:00
|
|
|
return cls._instance
|
|
|
|
|
2023-07-26 15:03:31 -04:00
|
|
|
def insert(self, city: CityHub, pickle_path, scenario, application_id, user_id: int):
|
2023-01-31 13:11:39 -05:00
|
|
|
"""
|
2023-02-01 07:29:06 -05:00
|
|
|
Inserts a city
|
2023-01-31 13:11:39 -05:00
|
|
|
:param city: The complete city instance
|
2023-02-13 05:17:25 -05:00
|
|
|
:param pickle_path: Path to the pickle
|
2023-07-26 15:03:31 -04:00
|
|
|
:param scenario: Simulation scenario name
|
2023-01-31 13:11:39 -05:00
|
|
|
:param application_id: Application id owning the instance
|
|
|
|
:param user_id: User id owning the instance
|
2023-05-19 13:15:40 -04:00
|
|
|
:return: Identity id
|
2023-01-31 13:11:39 -05:00
|
|
|
"""
|
2023-02-13 05:17:25 -05:00
|
|
|
city.save_compressed(pickle_path)
|
2022-11-11 16:16:31 -05:00
|
|
|
try:
|
2023-02-01 06:05:12 -05:00
|
|
|
db_city = Model(
|
2023-02-13 05:17:25 -05:00
|
|
|
pickle_path,
|
2023-02-01 06:05:12 -05:00
|
|
|
city.name,
|
2023-07-26 15:03:31 -04:00
|
|
|
scenario,
|
2023-02-01 06:05:12 -05:00
|
|
|
application_id,
|
|
|
|
user_id,
|
|
|
|
__version__)
|
2022-11-11 16:16:31 -05:00
|
|
|
|
2023-01-31 13:11:39 -05:00
|
|
|
self.session.add(db_city)
|
|
|
|
self.session.flush()
|
2023-02-13 05:17:25 -05:00
|
|
|
self.session.commit()
|
2023-02-02 13:00:58 -05:00
|
|
|
for building in city.buildings:
|
|
|
|
db_city_object = CityObject(db_city.id,
|
2023-05-10 17:06:51 -04:00
|
|
|
building)
|
2023-02-02 13:00:58 -05:00
|
|
|
self.session.add(db_city_object)
|
|
|
|
self.session.flush()
|
2023-01-31 13:11:39 -05:00
|
|
|
self.session.commit()
|
2023-05-19 13:15:40 -04:00
|
|
|
self.session.refresh(db_city)
|
|
|
|
return db_city.id
|
2022-11-11 16:16:31 -05:00
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 16:40:52 -04:00
|
|
|
logging.error('An error occurred while creating a city %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2022-11-11 16:16:31 -05:00
|
|
|
|
2023-02-01 07:29:06 -05:00
|
|
|
def update(self, city_id: int, city: CityHub):
|
|
|
|
"""
|
|
|
|
Updates a city name (other updates makes no sense)
|
|
|
|
:param city_id: the id of the city to be updated
|
|
|
|
:param city: the city object
|
2023-05-18 16:15:57 -04:00
|
|
|
:return: None
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
now = datetime.datetime.utcnow()
|
2023-05-16 18:06:22 -04:00
|
|
|
self.session.query(Model).filter(Model.id == city_id).update({'name': city.name, 'updated': now})
|
2023-02-01 07:29:06 -05:00
|
|
|
self.session.commit()
|
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 16:40:52 -04:00
|
|
|
logging.error('Error while updating city %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2023-02-01 07:29:06 -05:00
|
|
|
|
|
|
|
def delete(self, city_id: int):
|
|
|
|
"""
|
|
|
|
Deletes a City with the id
|
|
|
|
:param city_id: the city id
|
2023-05-18 16:15:57 -04:00
|
|
|
:return: None
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
try:
|
2023-02-22 22:45:33 -05:00
|
|
|
self.session.query(CityObject).filter(CityObject.city_id == city_id).delete()
|
2023-02-01 07:29:06 -05:00
|
|
|
self.session.query(Model).filter(Model.id == city_id).delete()
|
|
|
|
self.session.commit()
|
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 16:40:52 -04:00
|
|
|
logging.error('Error while fetching city %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2023-02-01 07:29:06 -05:00
|
|
|
|
2023-07-28 08:25:47 -04:00
|
|
|
def get_by_user_id_application_id_and_scenario(self, user_id, application_id, scenario) -> [Model]:
|
2022-11-11 16:16:31 -05:00
|
|
|
"""
|
2023-03-10 14:04:00 -05:00
|
|
|
Fetch city based on the user who created it
|
|
|
|
:param user_id: the user id
|
|
|
|
:param application_id: the application id
|
2023-07-26 15:03:31 -04:00
|
|
|
:param scenario: simulation scenario name
|
|
|
|
:return: [ModelCity]
|
2022-11-11 16:16:31 -05:00
|
|
|
"""
|
|
|
|
try:
|
2023-03-10 14:04:00 -05:00
|
|
|
result_set = self.session.execute(select(Model).where(Model.user_id == user_id,
|
|
|
|
Model.application_id == application_id,
|
2023-07-26 15:03:31 -04:00
|
|
|
Model.scenario == scenario
|
|
|
|
)).all()
|
2023-08-03 12:06:45 -04:00
|
|
|
self.session.close()
|
|
|
|
self.session = Session(self.engine)
|
2023-03-10 14:04:00 -05:00
|
|
|
return result_set
|
2022-11-11 16:16:31 -05:00
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 16:40:52 -04:00
|
|
|
logging.error('Error while fetching city by name %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2023-03-13 14:18:04 -04:00
|
|
|
|
|
|
|
def get_by_user_id_and_application_id(self, user_id, application_id) -> [Model]:
|
|
|
|
"""
|
|
|
|
Fetch city based on the user who created it
|
|
|
|
:param user_id: the user id
|
|
|
|
:param application_id: the application id
|
|
|
|
:return: ModelCity
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
result_set = self.session.execute(
|
|
|
|
select(Model).where(Model.user_id == user_id, Model.application_id == application_id)
|
|
|
|
)
|
|
|
|
return [r[0] for r in result_set]
|
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 16:40:52 -04:00
|
|
|
logging.error('Error while fetching city by name %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2023-07-28 08:25:47 -04:00
|
|
|
|