Complete implementation of mongo logic

This commit is contained in:
Guille Gutierrez 2024-06-22 17:00:20 +02:00
parent a9af75bc3b
commit 55638fe147
4 changed files with 69 additions and 27 deletions

View File

@ -18,6 +18,7 @@ import threading
import hub_api.helpers.session_helper as sh import hub_api.helpers.session_helper as sh
from hub_api.control.session import SessionStart, SessionEnd, KeepSessionAlive from hub_api.control.session import SessionStart, SessionEnd, KeepSessionAlive
from hub_api.control.uptime import Uptime from hub_api.control.uptime import Uptime
from hub_api.persistence.full_retrofit_results import FullRetrofitResults
from hub_api.persistence.retrofit_results import RetrofitResults from hub_api.persistence.retrofit_results import RetrofitResults
from hub_api.workflow.insel_montly_energy_balance import InselMonthlyEnergyBalance from hub_api.workflow.insel_montly_energy_balance import InselMonthlyEnergyBalance
from hub_api.workflow.costs import Costs from hub_api.workflow.costs import Costs
@ -42,6 +43,7 @@ api.add_resource(KeepSessionAlive, '/v1.4/session/keep-alive')
# persistence # persistence
api.add_resource(RetrofitResults, '/v1.4/persistence/retrofit-results') api.add_resource(RetrofitResults, '/v1.4/persistence/retrofit-results')
api.add_resource(FullRetrofitResults, '/v1.4/persistence/full-retrofit-results')
# energy plus # energy plus
api.add_resource(IdfGenerator, '/v1.4/energy-plus/idf-generator') api.add_resource(IdfGenerator, '/v1.4/energy-plus/idf-generator')

View File

@ -12,6 +12,7 @@ import hub.helpers.dictionaries
from hub.persistence.db_control import DBControl from hub.persistence.db_control import DBControl
from hub.persistence.repository import Repository from hub.persistence.repository import Repository
from hub.catalog_factories.energy_systems_catalog_factory import EnergySystemsCatalogFactory from hub.catalog_factories.energy_systems_catalog_factory import EnergySystemsCatalogFactory
from pymongo import MongoClient
class Config: class Config:
@ -21,11 +22,8 @@ class Config:
if platform.system() == 'Linux': if platform.system() == 'Linux':
dotenv_path = Path(dotenv_path).resolve() dotenv_path = Path(dotenv_path).resolve()
environment = 'PROD' environment = 'PROD'
self._database_name = os.getenv(f'{environment}_DB_NAME')
load_dotenv(dotenv_path=dotenv_path) load_dotenv(dotenv_path=dotenv_path)
self._mongodb = os.getenv(f'{environment}_MONGO_DB') self._database_name = os.getenv(f'{environment}_DB_NAME')
self._mongodb_database = os.getenv(f'{environment}_MONGO_DB_DATABASE')
self._mongodb_collection = os.getenv(f'{environment}_MONGO_DB_COLLECTION')
self._database = DBControl(db_name=self._database_name, app_env=environment, dotenv_path=dotenv_path) self._database = DBControl(db_name=self._database_name, app_env=environment, dotenv_path=dotenv_path)
self._repository = Repository(db_name=self._database_name, app_env=environment, dotenv_path=dotenv_path) self._repository = Repository(db_name=self._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
@ -36,6 +34,13 @@ class Config:
'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
} }
# mongodb
_mongodb = os.getenv(f'{environment}_MONGO_DB')
_mongodb_database = os.getenv(f'{environment}_MONGO_DB_DATABASE')
self._mongodb_collection_prefix = os.getenv(f'{environment}_MONGO_DB_COLLECTION_PREFIX')
_client = MongoClient(_mongodb)
self._montreal_retrofit_db = _client[_mongodb_database]
@property @property
def database(self): def database(self):
@ -53,10 +58,6 @@ class Config:
def max_file_size(): def max_file_size():
return 10 * 1024 * 1024 # 10 MB return 10 * 1024 * 1024 # 10 MB
@property
def function_dictionary(self, dictionary):
return self._dictionaries[dictionary]
@property @property
def insel(self): def insel(self):
return distutils.spawn.find_executable('insel') return distutils.spawn.find_executable('insel')
@ -66,13 +67,9 @@ class Config:
return distutils.spawn.find_executable('sra') return distutils.spawn.find_executable('sra')
@property @property
def mongodb(self): def montreal_retrofit_db(self):
return self._mongodb return self._montreal_retrofit_db
@property @property
def mongodb_database(self): def mongodb_collection_prefix(self):
return self._mongodb_database return self._mongodb_collection_prefix
@property
def mongodb_collection(self):
return self._mongodb_collection

View File

@ -0,0 +1,45 @@
import json
from flask import Response, request
from flask_restful import Resource
from hub_api.config import Config
from hub_api.helpers.session_helper import refresh_session
from hub_api.persistence.mock import dic
class FullRetrofitResults(Resource, Config):
def __init__(self):
super().__init__()
def post(self):
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)
results = {'current status': [],
'skin retrofit': [],
'system retrofit and pv': [],
'skin and system retrofit with pv': []
}
if _session is None:
return Response(json.dumps({'error': 'unauthorized'}), status=403)
else:
response_token = {'token': _session['token']}
json_request = request.get_json()
for scenario in json_request['scenarios']:
for key, buildings in scenario.items():
mongodb_collection = f'{self.mongodb_collection_prefix}{key.replace(" ", "_")}'
building_query = ''
for building in buildings:
building_query = f'{building_query} {{"alias": "{building}"}},'
query = f'{{"$or": [{building_query[:-1]}]}}'
cursor = self.montreal_retrofit_db[mongodb_collection].find(json.loads(query))
for result in cursor:
del result['_id']
result['building'] = result['alias']
results[key].append(result)
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=response_token)

View File

@ -1,12 +1,10 @@
import json import json
import threading import threading
from bson.json_util import dumps
from co2_emission.co2_emission import Co2Emission from co2_emission.co2_emission import Co2Emission
from costs.cost import Cost from costs.cost import Cost
from flask import Response, request from flask import Response, request
from flask_restful import Resource from flask_restful import Resource
from pymongo import MongoClient
from hub_api.config import Config from hub_api.config import Config
from hub_api.helpers.session_helper import session, refresh_session from hub_api.helpers.session_helper import session, refresh_session
@ -84,7 +82,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 libs are using default canadians values, in the future need to be optionally API configurable
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)
@ -126,19 +124,19 @@ class RetrofitResults(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': 1} results = {'meb': []}
results = {}
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:
client = MongoClient(self.mongodb) response_token = {'token': _session['token']}
montreal_retrofit_db = client[self.mongodb_database]
meb_collection = montreal_retrofit_db[self.mongodb_collection]
token = {'token': _session['token']}
buildings = request.get_json()['buildings'] buildings = request.get_json()['buildings']
building_query = '' building_query = ''
for building in buildings: for building in buildings:
building_query = f'{building_query} {{"alias": "{building}"}},' building_query = f'{building_query} {{"alias": "{building}"}},'
query = f'{{"$or": [{building_query[:-1]}]}}' query = f'{{"$or": [{building_query[:-1]}]}}'
results = dumps(meb_collection.find(json.loads(query))) cursor = self.mongodb_meb.find(json.loads(query))
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) for result in cursor:
del result['_id']
results['meb'].append(result)
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=response_token)