2022-07-27 19:53:41 +00:00
|
|
|
import json
|
2022-08-10 06:36:26 +00:00
|
|
|
import urllib
|
2022-07-27 19:53:41 +00:00
|
|
|
|
2022-07-27 12:12:16 +00:00
|
|
|
from django.conf import settings
|
2022-08-03 22:54:48 +00:00
|
|
|
from django.http import HttpResponse, JsonResponse
|
2022-07-21 12:51:55 +00:00
|
|
|
from django.shortcuts import render
|
2022-08-10 06:36:26 +00:00
|
|
|
from django.urls import reverse
|
2022-07-21 12:51:55 +00:00
|
|
|
from django.views import View
|
2022-08-09 06:20:30 +00:00
|
|
|
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
|
2022-07-27 12:12:16 +00:00
|
|
|
|
2022-07-27 19:53:41 +00:00
|
|
|
from core.lib.opensearch import query_results
|
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-10 08:27:01 +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-09 06:20:30 +00:00
|
|
|
tags = [f"{tag}: {elem}" for tag, elem in [x.split(":") for x in spl]]
|
|
|
|
return tags
|
2022-08-10 08:27:01 +00:00
|
|
|
|
|
|
|
|
2022-08-09 06:20:30 +00:00
|
|
|
def make_table(context):
|
|
|
|
table = DrilldownTable(context["object_list"])
|
|
|
|
context["table"] = table
|
|
|
|
# del context["results"]
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
def make_graph(results):
|
|
|
|
graph = json.dumps(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"text": item.get("msg", None) or item.get("id"),
|
|
|
|
"nick": item.get("nick", None),
|
|
|
|
"value": item.get("sentiment", None) or None,
|
|
|
|
"date": item.get("ts"),
|
|
|
|
}
|
|
|
|
for item in results
|
|
|
|
]
|
|
|
|
)
|
|
|
|
return graph
|
|
|
|
|
|
|
|
|
|
|
|
def drilldown_search(request, return_context=False, template=None):
|
|
|
|
if not template:
|
|
|
|
template_name = "ui/drilldown/table_results.html"
|
|
|
|
else:
|
|
|
|
template_name = template
|
|
|
|
if request.user.is_anonymous:
|
|
|
|
sizes = settings.OPENSEARCH_MAIN_SIZES_ANON
|
|
|
|
else:
|
|
|
|
sizes = settings.OPENSEARCH_MAIN_SIZES
|
|
|
|
|
2022-08-10 08:27:01 +00:00
|
|
|
if request.GET:
|
2022-08-09 06:20:30 +00:00
|
|
|
if not request.htmx:
|
|
|
|
template_name = "ui/drilldown/drilldown.html"
|
2022-08-10 08:27:01 +00:00
|
|
|
query_params = request.GET.dict()
|
|
|
|
elif request.POST:
|
|
|
|
query_params = request.POST.dict()
|
2022-08-09 06:20:30 +00:00
|
|
|
else:
|
|
|
|
template_name = "ui/drilldown/drilldown.html"
|
|
|
|
context = {"sizes": sizes}
|
|
|
|
return render(request, template_name, context)
|
2022-08-10 08:27:01 +00:00
|
|
|
|
2022-08-11 21:45:02 +00:00
|
|
|
if "index" in query_params:
|
|
|
|
if not request.user.is_superuser:
|
|
|
|
message = "You can't use the index parameter"
|
|
|
|
message_class = "danger"
|
|
|
|
context = {"message": message, "class": message_class}
|
|
|
|
return render(request, template_name, context)
|
|
|
|
|
2022-08-10 08:27:01 +00:00
|
|
|
# Parse the dates
|
|
|
|
if "dates" in query_params:
|
|
|
|
dates = parse_dates(query_params["dates"])
|
|
|
|
del query_params["dates"]
|
2022-08-09 06:20:30 +00:00
|
|
|
if dates:
|
|
|
|
if "message" in dates:
|
|
|
|
return render(request, 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"]
|
2022-08-10 08:27:01 +00:00
|
|
|
|
|
|
|
if request.GET:
|
|
|
|
context = query_results(request, query_params)
|
|
|
|
elif request.POST:
|
|
|
|
context = query_results(request, query_params)
|
|
|
|
|
2022-08-09 06:20:30 +00:00
|
|
|
# URI we're passing to the template for linking
|
|
|
|
if "csrfmiddlewaretoken" in query_params:
|
|
|
|
del query_params["csrfmiddlewaretoken"]
|
|
|
|
url_params = urllib.parse.urlencode(query_params)
|
|
|
|
context["client_uri"] = url_params
|
|
|
|
|
2022-08-10 08:27:01 +00:00
|
|
|
# Turn the query into tags for populating the taglist
|
2022-08-09 06:20:30 +00:00
|
|
|
if "query" in query_params:
|
|
|
|
tags = create_tags(query_params["query"])
|
|
|
|
context["tags"] = tags
|
2022-08-10 08:27:01 +00:00
|
|
|
|
|
|
|
context["params"] = query_params
|
|
|
|
if "message" in context:
|
2022-08-09 06:20:30 +00:00
|
|
|
response = render(request, template_name, context)
|
|
|
|
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
|
2022-08-10 08:27:01 +00:00
|
|
|
|
2022-08-09 06:20:30 +00:00
|
|
|
# Create data for chart.js sentiment graph
|
|
|
|
graph = make_graph(context["object_list"])
|
|
|
|
context["data"] = graph
|
|
|
|
|
2022-08-10 08:27:01 +00:00
|
|
|
if context:
|
2022-08-09 06:20:30 +00:00
|
|
|
context["sizes"] = sizes
|
|
|
|
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
|
|
|
|
|
2022-08-10 08:27:01 +00:00
|
|
|
response = render(request, template_name, context)
|
|
|
|
if request.GET:
|
2022-08-09 06:20:30 +00:00
|
|
|
if request.htmx:
|
|
|
|
response["HX-Push"] = reverse("home") + "?" + url_params
|
2022-08-10 08:27:01 +00:00
|
|
|
elif request.POST:
|
|
|
|
response["HX-Push"] = reverse("home") + "?" + url_params
|
2022-08-09 06:20:30 +00:00
|
|
|
if return_context:
|
|
|
|
return context
|
2022-08-10 08:27:01 +00:00
|
|
|
return response
|
|
|
|
else:
|
|
|
|
return HttpResponse("No results")
|
|
|
|
|
|
|
|
|
2022-08-09 06:20:30 +00:00
|
|
|
class DrilldownTableView(SingleTableView):
|
|
|
|
table_class = DrilldownTable
|
|
|
|
template_name = "ui/drilldown/table_results.html"
|
2022-08-10 19:45:55 +00:00
|
|
|
paginate_by = settings.DRILLDOWN_RESULTS_PER_PAGE
|
2022-08-09 06:20:30 +00:00
|
|
|
|
|
|
|
def get_queryset(self, request, **kwargs):
|
|
|
|
context = drilldown_search(request, return_context=True)
|
|
|
|
# Save the context as we will need to merge other attributes later
|
|
|
|
self.context = context
|
2022-08-10 19:36:55 +00:00
|
|
|
|
2022-08-09 06:20:30 +00:00
|
|
|
if "object_list" in context:
|
|
|
|
return context["object_list"]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.object_list = self.get_queryset(request)
|
2022-08-11 06:22:22 +00:00
|
|
|
show = []
|
2022-08-11 06:22:22 +00:00
|
|
|
show = set().union(*(d.keys() for d in self.object_list))
|
2022-08-09 06:20:30 +00:00
|
|
|
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()
|
|
|
|
if isinstance(self.context, HttpResponse):
|
|
|
|
return self.context
|
2022-08-10 19:36:55 +00:00
|
|
|
|
2022-08-09 06:20:30 +00:00
|
|
|
for k, v in self.context.items():
|
|
|
|
if k not in context:
|
|
|
|
context[k] = v
|
2022-08-11 06:22:22 +00:00
|
|
|
context["show"] = show
|
2022-08-09 06:20:30 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2022-08-02 21:22:22 +00:00
|
|
|
class Drilldown(View):
|
2022-07-27 12:12:16 +00:00
|
|
|
template_name = "ui/drilldown/drilldown.html"
|
|
|
|
plan_name = "drilldown"
|
|
|
|
|
|
|
|
def get(self, request):
|
2022-08-09 06:20:30 +00:00
|
|
|
return drilldown_search(request)
|
2022-07-21 12:51:55 +00:00
|
|
|
|
|
|
|
def post(self, request):
|
2022-08-10 08:27:01 +00:00
|
|
|
return drilldown_search(request)
|
2022-07-21 12:51:55 +00:00
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
def post(self, request):
|
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})
|
|
|
|
net = request.data["net"]
|
|
|
|
nick = request.data["nick"]
|
|
|
|
channel = request.data["channel"]
|
|
|
|
channels = get_chans(net, [nick])
|
|
|
|
users = get_users(net, [channel])
|
|
|
|
num_users = annotate_num_users(net, channels)
|
|
|
|
num_chans = annotate_num_chans(net, users)
|
|
|
|
if channels:
|
|
|
|
inter_users = get_users(net, channels)
|
|
|
|
else:
|
|
|
|
inter_users = []
|
|
|
|
if users:
|
|
|
|
inter_chans = get_chans(net, users)
|
|
|
|
else:
|
|
|
|
inter_chans = []
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
return render(request, self.template_name, context)
|