meb workflow in the api should now be completed

This commit is contained in:
Guille Gutierrez 2024-05-02 15:21:34 +02:00
parent 31ebe48f68
commit d98674a286
4 changed files with 61 additions and 38 deletions

View File

@ -29,7 +29,7 @@ from hub_api.config import Config
sh.begin_time = datetime.datetime.now() sh.begin_time = datetime.datetime.now()
app = flask.Flask('cerc_api') app = flask.Flask('cerc_api')
app.json_provider_class = LazyJSONEncoder app.json_provider_class = LazyJSONEncoder
app.config['MAX_CONTENT_LENGTH'] = Config.max_file_size app.config['MAX_CONTENT_LENGTH'] = int(Config.max_file_size())
api = Api(app) api = Api(app)
api.add_resource(Uptime, '/v1.4/uptime') api.add_resource(Uptime, '/v1.4/uptime')

View File

@ -23,17 +23,16 @@ class Config:
environment = 'PROD' environment = 'PROD'
database_name = 'montreal_retrofit' database_name = 'montreal_retrofit'
self._max_file_size = 10 * 1024 * 1024 # 10 MB
self._database = DBControl(db_name=database_name, app_env=environment, dotenv_path=dotenv_path) self._database = DBControl(db_name=database_name, app_env=environment, dotenv_path=dotenv_path)
self._repository = Repository(db_name=database_name, app_env=environment, dotenv_path=dotenv_path) self._repository = Repository(db_name=database_name, app_env=environment, dotenv_path=dotenv_path)
self._energy_systems_catalog = EnergySystemsCatalogFactory('montreal_custom').catalog self._energy_systems_catalog = EnergySystemsCatalogFactory('montreal_custom').catalog
self._dictionaries = { self._dictionaries = {
'pluto': hub.helpers.dictionaries.Dictionaries.pluto_function_to_hub_function, 'pluto': hub.helpers.dictionaries.Dictionaries().pluto_function_to_hub_function,
'htf': hub.helpers.dictionaries.Dictionaries.hft_function_to_hub_function, 'htf': hub.helpers.dictionaries.Dictionaries().hft_function_to_hub_function,
'montreal': hub.helpers.dictionaries.Dictionaries.montreal_function_to_hub_function, 'montreal': hub.helpers.dictionaries.Dictionaries().montreal_function_to_hub_function,
'alkis': hub.helpers.dictionaries.Dictionaries.alkis_function_to_hub_function, 'alkis': hub.helpers.dictionaries.Dictionaries().alkis_function_to_hub_function,
'eilat': hub.helpers.dictionaries.Dictionaries.eilat_function_to_hub_function 'eilat': hub.helpers.dictionaries.Dictionaries().eilat_function_to_hub_function
} }
@property @property
@ -48,10 +47,11 @@ class Config:
def energy_systems_catalog(self): def energy_systems_catalog(self):
return self._energy_systems_catalog return self._energy_systems_catalog
@property @staticmethod
def max_file_size(self): def max_file_size():
return self._max_file_size return 10 * 1024 * 1024 # 10 MB
@property
def function_dictionary(self, dictionary): def function_dictionary(self, dictionary):
return self._dictionaries[dictionary] return self._dictionaries[dictionary]

View File

