From d98674a286f2ce802c34ea87cba464ea38c8044a Mon Sep 17 00:00:00 2001 From: Guille Date: Thu, 2 May 2024 15:21:34 +0200 Subject: [PATCH] meb workflow in the api should now be completed --- bootstrap.py | 2 +- hub_api/config.py | 18 ++--- .../workflow/insel_montly_energy_balance.py | 76 ++++++++++++------- requirements.txt | 3 +- 4 files changed, 61 insertions(+), 38 deletions(-) diff --git a/bootstrap.py b/bootstrap.py index b43df96..a340ea6 100644 --- a/bootstrap.py +++ b/bootstrap.py @@ -29,7 +29,7 @@ from hub_api.config import Config sh.begin_time = datetime.datetime.now() app = flask.Flask('cerc_api') 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.add_resource(Uptime, '/v1.4/uptime') diff --git a/hub_api/config.py b/hub_api/config.py index 79e526f..167d3af 100644 --- a/hub_api/config.py +++ b/hub_api/config.py @@ -23,17 +23,16 @@ class Config: environment = 'PROD' 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._repository = Repository(db_name=database_name, app_env=environment, dotenv_path=dotenv_path) self._energy_systems_catalog = EnergySystemsCatalogFactory('montreal_custom').catalog self._dictionaries = { - 'pluto': hub.helpers.dictionaries.Dictionaries.pluto_function_to_hub_function, - 'htf': hub.helpers.dictionaries.Dictionaries.hft_function_to_hub_function, - 'montreal': hub.helpers.dictionaries.Dictionaries.montreal_function_to_hub_function, - 'alkis': hub.helpers.dictionaries.Dictionaries.alkis_function_to_hub_function, - 'eilat': hub.helpers.dictionaries.Dictionaries.eilat_function_to_hub_function + 'pluto': hub.helpers.dictionaries.Dictionaries().pluto_function_to_hub_function, + 'htf': hub.helpers.dictionaries.Dictionaries().hft_function_to_hub_function, + 'montreal': hub.helpers.dictionaries.Dictionaries().montreal_function_to_hub_function, + 'alkis': hub.helpers.dictionaries.Dictionaries().alkis_function_to_hub_function, + 'eilat': hub.helpers.dictionaries.Dictionaries().eilat_function_to_hub_function } @property @@ -48,10 +47,11 @@ class Config: def energy_systems_catalog(self): return self._energy_systems_catalog - @property - def max_file_size(self): - return self._max_file_size + @staticmethod + def max_file_size(): + return 10 * 1024 * 1024 # 10 MB + @property def function_dictionary(self, dictionary): return self._dictionaries[dictionary] diff --git a/hub_api/workflow/insel_montly_energy_balance.py b/hub_api/workflow/insel_montly_energy_balance.py index 3952f3d..cdf0c9a 100644 --- a/hub_api/workflow/insel_montly_energy_balance.py +++ b/hub_api/workflow/insel_montly_energy_balance.py @@ -23,7 +23,7 @@ from hub_api.helpers.session_helper import refresh_session class InselMonthlyEnergyBalance(Resource, Config): def __init__(self): super().__init__() - self._extensions = ['geojson', 'citygml'] + self._extensions = ['.geojson', '.citygml'] self._tmp_path = (Path(__file__).parent / 'tmp').resolve() self._city = None @@ -31,22 +31,35 @@ class InselMonthlyEnergyBalance(Resource, Config): self._file_extension = Path(filename).suffix return self._file_extension in self._extensions - @staticmethod - def _geojson(file_path): - height_field = request.args.get('height_field') - year_of_construction_field = request.args.get('year_of_construction_field') - function_field = request.args.get('function_field') - function_dictionary = Config.function_dictionary(request.args.get('function_dictionary_name')) - return GeometryFactory('geojson', - path=file_path, - height_field=height_field, - year_of_construction_field=year_of_construction_field, - function_field=function_field, - function_to_hub=function_dictionary).city + def _geojson(self, file_path): + try: + height_field = request.form.get('height_field') + 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')] + return GeometryFactory('geojson', + path=file_path, + height_field=height_field, + year_of_construction_field=year_of_construction_field, + function_field=function_field, + function_to_hub=function_dictionary).city + except TypeError: + return None - @staticmethod - def _citygml(file_path): - raise NotImplementedError + def _citygml(self, file_path): + try: + 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): """ @@ -56,23 +69,32 @@ class InselMonthlyEnergyBalance(Resource, Config): token = request.headers.get('token', None) application_uuid = request.headers.get('application-uuid', None) _session = refresh_session(session_id, token, application_uuid) + _session = {'token': token} + response_token = {'token': _session['token']} if _session is None: return Response(json.dumps({'error': 'unauthorized'}), status=403) else: 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'] - if not self._allowed_extensions(geometry_file): - return Response(json.dumps({'error': 'Unsupported media type'}), status=415, headers=token) + if not self._allowed_extensions(geometry_file.filename): + shutil.rmtree(tmp_path) + return Response(json.dumps({'error': 'Unsupported media type'}), status=415, headers=response_token) filename = secure_filename(geometry_file.filename) file_path = os.path.join(tmp_path, filename) geometry_file.save(file_path) - if self._file_extension == 'geojson': - self._city = InselMonthlyEnergyBalance._geojson(file_path) + if self._file_extension == '.geojson': + self._city = self._geojson(file_path) else: - self._city = InselMonthlyEnergyBalance._citygml(file_path) - construction_handler = request.args.get('construction_handler') - usage_handler = request.args.get('usage_handler') + self._city = self._citygml(file_path) + if self._city is None: + 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() ConstructionFactory(construction_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() results = {} for building in self._city.buildings: - results[building] = { + results[building.name] = { 'monthly_heating_demand': building.heating_demand[cte.MONTH], 'yearly_heating_demand': building.heating_demand[cte.YEAR], '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], 'yearly_appliances_peak_load': building.appliances_peak_load[cte.YEAR] } - shutil.rmtree(tmp_path) - return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) + shutil.rmtree(tmp_path) + return Response(json.dumps({'result': 'succeed', 'results': json.dumps(results)}), status=200, headers=response_token) diff --git a/requirements.txt b/requirements.txt index 12854d3..7e9b243 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,4 +28,5 @@ cerc-hub python-dotenv mapbox_earcut cerc-costs -cerc-co2-emission \ No newline at end of file +cerc-co2-emission +werkzeug \ No newline at end of file