Skip to content

Commit 6c4ff64

Browse files
authored
fix top k policy (#476)
* feat: fix top article query * fix: add comment for top article query * fix: docker test fail * fix: fix test for top query * fix: fix test for top query * fix: fix comment
1 parent e10acb2 commit 6c4ff64

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

.github/workflows/github-actions.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ jobs:
5656
5757
- name: Run test
5858
run: |
59-
docker-compose -f docker-compose.test.yml run api test
60-
docker-compose -f docker-compose.test.yml down
59+
docker compose -f docker-compose.test.yml run api test
60+
docker compose -f docker-compose.test.yml down
6161
- if: env.PUSH == 'true'
6262
name: Push docker image
6363
run: |

apps/core/views/viewsets/article.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import datetime
12
import time
23

34
from django.core.paginator import Paginator as DjangoPaginator
45
from django.db import models
56
from django.http import Http404
7+
from django.utils import timezone
68
from django.utils.functional import cached_property
79
from django.utils.translation import gettext
810
from rest_framework import (
@@ -479,10 +481,13 @@ def recent(self, request, *args, **kwargs):
479481

480482
@decorators.action(detail=False, methods=["get"])
481483
def top(self, request):
482-
# The most recent article at the top
483-
top_articles = Article.objects.exclude(topped_at__isnull=True).order_by(
484-
"-topped_at", "-pk"
484+
current_date = datetime.datetime.combine(
485+
timezone.now().date(), datetime.time.min, datetime.timezone.utc
485486
)
487+
# get the articles that are created_at within a week and order by hit_count
488+
top_articles = Article.objects.filter(
489+
created_at__gte=current_date - datetime.timedelta(days=7)
490+
).order_by("-hit_count", "-pk")
486491

487492
search_keyword = request.query_params.get("main_search__contains")
488493
if search_keyword:

tests/test_articles.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -717,29 +717,40 @@ def test_being_topped(self):
717717

718718
def test_top_ordered(self):
719719
"""
720-
The most recently topped article must come first. If the same, then
721-
the most recent article must come first.
720+
Article created in a week has to be in top query result
721+
The order is determined by hit_count
722722
"""
723+
724+
current_article_count = Article.objects.count()
725+
723726
board = Board.objects.create()
724727
articles = [
725728
Article.objects.create(created_by=self.user, parent_board=board)
726-
for _ in range(3)
729+
for _ in range(5)
727730
]
728731

729732
time_early = timezone.datetime(2001, 10, 18) # retro's birthday
730733
time_late = timezone.datetime(2003, 6, 17) # yuwol's birthday
731734

732-
articles[0].topped_at = time_early
733-
articles[1].topped_at = time_early
734-
articles[2].topped_at = time_late
735+
time_now = timezone.now()
736+
737+
articles[0].created_at = time_early
738+
articles[1].created_at = time_early
739+
articles[2].created_at = time_late
740+
articles[3].created_at = time_now
741+
articles[4].created_at = time_now
742+
743+
articles[3].hit_count = 15
744+
articles[4].hit_count = 10
745+
735746
for article in articles:
736747
article.save()
737748

738749
response = self.http_request(self.user, "get", "articles/top")
739750
assert response.status_code == status.HTTP_200_OK
740-
assert response.data["num_items"] == 3
751+
assert response.data["num_items"] == current_article_count + 2
741752

742-
oracle_indices = [2, 1, 0]
753+
oracle_indices = [3, 4]
743754
for idx, res in zip(oracle_indices, response.data["results"]):
744755
assert articles[idx].pk == res["id"]
745756

0 commit comments

Comments
 (0)