Skip to content

Commit

Permalink
add: [website] tree view history
Browse files Browse the repository at this point in the history
Flask session store history and after save it's store in DB
  • Loading branch information
DavidCruciani committed Feb 15, 2024
1 parent b38e4fe commit 0364dec
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 60 deletions.
28 changes: 27 additions & 1 deletion website/app/db_class/db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from .. import db


Expand Down Expand Up @@ -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)
Expand Down
47 changes: 43 additions & 4 deletions website/app/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
def home():
return render_template("home.html")

@home_blueprint.route("/home/<sid>", 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/<sid>")
def query(sid):
session = HomeModel.get_session(sid)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -145,4 +153,35 @@ def history():
def get_history():
"""Get all history"""
histories = HomeModel.get_history()
return histories
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/<sid>", 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 {}
153 changes: 142 additions & 11 deletions website/app/home_core.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions website/app/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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 = []
Expand Down Expand Up @@ -63,7 +63,7 @@ def status(self):
registered = len(self.result)

return {
'id': self.id,
'id': self.uuid,
'total': total,
'complete': complete,
'remaining': remaining,
Expand Down Expand Up @@ -110,7 +110,7 @@ def process(self):
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
Expand All @@ -124,7 +124,7 @@ def get_result(self):
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,
Expand Down

0 comments on commit 0364dec

Please sign in to comment.