Skip to content

Commit

Permalink
fix: OPTIC-1407: Optimize the tasks API paginated prediction and anno…
Browse files Browse the repository at this point in the history
…tation totals (#6735)
  • Loading branch information
bmartel authored Dec 2, 2024
1 parent 21060c3 commit f293118
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions label_studio/data_manager/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
ViewSerializer,
)
from django.conf import settings
from django.db.models import Sum
from django.db.models.functions import Coalesce
from django.utils.decorators import method_decorator
from django_filters.rest_framework import DjangoFilterBackend
from drf_yasg import openapi
Expand Down Expand Up @@ -224,11 +226,21 @@ def sync_paginate_queryset(self, queryset, request, view=None):
self.total_annotations = Annotation.objects.filter(task_id__in=queryset, was_cancelled=False).count()
return super().paginate_queryset(queryset, request, view)

def paginate_totals_queryset(self, queryset, request, view=None):
totals = queryset.values('id').aggregate(
total_annotations=Coalesce(Sum('total_annotations'), 0),
total_predictions=Coalesce(Sum('total_predictions'), 0),
)
self.total_annotations = totals['total_annotations']
self.total_predictions = totals['total_predictions']
return super().paginate_queryset(queryset, request, view)

def paginate_queryset(self, queryset, request, view=None):
if flag_set('fflag_fix_back_optic_1407_optimize_tasks_api_pagination_counts'):
return self.paginate_totals_queryset(queryset, request, view)
if flag_set('fflag_fix_back_leap_24_tasks_api_optimization_05092023_short'):
return self.async_paginate_queryset(queryset, request, view)
else:
return self.sync_paginate_queryset(queryset, request, view)
return self.sync_paginate_queryset(queryset, request, view)

def get_paginated_response(self, data):
return Response(
Expand Down

0 comments on commit f293118

Please sign in to comment.