neptune/core/views/ui/drilldown.py

472 lines
16 KiB
Python
Raw Normal View History

import urllib
import uuid
2022-07-27 19:53:41 +00:00
2022-09-30 06:22:22 +00:00
import orjson
from django.conf import settings
from django.http import HttpResponse, JsonResponse
2022-07-21 12:51:55 +00:00
from django.shortcuts import render
from django.urls import reverse
from django_tables2 import SingleTableView
2022-07-21 12:51:55 +00:00
from rest_framework.parsers import FormParser
from rest_framework.views import APIView
from core.db import add_defaults, remove_defaults
2022-09-27 14:15:08 +00:00
from core.db.storage import db
2022-07-21 12:51:55 +00:00
from core.lib.threshold import (
annotate_num_chans,
annotate_num_users,
get_chans,
get_users,
)
2022-08-09 08:04:31 +00:00
from core.views.ui.tables import DrilldownTable
2022-08-26 06:20:30 +00:00
# from copy import deepcopy
2022-08-09 08:04:31 +00:00
def parse_dates(dates):
spl = dates.split(" - ")
if all(spl):
spl = [f"{x.replace(' ', 'T')}" for x in spl]
if not len(spl) == 2:
message = "Invalid dates"
message_class = "danger"
return {"message": message, "class": message_class}
from_ts, to_ts = spl
from_date, from_time = from_ts.split("T")
to_date, to_time = to_ts.split("T")
return {
"from_date": from_date,
"to_date": to_date,
"from_time": from_time,
"to_time": to_time,
}
def create_tags(query):
"""
Grab the tags out of the query and make a list
we can add to the Bulma tags element when the page loads.
"""
spl = query.split("AND")
spl = [x.strip() for x in spl if ":" in x]
spl = [x.replace('"', "") for x in spl]
2022-08-11 22:17:45 +00:00
tags = [f"{tag}: {elem}" for tag, elem in [x.split(":")[:2] for x in spl]]
2022-08-09 06:20:30 +00:00
return tags
def parse_tags(tags_pre):
"""
Parse the tags from the variable tags_pre.
"""
2022-09-30 06:22:22 +00:00
tags = []
tags_spl = tags_pre.split(",")
if tags_spl:
for tag in tags_spl:
tag = tag.split(": ")
if len(tag) == 2:
key, val = tag
2022-09-30 06:22:22 +00:00
tags.append({key: val})
return tags
def make_table(context):
table = DrilldownTable(context["object_list"])
context["table"] = table
# del context["results"]
return context
def make_graph(results):
2022-08-27 16:47:33 +00:00
graph = []
for index, item in enumerate(results):
date = str(index)
graph.append(
{
2022-09-27 14:15:08 +00:00
"text": item.get("words_noun", None)
2022-08-27 16:47:33 +00:00
or item.get("msg", None)
or item.get("id"),
"nick": item.get("nick", None),
2022-08-27 16:47:33 +00:00
"channel": item.get("channel", None),
"net": item.get("net", None),
"value": item.get("sentiment", None) or None,
2022-08-27 16:47:33 +00:00
"date": date,
}
2022-08-27 16:47:33 +00:00
)
2022-09-30 06:22:22 +00:00
return orjson.dumps(graph).decode("utf-8")
class DrilldownTableView(SingleTableView):
table_class = DrilldownTable
template_name = "wm/widget.html"
window_content = "window-content/results.html"
# htmx_partial = "partials/"
2022-08-10 19:45:55 +00:00
paginate_by = settings.DRILLDOWN_RESULTS_PER_PAGE
def common_request(self, request, **kwargs):
extra_params = {}
2022-08-10 19:36:55 +00:00
if request.user.is_anonymous:
sizes = settings.MAIN_SIZES_ANON
else:
sizes = settings.MAIN_SIZES
if request.GET:
self.template_name = "index.html"
# GET arguments in URL like ?query=xyz
query_params = request.GET.dict()
2022-12-02 07:20:37 +00:00
if request.htmx:
if request.resolver_match.url_name == "search_partial":
self.template_name = "partials/results_table.html"
elif request.POST:
query_params = request.POST.dict()
else:
self.template_name = "index.html"
# No query, this is a fresh page load
# Don't try to search, since there's clearly nothing to do
params_with_defaults = {}
add_defaults(params_with_defaults)
context = {
"sizes": sizes,
"params": params_with_defaults,
"unique": "results",
"window_content": self.window_content,
"title": "Results",
}
return render(request, self.template_name, context)
# Merge everything together just in case
tmp_post = request.POST.dict()
tmp_get = request.GET.dict()
tmp_post = {k: v for k, v in tmp_post.items() if v and not v == "None"}
tmp_get = {k: v for k, v in tmp_get.items() if v and not v == "None"}
query_params.update(tmp_post)
query_params.update(tmp_get)
# URI we're passing to the template for linking
if "csrfmiddlewaretoken" in query_params:
del query_params["csrfmiddlewaretoken"]
# Parse the dates
if "dates" in query_params:
dates = parse_dates(query_params["dates"])
del query_params["dates"]
if dates:
if "message" in dates:
return render(request, self.template_name, dates)
query_params["from_date"] = dates["from_date"]
query_params["to_date"] = dates["to_date"]
query_params["from_time"] = dates["from_time"]
query_params["to_time"] = dates["to_time"]
# Remove null values
if "query" in query_params:
if query_params["query"] == "":
del query_params["query"]
# Remove null tags values
if "tags" in query_params:
if query_params["tags"] == "":
del query_params["tags"]
else:
# Parse the tags and populate cast to pass to search function
tags = parse_tags(query_params["tags"])
extra_params["tags"] = tags
2022-12-09 07:20:28 +00:00
if "dedup" in query_params:
if query_params["dedup"] == "on":
extra_params["dedup"] = True
else:
extra_params["dedup"] = False
else:
extra_params["dedup"] = False
context = db.query_results(request, query_params, **extra_params)
# Unique is for identifying the widgets.
# We don't want a random one since we only want one results pane.
context["unique"] = "results"
context["window_content"] = self.window_content
context["title"] = "Results"
# Valid sizes
context["sizes"] = sizes
# Add any default parameters to the context
params_with_defaults = dict(query_params)
add_defaults(params_with_defaults)
context["params"] = params_with_defaults
# Remove anything that we or the user set to a default for
# pretty URLs
remove_defaults(query_params)
url_params = urllib.parse.urlencode(query_params)
context["client_uri"] = url_params
# There's an error
if "message" in context:
response = render(request, self.template_name, context)
# Still push the URL so they can share it to get assistance
if request.GET:
if request.htmx:
response["HX-Push"] = reverse("home") + "?" + url_params
elif request.POST:
response["HX-Push"] = reverse("home") + "?" + url_params
return response
# Create data for chart.js sentiment graph
graph = make_graph(context["object_list"])
context["data"] = graph
# Create the table
context = make_table(context)
# URI we're passing to the template for linking, table fields removed
table_fields = ["page", "sort"]
clean_params = {k: v for k, v in query_params.items() if k not in table_fields}
clean_url_params = urllib.parse.urlencode(clean_params)
context["uri"] = clean_url_params
# unique = str(uuid.uuid4())[:8]
# self.context = context
return context
def get(self, request, *args, **kwargs):
self.context = self.common_request(request)
if isinstance(self.context, HttpResponse):
return self.context
self.object_list = self.context["object_list"]
show = []
show = set().union(*(d.keys() for d in self.object_list))
allow_empty = self.get_allow_empty()
if not allow_empty:
# When pagination is enabled and object_list is a queryset,
# it's better to do a cheap query than to load the unpaginated
# queryset in memory.
if self.get_paginate_by(self.object_list) is not None and hasattr(
self.object_list, "exists"
):
is_empty = not self.object_list.exists() # noqa
else:
is_empty = not self.object_list # noqa
context = self.get_context_data()
2022-08-10 19:36:55 +00:00
for k, v in self.context.items():
if k not in context:
context[k] = v
context["show"] = show
# if request.htmx:
# self.template_name = self.window_content
# if request.method == "GET":
# if not request.htmx:
# self.template_name = "ui/drilldown/drilldown.html"
response = self.render_to_response(context)
# if not request.method == "GET":
if "client_uri" in context:
response["HX-Push"] = reverse("home") + "?" + context["client_uri"]
return response
def post(self, request, *args, **kwargs):
return self.get(request, *args, **kwargs)
# class Drilldown(View):
# template_name = "ui/drilldown/drilldown.html"
# plan_name = "drilldown"
# def get(self, request):
# return drilldown_search(request)
2022-07-21 12:51:55 +00:00
# def post(self, request):
# return drilldown_search(request)
2022-07-21 12:51:55 +00:00
2022-08-12 08:08:44 +00:00
class DrilldownContextModal(APIView):
parser_classes = [FormParser]
plan_name = "drilldown"
template_name = "modals/context.html"
def post(self, request):
if request.resolver_match.url_name == "modal_context_table":
self.template_name = "partials/context_table.html"
2022-08-09 06:20:30 +00:00
size = 15
2022-08-16 18:43:55 +00:00
nicks_sensitive = None
2022-08-15 18:39:26 +00:00
query = False
2022-08-09 06:20:30 +00:00
# Create the query params from the POST arguments
2022-08-30 09:30:17 +00:00
mandatory = [
"net",
"channel",
"num",
"source",
"index",
"nick",
"type",
"mtype",
]
2022-08-09 06:20:30 +00:00
invalid = [None, False, "", "None"]
2022-08-09 06:20:30 +00:00
query_params = {k: v for k, v in request.data.items() if v}
for key in query_params:
if query_params[key] in invalid:
query_params[key] = None
for key in mandatory:
if key not in query_params:
query_params[key] = None
2022-08-18 06:20:30 +00:00
# Lookup the hash values but don't disclose them to the user
2022-08-26 06:20:30 +00:00
# if settings.HASHING:
# if query_params["source"] not in settings.SAFE_SOURCES:
# SAFE_PARAMS = deepcopy(query_params)
# hash_lookup(request.user, SAFE_PARAMS)
# else:
# SAFE_PARAMS = deepcopy(query_params)
# else:
# SAFE_PARAMS = query_params
2022-08-18 06:20:30 +00:00
type = None
2022-08-03 06:20:30 +00:00
if request.user.is_superuser:
2022-08-16 18:43:55 +00:00
if "type" in query_params:
type = query_params["type"]
2022-08-15 18:39:26 +00:00
if type == "znc":
2022-08-16 18:43:55 +00:00
query_params["channel"] = "*status"
2022-08-26 06:20:30 +00:00
# SAFE_PARAMS["channel"] = "*status"
2022-08-15 18:39:26 +00:00
if type in ["query", "notice"]:
2022-08-16 18:43:55 +00:00
nicks_sensitive = [
2022-08-26 06:20:30 +00:00
query_params["channel"],
query_params["nick"],
2022-08-16 18:43:55 +00:00
] # UNSAFE
# nicks = [query_params["channel"], query_params["nick"]]
2022-08-15 18:39:26 +00:00
query = True
if (
2022-09-05 06:20:30 +00:00
query_params["index"] == "internal"
2022-08-16 18:43:55 +00:00
and query_params["mtype"] == "msg"
2022-08-15 18:39:26 +00:00
and not type == "query"
):
2022-08-16 18:43:55 +00:00
query_params["index"] = "main"
2022-08-26 06:20:30 +00:00
# SAFE_PARAMS["index"] = "main"
2022-08-15 18:39:26 +00:00
2022-08-16 18:43:55 +00:00
if query_params["type"] in ["znc", "auth"]:
2022-08-15 18:39:26 +00:00
query = True
if not request.user.is_superuser:
2022-08-16 18:43:55 +00:00
query_params["index"] = "main"
2022-08-26 06:20:30 +00:00
# SAFE_PARAMS["index"] = "main"
2022-08-15 18:39:26 +00:00
2022-08-16 18:43:55 +00:00
query_params["sorting"] = "desc"
2022-08-26 06:20:30 +00:00
# SAFE_PARAMS["sorting"] = "desc"
2022-09-12 07:27:30 +00:00
# query_params["size"] = size
2022-08-15 16:59:09 +00:00
2022-08-18 06:20:30 +00:00
annotate = False
2022-08-30 09:30:17 +00:00
if query_params["source"] == "irc":
2022-08-16 18:43:55 +00:00
if query_params["type"] not in ["znc", "auth"]:
2022-08-18 06:20:30 +00:00
annotate = True
2022-08-09 06:20:30 +00:00
# Create the query with the context helper
2022-09-30 06:22:22 +00:00
if "num" in query_params:
if query_params["num"]:
if query_params["num"].isdigit():
query_params["num"] = int(query_params["num"])
else:
return {"message": "Invalid num value", "class": "danger"}
search_query = db.construct_context_query(
2022-08-16 18:43:55 +00:00
query_params["index"],
2022-08-26 06:20:30 +00:00
query_params["net"],
query_params["channel"],
2022-08-30 09:30:17 +00:00
query_params["source"],
2022-08-26 06:20:30 +00:00
query_params["num"],
2022-08-09 06:20:30 +00:00
size,
2022-08-15 16:59:09 +00:00
type=type,
2022-08-16 18:43:55 +00:00
nicks=nicks_sensitive,
2022-08-09 06:20:30 +00:00
)
2022-09-27 14:15:08 +00:00
results = db.query_results(
request,
2022-08-26 06:20:30 +00:00
query_params,
size=size,
annotate=annotate,
custom_query=search_query,
reverse=True,
dedup_fields=["net", "type", "msg"],
2022-08-09 06:20:30 +00:00
)
if "message" in results:
return render(request, self.template_name, results)
unique = str(uuid.uuid4())[:8]
2022-08-12 08:08:44 +00:00
context = {
2022-08-09 06:20:30 +00:00
"net": query_params["net"],
"channel": query_params["channel"],
2022-08-30 09:30:17 +00:00
"source": query_params["source"],
2022-08-09 06:20:30 +00:00
"ts": f"{query_params['date']} {query_params['time']}",
2022-08-09 06:20:30 +00:00
"object_list": results["object_list"],
2022-08-09 06:20:30 +00:00
"time": query_params["time"],
"date": query_params["date"],
"index": query_params["index"],
"num": query_params["num"],
"type": query_params["type"],
"mtype": query_params["mtype"],
2022-08-03 06:20:30 +00:00
"nick": query_params["nick"],
"params": query_params,
"unique": unique,
2022-08-12 08:08:44 +00:00
}
2022-08-15 18:39:26 +00:00
if request.user.is_superuser:
if query:
context["query"] = True
2022-08-09 06:20:30 +00:00
2022-08-12 08:08:44 +00:00
return render(request, self.template_name, context)
2022-08-02 21:22:22 +00:00
class ThresholdInfoModal(APIView):
2022-07-21 12:51:55 +00:00
parser_classes = [FormParser]
plan_name = "drilldown"
template_name = "modals/drilldown.html"
2022-08-28 19:26:15 +00:00
def post(self, request, type=None):
2022-08-02 16:10:41 +00:00
# if not request.user.has_plan(self.plan_name):
2022-08-02 21:22:22 +00:00
# return JsonResponse({"success": False})
2022-07-21 12:51:55 +00:00
if "net" not in request.data:
return JsonResponse({"success": False})
if "nick" not in request.data:
return JsonResponse({"success": False})
if "channel" not in request.data:
return JsonResponse({"success": False})
2022-08-28 19:26:15 +00:00
if type == "window":
self.template_name = "windows/drilldown.html"
2022-08-29 11:24:06 +00:00
elif type == "widget":
self.template_name = "widgets/drilldown.html"
2022-08-18 06:20:30 +00:00
2022-07-21 12:51:55 +00:00
net = request.data["net"]
nick = request.data["nick"]
channel = request.data["channel"]
2022-08-18 06:20:30 +00:00
2022-08-26 06:20:30 +00:00
channels = get_chans(net, [nick])
2023-02-01 07:20:15 +00:00
users = get_users(net, [channel])
2022-08-26 06:20:30 +00:00
num_users = annotate_num_users(net, channels)
num_chans = annotate_num_chans(net, users)
2022-07-21 12:51:55 +00:00
if channels:
2022-08-26 06:20:30 +00:00
inter_users = get_users(net, channels)
2022-07-21 12:51:55 +00:00
else:
inter_users = []
if users:
2022-08-26 06:20:30 +00:00
inter_chans = get_chans(net, users)
2022-07-21 12:51:55 +00:00
else:
inter_chans = []
2022-08-16 18:43:55 +00:00
unique = str(uuid.uuid4())[:8]
2022-07-21 12:51:55 +00:00
context = {
"net": net,
"nick": nick,
"channel": channel,
"chans": channels,
"users": users,
"inter_chans": inter_chans,
"inter_users": inter_users,
"num_users": num_users,
"num_chans": num_chans,
"unique": unique,
2022-07-21 12:51:55 +00:00
}
return render(request, self.template_name, context)