Skip to content

Commit

Permalink
Trying to sort scratch list by match percent (#1345)
Browse files Browse the repository at this point in the history
* Trying to sort scrach list by match percent

* Add Cast
  • Loading branch information
mkst authored Sep 21, 2024
1 parent bb5f06a commit 9c56aa0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 13 additions & 2 deletions backend/coreapp/views/scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import django_filters
from coreapp import compilers, platforms
from django.core.files import File
from django.db.models import F, FloatField, When, Case, Value
from django.db.models.functions import Cast
from django.http import HttpResponse, QueryDict
from rest_framework import filters, mixins, serializers, status
from rest_framework.decorators import action
Expand Down Expand Up @@ -310,7 +312,16 @@ class ScratchViewSet(
mixins.ListModelMixin,
GenericViewSet, # type: ignore
):
queryset = Scratch.objects.all()
match_percent = Case(
When(max_score__lte=0, then=Value(0.0)),
When(score__lt=0, then=Value(0.0)),
When(score__gt=F("max_score"), then=Value(0.0)),
When(score=0, then=Value(1.0)),
When(match_override=True, then=Value(1.0)),
default=1.0 - (F("score") / Cast("max_score", FloatField())),
)

queryset = Scratch.objects.all().annotate(match_percent=match_percent)
pagination_class = ScratchPagination
filterset_fields = ["platform", "compiler", "preset"]
filter_backends = [
Expand All @@ -319,7 +330,7 @@ class ScratchViewSet(
filters.OrderingFilter,
]
search_fields = ["name", "diff_label"]
ordering_fields = ["creation_time", "last_updated", "score"]
ordering_fields = ["creation_time", "last_updated", "score", "match_percent"]

def get_serializer_class(self) -> type[serializers.ModelSerializer[Scratch]]:
if self.action == "list":
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/Sort.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export enum SortMode {
NEWEST_FIRST = "-creation_time",
OLDEST_FIRST = "creation_time",
LAST_UPDATED = "-last_updated",
LEAST_MATCHED = "-score",
MOST_MATCHED = "score",
LEAST_MATCHED = "match_percent",
MOST_MATCHED = "-match_percent",
}

export function produceSortFunction(sortMode: SortMode): (a: TerseScratch, b: TerseScratch) => number {
Expand Down

0 comments on commit 9c56aa0

Please sign in to comment.