From 94be6d997f02ea172a280ea667df546965ca977b Mon Sep 17 00:00:00 2001 From: yuetsh <517252939@qq.com> Date: Thu, 6 Aug 2026 06:15:48 -0600 Subject: [PATCH] =?UTF-8?q?refactor(reaction):=20=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E5=8E=BB=E6=8E=89=E6=8E=92=E5=BA=8F=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=80=E8=BF=91=E8=AF=84=E4=BB=B7=E6=97=B6?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5 --- reaction/views/admin.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/reaction/views/admin.py b/reaction/views/admin.py index b835463..ac90513 100644 --- a/reaction/views/admin.py +++ b/reaction/views/admin.py @@ -1,13 +1,9 @@ -from django.db.models import Count, F, FloatField, Q -from django.db.models.functions import Cast +from django.db.models import Count, F, Max, Q from account.decorators import super_admin_required from reaction.models import Reaction, ReactionType from utils.api import APIView -MIN_USERS_FOR_RATIO = 3 -DEFAULT_ORDERING = "-users" - class ReactionStatsAPI(APIView): @super_admin_required @@ -15,27 +11,15 @@ class ReactionStatsAPI(APIView): queryset = ( Reaction.objects.values(pid=F("problem___id"), title=F("problem__title")) .annotate(users=Count("user", distinct=True)) + .annotate(last_time=Max("create_time")) .annotate(**{t.value: Count("id", filter=Q(type=t.value)) for t in ReactionType}) - .annotate(**{f"{t.value}_ratio": Cast(t.value, FloatField()) / Cast("users", FloatField()) for t in ReactionType}) + .order_by("-users") ) problem_id = request.GET.get("problem") if problem_id: queryset = queryset.filter(problem___id__iexact=problem_id, problem__contest_id__isnull=True) - ordering = request.GET.get("ordering") or DEFAULT_ORDERING - prefix = "-" if ordering.startswith("-") else "" - field = ordering.lstrip("-") - if field == "users": - order_by = f"{prefix}users" - elif field in ReactionType.values: - # 按占比排序时过滤低样本,避免 1 人点 1 个就冲到 100% - queryset = queryset.filter(users__gte=MIN_USERS_FOR_RATIO) - order_by = f"{prefix}{field}_ratio" - else: - order_by = DEFAULT_ORDERING - - queryset = queryset.order_by(order_by) data = self.paginate_data(request, queryset) data["results"] = list(data["results"]) return self.success(data)