diff --git a/website/app/db_class/db.py b/website/app/db_class/db.py index 493fcca..224eb96 100644 --- a/website/app/db_class/db.py +++ b/website/app/db_class/db.py @@ -1,3 +1,4 @@ +import json from .. import db @@ -32,13 +33,38 @@ class Session_db(db.Model): query_date = db.Column(db.DateTime, index=True) def to_json(self): - return + json_dict = { + "id": self.id, + "uuid": self.uuid, + "modules": json.loads(self.modules_list), + "query_enter": self.query_enter, + "input_query": self.input_query, + "config_module": json.loads(self.config_module), + "result": json.loads(self.result), + "nb_errors": self.nb_errors, + "query_date": self.query_date.strftime('%Y-%m-%d') + } + return json_dict + + def history_json(self): + json_dict = { + "uuid": self.uuid, + "modules": json.loads(self.modules_list), + "query": self.query_enter, + "input": self.input_query, + "query_date": self.query_date.strftime('%Y-%m-%d') + } + return json_dict class History(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) session_id = db.Column(db.Integer, index=True) +class History_Tree(db.Model): + id = db.Column(db.Integer, primary_key=True, autoincrement=True) + session_uuid = db.Column(db.String(36), index=True) + tree = db.Column(db.String) class Config(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) diff --git a/website/app/home.py b/website/app/home.py index 312d483..e809423 100644 --- a/website/app/home.py +++ b/website/app/home.py @@ -15,6 +15,13 @@ home_blueprint = Blueprint( def home(): return render_template("home.html") +@home_blueprint.route("/home/", methods=["GET", "POST"]) +def home_query(sid): + if "query" in request.args: + query = request.args.get("query") + return render_template("home.html", query=query, sid=sid) + return render_template("404.html") + @home_blueprint.route("/query/") def query(sid): session = HomeModel.get_session(sid) @@ -24,7 +31,7 @@ def query(sid): query_loc = session.query_enter else: for s in SessionModel.sessions: - if s.id == sid: + if s.uuid == sid: flag = True query_loc = s.query session=s @@ -64,6 +71,7 @@ def run_modules(): if "input" in request.json: if "modules" in request.json: session = SessionModel.Session_class(request.json) + HomeModel.set_flask_session(session, request.json["parent_id"]) session.start() SessionModel.sessions.append(session) return jsonify(session.status()), 201 @@ -79,7 +87,7 @@ def status(sid): return jsonify(HomeModel.get_status_db(sess)) else: for s in SessionModel.sessions: - if s.id == sid: + if s.uuid == sid: return jsonify(s.status()) return jsonify({'message': 'Scan session not found'}), 404 @@ -91,7 +99,7 @@ def result(sid): return jsonify(HomeModel.get_result_db(sess)) else: for s in SessionModel.sessions: - if s.id == sid: + if s.uuid == sid: return jsonify(s.get_result()) return jsonify({'message': 'Scan session not found'}), 404 @@ -145,4 +153,35 @@ def history(): def get_history(): """Get all history""" histories = HomeModel.get_history() - return histories \ No newline at end of file + return histories + +@home_blueprint.route("/history_session", methods=["GET"]) +def history_session(): + """View all history""" + return render_template("history_session.html", tree_view=False) + +@home_blueprint.route("/get_history_session", methods=["GET"]) +def get_history_session(): + """Get all history""" + histories = HomeModel.get_history_session() + if histories: + return histories + return {} + +@home_blueprint.route("/save_history/", methods=["GET"]) +def save_history(sid): + return HomeModel.save_history_core(sid) + + +@home_blueprint.route("/history_tree", methods=["GET"]) +def history_tree(): + """View all history""" + return render_template("history_session.html", tree_view=True) + +@home_blueprint.route("/get_history_tree", methods=["GET"]) +def get_history_tree(): + """Get all history""" + histories = HomeModel.get_history_tree() + if histories: + return histories + return {} \ No newline at end of file diff --git a/website/app/home_core.py b/website/app/home_core.py index 01deeab..c2ea143 100644 --- a/website/app/home_core.py +++ b/website/app/home_core.py @@ -1,7 +1,10 @@ import json -from .utils.utils import query_get_module +from .utils.utils import query_get_module, isUUID from . import db -from .db_class.db import History, Module, Config, Module_Config, Session_db +from .db_class.db import History, Module, Config, Module_Config, Session_db, History_Tree +from . import sess +from flask import session as sess +from sqlalchemy import desc def get_module(mid): @@ -29,7 +32,7 @@ def get_module_config_both(mid, cid): return Module_Config.query.filter_by(module_id=mid, config_id=cid).first() def get_session(sid): - """Return a session by id""" + """Return a session by uuid""" return Session_db.query.filter_by(uuid=sid).first() def get_modules(): @@ -138,14 +141,142 @@ def get_result_db(session): def get_history(): """Return history""" histories_list = list() - histories = History.query.all() + histories = History.query.order_by(desc(History.id)) for history in histories: session = Session_db.query.get(history.session_id) - histories_list.append({ - "uuid": session.uuid, - "query": session.query_enter, - "modules": json.loads(session.modules_list), - "input": session.input_query, - "query_date": session.query_date.strftime('%Y-%m-%d') - }) + histories_list.append(session.history_json()) return histories_list + + +def util_set_flask_session(parent_id, loc_session, current_session): + if parent_id == loc_session["uuid"]: + loc_json = { + "uuid": current_session.uuid, + "modules": current_session.modules_list, + "query": current_session.query, + "input": current_session.input_query, + "query_date": current_session.query_date.strftime('%Y-%m-%d') + } + loc_session["children"].append(loc_json) + return True + elif "children" in loc_session: + return deep_explore(loc_session["children"], parent_id, current_session) + +def deep_explore(session_dict, parent_id, current_session): + for loc_session in session_dict: + if not "children" in loc_session: + loc_session["children"] = list() + if util_set_flask_session(parent_id, loc_session, current_session): + return True + return False + +def set_flask_session(current_session, parent_id): + current_query = sess.get("current_query") + if not current_query or current_query not in sess: + loc_json = { + "uuid": current_session.uuid, + "modules": current_session.modules_list, + "query": current_session.query, + "input": current_session.input_query, + "query_date": current_session.query_date.strftime('%Y-%m-%d') + } + + sess["current_query"] = current_session.uuid + sess[sess.get("current_query")] = loc_json + sess[sess.get("current_query")]["children"] = list() + else: + # sess["uuid"] + loc_session = sess.get(sess.get("current_query")) + if not "children" in loc_session: + loc_session["children"] = list() + if not util_set_flask_session(parent_id, loc_session, current_session): + sess["current_query"] = current_session.uuid + +def get_history_session(): + current_query = sess.get("current_query") + loc_list = list() + if current_query: + # If current query have no children then don't display it + # It's already save in history + # Only parent-child tree structure is in flask session + current_query_value = sess.get(sess.get("current_query")) + if current_query_value and current_query_value["children"]: + loc_list.append(current_query_value) + for q in sess: + if isUUID(q): + # If query have no children then don't display it + q_value = sess.get(q) + if q_value["children"]: + if not q == current_query: + loc_list.append(q_value) + + return loc_list + + + + +def util_save_history(session): + loc_dict = dict() + loc_dict[session["uuid"]] = [] + + if "children" in session and session["children"]: + for child in session["children"]: + loc_dict[session["uuid"]].append(util_save_history(child)) + return loc_dict + + +def save_history_core(sid): + """Save history from session to db""" + if sid in sess: + session = sess.get(sid) + # Doesn't already exist + history_tree_db = History_Tree.query.filter_by(session_uuid=session["uuid"]).first() + if not history_tree_db: + if "children" in session and session["children"]: + # Get all children before add to db + loc_dict = util_save_history(session) + h = History_Tree( + session_uuid = session["uuid"], + tree=json.dumps(loc_dict) + ) + db.session.add(h) + db.session.commit() + return {"message": "History Save", 'toast_class': "success-subtle"} + return {"message": "No children", 'toast_class': "warning-subtle"} + # Save same session but with new value + elif not json.loads(history_tree_db.tree) == session: + if "children" in session and session["children"]: + # Get all children before add to db + loc_dict = util_save_history(session) + history_tree_db.tree = json.dumps(loc_dict) + db.session.commit() + return {"message": "History updated", 'toast_class': "success-subtle"} + return {"message": "History already saved", 'toast_class': "warning-subtle"} + return {"message": "Session not found", 'toast_class': "danger-subtle"} + + + +def util_get_history_tree(child): + loc_child = list(child.keys())[0] + loc_session = get_session(loc_child) + loc_json = loc_session.history_json() + loc_json["children"] = list() + if child[loc_child]: + for s_child in child[loc_child]: + loc_json["children"].append(util_get_history_tree(s_child)) + return loc_json + +def get_history_tree(): + """Return all histories saved as tree""" + histories_tree = History_Tree.query.order_by(desc(History_Tree.id)) + loc_dict = list() + for history_tree in histories_tree: + tree = json.loads(history_tree.tree) + + loc_session = get_session(history_tree.session_uuid) + loc_json = loc_session.history_json() + loc_json["children"] = list() + for child in tree[history_tree.session_uuid]: + loc_json["children"].append(util_get_history_tree(child)) + loc_dict.append(loc_json) + return loc_dict diff --git a/website/app/session.py b/website/app/session.py index b1bee96..baf832c 100644 --- a/website/app/session.py +++ b/website/app/session.py @@ -14,7 +14,7 @@ sessions = list() class Session_class: def __init__(self, request_json) -> None: - self.id = str(uuid4()) + self.uuid = str(uuid4()) self.thread_count = 4 self.jobs = Queue(maxsize=0) self.threads = [] @@ -63,7 +63,7 @@ class Session_class: registered = len(self.result) return { - 'id': self.id, + 'id': self.uuid, 'total': total, 'complete': complete, 'remaining': remaining, @@ -110,7 +110,7 @@ class Session_class: else: send_to = {"module": work[1], self.input_query: self.query, "config": loc_config} res = query_post_query(send_to) - print(res) + # print(res) if "error" in res: self.nb_errors += 1 self.result[work[1]] = res @@ -124,7 +124,7 @@ class Session_class: def save_info(self): """Save info in the db""" s = Session_db( - uuid=str(self.id), + uuid=str(self.uuid), modules_list=json.dumps(self.modules_list), query_enter=self.query, input_query=self.input_query, diff --git a/website/app/static/js/history_view.js b/website/app/static/js/history_view.js index aa17b9d..be79456 100644 --- a/website/app/static/js/history_view.js +++ b/website/app/static/js/history_view.js @@ -1,5 +1,4 @@ -const { ref, nextTick } = Vue export default { name: "History_view", delimiters: ['[[', ']]'], @@ -7,36 +6,63 @@ export default { history: Object, key_loop: Number }, - setup(props) { - - - return { - - } - }, template: ` -
+
+
    +
  • +
    + +
    +
  • +
+
+
` } \ No newline at end of file diff --git a/website/app/templates/history.html b/website/app/templates/history.html index 871ec52..1e7d06e 100644 --- a/website/app/templates/history.html +++ b/website/app/templates/history.html @@ -9,12 +9,32 @@

-
+ [Go Back Top] {% endblock %} @@ -22,7 +42,7 @@ {% block script %} +{% endblock %} \ No newline at end of file diff --git a/website/app/templates/home.html b/website/app/templates/home.html index c31cd24..7157be1 100644 --- a/website/app/templates/home.html +++ b/website/app/templates/home.html @@ -15,7 +15,8 @@
- + +
[[status_site]]
@@ -100,8 +101,9 @@ let error_flag = false let result_dict = {"modules": $("#modules_select").val(), - "input": $("#input_select").val(), - "query": current_query.value + "input": $("#input_select").val(), + "query": current_query.value, + "parent_id": $("#parent_id").val() } result_dict["config"] = {} for(let el in config_query.value){ diff --git a/website/app/templates/query.html b/website/app/templates/query.html index ff1d8bc..9ef7d2f 100644 --- a/website/app/templates/query.html +++ b/website/app/templates/query.html @@ -127,6 +127,7 @@
Value: [[attr.value]]
+ query
diff --git a/website/app/templates/sidebar.html b/website/app/templates/sidebar.html index abad4a0..bf2a35d 100644 --- a/website/app/templates/sidebar.html +++ b/website/app/templates/sidebar.html @@ -18,6 +18,12 @@ History + + History Session + + + History Tree + Config diff --git a/website/app/utils/utils.py b/website/app/utils/utils.py index b991ab0..0f72e11 100644 --- a/website/app/utils/utils.py +++ b/website/app/utils/utils.py @@ -1,5 +1,5 @@ # import os -# import uuid +import uuid import json import requests # import jsonschema @@ -31,12 +31,12 @@ def query_post_query(data, headers={'Content-type': 'application/json'}): return r.json() -# def isUUID(uid): -# try: -# uuid.UUID(str(uid)) -# return True -# except ValueError: -# return False +def isUUID(uid): + try: + uuid.UUID(str(uid)) + return True + except ValueError: + return False # def form_to_dict(form):