forked from p34709852/monkey
Add basic report logic
This commit is contained in:
parent
095f05370f
commit
22105eabe3
|
@ -15,7 +15,7 @@ from cc.resources.monkey_download import MonkeyDownload
|
|||
from cc.resources.netmap import NetMap
|
||||
from cc.resources.edge import Edge
|
||||
from cc.resources.node import Node
|
||||
|
||||
from cc.resources.report import Report
|
||||
from cc.resources.root import Root
|
||||
from cc.services.config import ConfigService
|
||||
|
||||
|
@ -88,5 +88,6 @@ def init_app(mongo_url):
|
|||
api.add_resource(NetMap, '/api/netmap', '/api/netmap/')
|
||||
api.add_resource(Edge, '/api/netmap/edge', '/api/netmap/edge/')
|
||||
api.add_resource(Node, '/api/netmap/node', '/api/netmap/node/')
|
||||
api.add_resource(Report, '/api/report', '/api/report/')
|
||||
|
||||
return app
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
import flask_restful
|
||||
|
||||
from cc.services.report import ReportService
|
||||
|
||||
__author__ = "itay.mizeretz"
|
||||
|
||||
|
||||
class Report(flask_restful.Resource):
|
||||
def get(self):
|
||||
return ReportService.get_report()
|
|
@ -0,0 +1,41 @@
|
|||
from cc.database import mongo
|
||||
|
||||
__author__ = "itay.mizeretz"
|
||||
|
||||
|
||||
class ReportService:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def get_first_monkey_time():
|
||||
return mongo.db.telemetry.find({}, {'timestamp': 1}).sort([('$natural', 1)]).limit(1)[0]['timestamp']
|
||||
|
||||
@staticmethod
|
||||
def get_last_monkey_dead_time():
|
||||
return mongo.db.telemetry.find({}, {'timestamp': 1}).sort([('$natural', -1)]).limit(1)[0]['timestamp']
|
||||
|
||||
@staticmethod
|
||||
def get_breach_count():
|
||||
return mongo.db.edge.count({'exploits.result': True})
|
||||
|
||||
@staticmethod
|
||||
def get_successful_exploit_types():
|
||||
exploit_types = mongo.db.command({'distinct': 'edge', 'key': 'exploits.exploiter'})['values']
|
||||
return [exploit for exploit in exploit_types if ReportService.did_exploit_type_succeed(exploit)]
|
||||
|
||||
@staticmethod
|
||||
def get_report():
|
||||
return \
|
||||
{
|
||||
'first_monkey_time': ReportService.get_first_monkey_time(),
|
||||
'last_monkey_dead_time': ReportService.get_last_monkey_dead_time(),
|
||||
'breach_count': ReportService.get_breach_count(),
|
||||
'successful_exploit_types': ReportService.get_successful_exploit_types(),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def did_exploit_type_succeed(exploit_type):
|
||||
return mongo.db.edge.count(
|
||||
{'exploits': {'$elemMatch': {'exploiter': exploit_type, 'result': True}}},
|
||||
limit=1) > 0
|
Loading…
Reference in New Issue