persistence changes

This commit is contained in:
Guille Gutierrez 2023-12-06 15:15:36 -05:00
parent 716465f1eb
commit 27ceed01fe
4 changed files with 37 additions and 43 deletions

View File

@ -4,10 +4,8 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project CoderPeter Yefi peteryefi@gmail.com
"""
import json
from typing import Dict
from hub.persistence.repositories.application import Application
from hub.persistence.repositories.city import City
from hub.persistence.repositories.city_object import CityObject
@ -99,17 +97,18 @@ class DBControl:
"""
return self._city_object.get_by_name_or_alias_in_cities(name, cities)
def buildings_info(self, request_values, city_id) -> [CityObject]:
def buildings_info(self, user_id, application_id, names_or_aliases) -> [CityObject]:
"""
Retrieve the buildings info from the database
:param request_values: Building names
:param city_id: City ID
:param user_id: User ID
:param application_id: Application ID
:param names_or_aliases: A list of names or alias for the buildings
:return: [CityObject]
"""
buildings = []
for name in request_values['names']:
buildings.append(self.building_info(name, city_id))
return buildings
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
def results(self, user_id, application_id, request_values, result_names=None) -> Dict:
"""

View File

@ -14,6 +14,7 @@ from sqlalchemy.orm import Session
from hub.city_model_structure.building import Building
from hub.persistence.models import CityObject as Model
from hub.persistence.models import City as CityModel
from hub.persistence.repository import Repository
@ -73,7 +74,7 @@ class CityObject(Repository):
with Session(self.engine) as session:
session.query(Model).filter(Model.name == building.name, Model.city_id == city_id).update(
{'name': building.name,
'alias': building.alias,
'aliases': building.aliases,
'object_type': building.type,
'year_of_construction': building.year_of_construction,
'function': building.function,
@ -135,6 +136,26 @@ class CityObject(Repository):
logging.error('Error while fetching city object by name and city, empty result %s', err)
raise IndexError from err
def get_by_name_or_alias_for_user_app(self, user_id, application_id, names) -> Union[Model, None]:
"""
Fetch city objects belonging to the user and application where the name or alias is in the names list
:param user_id: User ID
:param application_id: Application ID
:param names: a list of building aliases or names
:return [CityObject] or None
"""
with Session(self.engine) as session:
cities = session.execute(select(CityModel).where(
CityModel.user_id == user_id, CityModel.application_id == application_id
)).all()
ids = [c[0].id for c in cities]
buildings = session.execute(select(Model).where(
Model.city_id.in_(ids), Model.name.in_(names)
))
results = [r[0] for r in buildings]
print(ids, buildings)
return None
def get_by_name_or_alias_and_city(self, name, city_id) -> Union[Model, None]:
"""
Fetch a city object based on name and city id

View File

@ -6,7 +6,6 @@ Project Coder Peter Yefi peteryefi@gmail.com
"""
import distutils.spawn
import glob
import json
import logging
import os
import subprocess
@ -286,6 +285,8 @@ TestDBFactory
results, city_object_id=db_building_id)
self.assertEqual(17, len(city_objects_id), 'wrong number of results')
self.assertIsNotNone(city_objects_id[0], 'city_object_id is None')
results = control.database.results(control.user_id, control.application_id, request_values)
for _id in city_objects_id:
control.database.delete_results_by_name('insel meb', city_object_id=_id)
control.database.delete_city(city_id)

View File

@ -5,11 +5,8 @@ Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
import distutils.spawn
import glob
import json
import logging
import os
import subprocess
import unittest
from pathlib import Path
from unittest import TestCase
@ -18,19 +15,7 @@ import sqlalchemy.exc
from sqlalchemy import create_engine
from sqlalchemy.exc import ProgrammingError
import hub.helpers.constants as cte
from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory
from hub.exports.exports_factory import ExportsFactory
from hub.helpers.data.montreal_function_to_hub_function import MontrealFunctionToHubFunction
from hub.imports.construction_factory import ConstructionFactory
from hub.imports.energy_systems_factory import EnergySystemsFactory
from hub.imports.geometry_factory import GeometryFactory
from hub.imports.results_factory import ResultFactory
from hub.imports.usage_factory import UsageFactory
from hub.imports.weather_factory import WeatherFactory
from hub.persistence.db_control import DBControl
from hub.persistence.models import City, Application, CityObject, SimulationResults
from hub.persistence.models import User, UserRoles
from hub.persistence.repository import Repository
@ -129,24 +114,12 @@ TestDBFactory
"""
@unittest.skipIf(control.skip_test, control.skip_reason)
def test_retrieve_results(self):
def test_buildings_info(self):
request_values = {
"scenarios": [
{
"current status": ["01002777", "01002773", "01036804"]
},
{
"skin retrofit": ["01002777", "01002773", "01036804"]
},
{
"system retrofit and pv": ["01002777", "01002773", "01036804"]
},
{
"skin and system retrofit with pv": ["01002777", "01002773", "01036804"]
}
"buildings": [
"01002777", "01002773", "01036804"
]
}
results = control.database.results(control.user_id, control.application_id, request_values)
results = control.database.buildings_info(control.user_id, control.application_id, request_values)
self.assertEqual(12, len(results), 'wrong number of results')
print(results)