2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
Simulation results repository with database CRUD operations
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca
|
|
|
|
"""
|
|
|
|
import datetime
|
2023-05-17 17:10:30 -04:00
|
|
|
import logging
|
2023-02-01 07:29:06 -05:00
|
|
|
|
2023-05-17 17:10:30 -04:00
|
|
|
from sqlalchemy import or_
|
2023-02-01 07:29:06 -05:00
|
|
|
from sqlalchemy import select
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2023-10-13 02:57:42 -04:00
|
|
|
from sqlalchemy.orm import Session
|
2023-02-01 07:29:06 -05:00
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
from hub.persistence.repository import Repository
|
2023-02-01 07:29:06 -05:00
|
|
|
from hub.persistence.models import City
|
|
|
|
from hub.persistence.models import CityObject
|
2023-05-17 17:10:30 -04:00
|
|
|
from hub.persistence.models import SimulationResults as Model
|
2023-02-01 07:29:06 -05:00
|
|
|
|
|
|
|
|
|
|
|
class SimulationResults(Repository):
|
2023-05-18 12:29:28 -04:00
|
|
|
"""
|
|
|
|
Simulation results repository
|
|
|
|
"""
|
2023-02-01 07:29:06 -05:00
|
|
|
_instance = None
|
|
|
|
|
|
|
|
def __init__(self, db_name: str, dotenv_path: str, app_env: str):
|
|
|
|
super().__init__(db_name, dotenv_path, app_env)
|
|
|
|
|
|
|
|
def __new__(cls, db_name, dotenv_path, app_env):
|
|
|
|
"""
|
|
|
|
Implemented for a singleton pattern
|
|
|
|
"""
|
|
|
|
if cls._instance is None:
|
|
|
|
cls._instance = super(SimulationResults, cls).__new__(cls)
|
|
|
|
return cls._instance
|
|
|
|
|
2023-05-18 16:40:52 -04:00
|
|
|
def insert(self, name: str, values: str, city_id=None, city_object_id=None):
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
Inserts simulations results linked either with a city as a whole or with a city object
|
|
|
|
:param name: results name
|
|
|
|
:param values: the simulation results in json format
|
|
|
|
:param city_id: optional city id
|
|
|
|
:param city_object_id: optional city object id
|
2023-05-19 13:15:40 -04:00
|
|
|
:return: Identity id
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
if city_id is not None:
|
2023-05-18 16:40:52 -04:00
|
|
|
_ = self._get_city(city_id)
|
2023-02-01 07:29:06 -05:00
|
|
|
else:
|
2023-05-18 16:40:52 -04:00
|
|
|
_ = self._get_city_object(city_object_id)
|
2023-02-01 07:29:06 -05:00
|
|
|
try:
|
|
|
|
simulation_result = Model(name=name,
|
|
|
|
values=values,
|
|
|
|
city_id=city_id,
|
|
|
|
city_object_id=city_object_id)
|
2023-10-13 02:57:42 -04:00
|
|
|
with Session(self.engine) as session:
|
|
|
|
session.add(simulation_result)
|
|
|
|
session.flush()
|
|
|
|
session.commit()
|
|
|
|
session.refresh(simulation_result)
|
|
|
|
return simulation_result.id
|
2023-02-01 07:29:06 -05:00
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 16:40:52 -04:00
|
|
|
logging.error('An error occurred while creating city_object %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2023-02-01 07:29:06 -05:00
|
|
|
|
2023-05-18 16:40:52 -04:00
|
|
|
def update(self, name: str, values: str, city_id=None, city_object_id=None):
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
Updates simulation results for a city or a city object
|
|
|
|
:param name: The simulation results tool and workflow name
|
|
|
|
:param values: the simulation results in json format
|
|
|
|
:param city_id: optional city id
|
|
|
|
:param city_object_id: optional city object id
|
2023-05-18 16:40:52 -04:00
|
|
|
:return: None
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
try:
|
2023-10-13 02:57:42 -04:00
|
|
|
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(
|
2023-02-01 07:29:06 -05:00
|
|
|
{
|
|
|
|
'values': values,
|
|
|
|
'updated': datetime.datetime.utcnow()
|
|
|
|
})
|
2023-10-13 02:57:42 -04:00
|
|
|
session.commit()
|
|
|
|
elif city_object_id is not None:
|
|
|
|
session.query(Model).filter(Model.name == name, Model.city_object_id == city_object_id).update(
|
|
|
|
{
|
|
|
|
'values': values,
|
|
|
|
'updated': datetime.datetime.utcnow()
|
|
|
|
})
|
|
|
|
session.commit()
|
|
|
|
else:
|
|
|
|
raise NotImplementedError('Missing either city_id or city_object_id')
|
2023-02-01 07:29:06 -05:00
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 16:40:52 -04:00
|
|
|
logging.error('Error while updating city object %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2023-02-01 07:29:06 -05:00
|
|
|
|
2023-05-18 16:40:52 -04:00
|
|
|
def delete(self, name: str, city_id=None, city_object_id=None):
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
Deletes an application with the application_uuid
|
|
|
|
:param name: The simulation results tool and workflow name
|
|
|
|
:param city_id: The id for the city owning the simulation results
|
2023-03-13 14:18:04 -04:00
|
|
|
:param city_object_id: the id for the city_object owning these simulation results
|
2023-05-18 16:40:52 -04:00
|
|
|
:return: None
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
try:
|
2023-10-13 02:57:42 -04:00
|
|
|
with Session(self.engine) as session:
|
|
|
|
if city_id is not None:
|
|
|
|
session.query(Model).filter(Model.name == name, Model.city_id == city_id).delete()
|
|
|
|
session.commit()
|
|
|
|
elif city_object_id is not None:
|
|
|
|
session.query(Model).filter(Model.name == name, Model.city_object_id == city_object_id).delete()
|
|
|
|
session.commit()
|
|
|
|
else:
|
|
|
|
raise NotImplementedError('Missing either city_id or city_object_id')
|
2023-02-01 07:29:06 -05:00
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 12:29:28 -04:00
|
|
|
logging.error('Error while deleting application: %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2023-02-01 07:29:06 -05:00
|
|
|
|
2023-05-18 16:40:52 -04:00
|
|
|
def _get_city(self, city_id) -> City:
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
Fetch a city object based city id
|
|
|
|
:param city_id: a city identifier
|
|
|
|
:return: [City] with the provided city_id
|
|
|
|
"""
|
|
|
|
try:
|
2023-10-13 02:57:42 -04:00
|
|
|
with Session(self.engine) as session:
|
|
|
|
return session.execute(select(City).where(City.id == city_id)).first()
|
2023-02-01 07:29:06 -05:00
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 12:29:28 -04:00
|
|
|
logging.error('Error while fetching city by city_id: %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2023-02-01 07:29:06 -05:00
|
|
|
|
2023-03-10 14:04:00 -05:00
|
|
|
def _get_city_object(self, city_object_id) -> [CityObject]:
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
Fetch a city object based city id
|
|
|
|
:param city_object_id: a city object identifier
|
|
|
|
:return: [CityObject] with the provided city_object_id
|
|
|
|
"""
|
|
|
|
try:
|
2023-10-13 02:57:42 -04:00
|
|
|
with Session(self.engine) as session:
|
|
|
|
return session.execute(select(CityObject).where(CityObject.id == city_object_id)).first()
|
2023-02-01 07:29:06 -05:00
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 12:29:28 -04:00
|
|
|
logging.error('Error while fetching city by city_id: %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2023-03-13 14:18:04 -04:00
|
|
|
|
2023-05-18 16:46:50 -04:00
|
|
|
def get_simulation_results_by_city_id_city_object_id_and_names(self, city_id, city_object_id, result_names=None) -> [Model]:
|
2023-03-13 14:18:04 -04:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
:param city_object_id: the city object id
|
|
|
|
:param result_names: if given filter the results
|
|
|
|
:return: [SimulationResult]
|
|
|
|
"""
|
|
|
|
try:
|
2023-10-13 02:57:42 -04:00
|
|
|
with Session(self.engine) as session:
|
|
|
|
result_set = session.execute(select(Model).where(or_(
|
|
|
|
Model.city_id == city_id,
|
|
|
|
Model.city_object_id == city_object_id
|
|
|
|
)))
|
|
|
|
results = [r[0] for r in result_set]
|
|
|
|
if not result_names:
|
|
|
|
return results
|
|
|
|
filtered_results = []
|
|
|
|
for result in results:
|
|
|
|
if result.name in result_names:
|
|
|
|
filtered_results.append(result)
|
|
|
|
return filtered_results
|
2023-03-13 14:18:04 -04:00
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 12:29:28 -04:00
|
|
|
logging.error('Error while fetching city by city_id: %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2023-11-16 01:39:46 -05:00
|
|
|
|
|
|
|
def get_simulation_results_by_city_object_id_and_names(self, city_object_id, result_names=None) -> [Model]:
|
|
|
|
"""
|
|
|
|
Fetch the simulation results based in the city_object_id with the given names or all
|
|
|
|
:param city_object_id: the city object id
|
|
|
|
:param result_names: if given filter the results
|
|
|
|
:return: [SimulationResult]
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
with Session(self.engine) as session:
|
|
|
|
result_set = session.execute(select(Model).where(
|
|
|
|
Model.city_object_id == city_object_id
|
|
|
|
))
|
|
|
|
results = [r[0] for r in result_set]
|
|
|
|
if not result_names:
|
|
|
|
return results
|
|
|
|
filtered_results = []
|
|
|
|
for result in results:
|
|
|
|
if result.name in result_names:
|
|
|
|
filtered_results.append(result)
|
|
|
|
return filtered_results
|
|
|
|
except SQLAlchemyError as err:
|
|
|
|
logging.error('Error while fetching city by city_id: %s', err)
|
|
|
|
raise SQLAlchemyError from err
|