Skip to content

Commit 5af4098

Browse files
pakelleymatt-bernsteinhakan458
authored
feat: DIA-2206: prefetch completed_by info (#7417)
Co-authored-by: matt-bernstein <matt-bernstein@users.noreply.github.com> Co-authored-by: hakan458 <hakan@heartex.com>
1 parent 9b33636 commit 5af4098

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

label_studio/data_export/mixins.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from django.db.models.query_utils import Q
2626
from django.utils import dateformat, timezone
2727
from label_studio_sdk.converter import Converter
28-
from tasks.models import Annotation, Task
28+
from tasks.models import Annotation, AnnotationDraft, Task
2929

3030
ONLY = 'only'
3131
EXCLUDE = 'exclude'
@@ -96,27 +96,29 @@ def _get_filtered_annotations_queryset(self, annotation_filter_options=None):
9696
})
9797
"""
9898
queryset = Annotation.objects.all()
99-
if not isinstance(annotation_filter_options, dict):
100-
return queryset
101-
102-
q_list = []
103-
if annotation_filter_options.get('usual'):
104-
q_list.append(Q(was_cancelled=False, ground_truth=False))
105-
if annotation_filter_options.get('ground_truth'):
106-
q_list.append(Q(ground_truth=True))
107-
if annotation_filter_options.get('skipped'):
108-
q_list.append(Q(was_cancelled=True))
109-
if not q_list:
110-
return queryset
111-
112-
q = reduce(lambda x, y: x | y, q_list)
113-
annotations_qs = queryset.filter(q)
114-
99+
if isinstance(annotation_filter_options, dict):
100+
q_list = []
101+
if annotation_filter_options.get('usual'):
102+
q_list.append(Q(was_cancelled=False, ground_truth=False))
103+
if annotation_filter_options.get('ground_truth'):
104+
q_list.append(Q(ground_truth=True))
105+
if annotation_filter_options.get('skipped'):
106+
q_list.append(Q(was_cancelled=True))
107+
if q_list:
108+
q = reduce(lambda x, y: x | y, q_list)
109+
queryset = queryset.filter(q)
110+
111+
# pre-select completed_by user info
112+
queryset = queryset.select_related('completed_by')
115113
# prefetch reviews in LSE
116-
if hasattr(annotations_qs.model, 'reviews'):
117-
annotations_qs = annotations_qs.prefetch_related('reviews')
114+
if hasattr(queryset.model, 'reviews'):
115+
from reviews.models import AnnotationReview
116+
117+
queryset = queryset.prefetch_related(
118+
Prefetch('reviews', queryset=AnnotationReview.objects.select_related('created_by'))
119+
)
118120

119-
return annotations_qs
121+
return queryset
120122

121123
@staticmethod
122124
def _get_export_serializer_option(serialization_options):
@@ -153,13 +155,12 @@ def get_task_queryset(self, ids, annotation_filter_options):
153155

154156
return (
155157
Task.objects.filter(id__in=ids)
158+
.select_related('file_upload') # select_related more efficient for regular foreign-key relationship
156159
.prefetch_related(
157-
Prefetch(
158-
'annotations',
159-
queryset=annotations_qs,
160-
)
160+
Prefetch('annotations', queryset=annotations_qs),
161+
Prefetch('drafts', queryset=AnnotationDraft.objects.select_related('user')),
162+
'comment_authors',
161163
)
162-
.prefetch_related('predictions', 'drafts', 'comment_authors', 'file_upload')
163164
)
164165

165166
def get_export_data(self, task_filter_options=None, annotation_filter_options=None, serialization_options=None):

0 commit comments

Comments
 (0)