Commit 1aa36851 authored by Alexander Lercher's avatar Alexander Lercher

Extracted database access logic

parent 48c4791d
Pipeline #11 failed with stages
import pymongo
import network_constants as netconst
class MongoRepository:
class MongoRepositoryBase:
'''Base class to connect to, insert and read from a single MongoDB collection'''
# TODO extract to docker env var
_username = 'root'
_password = 'root'
......@@ -9,18 +10,18 @@ class MongoRepository:
_collection: pymongo.collection.Collection = None
_mongo_client: pymongo.MongoClient = None
def __init__(self, username=_username, password=_password):
def __init__(self, database_name, collection_name, username=_username, password=_password):
self._mongo_client = pymongo.MongoClient(f"mongodb://{username}:{password}@{netconst.MONGO_DB_HOSTNAME}:{netconst.MONGO_DB_PORT}/")
database = self._mongo_client['traceRetrievalDB']
self._collection = database['traces']
database = self._mongo_client[database_name]
self._collection = database[collection_name]
def insert_trace(self, content: dict):
def insert_entry(self, content: dict):
self._collection.insert_one(content)
def get_traces(self, selection: dict = {}, projection: dict = {'_': 0}) -> pymongo.cursor.Cursor:
def get_entries(self, selection: dict = {}, projection: dict = {'_': 0}) -> pymongo.cursor.Cursor:
return self._collection.find(selection, projection)
def close_connection(self):
self._mongo_client.close()
self._collection = None
self._mongo_client = None
self._collection = None
import pymongo
import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
class MongoRepository(MongoRepositoryBase):
# TODO extract to docker env var
_username = 'root'
_password = 'root'
_collection: pymongo.collection.Collection = None
_mongo_client: pymongo.MongoClient = None
def __init__(self, username=_username, password=_password):
super().__init__('traceRetrievalDB', 'traces')
def insert_trace(self, content: dict):
super().insert_entry(content)
def get_traces(self, selection: dict = {}, projection: dict = {'_': 0}) -> pymongo.cursor.Cursor:
return super().get_entries(selection, projection)
......@@ -14,7 +14,7 @@ LOGGER = logging.getLogger(__name__)
#############################
import connexion
from db.MongoRepository import MongoRepository
from database.MongoRepository import MongoRepository
from messaging.MessageHandler import MessageHandler
from messaging.ReconnectingMessageManager import ReconnectingMessageManager
......
from flask import request, Response
from db.MongoRepository import MongoRepository
from database.MongoRepository import MongoRepository
mongo_repo = MongoRepository()
......
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