NDOJ/judge/views/internal.py

171 lines
5.6 KiB
Python
Raw Normal View History

2023-03-07 07:19:45 +00:00
import logging
import json
2022-05-03 02:44:14 +00:00
from django.views.generic import ListView
from django.utils.translation import gettext as _, gettext_lazy
2023-08-29 23:52:24 +00:00
from django.db.models import Count, Q
2022-05-03 02:44:14 +00:00
from django.http import HttpResponseForbidden
2023-03-07 07:19:45 +00:00
from django.urls import reverse
2023-11-02 02:17:54 +00:00
from django.shortcuts import render
2022-05-03 02:44:14 +00:00
from judge.utils.diggpaginator import DiggPaginator
from judge.models import VolunteerProblemVote, Problem
2022-05-14 17:57:27 +00:00
2023-03-07 07:19:45 +00:00
class InternalView(object):
def get(self, request, *args, **kwargs):
if request.user.is_superuser:
return super(InternalView, self).get(request, *args, **kwargs)
return HttpResponseForbidden()
2023-03-08 00:30:47 +00:00
class InternalProblem(InternalView, ListView):
2022-05-03 02:44:14 +00:00
model = Problem
2022-05-14 17:57:27 +00:00
title = _("Internal problems")
2023-11-02 02:17:54 +00:00
template_name = "internal/problem/problem.html"
2022-05-03 02:44:14 +00:00
paginate_by = 100
2022-05-14 17:57:27 +00:00
context_object_name = "problems"
def get_paginator(
self, queryset, per_page, orphans=0, allow_empty_first_page=True, **kwargs
):
return DiggPaginator(
queryset,
per_page,
body=6,
padding=2,
orphans=orphans,
allow_empty_first_page=allow_empty_first_page,
**kwargs
)
2022-05-03 02:44:14 +00:00
2023-08-29 23:52:24 +00:00
def get_search_query(self):
return self.request.GET.get("q") or self.request.POST.get("q")
2022-05-03 02:44:14 +00:00
def get_queryset(self):
2023-08-29 23:52:24 +00:00
queryset = Problem.objects.annotate(
vote_count=Count("volunteer_user_votes")
).filter(vote_count__gte=1)
query = self.get_search_query()
if query:
queryset = queryset.filter(
Q(code__icontains=query) | Q(name__icontains=query)
)
return queryset.order_by("-vote_count")
2022-05-03 02:44:14 +00:00
def get_context_data(self, **kwargs):
context = super(InternalProblem, self).get_context_data(**kwargs)
2022-05-14 17:57:27 +00:00
context["page_type"] = "problem"
context["title"] = self.title
2022-07-27 21:22:01 +00:00
context["page_prefix"] = self.request.path + "?page="
context["first_page_href"] = self.request.path
2023-08-29 23:52:24 +00:00
context["query"] = self.get_search_query()
2022-07-29 08:53:35 +00:00
2022-05-03 02:44:14 +00:00
return context
2023-03-07 07:19:45 +00:00
2023-11-02 02:17:54 +00:00
def get_problem_votes(request):
if not request.user.is_superuser:
return HttpResponseForbidden()
try:
problem = Problem.objects.get(id=request.GET.get("id"))
except:
return HttpResponseForbidden()
votes = (
problem.volunteer_user_votes.select_related("voter")
.prefetch_related("types")
.order_by("id")
)
return render(
request,
"internal/problem/votes.html",
{
"problem": problem,
"votes": votes,
},
)
2023-03-07 07:19:45 +00:00
class RequestTimeMixin(object):
def get_requests_data(self):
logger = logging.getLogger(self.log_name)
log_filename = logger.handlers[0].baseFilename
requests = []
with open(log_filename, "r") as f:
for line in f:
try:
info = json.loads(line)
requests.append(info)
except:
continue
return requests
2023-03-08 00:30:47 +00:00
class InternalRequestTime(InternalView, ListView, RequestTimeMixin):
2023-03-07 07:19:45 +00:00
title = _("Request times")
template_name = "internal/request_time.html"
context_object_name = "pages"
log_name = "judge.request_time"
detail_url_name = "internal_request_time_detail"
page_type = "request_time"
def get_queryset(self):
requests = self.get_requests_data()
table = {}
for r in requests:
url_name = r["url_name"]
if url_name not in table:
table[url_name] = {
"time": 0,
"count": 0,
"url_name": url_name,
}
old_sum = table[url_name]["time"] * table[url_name]["count"]
table[url_name]["count"] += 1
table[url_name]["time"] = (old_sum + float(r["response_time"])) / table[
url_name
]["count"]
order = self.request.GET.get("order", "time")
return sorted(table.values(), key=lambda x: x[order], reverse=True)
def get_context_data(self, **kwargs):
context = super(InternalRequestTime, self).get_context_data(**kwargs)
context["page_type"] = self.page_type
context["title"] = self.title
context["current_path"] = self.request.path
context["detail_path"] = reverse(self.detail_url_name)
return context
class InternalRequestTimeDetail(InternalRequestTime):
template_name = "internal/request_time_detail.html"
context_object_name = "requests"
def get_queryset(self):
url_name = self.request.GET.get("url_name", None)
if not url_name:
return HttpResponseForbidden()
if url_name == "None":
url_name = None
self.title = url_name
requests = self.get_requests_data()
filtered_requests = [r for r in requests if r["url_name"] == url_name]
order = self.request.GET.get("order", "response_time")
return sorted(filtered_requests, key=lambda x: x[order], reverse=True)[:200]
def get_context_data(self, **kwargs):
context = super(InternalRequestTimeDetail, self).get_context_data(**kwargs)
context["url_name"] = self.request.GET.get("url_name", None)
return context
class InternalSlowRequest(InternalRequestTime):
log_name = "judge.slow_request"
detail_url_name = "internal_slow_request_detail"
page_type = "slow_request"
class InternalSlowRequestDetail(InternalRequestTimeDetail):
log_name = "judge.slow_request"