2022-07-21 12:49:01 +00:00
|
|
|
import pprint
|
|
|
|
|
|
|
|
from django.conf import settings
|
2022-07-21 12:46:34 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.shortcuts import render
|
2022-07-21 12:46:48 +00:00
|
|
|
from django.views import View
|
|
|
|
|
|
|
|
from core.lib.opensearch import initialise_opensearch, run_main_query
|
|
|
|
|
|
|
|
client = initialise_opensearch()
|
2022-07-21 12:49:01 +00:00
|
|
|
pp = pprint.PrettyPrinter(indent=4)
|
2022-07-21 12:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Drilldown(LoginRequiredMixin, View):
|
|
|
|
template_name = "ui/drilldown.html"
|
2022-07-21 12:47:16 +00:00
|
|
|
plan_name = "drilldown"
|
2022-07-21 12:46:34 +00:00
|
|
|
|
2022-07-21 12:46:48 +00:00
|
|
|
def get(self, request):
|
2022-07-21 12:47:16 +00:00
|
|
|
if not request.user.has_plan(self.plan_name):
|
2022-07-21 12:46:48 +00:00
|
|
|
return render(request, "denied.html")
|
2022-07-21 12:49:32 +00:00
|
|
|
context = {
|
|
|
|
"fields": settings.OPENSEARCH_MAIN_SEARCH_FIELDS,
|
|
|
|
"sizes": settings.OPENSEARCH_MAIN_SIZES,
|
|
|
|
"timescales": settings.OPENSEARCH_MAIN_TIMESCALES,
|
|
|
|
}
|
2022-07-21 12:49:11 +00:00
|
|
|
return render(request, self.template_name, context)
|
2022-07-21 12:46:48 +00:00
|
|
|
|
|
|
|
def post(self, request):
|
2022-07-21 12:47:16 +00:00
|
|
|
if not request.user.has_plan(self.plan_name):
|
2022-07-21 12:46:34 +00:00
|
|
|
return render(request, "denied.html")
|
2022-07-21 12:49:01 +00:00
|
|
|
fields = None
|
|
|
|
if "fields" in request.POST:
|
2022-07-21 12:49:11 +00:00
|
|
|
fields = request.POST.getlist("fields")
|
2022-07-21 12:49:32 +00:00
|
|
|
if "size" in request.POST:
|
|
|
|
size = request.POST["size"]
|
2022-07-21 12:46:48 +00:00
|
|
|
if "query" in request.POST:
|
|
|
|
query = request.POST["query"]
|
2022-07-21 12:49:32 +00:00
|
|
|
results = run_main_query(client, request.user, query, fields, size)
|
|
|
|
if not results:
|
|
|
|
return render(request, "denied.html")
|
2022-07-21 12:49:27 +00:00
|
|
|
# pp.pprint(results)
|
2022-07-21 12:47:16 +00:00
|
|
|
results_parsed = []
|
|
|
|
if "hits" in results.keys():
|
|
|
|
if "hits" in results["hits"]:
|
|
|
|
for item in results["hits"]["hits"]:
|
|
|
|
results_parsed.append(item["_source"])
|
2022-07-21 12:49:01 +00:00
|
|
|
context = {
|
|
|
|
"query": query,
|
|
|
|
"results": results_parsed,
|
|
|
|
"card": results["hits"]["total"]["value"],
|
2022-07-21 12:49:17 +00:00
|
|
|
"took": results["took"],
|
2022-07-21 12:49:27 +00:00
|
|
|
"redacted": results["redacted"],
|
2022-07-21 12:49:32 +00:00
|
|
|
"exemption": results["exemption"],
|
2022-07-21 12:49:01 +00:00
|
|
|
"fields": settings.OPENSEARCH_MAIN_SEARCH_FIELDS,
|
2022-07-21 12:49:32 +00:00
|
|
|
"sizes": settings.OPENSEARCH_MAIN_SIZES,
|
|
|
|
"timescales": settings.OPENSEARCH_MAIN_TIMESCALES,
|
2022-07-21 12:49:01 +00:00
|
|
|
}
|
2022-07-21 12:46:48 +00:00
|
|
|
return render(request, self.template_name, context)
|
|
|
|
return render(request, self.template_name)
|