persistence changes

This commit is contained in:
Guille Gutierrez 2023-07-28 08:25:47 -04:00
parent 4add8b2cff
commit f95c45660e
5 changed files with 59 additions and 31 deletions

View File

@ -86,25 +86,33 @@ class DBControl:
result_names = [] result_names = []
results = {} results = {}
for city in cities['cities']: for city in cities['cities']:
city_name = next(iter(city)) scenario_name = next(iter(city))
result_set = self._city_repository.get_by_user_id_application_id_and_name(user_id, application_id, city_name) result_sets = self._city_repository.get_by_user_id_application_id_and_scenario(
if result_set is None: user_id,
application_id,
scenario_name
)
if result_sets is None:
continue continue
city_id = result_set.id for result_set in result_sets:
results[city_name] = [] city_id = result_set[0].id
for building_name in city[city_name]: print('city ids', city_id)
if self._city_object.get_by_name_or_alias_and_city(building_name, city_id) is None: results[scenario_name] = []
continue for building_name in city[scenario_name]:
city_object_id = self._city_object.get_by_name_or_alias_and_city(building_name, city_id).id _building = self._city_object.get_by_name_or_alias_and_city(building_name, city_id)
_ = self._simulation_results.get_simulation_results_by_city_id_city_object_id_and_names( if _building is None:
city_id, continue
city_object_id, city_object_id = _building.id
result_names) print('city object ids', city_object_id)
_ = self._simulation_results.get_simulation_results_by_city_id_city_object_id_and_names(
city_id,
city_object_id,
result_names)
for value in _: for value in _:
values = json.loads(value.values) values = json.loads(value.values)
values["building"] = building_name values["building"] = building_name
results[city_name].append(values) results[scenario_name].append(values)
return results return results
def persist_city(self, city: City, pickle_path, scenario, application_id: int, user_id: int): def persist_city(self, city: City, pickle_path, scenario, application_id: int, user_id: int):

View File

@ -98,7 +98,7 @@ class City(Repository):
logging.error('Error while fetching city %s', err) logging.error('Error while fetching city %s', err)
raise SQLAlchemyError from err raise SQLAlchemyError from err
def get_by_user_id_application_id_and_scenario(self, user_id, application_id, scenario) -> Model: def get_by_user_id_application_id_and_scenario(self, user_id, application_id, scenario) -> [Model]:
""" """
Fetch city based on the user who created it Fetch city based on the user who created it
:param user_id: the user id :param user_id: the user id
@ -131,3 +131,4 @@ class City(Repository):
except SQLAlchemyError as err: except SQLAlchemyError as err:
logging.error('Error while fetching city by name %s', err) logging.error('Error while fetching city by name %s', err)
raise SQLAlchemyError from err raise SQLAlchemyError from err

View File

@ -101,14 +101,23 @@ class CityObject(Repository):
Fetch a city object based on name and city id Fetch a city object based on name and city id
:param name: city object name :param name: city object name
:param city_id: a city identifier :param city_id: a city identifier
:return: [CityObject] with the provided name belonging to the city with id city_id :return: [CityObject] with the provided name or alias belonging to the city with id city_id
""" """
_city_object = None
try: try:
_city_object = self.session.execute(select(Model).where( _city_objects = self.session.execute(select(Model).where(
or_(Model.name == name, Model.aliases.contains(f'%{name}%')), Model.city_id == city_id or_(Model.name == name, Model.aliases.contains(f'{name}')), Model.city_id == city_id
)).first() )).all()
return _city_object[0] for city_object in _city_objects:
if city_object[0].name == name:
return city_object[0]
aliases = city_object[0].aliases.replace('{', '').replace('}', '').split(',')
for alias in aliases:
print(alias, name)
if alias == name:
# force the name as the alias
city_object[0].name = name
return city_object[0]
return None
except SQLAlchemyError as err: except SQLAlchemyError as err:
logging.error('Error while fetching city object by name and city: %s', err) logging.error('Error while fetching city object by name and city: %s', err)
raise SQLAlchemyError from err raise SQLAlchemyError from err

View File

@ -65,13 +65,13 @@ class Control:
self._skip_test = True self._skip_test = True
self._skip_reason = f'{operational_error}' self._skip_reason = f'{operational_error}'
return return
"""
Application.__table__.create(bind=repository.engine, checkfirst=True) Application.__table__.create(bind=repository.engine, checkfirst=True)
User.__table__.create(bind=repository.engine, checkfirst=True) User.__table__.create(bind=repository.engine, checkfirst=True)
City.__table__.create(bind=repository.engine, checkfirst=True) City.__table__.create(bind=repository.engine, checkfirst=True)
CityObject.__table__.create(bind=repository.engine, checkfirst=True) CityObject.__table__.create(bind=repository.engine, checkfirst=True)
SimulationResults.__table__.create(bind=repository.engine, checkfirst=True) SimulationResults.__table__.create(bind=repository.engine, checkfirst=True)
"""
city_file = Path('tests_data/test.geojson').resolve() city_file = Path('tests_data/test.geojson').resolve()
output_path = Path('tests_outputs/').resolve() output_path = Path('tests_outputs/').resolve()
self._city = GeometryFactory('geojson', self._city = GeometryFactory('geojson',
@ -103,11 +103,19 @@ class Control:
app_env='TEST', app_env='TEST',
dotenv_path=dotenv_path) dotenv_path=dotenv_path)
self._application_uuid = str(uuid.uuid4()) self._application_uuid = '60b7fc1b-f389-4254-9ffd-22a4cf32c7a3'
self._application_id = self._database.persist_application('test', 'test application', self.application_uuid) self._application_id = 1
self._user_id = self._database.create_user('Admin', self._application_id, 'Admin@123', UserRoles.Admin) self._user_id = 1
"""
self._application_id = self._database.persist_application(
'City_layers',
'City layers test user',
self.application_uuid
)
self._user_id = self._database.create_user('city_layers', self._application_id, 'city_layers', UserRoles.Admin)
"""
self._pickle_path = 'tests_data/pickle_path.bz2' self._pickle_path = 'tests_data/pickle_path.bz2'
print('done')
@property @property
def database(self): def database(self):
@ -182,6 +190,7 @@ TestDBFactory
def test_get_update_city(self): def test_get_update_city(self):
city_id = control.database.persist_city(control.city, city_id = control.database.persist_city(control.city,
control.pickle_path, control.pickle_path,
control.city.name,
control.application_id, control.application_id,
control.user_id) control.user_id)
control.city.name = "Ottawa" control.city.name = "Ottawa"
@ -200,6 +209,7 @@ TestDBFactory
def test_save_results(self): def test_save_results(self):
city_id = control.database.persist_city(control.city, city_id = control.database.persist_city(control.city,
control.pickle_path, control.pickle_path,
'current status',
control.application_id, control.application_id,
control.user_id) control.user_id)
city_objects_id = [] city_objects_id = []
@ -279,4 +289,4 @@ TestDBFactory
def tearDownClass(cls): def tearDownClass(cls):
control.database.delete_application(control.application_uuid) control.database.delete_application(control.application_uuid)
control.database.delete_user(control.user_id) control.database.delete_user(control.user_id)
""" """

Binary file not shown.