Skip to content

Commit

Permalink
fix top k policy (#476)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hyukychang authored Nov 29, 2024
1 parent e10acb2 commit 6c4ff64
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ jobs:
- name: Run test
run: |
docker-compose -f docker-compose.test.yml run api test
docker-compose -f docker-compose.test.yml down
docker compose -f docker-compose.test.yml run api test
docker compose -f docker-compose.test.yml down
- if: env.PUSH == 'true'
name: Push docker image
run: |
Expand Down
11 changes: 8 additions & 3 deletions apps/core/views/viewsets/article.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import datetime
import time

from django.core.paginator import Paginator as DjangoPaginator
from django.db import models
from django.http import Http404
from django.utils import timezone
from django.utils.functional import cached_property
from django.utils.translation import gettext
from rest_framework import (
Expand Down Expand Up @@ -479,10 +481,13 @@ def recent(self, request, *args, **kwargs):

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

search_keyword = request.query_params.get("main_search__contains")
if search_keyword:
Expand Down
27 changes: 19 additions & 8 deletions tests/test_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,29 +717,40 @@ def test_being_topped(self):

def test_top_ordered(self):
"""
The most recently topped article must come first. If the same, then
the most recent article must come first.
Article created in a week has to be in top query result
The order is determined by hit_count
"""

current_article_count = Article.objects.count()

board = Board.objects.create()
articles = [
Article.objects.create(created_by=self.user, parent_board=board)
for _ in range(3)
for _ in range(5)
]

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

articles[0].topped_at = time_early
articles[1].topped_at = time_early
articles[2].topped_at = time_late
time_now = timezone.now()

articles[0].created_at = time_early
articles[1].created_at = time_early
articles[2].created_at = time_late
articles[3].created_at = time_now
articles[4].created_at = time_now

articles[3].hit_count = 15
articles[4].hit_count = 10

for article in articles:
article.save()

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

oracle_indices = [2, 1, 0]
oracle_indices = [3, 4]
for idx, res in zip(oracle_indices, response.data["results"]):
assert articles[idx].pk == res["id"]

Expand Down

0 comments on commit 6c4ff64

Please sign in to comment.