Commit db393636 authored by Alexander's avatar Alexander

loading and storing location and clusters in mongodb

parent b57623ef
from db.location_datastore import LocationDatastore
import pymongo
import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
import json
from db.agi.agi_repository import AgiRepository
from db.entities import Location, UserCluster, PopularLocation
from typing import List
from db.agi.agi_repository import AgiRepository
class Repository:
class Repository(MongoRepositoryBase):
def __init__(self):
self.store = LocationDatastore.get_instance()
super().__init__(netconst.COMMUNITY_DETECTION_DB_HOSTNAME,
netconst.COMMUNITY_DETECTION_DB_PORT, 'communityDetectionDb')
self._location_collection = 'location'
self._cluster_collection = 'cluster'
self.agi_repo = AgiRepository()
def add_location(self, location: Location):
self.store.add(location)
super().insert_entry(self._location_collection, location.to_serializable_dict())
def get_locations(self) -> List[Location]:
locations = super().get_entries(self._location_collection)
return [Location(l) for l in locations]
def get_agi_locations(self) -> List[Location]:
agi_locations = self.agi_repo.getLocations()
return [Location(agi_loc) for agi_loc in agi_locations]
def add_user_cluster(self, cluster: UserCluster):
print(cluster)
super().insert_entry(self._cluster_collection, cluster.to_serializable_dict(for_db=True))
def get_user_clusters(self) -> List[UserCluster]:
clusters = super().get_entries(self._cluster_collection)
return [UserCluster(c['date'], int(c['hour']), json.loads(c['clusters'])) for c in clusters]
def add_popular_location(self, popular_location: PopularLocation):
print(popular_location)
pass
# add modules folder to interpreter path
import sys
import os
modules_path = '../../../modules/'
if os.path.exists(modules_path):
sys.path.insert(1, modules_path)
### init logging ###
import logging
LOG_FORMAT = ('%(levelname) -5s %(asctime)s %(name)s:%(funcName) -35s %(lineno) -5d: %(message)s')
......
......@@ -8,13 +8,12 @@ repo = Repository()
clusterer = Clusterer()
def get():
locations = repo.getLocations()
clusters = clusterer.run(locations)
return clusters
clusters = repo.get_user_clusters()
return [c.to_serializable_dict() for c in clusters]
def get_image():
return Response(status=501)
locations = repo.getLocations()
fig = clusterer.draw_locations(locations)
......
from flask import request, Response
from db.repository import Repository
from db.entities import Location
repo = Repository()
def post():
body = request.json
repo.addLocation(body)
repo.add_location(Location(body))
return Response(status=201)
def get():
return repo.getLocations()
return [l.to_serializable_dict() for l in repo.get_locations()]
......@@ -32,3 +32,43 @@ spec:
image: alexx882/community-detection-microservice
ports:
- containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
name: community-detection-db
spec:
type: LoadBalancer
selector:
app: community-detection-db
ports:
- name: http
port: 27017
targetPort: 27017
nodePort: 30110
protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: community-detection-db
spec:
replicas: 1
selector:
matchLabels:
app: community-detection-db
template:
metadata:
labels:
app: community-detection-db
spec:
containers:
- name: community-detection-db
image: mongo
env:
- name: MONGO_INITDB_ROOT_USERNAME
value: root
- name: MONGO_INITDB_ROOT_PASSWORD
value: root
ports:
- containerPort: 27017
\ No newline at end of file
import pymongo
from pymongo import MongoClient, cursor
import network_constants as netconst
class MongoRepositoryBase:
'''Base class to connect to, insert and read from a single MongoDB collection'''
'''Base class to connect to a MongoDB database'''
# TODO extract to docker env var
_username = 'root'
_password = 'root'
_collection: pymongo.collection.Collection = None
_mongo_client: pymongo.MongoClient = None
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[database_name]
self._collection = database[collection_name]
def __init__(self, hostname, port, database_name, username=_username, password=_password):
self._mongo_client = MongoClient(f"mongodb://{username}:{password}@{hostname}:{port}/")
self._database = self._mongo_client[database_name]
def insert_entry(self, content: dict):
self._collection.insert_one(content)
def insert_entry(self, collection_name, content: dict):
collection = self._database[collection_name]
collection.insert_one(content)
def get_entries(self, selection: dict = {}, projection: dict = {'_': 0}) -> pymongo.cursor.Cursor:
return self._collection.find(selection, projection)
def get_entries(self, collection_name, selection: dict = {}, projection: dict = {'_': 0}) -> cursor.Cursor:
collection = self._database[collection_name]
return collection.find(selection, projection)
def close_connection(self):
self._mongo_client.close()
self._mongo_client = None
self._collection = None
......@@ -3,19 +3,15 @@ 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 __init__(self):
super().__init__(netconst.TRACE_RETRIEVAL_DB_HOSTNAME,
netconst.TRACE_RETRIEVAL_DB_PORT, 'traceRetrievalDB')
self._collection_name = 'traces'
def insert_trace(self, content: dict):
super().insert_entry(content)
super().insert_entry(self._collection_name, content)
def get_traces(self, selection: dict = {}, projection: dict = {'_': 0}) -> pymongo.cursor.Cursor:
return super().get_entries(selection, projection)
return super().get_entries(self._collection_name, selection, projection)
......@@ -4,7 +4,7 @@ from database.MongoRepository import MongoRepository
mongo_repo = MongoRepository()
def post():
return Response(status=501)
return Response(response='Use the RESTful Gateway instead', status=405)
def get():
return list(mongo_repo.get_traces(projection={'_id': 0}))
\ No newline at end of file
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