2023-02-24 16:57:51 -05:00
|
|
|
import json
|
2023-04-12 15:26:56 -04:00
|
|
|
|
2023-02-24 16:57:51 -05:00
|
|
|
from flask import Response, request
|
|
|
|
from flask_restful import Resource
|
|
|
|
|
|
|
|
from hub_api.config import Config
|
2023-04-12 15:26:56 -04:00
|
|
|
from hub_api.helpers.session_helper import session, refresh_session
|
|
|
|
|
2023-02-24 16:57:51 -05:00
|
|
|
|
2023-07-20 20:15:24 -04:00
|
|
|
class RetrofitResults(Resource, Config):
|
2023-02-24 16:57:51 -05:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
2023-03-13 11:56:05 -04:00
|
|
|
def post(self):
|
2023-03-10 10:15:20 -05:00
|
|
|
"""
|
2023-07-21 11:48:00 -04:00
|
|
|
API call for requesting a specified list of enriched persistence
|
2023-03-10 10:15:20 -05:00
|
|
|
"""
|
2023-07-21 16:59:56 -04:00
|
|
|
session_id = request.headers.get('session-id', None)
|
2023-02-24 16:57:51 -05:00
|
|
|
token = request.headers.get('token', None)
|
2023-07-21 16:59:56 -04:00
|
|
|
application_uuid = request.headers.get('application-uuid', None)
|
2023-04-12 15:26:56 -04:00
|
|
|
_session = refresh_session(session_id, token, application_uuid)
|
|
|
|
if _session is None:
|
|
|
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
2023-04-13 09:49:10 -04:00
|
|
|
token = {'token': _session['token']}
|
2023-04-12 15:26:56 -04:00
|
|
|
application_id = session(session_id)['application_id']
|
|
|
|
user_id = session(session_id)['user_id']
|
|
|
|
payload = request.get_json()
|
2023-07-28 14:54:30 -04:00
|
|
|
if 'scenarios' not in payload:
|
|
|
|
return Response(json.dumps({'error': 'Bad request'}), status=400, headers=token)
|
|
|
|
|
|
|
|
# retrieve the buildings info
|
|
|
|
buildings = []
|
|
|
|
buildings_info = []
|
2023-07-31 14:31:50 -04:00
|
|
|
scenario_name = None
|
|
|
|
|
2023-07-28 14:54:30 -04:00
|
|
|
for scenario in payload['scenarios']:
|
2023-07-31 14:31:50 -04:00
|
|
|
scenario_name = next(iter(scenario))
|
|
|
|
for name in scenario[scenario_name]:
|
2023-07-28 14:54:30 -04:00
|
|
|
if name not in buildings:
|
|
|
|
buildings.append(name)
|
|
|
|
for building in buildings:
|
2023-07-31 14:31:50 -04:00
|
|
|
building_info = self.database.building(building, user_id, application_id, scenario_name)
|
|
|
|
self.energy_systems_catalog.get_entry(building_info.system_name)
|
|
|
|
buildings_info.append(building_info)
|
2023-07-21 16:59:56 -04:00
|
|
|
results = self.database.results(user_id, application_id, payload)
|
2023-04-12 15:26:56 -04:00
|
|
|
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
|
2023-07-28 14:54:30 -04:00
|
|
|
scenario = next(iter(results))
|
|
|
|
for building_results in results[scenario]:
|
2023-04-12 15:26:56 -04:00
|
|
|
values = []
|
2023-07-21 16:59:56 -04:00
|
|
|
for value in building_results['insel meb']:
|
2023-04-12 15:26:56 -04:00
|
|
|
key = next(iter(value))
|
2023-07-21 16:59:56 -04:00
|
|
|
values.append({key: json.loads(str(value[key]))})
|
|
|
|
building_results['insel meb'] = values
|
2023-04-12 15:26:56 -04:00
|
|
|
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
|