refactor: replace sync_to_async aggregate with aaggregate in CommentStatisticsAPI
This commit is contained in:
@@ -1,15 +1,15 @@
|
|||||||
from django.db.models import Avg, Count
|
from django.db.models import Avg, Count
|
||||||
from django.db.models.functions import Round
|
from django.db.models.functions import Round
|
||||||
|
|
||||||
from account.decorators import login_required
|
from account.decorators import login_required
|
||||||
from comment.models import Comment
|
from comment.models import Comment
|
||||||
from comment.serializers import CommentSerializer, CreateCommentSerializer
|
from comment.serializers import CommentSerializer, CreateCommentSerializer
|
||||||
from problem.models import Problem
|
from problem.models import Problem
|
||||||
from submission.models import JudgeStatus, Submission
|
from submission.models import JudgeStatus, Submission
|
||||||
from utils.api import AsyncAPIView
|
from utils.api import AsyncAPIView
|
||||||
from utils.api.api import validate_serializer
|
from utils.api.api import validate_serializer
|
||||||
from utils.async_helpers import async_cache_delete, async_cache_get, async_cache_set
|
from utils.async_helpers import async_cache_delete, async_cache_get, async_cache_set
|
||||||
from utils.constants import CacheKey
|
from utils.constants import CacheKey
|
||||||
|
|
||||||
|
|
||||||
class CommentAPI(AsyncAPIView):
|
class CommentAPI(AsyncAPIView):
|
||||||
@@ -48,8 +48,8 @@ class CommentAPI(AsyncAPIView):
|
|||||||
comprehensive_rating=data["comprehensive_rating"],
|
comprehensive_rating=data["comprehensive_rating"],
|
||||||
content=data["content"],
|
content=data["content"],
|
||||||
)
|
)
|
||||||
await async_cache_delete(f"{CacheKey.comment_stats}:{problem.id}")
|
await async_cache_delete(f"{CacheKey.comment_stats}:{problem.id}")
|
||||||
return self.success()
|
return self.success()
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
async def get(self, request):
|
async def get(self, request):
|
||||||
@@ -58,25 +58,22 @@ class CommentAPI(AsyncAPIView):
|
|||||||
Comment.objects.select_related("problem")
|
Comment.objects.select_related("problem")
|
||||||
.filter(user=request.user, problem_id=problem_id)
|
.filter(user=request.user, problem_id=problem_id)
|
||||||
.afirst()
|
.afirst()
|
||||||
)
|
)
|
||||||
if comment:
|
if comment:
|
||||||
return self.success(await self.async_serialize_data(CommentSerializer, comment))
|
return self.success(await self.async_serialize_data(CommentSerializer, comment))
|
||||||
else:
|
else:
|
||||||
return self.success()
|
return self.success()
|
||||||
|
|
||||||
|
|
||||||
class CommentStatisticsAPI(AsyncAPIView):
|
class CommentStatisticsAPI(AsyncAPIView):
|
||||||
async def get(self, request):
|
async def get(self, request):
|
||||||
problem_id = request.GET.get("problem_id")
|
problem_id = request.GET.get("problem_id")
|
||||||
cache_key = f"{CacheKey.comment_stats}:{problem_id}"
|
cache_key = f"{CacheKey.comment_stats}:{problem_id}"
|
||||||
cached = await async_cache_get(cache_key)
|
cached = await async_cache_get(cache_key)
|
||||||
if cached is not None:
|
if cached is not None:
|
||||||
return self.success(cached)
|
return self.success(cached)
|
||||||
|
|
||||||
from asgiref.sync import sync_to_async
|
agg = await Comment.objects.filter(problem_id=problem_id).aaggregate(
|
||||||
agg = await sync_to_async(
|
|
||||||
Comment.objects.filter(problem_id=problem_id).aggregate
|
|
||||||
)(
|
|
||||||
count=Count("id"),
|
count=Count("id"),
|
||||||
description=Round(Avg("description_rating"), 2),
|
description=Round(Avg("description_rating"), 2),
|
||||||
difficulty=Round(Avg("difficulty_rating"), 2),
|
difficulty=Round(Avg("difficulty_rating"), 2),
|
||||||
@@ -90,5 +87,5 @@ class CommentStatisticsAPI(AsyncAPIView):
|
|||||||
"difficulty": agg["difficulty"],
|
"difficulty": agg["difficulty"],
|
||||||
"comprehensive": agg["comprehensive"],
|
"comprehensive": agg["comprehensive"],
|
||||||
}}
|
}}
|
||||||
await async_cache_set(cache_key, data, 3600)
|
await async_cache_set(cache_key, data, 3600)
|
||||||
return self.success(data)
|
return self.success(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user