2022-07-27 19:53:41 +00:00
|
|
|
from ast import literal_eval
|
|
|
|
|
2022-07-21 12:51:55 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2022-07-27 19:53:41 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseForbidden, JsonResponse
|
2022-07-21 12:51:55 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.views import View
|
|
|
|
from rest_framework.parsers import FormParser
|
2022-07-27 12:12:16 +00:00
|
|
|
from rest_framework.views import APIView
|
2022-07-27 19:53:41 +00:00
|
|
|
|
2022-07-21 12:52:41 +00:00
|
|
|
from core.lib.meta import get_meta
|
2022-07-21 12:52:10 +00:00
|
|
|
from core.lib.nicktrace import get_nicks
|
2022-07-21 12:51:55 +00:00
|
|
|
from core.lib.opensearch import query_single_result
|
|
|
|
from core.lib.threshold import (
|
|
|
|
annotate_num_chans,
|
|
|
|
annotate_num_users,
|
2022-07-21 12:52:31 +00:00
|
|
|
annotate_online,
|
2022-07-21 12:51:55 +00:00
|
|
|
get_chans,
|
|
|
|
get_users,
|
|
|
|
)
|
2022-07-27 12:12:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Insights(LoginRequiredMixin, View):
|
|
|
|
template_name = "ui/insights/insights.html"
|
|
|
|
plan_name = "drilldown"
|
2022-07-21 12:51:55 +00:00
|
|
|
|
2022-07-27 12:12:16 +00:00
|
|
|
def get(self, request):
|
|
|
|
if not request.user.has_plan(self.plan_name):
|
|
|
|
return render(request, "denied.html")
|
|
|
|
return render(request, self.template_name)
|
2022-07-21 12:51:55 +00:00
|
|
|
|
2022-07-27 19:53:41 +00:00
|
|
|
|
2022-07-21 12:51:55 +00:00
|
|
|
class InsightsSearch(LoginRequiredMixin, View):
|
|
|
|
# parser_classes = [JSONParser]
|
2022-07-21 12:52:10 +00:00
|
|
|
template_name = "ui/insights/info.html"
|
2022-07-21 12:51:55 +00:00
|
|
|
plan_name = "drilldown"
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
if not request.user.has_plan(self.plan_name):
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
results, context = query_single_result(request)
|
|
|
|
if not context:
|
|
|
|
return HttpResponseForbidden()
|
2022-07-21 12:52:20 +00:00
|
|
|
return render(request, self.template_name, context)
|
2022-07-21 12:51:55 +00:00
|
|
|
|
|
|
|
|
2022-07-21 12:52:06 +00:00
|
|
|
class InsightsChannels(LoginRequiredMixin, APIView):
|
|
|
|
parser_classes = [FormParser]
|
|
|
|
template_name = "ui/insights/channels.html"
|
|
|
|
plan_name = "drilldown"
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
if not request.user.has_plan(self.plan_name):
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
if "net" not in request.data:
|
|
|
|
return HttpResponse("No net")
|
|
|
|
if "nick" not in request.data:
|
|
|
|
return HttpResponse("No nick")
|
|
|
|
net = request.data["net"]
|
|
|
|
nick = request.data["nick"]
|
|
|
|
chans = get_chans(net, [nick])
|
2022-07-21 12:52:27 +00:00
|
|
|
num_users = annotate_num_users(net, chans)
|
2022-07-21 12:52:06 +00:00
|
|
|
if not chans:
|
|
|
|
return HttpResponseForbidden()
|
2022-07-21 12:52:27 +00:00
|
|
|
context = {"net": net, "nick": nick, "chans": chans, "num_users": num_users}
|
2022-07-21 12:52:20 +00:00
|
|
|
return render(request, self.template_name, context)
|
2022-07-21 12:52:06 +00:00
|
|
|
|
|
|
|
|
2022-07-21 12:52:10 +00:00
|
|
|
class InsightsNicks(LoginRequiredMixin, APIView):
|
|
|
|
parser_classes = [FormParser]
|
|
|
|
template_name = "ui/insights/nicks.html"
|
|
|
|
plan_name = "drilldown"
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
if not request.user.has_plan(self.plan_name):
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
if "net" not in request.data:
|
|
|
|
return HttpResponse("No net")
|
|
|
|
if "nick" not in request.data:
|
|
|
|
return HttpResponse("No nick")
|
|
|
|
net = request.data["net"]
|
|
|
|
nick = request.data["nick"]
|
2022-08-03 22:26:22 +00:00
|
|
|
if not nick:
|
|
|
|
return HttpResponseForbidden()
|
2022-07-21 12:52:34 +00:00
|
|
|
nicks = get_nicks(request, net, [nick])
|
|
|
|
# Filter Guest
|
|
|
|
nicks = [x for x in nicks if not x.startswith("Guest")]
|
2022-07-21 12:52:31 +00:00
|
|
|
online = annotate_online(net, nicks)
|
2022-07-21 12:52:20 +00:00
|
|
|
if not nicks:
|
|
|
|
return HttpResponseForbidden()
|
2022-07-21 12:52:31 +00:00
|
|
|
context = {"net": net, "nick": nick, "nicks": nicks, "online": online}
|
2022-07-21 12:52:10 +00:00
|
|
|
return render(request, self.template_name, context)
|
|
|
|
|
|
|
|
|
|
|
|
class InsightsMeta(LoginRequiredMixin, APIView):
|
|
|
|
parser_classes = [FormParser]
|
|
|
|
template_name = "ui/insights/meta.html"
|
|
|
|
plan_name = "drilldown"
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
if not request.user.has_plan(self.plan_name):
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
if "net" not in request.data:
|
|
|
|
return HttpResponse("No net")
|
2022-07-21 12:52:48 +00:00
|
|
|
if "nicks" not in request.data:
|
|
|
|
return HttpResponse("No nicks")
|
2022-07-21 12:52:10 +00:00
|
|
|
net = request.data["net"]
|
2022-07-21 12:52:48 +00:00
|
|
|
nicks = request.data["nicks"]
|
|
|
|
nicks = literal_eval(nicks)
|
|
|
|
meta = get_meta(request, net, nicks)
|
2022-07-21 12:52:41 +00:00
|
|
|
unique_values = {}
|
2022-07-21 12:52:48 +00:00
|
|
|
# Create a map of unique values for each key for each nick
|
2022-07-21 12:52:41 +00:00
|
|
|
for x in meta:
|
2022-07-21 12:52:48 +00:00
|
|
|
nick = x["nick"]
|
|
|
|
if nick not in unique_values:
|
|
|
|
unique_values[nick] = {}
|
2022-07-21 12:52:41 +00:00
|
|
|
for k, v in x.items():
|
2022-07-21 12:52:48 +00:00
|
|
|
if k not in unique_values[nick]:
|
|
|
|
unique_values[nick][k] = set()
|
|
|
|
if k in ["ts", "time", "date", "id"]:
|
|
|
|
continue
|
|
|
|
unique_values[nick][k].add(v)
|
2022-07-21 12:53:02 +00:00
|
|
|
meta_dedup = {}
|
2022-07-21 12:52:41 +00:00
|
|
|
for x in meta:
|
2022-07-21 12:52:48 +00:00
|
|
|
nick = x["nick"]
|
2022-07-21 12:52:41 +00:00
|
|
|
for k, v in x.items():
|
2022-07-21 12:52:48 +00:00
|
|
|
if v in unique_values[nick][k]:
|
2022-07-21 12:53:02 +00:00
|
|
|
if k not in meta_dedup:
|
|
|
|
meta_dedup[k] = set()
|
|
|
|
meta_dedup[k].add(v)
|
2022-07-21 12:52:48 +00:00
|
|
|
unique_values[nick][k].remove(v)
|
2022-07-21 12:52:41 +00:00
|
|
|
|
2022-07-21 12:52:48 +00:00
|
|
|
context = {"net": net, "nicks": nicks, "meta": meta_dedup}
|
2022-07-21 12:52:10 +00:00
|
|
|
return render(request, self.template_name, context)
|
|
|
|
|
|
|
|
|
2022-07-21 12:51:55 +00:00
|
|
|
class InsightsInfoModal(LoginRequiredMixin, APIView):
|
|
|
|
parser_classes = [FormParser]
|
|
|
|
plan_name = "drilldown"
|
2022-07-21 12:52:06 +00:00
|
|
|
template_name = "modals/drilldown.html"
|
2022-07-21 12:51:55 +00:00
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
if not request.user.has_plan(self.plan_name):
|
|
|
|
return JsonResponse({"success": False})
|
|
|
|
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)
|