From 3d14bac434ca8be4115fdc568424f98c691a69fa Mon Sep 17 00:00:00 2001 From: terrtia Date: Wed, 27 Nov 2024 14:17:22 +0100 Subject: [PATCH] chg: [dashboard] add objects tooltip --- bin/lib/Tag.py | 8 + bin/lib/ail_stats.py | 17 ++ bin/lib/objects/ail_objects.py | 6 + var/www/Flask_server.py | 7 +- var/www/blueprints/objects_objs.py | 11 +- var/www/static/js/helper.js | 121 ++++++++++++++ var/www/templates/objects/objs_dashboard.html | 157 +++++++++++++++++- 7 files changed, 320 insertions(+), 7 deletions(-) diff --git a/bin/lib/Tag.py b/bin/lib/Tag.py index 86bffd3a..71172d8f 100755 --- a/bin/lib/Tag.py +++ b/bin/lib/Tag.py @@ -709,7 +709,15 @@ def add_object_tag(tag, obj_type, obj_id, subtype=''): else: r_tags.sadd(f'{obj_type}:{subtype}:{tag}', obj_id) + # STATS r_tags.hincrby(f'daily_tags:{datetime.date.today().strftime("%Y%m%d")}', tag, 1) + mess = f'{int(time.time())}:{obj_type}:{subtype}:{obj_id}' + r_tags.lpush('dashboard:tags', mess) + r_tags.ltrim('dashboard:tags', 0, 19) + +def get_tags_dashboard(): + return r_tags.lrange('dashboard:tags', 0, -1) + # obj -> Object() def confirm_tag(tag, obj): diff --git a/bin/lib/ail_stats.py b/bin/lib/ail_stats.py index 3961b8c9..9a741fa6 100755 --- a/bin/lib/ail_stats.py +++ b/bin/lib/ail_stats.py @@ -15,6 +15,8 @@ sys.path.append(os.environ['AIL_BIN']) ################################## from lib.ConfigLoader import ConfigLoader from lib.objects import ail_objects +from lib import Tag +from lib import Tracker # Config @@ -93,5 +95,20 @@ def get_nb_objs_dashboard(): date = datetime.date.today().strftime("%Y%m%d") return ail_objects.get_nb_objects_dashboard(date) +def get_tagged_objs_dashboard(): + tagged_objs = [] + for tagged_obj in Tag.get_tags_dashboard(): + timestamp, obj_gid = tagged_obj.split(':', 1) + timestamp = datetime.datetime.utcfromtimestamp(int(timestamp)).strftime('%H:%M:%S') + obj_meta = ail_objects.get_obj_basic_meta(ail_objects.get_obj_from_global_id(obj_gid), flask_context=True) + obj_meta['date_tag'] = timestamp + tagged_objs.append(obj_meta) + return tagged_objs + +def get_tracked_objs_dashboard(user_org, user_id): + trackers = Tracker.get_trackers_dashboard(user_org, user_id) + for t in trackers: + t['obj'] = ail_objects.get_obj_basic_meta(ail_objects.get_obj_from_global_id(t['obj'])) + return trackers diff --git a/bin/lib/objects/ail_objects.py b/bin/lib/objects/ail_objects.py index 3df4688b..b6a6c505 100755 --- a/bin/lib/objects/ail_objects.py +++ b/bin/lib/objects/ail_objects.py @@ -258,6 +258,12 @@ def add_obj_tags(obj_type, subtype, id, tags): #### OBJ META #### +def get_obj_basic_meta(obj, flask_context=False): + meta = obj.get_default_meta(tags=True) + meta['icon'] = obj.get_svg_icon() + meta['link'] = obj.get_link(flask_context=flask_context) + meta['gid'] = obj.get_global_id() + return meta def get_object_meta(obj_type, subtype, id, options=set(), flask_context=False): obj = get_object(obj_type, subtype, id) diff --git a/var/www/Flask_server.py b/var/www/Flask_server.py index a6050194..01e8bef3 100755 --- a/var/www/Flask_server.py +++ b/var/www/Flask_server.py @@ -302,7 +302,8 @@ sock = Sock(app) @login_required @sock.route('/ws/dashboard') def ws_dashboard(ws): - # TODO wait %30 + user_org = current_user.get_org() + user_id = current_user.get_user_id() next_feeders = ail_stats.get_next_feeder_timestamp(int(time.time())) + 1 try: while True: @@ -313,7 +314,9 @@ def ws_dashboard(ws): if int(time.time()) >= next_feeders: feeders = ail_stats.get_feeders_dashboard() objs = ail_stats.get_nb_objs_today() - ws.send(json.dumps({'feeders': feeders, 'objs': objs})) + tags = ail_stats.get_tagged_objs_dashboard() + trackers = ail_stats.get_tracked_objs_dashboard(user_org, user_id) + ws.send(json.dumps({'feeders': feeders, 'objs': objs, 'tags': tags, 'trackers': trackers})) next_feeders = next_feeders + 30 time.sleep(1) except Exception as e: # ConnectionClosed ? diff --git a/var/www/blueprints/objects_objs.py b/var/www/blueprints/objects_objs.py index 22f51e09..d282ecb5 100644 --- a/var/www/blueprints/objects_objs.py +++ b/var/www/blueprints/objects_objs.py @@ -10,7 +10,7 @@ import sys import json from flask import Flask, render_template, jsonify, request, Blueprint, redirect, url_for, Response, abort, send_file, stream_with_context -from flask_login import login_required +from flask_login import login_required, current_user # Import Role_Manager from Role_Manager import login_admin, login_read_only @@ -37,10 +37,15 @@ bootstrap_label = ['primary', 'success', 'danger', 'warning', 'info'] @login_required @login_read_only def objects(): + user_org = current_user.get_org() + user_id = current_user.get_user_id() nb_objects = ail_stats.get_nb_objs_dashboard() - print(nb_objects) feeders_dashboard = ail_stats.get_feeders_dashboard_full() - return render_template("objs_dashboard.html", feeders_dashboard=feeders_dashboard, nb_objects=nb_objects) + trackers = ail_stats.get_tracked_objs_dashboard(user_org, user_id) + tagged_objs = ail_stats.get_tagged_objs_dashboard() + return render_template("objs_dashboard.html", feeders_dashboard=feeders_dashboard, + nb_objects=nb_objects, trackers=trackers, tagged_objs=tagged_objs, + bootstrap_label=bootstrap_label) diff --git a/var/www/static/js/helper.js b/var/www/static/js/helper.js index 0f493bc4..1cb824a3 100644 --- a/var/www/static/js/helper.js +++ b/var/www/static/js/helper.js @@ -2,3 +2,124 @@ function sanitize_text(str_to_sanitize) { return $("").text(str_to_sanitize).html() } + +// REQUIRE var url_obj_description +function show_obj_tooltip(container, obj_gid) { + container = $(container); + + if (container.data('bs.popover')) { + container.popover('show'); + } else { + let pop_header = "
" + sanitize_text(obj_gid) + "/div>"; + let spinner = "
Loading...
"; + + container.popover({ + title: pop_header, + content: spinner, + html: true, + container: container, + }) + container.popover('show'); + + let popoverInstance = container.data('bs.popover'); + + + $.getJSON(url_obj_description + obj_gid, function (data) { + + let desc = "
" + Object.keys(data).forEach(function(key) { + if (key=="status") { + desc = desc + "
status
UP" + } else { + desc = desc + "fa-times-circle\">DOWN" + } + desc = desc + "
" + } else if (key!=="tags" && key!=="id" && key!=="img" && key!=="svg_icon" && key!=="icon" && key!=="link" && key!=="type") { + if (data[key]) { + if ((key==="first_seen" || key==="last_seen") && data[key].length===8) { + let date = sanitize_text(data[key]) + desc = desc + "
" + sanitize_text(key) + "
" + date.slice(0, 4) + "-" + date.slice(4, 6) + "-" + date.slice(6, 8) + "
" + } else { + desc = desc + "
" + sanitize_text(key) + "
" + sanitize_text(data[key]) + "
" + } + } + } + }); + desc = desc + "
" + + if (data["tags"]) { + data["tags"].forEach(function(tag) { + desc = desc + ""+ sanitize_text(tag) +""; + }); + } + + /*if (data["img"]) { + if (data["tags_safe"]) { + if (data["type"] === "screenshot") { + desc = desc + ""; + } else { + desc = desc + ""; + } + }*/ + + desc = desc + "
" + //div.html(desc) + // .style("left", (d3_pageX) + "px") + // .style("top", (d3_pageY - 28) + "px"); + //d.popover = desc + + if (data["img"]) { + blur_tooltip(); + } + + popoverInstance.config.content = desc; + popoverInstance.setContent(); + popoverInstance.update(); + + + //let popoverid = container.attr('aria-describedby'); + //$('#' + popoverid).find('.popover-header').html(newTitle); + //$('#' + popoverid).find('.popover-body').html('newContesssnt'); + + }).fail(function(error) { + let desc = "
"+ error.statusText +"
" + popoverInstance.config.content = desc; + popoverInstance.setContent(); + popoverInstance.update(); + + }); + + container.popover('hide'); + container.popover('show'); + } + +} + +function hide_obj_tooltip(container) { + container = $(container); + container.popover('hide') + +} + +function blur_tooltip(){ + var image = $('#tooltip_screenshot_correlation')[0]; + if (image) { + let blurValue = $('#blur-slider-correlation').val(); + blurValue = 15 - blurValue; + image.style.filter = "blur(" + blurValue + "px)"; + } +} \ No newline at end of file diff --git a/var/www/templates/objects/objs_dashboard.html b/var/www/templates/objects/objs_dashboard.html index f1f9a3fd..33e9a4e2 100644 --- a/var/www/templates/objects/objs_dashboard.html +++ b/var/www/templates/objects/objs_dashboard.html @@ -11,6 +11,8 @@ + + @@ -24,6 +26,10 @@