neptune/core/api/views/threshold.py

118 lines
3.8 KiB
Python
Raw Normal View History

2022-07-21 12:51:12 +00:00
import logging
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from rest_framework.parsers import FormParser
from rest_framework.views import APIView
from core.lib.threshold import (
annotate_num_chans,
annotate_num_users,
annotate_online,
get_chans,
get_users,
)
2022-07-21 12:51:12 +00:00
logger = logging.getLogger(__name__)
class ThresholdChans(LoginRequiredMixin, APIView):
parser_classes = [FormParser]
plan_name = "drilldown"
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 "query" not in request.data:
return JsonResponse({"success": False})
net = request.data["net"]
query = request.data["query"]
channels = get_chans(net, [query])
if not channels:
return HttpResponse("")
channels_human = ", ".join(channels)
return HttpResponse(channels_human)
class ThresholdUsers(LoginRequiredMixin, APIView):
parser_classes = [FormParser]
plan_name = "drilldown"
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 "query" not in request.data:
return JsonResponse({"success": False})
net = request.data["net"]
query = request.data["query"]
users = get_users(net, [query])
if not users:
return HttpResponse("")
users_human = ", ".join(users)
return HttpResponse(users_human)
class ThresholdOnline(LoginRequiredMixin, APIView):
parser_classes = [FormParser]
plan_name = "drilldown"
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 "query" not in request.data:
return JsonResponse({"success": False})
net = request.data["net"]
query = request.data["query"]
online_info = annotate_online(net, query)
return JsonResponse(online_info)
class ThresholdInfoModal(LoginRequiredMixin, APIView):
parser_classes = [FormParser]
plan_name = "drilldown"
template_name = "modals/info.html"
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)
2022-07-21 12:51:12 +00:00
if channels:
inter_users = get_users(net, channels)
else:
inter_users = []
if users:
inter_chans = get_chans(net, users)
else:
inter_chans = []
context = {
2022-07-21 12:51:22 +00:00
"net": net,
2022-07-21 12:51:12 +00:00
"nick": nick,
"channel": channel,
"chans": channels,
"users": users,
"inter_chans": inter_chans,
"inter_users": inter_users,
"num_users": num_users,
"num_chans": num_chans,
2022-07-21 12:51:12 +00:00
}
return render(request, self.template_name, context)