@ -23,7 +23,7 @@ from hub_api.helpers.session_helper import refresh_session
class InselMonthlyEnergyBalance(Resource, Config): class InselMonthlyEnergyBalance(Resource, Config):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self._extensions = ['geojson', 'citygml'] self._extensions = ['.geojson', '.citygml']
self._tmp_path = (Path(__file__).parent / 'tmp').resolve() self._tmp_path = (Path(__file__).parent / 'tmp').resolve()
self._city = None self._city = None
@ -31,22 +31,35 @@ class InselMonthlyEnergyBalance(Resource, Config):
self._file_extension = Path(filename).suffix self._file_extension = Path(filename).suffix
return self._file_extension in self._extensions return self._file_extension in self._extensions
@staticmethod def _geojson(self, file_path):
def _geojson(file_path): try:
height_field = request.args.get('height_field') height_field = request.form.get('height_field')
year_of_construction_field = request.args.get('year_of_construction_field') year_of_construction_field = request.form.get('year_of_construction_field')
function_field = request.args.get('function_field') function_field = request.form.get('function_field')
function_dictionary = Config.function_dictionary(request.args.get('function_dictionary_name')) function_dictionary = self._dictionaries[request.form.get('function_dictionary_name')]
return GeometryFactory('geojson', return GeometryFactory('geojson',
path=file_path, path=file_path,
height_field=height_field, height_field=height_field,
year_of_construction_field=year_of_construction_field, year_of_construction_field=year_of_construction_field,
function_field=function_field, function_field=function_field,
function_to_hub=function_dictionary).city function_to_hub=function_dictionary).city
except TypeError:
return None
@staticmethod def _citygml(self, file_path):
def _citygml(file_path): try:
raise NotImplementedError year_of_construction_field = request.form.get('year_of_construction_field')
function_field = request.form.get('function_field')
function_dictionary = self._dictionaries[request.form.get('function_dictionary_name')]
hub_crs = request.form.get('hub_crs')
return GeometryFactory('citygml',
path=file_path,
year_of_construction_field=year_of_construction_field,
function_field=function_field,
function_to_hub=function_dictionary,
hub_crs=hub_crs).city
except TypeError:
return None
def post(self): def post(self):
""" """
@ -56,23 +69,32 @@ class InselMonthlyEnergyBalance(Resource, Config):
token = request.headers.get('token', None) token = request.headers.get('token', None)
application_uuid = request.headers.get('application-uuid', None) application_uuid = request.headers.get('application-uuid', None)
_session = refresh_session(session_id, token, application_uuid) _session = refresh_session(session_id, token, application_uuid)
_session = {'token': token}
response_token = {'token': _session['token']}
if _session is None: if _session is None:
return Response(json.dumps({'error': 'unauthorized'}), status=403) return Response(json.dumps({'error': 'unauthorized'}), status=403)
else: else:
tmp_path = (self._tmp_path / token).resolve() tmp_path = (self._tmp_path / token).resolve()
os.mkdir(tmp_path) try:
os.mkdir(tmp_path)
except:
print('already exist')
geometry_file = request.files['geometry_file'] geometry_file = request.files['geometry_file']
if not self._allowed_extensions(geometry_file): if not self._allowed_extensions(geometry_file.filename):
return Response(json.dumps({'error': 'Unsupported media type'}), status=415, headers=token) shutil.rmtree(tmp_path)
return Response(json.dumps({'error': 'Unsupported media type'}), status=415, headers=response_token)
filename = secure_filename(geometry_file.filename) filename = secure_filename(geometry_file.filename)
file_path = os.path.join(tmp_path, filename) file_path = os.path.join(tmp_path, filename)
geometry_file.save(file_path) geometry_file.save(file_path)
if self._file_extension == 'geojson': if self._file_extension == '.geojson':
self._city = InselMonthlyEnergyBalance._geojson(file_path) self._city = self._geojson(file_path)
else: else:
self._city = InselMonthlyEnergyBalance._citygml(file_path) self._city = self._citygml(file_path)
construction_handler = request.args.get('construction_handler') if self._city is None:
usage_handler = request.args.get('usage_handler') shutil.rmtree(tmp_path)
return Response(json.dumps({'error': 'Bad request'}), status=400, headers=response_token)
construction_handler = request.form.get('construction_handler')
usage_handler = request.form.get('usage_handler')
WeatherFactory('epw', self._city).enrich() WeatherFactory('epw', self._city).enrich()
ConstructionFactory(construction_handler, self._city).enrich() ConstructionFactory(construction_handler, self._city).enrich()
UsageFactory(usage_handler, self._city).enrich() UsageFactory(usage_handler, self._city).enrich()
@ -87,7 +109,7 @@ class InselMonthlyEnergyBalance(Resource, Config):
ResultFactory('insel_monthly_energy_balance', self._city, tmp_path).enrich() ResultFactory('insel_monthly_energy_balance', self._city, tmp_path).enrich()
results = {} results = {}
for building in self._city.buildings: for building in self._city.buildings:
results[building] = { results[building.name] = {
'monthly_heating_demand': building.heating_demand[cte.MONTH], 'monthly_heating_demand': building.heating_demand[cte.MONTH],
'yearly_heating_demand': building.heating_demand[cte.YEAR], 'yearly_heating_demand': building.heating_demand[cte.YEAR],
'monthly_cooling_demand': building.cooling_demand[cte.MONTH], 'monthly_cooling_demand': building.cooling_demand[cte.MONTH],
@ -97,5 +119,5 @@ class InselMonthlyEnergyBalance(Resource, Config):
'monthly_appliances_peak_load': building.appliances_peak_load[cte.MONTH], 'monthly_appliances_peak_load': building.appliances_peak_load[cte.MONTH],
'yearly_appliances_peak_load': building.appliances_peak_load[cte.YEAR] 'yearly_appliances_peak_load': building.appliances_peak_load[cte.YEAR]
} }
shutil.rmtree(tmp_path) shutil.rmtree(tmp_path)
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) return Response(json.dumps({'result': 'succeed', 'results': json.dumps(results)}), status=200, headers=response_token)

View File

@ -28,4 +28,5 @@ cerc-hub
python-dotenv python-dotenv
mapbox_earcut mapbox_earcut
cerc-costs cerc-costs
cerc-co2-emission cerc-co2-emission
werkzeug