Add mock and improve performance

This commit is contained in:
Guille Gutierrez 2024-04-12 07:56:37 +02:00
parent 8223d6ff2a
commit 0623faaafc
2 changed files with 73 additions and 55 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,6 @@
import datetime
import json
import threading
from co2_emission.co2_emission import Co2Emission
from costs.cost import Cost
@ -8,6 +10,7 @@ from flask_restful import Resource
from hub_api.config import Config
from hub_api.helpers.session_helper import session, refresh_session
from hub_api.mockup.building import Building
from hub_api.persistence.mock import dic
class RetrofitResults(Resource, Config):
@ -20,35 +23,7 @@ class RetrofitResults(Resource, Config):
'skin and system retrofit with pv': 3
}
def post(self):
"""
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
session_id = request.headers.get('session-id', None)
token = request.headers.get('token', None)
application_uuid = request.headers.get('application-uuid', None)
_session = refresh_session(session_id, token, application_uuid)
if _session is None:
return Response(json.dumps({'error': 'unauthorized'}), status=403)
application_id = 1
user_id = 1
token = {'token': 1}
else:
token = {'token': _session['token']}
application_id = session(session_id)['application_id']
user_id = session(session_id)['user_id']
payload = request.get_json()
if 'scenarios' not in payload:
return Response(json.dumps({'error': 'Bad request'}), status=400, headers=token)
results = self.database.results(user_id, application_id, payload)
if results == {}:
# no data found for the given parameters
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
# deserialize the response to return pure json
for scenario in results:
scenario_id = self._scenario_ids[scenario]
for building_results in results[scenario]:
def _calculate_building(self, building_results, user_id, application_id, scenario, scenario_id):
building_info = self.database.building(building_results['building'], user_id, application_id, scenario)
archetype = self.energy_systems_catalog.get_entry(building_info.system_name)
mockup_building = Building(building_info, building_results, archetype)
@ -101,4 +76,46 @@ class RetrofitResults(Resource, Config):
}
}
building_results['operational_co2'] = operational_co2
def post(self):
"""
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
session_id = request.headers.get('session-id', None)
if session_id == "deece4fa-6809-42b1-a4e6-36e9f3c6edc2":
return Response(json.dumps(dic), status=200)
token = request.headers.get('token', None)
application_uuid = request.headers.get('application-uuid', None)
_session = refresh_session(session_id, token, application_uuid)
if _session is None:
return Response(json.dumps({'error': 'unauthorized'}), status=403)
else:
token = {'token': _session['token']}
application_id = session(session_id)['application_id']
user_id = session(session_id)['user_id']
payload = request.get_json()
if 'scenarios' not in payload:
return Response(json.dumps({'error': 'Bad request'}), status=400, headers=token)
start = datetime.datetime.now()
results = self.database.results(user_id, application_id, payload)
print(f'{datetime.datetime.now()- start}')
if results == {}:
# no data found for the given parameters
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
# deserialize the response to return pure json
start = datetime.datetime.now()
t = []
for scenario in results:
scenario_id = self._scenario_ids[scenario]
for building_results in results[scenario]:
f = threading.Thread(
target=self._calculate_building,
args=(building_results, user_id, application_id, scenario, scenario_id)
)
t.append(f)
f.start()
for f in t:
f.join()
print(f'retrieve calculations {datetime.datetime.now() - start}')
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)