2022-11-15 20:48:42 -05:00
|
|
|
"""
|
|
|
|
DBFactory performs read related operations
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project CoderPeter Yefi peteryefi@gmail.com
|
|
|
|
"""
|
2023-05-19 13:15:40 -04:00
|
|
|
from typing import Dict
|
2023-03-13 14:18:04 -04:00
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
from hub.persistence.repositories.application import Application
|
2023-07-31 12:42:02 -04:00
|
|
|
from hub.persistence.repositories.city import City
|
2023-05-19 13:15:40 -04:00
|
|
|
from hub.persistence.repositories.city_object import CityObject
|
|
|
|
from hub.persistence.repositories.simulation_results import SimulationResults
|
|
|
|
from hub.persistence.repositories.user import User
|
|
|
|
from hub.persistence.repositories.user import UserRoles
|
2022-11-15 20:48:42 -05:00
|
|
|
|
|
|
|
|
2023-05-17 12:30:11 -04:00
|
|
|
class DBControl:
|
2022-11-15 20:48:42 -05:00
|
|
|
"""
|
|
|
|
DBFactory class
|
|
|
|
"""
|
|
|
|
|
2022-12-07 19:06:17 -05:00
|
|
|
def __init__(self, db_name, app_env, dotenv_path):
|
2023-07-31 12:42:02 -04:00
|
|
|
self._city = City(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
|
2023-02-02 06:12:24 -05:00
|
|
|
self._application = Application(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
|
2023-02-02 13:00:58 -05:00
|
|
|
self._user = User(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
|
|
|
|
self._city_object = CityObject(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
|
2023-03-03 17:19:36 -05:00
|
|
|
self._simulation_results = SimulationResults(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
|
2022-11-15 20:48:42 -05:00
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
def application_info(self, application_uuid) -> Application:
|
2023-03-03 17:19:36 -05:00
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Retrieve the application info for the given uuid from the database
|
2023-03-10 14:04:00 -05:00
|
|
|
:param application_uuid: the uuid for the application
|
2023-05-19 13:15:40 -04:00
|
|
|
:return: Application
|
2023-03-03 17:19:36 -05:00
|
|
|
"""
|
2023-02-02 06:12:24 -05:00
|
|
|
return self._application.get_by_uuid(application_uuid)
|
2022-11-22 20:20:12 -05:00
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
def user_info(self, name, password, application_id) -> User:
|
2023-03-13 14:18:04 -04:00
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Retrieve the user info for the given name and password and application_id from the database
|
2023-05-16 18:06:22 -04:00
|
|
|
:param name: the username
|
2023-03-13 14:18:04 -04:00
|
|
|
:param password: the user password
|
|
|
|
:param application_id: the application id
|
2023-05-19 13:15:40 -04:00
|
|
|
:return: User
|
2023-03-13 14:18:04 -04:00
|
|
|
"""
|
|
|
|
return self._user.get_by_name_application_id_and_password(name, password, application_id)
|
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
def user_login(self, name, password, application_uuid) -> User:
|
2023-03-10 14:04:00 -05:00
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Retrieve the user info from the database
|
2023-05-16 18:06:22 -04:00
|
|
|
:param name: the username
|
2023-03-10 14:04:00 -05:00
|
|
|
:param password: the user password
|
|
|
|
:param application_uuid: the application uuid
|
2023-05-19 13:15:40 -04:00
|
|
|
:return: User
|
2023-03-10 14:04:00 -05:00
|
|
|
"""
|
2023-02-21 09:41:26 -05:00
|
|
|
return self._user.get_by_name_application_uuid_and_password(name, password, application_uuid)
|
2023-02-02 13:00:58 -05:00
|
|
|
|
2023-03-13 14:18:04 -04:00
|
|
|
def cities_by_user_and_application(self, user_id, application_id) -> [City]:
|
2023-03-13 14:41:25 -04:00
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Retrieve the cities belonging to the user and the application from the database
|
2023-03-13 14:41:25 -04:00
|
|
|
:param user_id: User id
|
|
|
|
:param application_id: Application id
|
|
|
|
:return: [City]
|
|
|
|
"""
|
2023-07-31 12:42:02 -04:00
|
|
|
return self._city.get_by_user_id_and_application_id(user_id, application_id)
|
|
|
|
|
|
|
|
def building(self, name, user_id, application_id, scenario) -> CityObject:
|
|
|
|
"""
|
|
|
|
Retrieve the building from the database
|
|
|
|
:param name: Building name
|
|
|
|
:param user_id: User id
|
|
|
|
:param application_id: Application id
|
|
|
|
:param scenario: Scenario
|
|
|
|
:
|
|
|
|
"""
|
|
|
|
cities = self._city.get_by_user_id_application_id_and_scenario(user_id, application_id, scenario)
|
2023-11-20 09:38:49 -05:00
|
|
|
c = [c[0].id for c in cities]
|
2023-11-27 01:00:39 -05:00
|
|
|
result = self._city_object.building_in_cities_info(name, c)
|
|
|
|
if result is not None:
|
|
|
|
return result
|
2023-07-31 12:42:02 -04:00
|
|
|
return None
|
2023-03-13 14:18:04 -04:00
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
def building_info(self, name, city_id) -> CityObject:
|
2023-03-13 14:41:25 -04:00
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Retrieve the building info from the database
|
2023-03-13 14:41:25 -04:00
|
|
|
:param name: Building name
|
2023-05-17 17:10:30 -04:00
|
|
|
:param city_id: City ID
|
2023-05-19 13:15:40 -04:00
|
|
|
:return: CityObject
|
2023-03-13 14:41:25 -04:00
|
|
|
"""
|
2023-07-26 15:03:31 -04:00
|
|
|
return self._city_object.get_by_name_or_alias_and_city(name, city_id)
|
2023-03-13 14:41:25 -04:00
|
|
|
|
2023-11-20 09:38:49 -05:00
|
|
|
def building_info_in_cities(self, name, cities) -> CityObject:
|
|
|
|
"""
|
|
|
|
Retrieve the building info from the database
|
|
|
|
:param name: Building name
|
|
|
|
:param cities: [City ID]
|
|
|
|
:return: CityObject
|
|
|
|
"""
|
|
|
|
return self._city_object.get_by_name_or_alias_in_cities(name, cities)
|
|
|
|
|
2023-12-06 15:15:36 -05:00
|
|
|
def buildings_info(self, user_id, application_id, names_or_aliases) -> [CityObject]:
|
2023-07-28 14:55:06 -04:00
|
|
|
"""
|
2023-07-31 12:42:02 -04:00
|
|
|
Retrieve the buildings info from the database
|
2023-12-06 15:15:36 -05:00
|
|
|
:param user_id: User ID
|
|
|
|
:param application_id: Application ID
|
|
|
|
:param names_or_aliases: A list of names or alias for the buildings
|
2023-07-28 14:55:06 -04:00
|
|
|
:return: [CityObject]
|
|
|
|
"""
|
2023-12-06 15:15:36 -05:00
|
|
|
results = self._city_object.get_by_name_or_alias_for_user_app(user_id, application_id, names_or_aliases)
|
|
|
|
if results is None:
|
|
|
|
return []
|
|
|
|
return results
|
2023-07-28 14:55:06 -04:00
|
|
|
|
|
|
|
def results(self, user_id, application_id, request_values, result_names=None) -> Dict:
|
2023-03-10 14:04:00 -05:00
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Retrieve the simulation results for the given cities from the database
|
2023-03-10 14:04:00 -05:00
|
|
|
:param user_id: the user id owning the results
|
|
|
|
:param application_id: the application id owning the results
|
2023-07-28 14:55:06 -04:00
|
|
|
:param request_values: dictionary containing the scenario and building names to grab the results
|
2023-03-10 14:04:00 -05:00
|
|
|
:param result_names: if given, filter the results to the selected names
|
|
|
|
"""
|
2023-03-13 14:41:25 -04:00
|
|
|
if result_names is None:
|
|
|
|
result_names = []
|
2023-03-13 14:18:04 -04:00
|
|
|
results = {}
|
2023-07-28 14:55:06 -04:00
|
|
|
for scenario in request_values['scenarios']:
|
2023-08-03 12:06:45 -04:00
|
|
|
for scenario_name in scenario.keys():
|
|
|
|
result_sets = self._city.get_by_user_id_application_id_and_scenario(
|
|
|
|
user_id,
|
|
|
|
application_id,
|
|
|
|
scenario_name
|
|
|
|
)
|
|
|
|
if result_sets is None:
|
|
|
|
continue
|
2023-10-27 04:28:20 -04:00
|
|
|
results[scenario_name] = []
|
2023-11-16 01:39:46 -05:00
|
|
|
city_ids = [r[0].id for r in result_sets]
|
|
|
|
for building_name in scenario[scenario_name]:
|
|
|
|
_building = self._city_object.get_by_name_or_alias_in_cities(building_name, city_ids)
|
|
|
|
if _building is None:
|
|
|
|
continue
|
|
|
|
city_object_id = _building.id
|
|
|
|
_ = self._simulation_results.get_simulation_results_by_city_object_id_and_names(
|
|
|
|
city_object_id,
|
|
|
|
result_names)
|
2023-08-03 12:06:45 -04:00
|
|
|
|
2023-11-16 01:39:46 -05:00
|
|
|
for value in _:
|
2023-11-24 03:01:49 -05:00
|
|
|
values = value.values
|
2023-11-16 01:39:46 -05:00
|
|
|
values["building"] = building_name
|
|
|
|
results[scenario_name].append(values)
|
2023-03-10 14:04:00 -05:00
|
|
|
return results
|
2023-05-17 12:30:11 -04:00
|
|
|
|
2023-07-26 15:03:31 -04:00
|
|
|
def persist_city(self, city: City, pickle_path, scenario, application_id: int, user_id: int):
|
2023-05-17 12:30:11 -04:00
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Creates a city into the database
|
2023-05-17 12:30:11 -04:00
|
|
|
:param city: City to be stored
|
|
|
|
:param pickle_path: Path to save the pickle file
|
2023-07-26 15:03:31 -04:00
|
|
|
:param scenario: Simulation scenario name
|
2023-05-17 12:30:11 -04:00
|
|
|
:param application_id: Application id owning this city
|
|
|
|
:param user_id: User who create the city
|
2023-05-19 13:15:40 -04:00
|
|
|
return identity_id
|
2023-05-17 12:30:11 -04:00
|
|
|
"""
|
2023-07-31 12:42:02 -04:00
|
|
|
return self._city.insert(city, pickle_path, scenario, application_id, user_id)
|
2023-05-17 12:30:11 -04:00
|
|
|
|
|
|
|
def update_city(self, city_id, city):
|
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Update an existing city in the database
|
2023-05-17 12:30:11 -04:00
|
|
|
:param city_id: the id of the city to update
|
|
|
|
:param city: the updated city object
|
|
|
|
"""
|
2023-07-31 12:42:02 -04:00
|
|
|
return self._city.update(city_id, city)
|
2023-05-17 12:30:11 -04:00
|
|
|
|
|
|
|
def persist_application(self, name: str, description: str, application_uuid: str):
|
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Creates information for an application in the database
|
2023-05-17 12:30:11 -04:00
|
|
|
:param name: name of application
|
|
|
|
:param description: the description of the application
|
|
|
|
:param application_uuid: the uuid of the application to be created
|
|
|
|
"""
|
|
|
|
return self._application.insert(name, description, application_uuid)
|
|
|
|
|
|
|
|
def update_application(self, name: str, description: str, application_uuid: str):
|
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Update the application information stored in the database
|
2023-05-17 12:30:11 -04:00
|
|
|
:param name: name of application
|
|
|
|
:param description: the description of the application
|
|
|
|
:param application_uuid: the uuid of the application to be created
|
|
|
|
"""
|
|
|
|
return self._application.update(application_uuid, name, description)
|
|
|
|
|
|
|
|
def add_simulation_results(self, name, values, city_id=None, city_object_id=None):
|
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Add simulation results to the city or to the city_object to the database
|
2023-05-17 12:30:11 -04:00
|
|
|
:param name: simulation and simulation engine name
|
|
|
|
:param values: simulation values in json format
|
|
|
|
:param city_id: city id or None
|
|
|
|
:param city_object_id: city object id or None
|
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
return self._simulation_results.insert(name, values, city_id, city_object_id)
|
2023-05-17 12:30:11 -04:00
|
|
|
|
|
|
|
def create_user(self, name: str, application_id: int, password: str, role: UserRoles):
|
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Creates a new user in the database
|
2023-05-17 12:30:11 -04:00
|
|
|
:param name: the name of the user
|
|
|
|
:param application_id: the application id of the user
|
|
|
|
:param password: the password of the user
|
|
|
|
:param role: the role of the user
|
|
|
|
"""
|
|
|
|
return self._user.insert(name, password, role, application_id)
|
|
|
|
|
|
|
|
def update_user(self, user_id: int, name: str, password: str, role: UserRoles):
|
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Updates a user in the database
|
2023-05-17 12:30:11 -04:00
|
|
|
:param user_id: the id of the user
|
|
|
|
:param name: the name of the user
|
|
|
|
:param password: the password of the user
|
|
|
|
:param role: the role of the user
|
|
|
|
"""
|
|
|
|
return self._user.update(user_id, name, password, role)
|
|
|
|
|
2023-05-23 14:36:48 -04:00
|
|
|
def get_by_name_and_application(self, name: str, application: int):
|
|
|
|
"""
|
|
|
|
Retrieve a single user from the database
|
|
|
|
:param name: username
|
|
|
|
:param application: application accessing hub
|
|
|
|
"""
|
|
|
|
return self._user.get_by_name_and_application(name, application)
|
|
|
|
|
2023-05-17 12:30:11 -04:00
|
|
|
def delete_user(self, user_id):
|
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Delete a single user from the database
|
2023-05-17 12:30:11 -04:00
|
|
|
:param user_id: the id of the user to delete
|
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
self._user.delete(user_id)
|
2023-05-17 12:30:11 -04:00
|
|
|
|
2023-05-23 14:36:48 -04:00
|
|
|
def delete_city(self, city_id):
|
2023-05-17 17:10:30 -04:00
|
|
|
"""
|
2023-05-23 14:36:48 -04:00
|
|
|
Deletes a single city from the database
|
|
|
|
:param city_id: the id of the city to get
|
2023-05-17 17:10:30 -04:00
|
|
|
"""
|
2023-07-31 12:42:02 -04:00
|
|
|
self._city.delete(city_id)
|
2023-05-23 14:36:48 -04:00
|
|
|
|
|
|
|
def delete_results_by_name(self, name, city_id=None, city_object_id=None):
|
|
|
|
"""
|
|
|
|
Deletes city object simulation results from the database
|
|
|
|
:param name: simulation name
|
|
|
|
:param city_id: if given, delete delete the results for the city with id city_id
|
|
|
|
:param city_object_id: if given, delete delete the results for the city object with id city_object_id
|
|
|
|
"""
|
|
|
|
self._simulation_results.delete(name, city_id=city_id, city_object_id=city_object_id)
|
|
|
|
|
|
|
|
def delete_application(self, application_uuid):
|
|
|
|
"""
|
|
|
|
Deletes a single application from the database
|
|
|
|
:param application_uuid: the id of the application to get
|
|
|
|
"""
|
|
|
|
self._application.delete(application_uuid)
|