forked from p34709852/monkey
Fix CR
This commit is contained in:
parent
e11a75eb48
commit
d8946feb69
|
@ -6,7 +6,7 @@ from bson.json_util import dumps
|
||||||
from flask import Flask, send_from_directory, make_response
|
from flask import Flask, send_from_directory, make_response
|
||||||
from werkzeug.exceptions import NotFound
|
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.client_run import ClientRun
|
||||||
from cc.resources.edge import Edge
|
from cc.resources.edge import Edge
|
||||||
from cc.resources.local_run import LocalRun
|
from cc.resources.local_run import LocalRun
|
||||||
|
@ -75,6 +75,7 @@ def init_app(mongo_url):
|
||||||
mongo.init_app(app)
|
mongo.init_app(app)
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
|
database.init()
|
||||||
ConfigService.init_config()
|
ConfigService.init_config()
|
||||||
|
|
||||||
app.add_url_rule('/', 'serve_home', serve_home)
|
app.add_url_rule('/', 'serve_home', serve_home)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from flask_pymongo import PyMongo
|
import gridfs
|
||||||
from flask_pymongo import MongoClient
|
from flask_pymongo import MongoClient, PyMongo
|
||||||
from pymongo.errors import ServerSelectionTimeoutError
|
from pymongo.errors import ServerSelectionTimeoutError
|
||||||
|
|
||||||
__author__ = 'Barak'
|
__author__ = 'Barak'
|
||||||
|
@ -7,6 +7,17 @@ __author__ = 'Barak'
|
||||||
mongo = PyMongo()
|
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):
|
def is_db_server_up(mongo_url):
|
||||||
client = MongoClient(mongo_url, serverSelectionTimeoutMS=100)
|
client = MongoClient(mongo_url, serverSelectionTimeoutMS=100)
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -24,7 +24,7 @@ class Log(flask_restful.Resource):
|
||||||
telemetry_json = json.loads(request.data)
|
telemetry_json = json.loads(request.data)
|
||||||
|
|
||||||
monkey_id = NodeService.get_monkey_by_guid(telemetry_json['monkey_guid'])['_id']
|
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_data = str(telemetry_json['log'])
|
||||||
log_id = LogService.add_log(monkey_id, log_data)
|
log_id = LogService.add_log(monkey_id, log_data)
|
||||||
|
|
||||||
|
|
|
@ -33,8 +33,7 @@ class Root(flask_restful.Resource):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def reset_db():
|
def reset_db():
|
||||||
[mongo.db[x].drop() for x in
|
[mongo.db[x].drop() for x in mongo.db.collection_names()]
|
||||||
['config', 'monkey', 'telemetry', 'node', 'edge', 'report', 'log', 'fs.chunks', 'fs.files']]
|
|
||||||
ConfigService.init_config()
|
ConfigService.init_config()
|
||||||
return jsonify(status='OK')
|
return jsonify(status='OK')
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import gridfs
|
|
||||||
|
|
||||||
import cc.services.node
|
import cc.services.node
|
||||||
from cc.database import mongo
|
from cc.database import mongo, database
|
||||||
|
|
||||||
__author__ = "itay.mizeretz"
|
__author__ = "itay.mizeretz"
|
||||||
|
|
||||||
|
@ -16,8 +14,7 @@ class LogService:
|
||||||
def get_log_by_monkey_id(monkey_id):
|
def get_log_by_monkey_id(monkey_id):
|
||||||
log = mongo.db.log.find_one({'monkey_id': monkey_id})
|
log = mongo.db.log.find_one({'monkey_id': monkey_id})
|
||||||
if log:
|
if log:
|
||||||
fs = gridfs.GridFS(mongo.db)
|
log_file = database.gridfs.get(log['file_id'])
|
||||||
log_file = fs.get(log['file_id'])
|
|
||||||
monkey_label = cc.services.node.NodeService.get_monkey_label(
|
monkey_label = cc.services.node.NodeService.get_monkey_label(
|
||||||
cc.services.node.NodeService.get_monkey_by_id(log['monkey_id']))
|
cc.services.node.NodeService.get_monkey_by_id(log['monkey_id']))
|
||||||
return \
|
return \
|
||||||
|
@ -29,16 +26,15 @@ class LogService:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def remove_logs_by_monkey_id(monkey_id):
|
def remove_logs_by_monkey_id(monkey_id):
|
||||||
fs = gridfs.GridFS(mongo.db)
|
log = mongo.db.log.find_one({'monkey_id': monkey_id})
|
||||||
for log in mongo.db.log.find({'monkey_id': monkey_id}):
|
if log is not None:
|
||||||
fs.delete(log['file_id'])
|
database.gridfs.delete(log['file_id'])
|
||||||
mongo.db.log.delete_many({'monkey_id': monkey_id})
|
mongo.db.log.delete_one({'monkey_id': monkey_id})
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add_log(monkey_id, log_data, timestamp=datetime.now()):
|
def add_log(monkey_id, log_data, timestamp=datetime.now()):
|
||||||
LogService.remove_logs_by_monkey_id(monkey_id)
|
LogService.remove_logs_by_monkey_id(monkey_id)
|
||||||
fs = gridfs.GridFS(mongo.db)
|
file_id = database.gridfs.put(log_data)
|
||||||
file_id = fs.put(log_data)
|
|
||||||
return mongo.db.log.insert(
|
return mongo.db.log.insert(
|
||||||
{
|
{
|
||||||
'monkey_id': monkey_id,
|
'monkey_id': monkey_id,
|
||||||
|
|
Loading…
Reference in New Issue