implement idf generator

This commit is contained in:
Guille Gutierrez 2024-06-14 06:09:24 +02:00
parent 8fbfd4be89
commit 73641f985e
3 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,68 @@
"""
Config
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2023 Project Peter Yefi peteryefi@gmail.com
"""
import distutils
import os
import platform
from pathlib import Path
import hub.helpers.dictionaries
from hub.persistence.db_control import DBControl
from hub.persistence.repository import Repository
from hub.catalog_factories.energy_systems_catalog_factory import EnergySystemsCatalogFactory
class Config:
def __init__(self):
dotenv_path = "{}/.local/etc/hub_api/.env".format(os.path.expanduser('~'))
if platform.system() == 'Linux':
dotenv_path = Path(dotenv_path).resolve()
environment = 'PROD'
database_name = 'montreal_retrofit'
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
}
@property
def database(self):
return self._database
@property
def repository(self):
return self._repository
@property
def energy_systems_catalog(self):
return self._energy_systems_catalog
@staticmethod
def max_file_size():
return 10 * 1024 * 1024 # 10 MB
@property
def function_dictionary(self, dictionary):
return self._dictionaries[dictionary]
@property
def insel(self):
return distutils.spawn.find_executable('insel')
@property
def sra(self):
return distutils.spawn.find_executable('sra')
@property
def base_uri(self):
return self._base_uri

View File

@ -1,3 +1,4 @@
import datetime
import json import json
import os import os
from pathlib import Path from pathlib import Path
@ -21,6 +22,7 @@ class IdfGenerator(Resource, Config):
""" """
API call generate the IDF file for the input data API call generate the IDF file for the input data
""" """
start = datetime.datetime.now()
session_id = request.headers.get('session-id', None) session_id = request.headers.get('session-id', None)
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)
@ -31,6 +33,7 @@ class IdfGenerator(Resource, Config):
token = _session['token'] token = _session['token']
application_id = session(session_id)['application_id'] application_id = session(session_id)['application_id']
user_id = session(session_id)['user_id'] user_id = session(session_id)['user_id']
tmp_path = (self._tmp_path / token).resolve() tmp_path = (self._tmp_path / token).resolve()
try: try:
os.mkdir(tmp_path) os.mkdir(tmp_path)
@ -39,6 +42,7 @@ class IdfGenerator(Resource, Config):
payload = request.get_json() payload = request.get_json()
for key, value in payload.items(): for key, value in payload.items():
db_city = self.database.get_city(self.database.building(value, user_id, application_id, key).city_id) db_city = self.database.get_city(self.database.building(value, user_id, application_id, key).city_id)
print(f'{datetime.datetime.now() - start}')
if version != db_city.hub_release: if version != db_city.hub_release:
return Response(json.dumps({ return Response(json.dumps({
'error': 'The selected building belongs to an old hub release and cannot be loaded.' 'error': 'The selected building belongs to an old hub release and cannot be loaded.'

View File

@ -84,6 +84,7 @@ class RetrofitResults(Resource, Config):
API call for requesting a specified list of enriched persistence API call for requesting a specified list of enriched persistence
""" """
# todo: cost and co2 libraries are using default canadians values, in the future need to be optionally API configurable # todo: cost and co2 libraries are using default canadians values, in the future need to be optionally API configurable
start = datetime.datetime.now()
session_id = request.headers.get('session-id', None) session_id = request.headers.get('session-id', None)
if session_id == "deece4fa-6809-42b1-a4e6-36e9f3c6edc2": if session_id == "deece4fa-6809-42b1-a4e6-36e9f3c6edc2":
return Response(json.dumps(dic), status=200) return Response(json.dumps(dic), status=200)
@ -104,6 +105,7 @@ class RetrofitResults(Resource, Config):
# no data found for the given parameters # no data found for the given parameters
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
# deserialize the response to return pure json # deserialize the response to return pure json
print(f'session {datetime.datetime.now() - start}')
start = datetime.datetime.now() start = datetime.datetime.now()
t = [] t = []
for scenario in results: for scenario in results:
@ -117,4 +119,5 @@ class RetrofitResults(Resource, Config):
f.start() f.start()
for f in t: for f in t:
f.join() f.join()
print(f'calculated {datetime.datetime.now() - start}')
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)