This commit is contained in:
Itay Mizeretz 2018-02-27 19:13:28 +02:00
parent e11a75eb48
commit d8946feb69
5 changed files with 24 additions and 17 deletions

View File

@ -6,7 +6,7 @@ from bson.json_util import dumps
from flask import Flask, send_from_directory, make_response
from werkzeug.exceptions import NotFound
from cc.database import mongo
from cc.database import mongo, database
from cc.resources.client_run import ClientRun
from cc.resources.edge import Edge
from cc.resources.local_run import LocalRun
@ -75,6 +75,7 @@ def init_app(mongo_url):
mongo.init_app(app)
with app.app_context():
database.init()
ConfigService.init_config()
app.add_url_rule('/', 'serve_home', serve_home)

View File

@ -1,5 +1,5 @@
from flask_pymongo import PyMongo
from flask_pymongo import MongoClient
import gridfs
from flask_pymongo import MongoClient, PyMongo
from pymongo.errors import ServerSelectionTimeoutError
__author__ = 'Barak'
@ -7,6 +7,17 @@ __author__ = 'Barak'
mongo = PyMongo()
class Database:
def __init__(self):
self.gridfs = None
def init(self):
self.gridfs = gridfs.GridFS(mongo.db)
database = Database()
def is_db_server_up(mongo_url):
client = MongoClient(mongo_url, serverSelectionTimeoutMS=100)
try:

View File

@ -24,7 +24,7 @@ class Log(flask_restful.Resource):
telemetry_json = json.loads(request.data)
monkey_id = NodeService.get_monkey_by_guid(telemetry_json['monkey_guid'])['_id']
# This is base64 so no data will be lost. this'll take 2 time less space.
# This shouldn't contain any unicode characters. this'll take 2 time less space.
log_data = str(telemetry_json['log'])
log_id = LogService.add_log(monkey_id, log_data)

View File

@ -33,8 +33,7 @@ class Root(flask_restful.Resource):
@staticmethod
def reset_db():
[mongo.db[x].drop() for x in
['config', 'monkey', 'telemetry', 'node', 'edge', 'report', 'log', 'fs.chunks', 'fs.files']]
[mongo.db[x].drop() for x in mongo.db.collection_names()]
ConfigService.init_config()
return jsonify(status='OK')

View File

@ -1,9 +1,7 @@
from datetime import datetime
import gridfs
import cc.services.node
from cc.database import mongo
from cc.database import mongo, database
__author__ = "itay.mizeretz"
@ -16,8 +14,7 @@ class LogService:
def get_log_by_monkey_id(monkey_id):
log = mongo.db.log.find_one({'monkey_id': monkey_id})
if log:
fs = gridfs.GridFS(mongo.db)
log_file = fs.get(log['file_id'])
log_file = database.gridfs.get(log['file_id'])
monkey_label = cc.services.node.NodeService.get_monkey_label(
cc.services.node.NodeService.get_monkey_by_id(log['monkey_id']))
return \
@ -29,16 +26,15 @@ class LogService:
@staticmethod
def remove_logs_by_monkey_id(monkey_id):
fs = gridfs.GridFS(mongo.db)
for log in mongo.db.log.find({'monkey_id': monkey_id}):
fs.delete(log['file_id'])
mongo.db.log.delete_many({'monkey_id': monkey_id})
log = mongo.db.log.find_one({'monkey_id': monkey_id})
if log is not None:
database.gridfs.delete(log['file_id'])
mongo.db.log.delete_one({'monkey_id': monkey_id})
@staticmethod
def add_log(monkey_id, log_data, timestamp=datetime.now()):
LogService.remove_logs_by_monkey_id(monkey_id)
fs = gridfs.GridFS(mongo.db)
file_id = fs.put(log_data)
file_id = database.gridfs.put(log_data)
return mongo.db.log.insert(
{
'monkey_id': monkey_id,