Commit ffb7622f authored by Alexander Lercher's avatar Alexander Lercher

Trace Retrieval: Receives traces as message; saves to MongoDB

parent 447c76aa
from flask import request, Response
def receive():
# print(request.json)
return Response(status=201)
return Response(status=501)
import pymongo
MONGO_DB_HOST = 'trace-retrieval-db'
MONGO_DB_PORT = '27017'
class MongoRepository:
# TODO extract to docker env var
_username = 'root'
_password = 'root'
_collection : pymongo.collection.Collection = None
def __init__(self, username=_username, password=_password):
myclient = pymongo.MongoClient(f"mongodb://{username}:{password}@{MONGO_DB_HOST}:{MONGO_DB_PORT}/")
database = myclient['traceRetrievalDB'] # trace retrieval
self._collection = database['traces']
def insert_trace(self, content: dict):
self._collection.insert_one(content)
def get_traces(self, selection: dict = {}):
return self._collection.find(selection)
......@@ -8,10 +8,11 @@ if os.path.exists(modules_path):
# init logging to file
import logging
LOG_FORMAT = ('%(levelname) -5s %(asctime)s %(name)s:%(funcName) -35s %(lineno) -5d: %(message)s')
logging.basicConfig(filename='trace-retrieval.log', level=logging.WARNING, format=LOG_FORMAT)
logging.basicConfig(filename='error.log', level=logging.WARNING, format=LOG_FORMAT)
LOGGER = logging.getLogger(__name__)
import connexion
from messaging.MessageHandler import MessageHandler
from messaging.MessageReceiver import MessageReceiver
from messaging.MessageSender import MessageSender
......@@ -21,9 +22,9 @@ RABBIT_MQ_PORT = '5672'
message_sender = None
# init message handler
message_handler = MessageHandler()
def message_received_callback(channel, method, properties, body):
print(f"### Received: {body}")
# channel.basic_ack(delivery_tag=method.delivery_tag)
message_handler.handle_message(body)
message_sender.send('rest-gateway', str(body) + ' (sent from trace-retrieval-microservice)', 'rest-gateway')
# load swagger config
......
from db.MongoRepository import MongoRepository
import logging
LOGGER = logging.getLogger(__name__)
class MessageHandler:
_mongo_repo = None
def __init__(self):
self._mongo_repo = MongoRepository()
def handle_message(self, body):
LOGGER.info(f"Received message: {body}")
if not 'type' in body:
LOGGER.warning(f"Message has no type field")
return
if body['type'] == 'blockchain-transaction':
self.handle_blockchain_transaction(body['content'])
def handle_blockchain_transaction(self, transaction):
self._mongo_repo.insert_trace(transaction)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment