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 08:04:31 +00:00
|
|
|
from django_tables2 import SingleTableMixin
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class DrilldownTableView(View, SingleTableMixin):
|
|
|
|
table_class = DrilldownTable
|
|
|
|
template_name = "ui/drilldown/table_results.html"
|
|
|
|
paginate_by = 5
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
context = query_results(request)
|
|
|
|
table = DrilldownTable(context["results"])
|
|
|
|
context["table"] = table
|
|
|
|
del context["results"]
|
|
|
|
if "message" in context:
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
|
|
|
|
if self.request.htmx:
|
|
|
|
print("IS HTMX")
|
|
|
|
template_name = "ui/drilldown/table_results.html"
|
|
|
|
else:
|
|
|
|
print("IS NOT HTMX")
|
|
|
|
template_name = "ui/drilldown/table_results_partial.html"
|
|
|
|
|
|
|
|
if context:
|
2022-08-09 10:54:44 +00:00
|
|
|
return render(request, template_name, context)
|
2022-08-09 08:04:31 +00:00
|
|
|
else:
|
|
|
|
return HttpResponse("No results")
|
2022-07-21 12:51:55 +00:00
|
|
|
|
2022-07-27 19:53:41 +00:00
|
|
|
|
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-03 20:56:27 +00:00
|
|
|
if request.user.is_anonymous:
|
|
|
|
sizes = settings.OPENSEARCH_MAIN_SIZES_ANON
|
2022-08-02 21:22:22 +00:00
|
|
|
else:
|
|
|
|
sizes = settings.OPENSEARCH_MAIN_SIZES
|
2022-07-27 12:12:16 +00:00
|
|
|
context = {
|
2022-08-02 21:22:22 +00:00
|
|
|
"sizes": sizes,
|
2022-07-27 12:12:16 +00:00
|
|
|
}
|
|
|
|
return render(request, self.template_name, context)
|
2022-07-21 12:51:55 +00:00
|
|
|
|
|
|
|
def post(self, request):
|
2022-08-10 06:36:26 +00:00
|
|
|
template_name = "ui/drilldown/results.html"
|
|
|
|
data_args = request.POST.dict()
|
|
|
|
del data_args["csrfmiddlewaretoken"]
|
|
|
|
print("rep", repr(data_args["dates"]))
|
|
|
|
if data_args["dates"] == " - ":
|
|
|
|
del data_args["dates"]
|
|
|
|
url_params = urllib.parse.urlencode(data_args)
|
|
|
|
print("url_params", url_params)
|
2022-07-21 12:51:55 +00:00
|
|
|
context = query_results(request)
|
2022-08-03 22:26:22 +00:00
|
|
|
if "message" in context:
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
|
2022-07-21 12:51:55 +00:00
|
|
|
context["data"] = 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 context["results"]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
if context:
|
2022-08-10 06:36:26 +00:00
|
|
|
response = render(request, template_name, context)
|
|
|
|
response["HX-Push"] = reverse("home") + "?" + url_params
|
|
|
|
return response
|
2022-07-21 12:51:55 +00:00
|
|
|
else:
|
|
|
|
return HttpResponse("No results")
|
|
|
|
|
|
|
|
|
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)
|