Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize DB requests in project list, job list, and other endpoints #8275

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cvat/apps/engine/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,10 +1360,14 @@ class Meta:

def to_representation(self, instance):
response = super().to_representation(instance)
task_subsets = {task.subset for task in instance.tasks.all()}
task_subsets.discard('')

task_subsets = {task.subset for task in instance.tasks.all() if task.subset}
task_dimension = next(
(task.dimension for task in instance.tasks.all() if task.dimension),
None
)
response['task_subsets'] = list(task_subsets)
response['dimension'] = getattr(instance.tasks.first(), 'dimension', None)
response['dimension'] = task_dimension
return response

class ProjectWriteSerializer(serializers.ModelSerializer):
Expand Down
14 changes: 12 additions & 2 deletions cvat/apps/engine/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,8 +1705,18 @@ class JobViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.CreateMo
mixins.RetrieveModelMixin, PartialUpdateModelMixin, mixins.DestroyModelMixin,
UploadMixin, DatasetMixin, CsrfWorkaroundMixin
):
queryset = Job.objects.select_related('assignee', 'segment__task__data',
'segment__task__project', 'segment__task__annotation_guide', 'segment__task__project__annotation_guide',
queryset = Job.objects.select_related(
'assignee',
'segment__task__data',
'segment__task__project',
'segment__task__annotation_guide',
'segment__task__project__annotation_guide',
'segment__task__source_storage',
'segment__task__target_storage',
'segment__task__organization',
'segment__task__project__organization',
'segment__task__owner',
'segment__task__project__owner',
).annotate(
Count('issues', distinct=True),
).all()
Expand Down
7 changes: 7 additions & 0 deletions cvat/apps/iam/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def get_organization(request, obj):

raise exc

if not organization_id:
return None

if org := getattr(obj, 'organization', None):
if isinstance(org, Organization):
return org

try:
return Organization.objects.select_related('owner').get(id=organization_id)
except Organization.DoesNotExist:
Expand Down
Loading