neptune/core/views/dynamic/search.py

120 lines
4.2 KiB
Python
Raw Normal View History

2022-07-21 12:50:56 +00:00
import json
2022-07-21 12:50:51 +00:00
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
2022-07-21 12:51:27 +00:00
from django.http import HttpResponse, HttpResponseForbidden, JsonResponse
2022-07-21 12:50:56 +00:00
from django.shortcuts import render
2022-07-21 12:50:51 +00:00
from django.views import View
2022-07-21 12:50:56 +00:00
2022-07-21 12:50:51 +00:00
from core.lib.opensearch import initialise_opensearch, run_main_query
from core.lib.threshold import annotate_num_chans, annotate_num_users, annotate_online
2022-07-21 12:50:56 +00:00
2022-07-21 12:50:51 +00:00
client = initialise_opensearch()
def query_results(request, post_params, api=False):
fields = None
if "fields" in request.POST:
fields = request.POST.getlist("fields")
if "size" in request.POST:
size = request.POST["size"]
if "query" in request.POST:
query = request.POST["query"]
results = run_main_query(client, request.user, query, fields, size)
if not results:
return False
results_parsed = []
if "hits" in results.keys():
if "hits" in results["hits"]:
for item in results["hits"]["hits"]:
2022-07-21 12:51:12 +00:00
element = item["_source"]
element["id"] = item["_id"]
ts = element["ts"]
ts_spl = ts.split("T")
date = ts_spl[0]
time = ts_spl[1]
element["date"] = date
element["time"] = time
results_parsed.append(element)
# Figure out items with net (not discord)
nets = set()
for x in results_parsed:
if "net" in x:
nets.add(x["net"])
for net in nets:
# Annotate the online attribute from Threshold
2022-07-21 12:51:12 +00:00
online_info = annotate_online(
net, [x["nick"] for x in results_parsed if x["src"] == "irc"]
)
# Annotate the number of users in the channel
num_users = annotate_num_users(
net, [x["channel"] for x in results_parsed if x["src"] == "irc"]
)
# Annotate the number channels the user is on
num_chans = annotate_num_chans(
net, [x["nick"] for x in results_parsed if x["src"] == "irc"]
)
2022-07-21 12:51:12 +00:00
for item in results_parsed:
if item["nick"] in online_info:
item["online"] = online_info[item["nick"]]
if item["channel"] in num_users:
item["num_users"] = num_users[item["channel"]]
if item["nick"] in num_chans:
item["num_chans"] = num_chans[item["nick"]]
2022-07-21 12:51:12 +00:00
2022-07-21 12:50:51 +00:00
context = {
"query": query,
"results": results_parsed,
"card": results["hits"]["total"]["value"],
"took": results["took"],
"redacted": results["redacted"],
"exemption": results["exemption"],
"fields": settings.OPENSEARCH_MAIN_SEARCH_FIELDS,
"sizes": settings.OPENSEARCH_MAIN_SIZES,
"timescales": settings.OPENSEARCH_MAIN_TIMESCALES,
}
return context
2022-07-21 12:50:56 +00:00
2022-07-21 12:50:51 +00:00
class Search(LoginRequiredMixin, View):
2022-07-21 12:50:56 +00:00
# parser_classes = [JSONParser]
2022-07-21 12:50:51 +00:00
template_name = "ui/results.html"
plan_name = "drilldown"
def post(self, request):
if not request.user.has_plan(self.plan_name):
2022-07-21 12:51:27 +00:00
return HttpResponseForbidden()
2022-07-21 12:50:56 +00:00
2022-07-21 12:50:51 +00:00
context = query_results(request, request.POST)
2022-07-21 12:51:27 +00:00
if not context:
return HttpResponseForbidden()
2022-07-21 12:50:56 +00:00
context["data"] = json.dumps(
2022-07-21 12:50:51 +00:00
[
{
2022-07-21 12:51:01 +00:00
"text": item.get("msg", None) or item.get("id"),
"nick": item.get("nick", None),
"value": item.get("sentiment", None) or None,
2022-07-21 12:50:56 +00:00
"date": item.get("ts"),
2022-07-21 12:50:51 +00:00
}
for item in context["results"]
]
)
if context:
return render(request, self.template_name, context)
else:
return HttpResponse("No results")
2022-07-21 12:50:56 +00:00
2022-07-21 12:50:51 +00:00
class APISearch(LoginRequiredMixin, View):
2022-07-21 12:50:56 +00:00
# parser_classes = [JSONParser]
2022-07-21 12:50:51 +00:00
template_name = "ui/results.html"
plan_name = "drilldown"
def post(self, request):
if not request.user.has_plan(self.plan_name):
return JsonResponse({"success": False})
2022-07-21 12:50:56 +00:00
2022-07-21 12:50:51 +00:00
context = query_results(request, request.POST)
return JsonResponse(context)