Complete implementation of mongo logic
This commit is contained in:
parent
a9af75bc3b
commit
55638fe147
|
@ -18,6 +18,7 @@ import threading
|
|||
import hub_api.helpers.session_helper as sh
|
||||
from hub_api.control.session import SessionStart, SessionEnd, KeepSessionAlive
|
||||
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.workflow.insel_montly_energy_balance import InselMonthlyEnergyBalance
|
||||
from hub_api.workflow.costs import Costs
|
||||
|
@ -42,6 +43,7 @@ api.add_resource(KeepSessionAlive, '/v1.4/session/keep-alive')
|
|||
|
||||
# persistence
|
||||
api.add_resource(RetrofitResults, '/v1.4/persistence/retrofit-results')
|
||||
api.add_resource(FullRetrofitResults, '/v1.4/persistence/full-retrofit-results')
|
||||
|
||||
# energy plus
|
||||
api.add_resource(IdfGenerator, '/v1.4/energy-plus/idf-generator')
|
||||
|
|
|
@ -12,6 +12,7 @@ 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
|
||||
from pymongo import MongoClient
|
||||
|
||||
|
||||
class Config:
|
||||
|
@ -21,11 +22,8 @@ class Config:
|
|||
if platform.system() == 'Linux':
|
||||
dotenv_path = Path(dotenv_path).resolve()
|
||||
environment = 'PROD'
|
||||
self._database_name = os.getenv(f'{environment}_DB_NAME')
|
||||
load_dotenv(dotenv_path=dotenv_path)
|
||||
self._mongodb = os.getenv(f'{environment}_MONGO_DB')
|
||||
self._mongodb_database = os.getenv(f'{environment}_MONGO_DB_DATABASE')
|
||||
self._mongodb_collection = os.getenv(f'{environment}_MONGO_DB_COLLECTION')
|
||||
self._database_name = os.getenv(f'{environment}_DB_NAME')
|
||||
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._energy_systems_catalog = EnergySystemsCatalogFactory('montreal_custom').catalog
|
||||
|
@ -36,6 +34,13 @@ class Config:
|
|||
'alkis': hub.helpers.dictionaries.Dictionaries().alkis_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
|
||||
def database(self):
|
||||
|
@ -53,10 +58,6 @@ class Config:
|
|||
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')
|
||||
|
@ -66,13 +67,9 @@ class Config:
|
|||
return distutils.spawn.find_executable('sra')
|
||||
|
||||
@property
|
||||
def mongodb(self):
|
||||
return self._mongodb
|
||||
def montreal_retrofit_db(self):
|
||||
return self._montreal_retrofit_db
|
||||
|
||||
@property
|
||||
def mongodb_database(self):
|
||||
return self._mongodb_database
|
||||
|
||||
@property
|
||||
def mongodb_collection(self):
|
||||
return self._mongodb_collection
|
||||
def mongodb_collection_prefix(self):
|
||||
return self._mongodb_collection_prefix
|
||||
|
|
45
hub_api/persistence/full_retrofit_results.py
Normal file
45
hub_api/persistence/full_retrofit_results.py
Normal 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)
|
|
@ -1,12 +1,10 @@
|
|||
import json
|
||||
import threading
|
||||
|
||||
from bson.json_util import dumps
|
||||
from co2_emission.co2_emission import Co2Emission
|
||||
from costs.cost import Cost
|
||||
from flask import Response, request
|
||||
from flask_restful import Resource
|
||||
from pymongo import MongoClient
|
||||
|
||||
from hub_api.config import Config
|
||||
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
|
||||
"""
|
||||
# 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)
|
||||
if session_id == "deece4fa-6809-42b1-a4e6-36e9f3c6edc2":
|
||||
return Response(json.dumps(dic), status=200)
|
||||
|
@ -126,19 +124,19 @@ class RetrofitResults(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': 1}
|
||||
results = {}
|
||||
results = {'meb': []}
|
||||
if _session is None:
|
||||
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
||||
else:
|
||||
client = MongoClient(self.mongodb)
|
||||
montreal_retrofit_db = client[self.mongodb_database]
|
||||
meb_collection = montreal_retrofit_db[self.mongodb_collection]
|
||||
token = {'token': _session['token']}
|
||||
response_token = {'token': _session['token']}
|
||||
buildings = request.get_json()['buildings']
|
||||
building_query = ''
|
||||
for building in buildings:
|
||||
building_query = f'{building_query} {{"alias": "{building}"}},'
|
||||
query = f'{{"$or": [{building_query[:-1]}]}}'
|
||||
results = dumps(meb_collection.find(json.loads(query)))
|
||||
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
|
||||
cursor = self.mongodb_meb.find(json.loads(query))
|
||||
for result in cursor:
|
||||
del result['_id']
|
||||
results['meb'].append(result)
|
||||
|
||||
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=response_token)
|
||||
|
|
Loading…
Reference in New Issue
Block a user