[WEB-4657] refactor: optimize project v2 endpoint and issue detail endpoint #7558
This commit is contained in:
parent
e3deeb2b43
commit
1ef30746a2
1 changed files with 98 additions and 89 deletions
|
|
@ -15,6 +15,7 @@ from django.db.models import (
|
||||||
UUIDField,
|
UUIDField,
|
||||||
Value,
|
Value,
|
||||||
Subquery,
|
Subquery,
|
||||||
|
Count,
|
||||||
)
|
)
|
||||||
from django.db.models.functions import Coalesce
|
from django.db.models.functions import Coalesce
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
@ -453,10 +454,12 @@ class IssueViewSet(BaseViewSet):
|
||||||
project = Project.objects.get(pk=project_id, workspace__slug=slug)
|
project = Project.objects.get(pk=project_id, workspace__slug=slug)
|
||||||
|
|
||||||
issue = (
|
issue = (
|
||||||
Issue.objects.filter(project_id=self.kwargs.get("project_id"))
|
Issue.objects.filter(
|
||||||
.filter(workspace__slug=self.kwargs.get("slug"))
|
project_id=self.kwargs.get("project_id"),
|
||||||
.select_related("workspace", "project", "state", "parent")
|
workspace__slug=self.kwargs.get("slug"),
|
||||||
.prefetch_related("assignees", "labels", "issue_module__module")
|
pk=pk,
|
||||||
|
)
|
||||||
|
.select_related("state")
|
||||||
.annotate(
|
.annotate(
|
||||||
cycle_id=Subquery(
|
cycle_id=Subquery(
|
||||||
CycleIssue.objects.filter(issue=OuterRef("id")).values("cycle_id")[
|
CycleIssue.objects.filter(issue=OuterRef("id")).values("cycle_id")[
|
||||||
|
|
@ -465,60 +468,63 @@ class IssueViewSet(BaseViewSet):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
link_count=IssueLink.objects.filter(issue=OuterRef("id"))
|
link_count=Subquery(
|
||||||
.order_by()
|
IssueLink.objects.filter(issue=OuterRef("id"))
|
||||||
.annotate(count=Func(F("id"), function="Count"))
|
.values("issue")
|
||||||
|
.annotate(count=Count("id"))
|
||||||
.values("count")
|
.values("count")
|
||||||
)
|
)
|
||||||
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
attachment_count=FileAsset.objects.filter(
|
attachment_count=Subquery(
|
||||||
|
FileAsset.objects.filter(
|
||||||
issue_id=OuterRef("id"),
|
issue_id=OuterRef("id"),
|
||||||
entity_type=FileAsset.EntityTypeContext.ISSUE_ATTACHMENT,
|
entity_type=FileAsset.EntityTypeContext.ISSUE_ATTACHMENT,
|
||||||
)
|
)
|
||||||
.order_by()
|
.values("issue_id")
|
||||||
.annotate(count=Func(F("id"), function="Count"))
|
.annotate(count=Count("id"))
|
||||||
.values("count")
|
.values("count")
|
||||||
)
|
)
|
||||||
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
sub_issues_count=Issue.issue_objects.filter(parent=OuterRef("id"))
|
sub_issues_count=Subquery(
|
||||||
.order_by()
|
Issue.issue_objects.filter(parent=OuterRef("id"))
|
||||||
.annotate(count=Func(F("id"), function="Count"))
|
.values("parent")
|
||||||
|
.annotate(count=Count("id"))
|
||||||
.values("count")
|
.values("count")
|
||||||
)
|
)
|
||||||
.filter(pk=pk)
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
label_ids=Coalesce(
|
label_ids=Coalesce(
|
||||||
ArrayAgg(
|
Subquery(
|
||||||
"labels__id",
|
IssueLabel.objects.filter(issue_id=OuterRef("pk"))
|
||||||
distinct=True,
|
.values("issue_id")
|
||||||
filter=Q(
|
.annotate(arr=ArrayAgg("label_id", distinct=True))
|
||||||
~Q(labels__id__isnull=True)
|
.values("arr")
|
||||||
& Q(label_issue__deleted_at__isnull=True)
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
Value([], output_field=ArrayField(UUIDField())),
|
Value([], output_field=ArrayField(UUIDField())),
|
||||||
),
|
),
|
||||||
assignee_ids=Coalesce(
|
assignee_ids=Coalesce(
|
||||||
ArrayAgg(
|
Subquery(
|
||||||
"assignees__id",
|
IssueAssignee.objects.filter(
|
||||||
distinct=True,
|
issue_id=OuterRef("pk"),
|
||||||
filter=Q(
|
assignee__member_project__is_active=True,
|
||||||
~Q(assignees__id__isnull=True)
|
)
|
||||||
& Q(assignees__member_project__is_active=True)
|
.values("issue_id")
|
||||||
& Q(issue_assignee__deleted_at__isnull=True)
|
.annotate(arr=ArrayAgg("assignee_id", distinct=True))
|
||||||
),
|
.values("arr")
|
||||||
),
|
),
|
||||||
Value([], output_field=ArrayField(UUIDField())),
|
Value([], output_field=ArrayField(UUIDField())),
|
||||||
),
|
),
|
||||||
module_ids=Coalesce(
|
module_ids=Coalesce(
|
||||||
ArrayAgg(
|
Subquery(
|
||||||
"issue_module__module_id",
|
ModuleIssue.objects.filter(
|
||||||
distinct=True,
|
issue_id=OuterRef("pk"),
|
||||||
filter=Q(
|
module__archived_at__isnull=True,
|
||||||
~Q(issue_module__module_id__isnull=True)
|
)
|
||||||
& Q(issue_module__module__archived_at__isnull=True)
|
.values("issue_id")
|
||||||
& Q(issue_module__deleted_at__isnull=True)
|
.annotate(arr=ArrayAgg("module_id", distinct=True))
|
||||||
),
|
.values("arr")
|
||||||
),
|
),
|
||||||
Value([], output_field=ArrayField(UUIDField())),
|
Value([], output_field=ArrayField(UUIDField())),
|
||||||
),
|
),
|
||||||
|
|
@ -786,37 +792,42 @@ class IssuePaginatedViewSet(BaseViewSet):
|
||||||
)
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
issue_queryset.select_related("workspace", "project", "state", "parent")
|
issue_queryset.select_related("state")
|
||||||
.prefetch_related("assignees", "labels", "issue_module__module")
|
|
||||||
.annotate(
|
.annotate(
|
||||||
cycle_id=Subquery(
|
cycle_id=Subquery(
|
||||||
CycleIssue.objects.filter(
|
CycleIssue.objects.filter(issue=OuterRef("id")).values("cycle_id")[
|
||||||
issue=OuterRef("id"), deleted_at__isnull=True
|
:1
|
||||||
).values("cycle_id")[:1]
|
]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
link_count=IssueLink.objects.filter(issue=OuterRef("id"))
|
link_count=Subquery(
|
||||||
.order_by()
|
IssueLink.objects.filter(issue=OuterRef("id"))
|
||||||
.annotate(count=Func(F("id"), function="Count"))
|
.values("issue")
|
||||||
|
.annotate(count=Count("id"))
|
||||||
.values("count")
|
.values("count")
|
||||||
)
|
)
|
||||||
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
attachment_count=FileAsset.objects.filter(
|
attachment_count=Subquery(
|
||||||
|
FileAsset.objects.filter(
|
||||||
issue_id=OuterRef("id"),
|
issue_id=OuterRef("id"),
|
||||||
entity_type=FileAsset.EntityTypeContext.ISSUE_ATTACHMENT,
|
entity_type=FileAsset.EntityTypeContext.ISSUE_ATTACHMENT,
|
||||||
)
|
)
|
||||||
.order_by()
|
.values("issue_id")
|
||||||
.annotate(count=Func(F("id"), function="Count"))
|
.annotate(count=Count("id"))
|
||||||
.values("count")
|
.values("count")
|
||||||
)
|
)
|
||||||
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
sub_issues_count=Issue.issue_objects.filter(parent=OuterRef("id"))
|
sub_issues_count=Subquery(
|
||||||
.order_by()
|
Issue.issue_objects.filter(parent=OuterRef("id"))
|
||||||
.annotate(count=Func(F("id"), function="Count"))
|
.values("parent")
|
||||||
|
.annotate(count=Count("id"))
|
||||||
.values("count")
|
.values("count")
|
||||||
)
|
)
|
||||||
).distinct()
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def process_paginated_result(self, fields, results, timezone):
|
def process_paginated_result(self, fields, results, timezone):
|
||||||
paginated_data = results.values(*fields)
|
paginated_data = results.values(*fields)
|
||||||
|
|
@ -896,37 +907,35 @@ class IssuePaginatedViewSet(BaseViewSet):
|
||||||
|
|
||||||
queryset = queryset.annotate(
|
queryset = queryset.annotate(
|
||||||
label_ids=Coalesce(
|
label_ids=Coalesce(
|
||||||
ArrayAgg(
|
Subquery(
|
||||||
"labels__id",
|
IssueLabel.objects.filter(issue_id=OuterRef("pk"))
|
||||||
distinct=True,
|
.values("issue_id")
|
||||||
filter=Q(
|
.annotate(arr=ArrayAgg("label_id", distinct=True))
|
||||||
~Q(labels__id__isnull=True)
|
.values("arr")
|
||||||
& Q(label_issue__deleted_at__isnull=True)
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
Value([], output_field=ArrayField(UUIDField())),
|
Value([], output_field=ArrayField(UUIDField())),
|
||||||
),
|
),
|
||||||
assignee_ids=Coalesce(
|
assignee_ids=Coalesce(
|
||||||
ArrayAgg(
|
Subquery(
|
||||||
"assignees__id",
|
IssueAssignee.objects.filter(
|
||||||
distinct=True,
|
issue_id=OuterRef("pk"),
|
||||||
filter=Q(
|
assignee__member_project__is_active=True,
|
||||||
~Q(assignees__id__isnull=True)
|
)
|
||||||
& Q(assignees__member_project__is_active=True)
|
.values("issue_id")
|
||||||
& Q(issue_assignee__deleted_at__isnull=True)
|
.annotate(arr=ArrayAgg("assignee_id", distinct=True))
|
||||||
),
|
.values("arr")
|
||||||
),
|
),
|
||||||
Value([], output_field=ArrayField(UUIDField())),
|
Value([], output_field=ArrayField(UUIDField())),
|
||||||
),
|
),
|
||||||
module_ids=Coalesce(
|
module_ids=Coalesce(
|
||||||
ArrayAgg(
|
Subquery(
|
||||||
"issue_module__module_id",
|
ModuleIssue.objects.filter(
|
||||||
distinct=True,
|
issue_id=OuterRef("pk"),
|
||||||
filter=Q(
|
module__archived_at__isnull=True,
|
||||||
~Q(issue_module__module_id__isnull=True)
|
)
|
||||||
& Q(issue_module__module__archived_at__isnull=True)
|
.values("issue_id")
|
||||||
& Q(issue_module__deleted_at__isnull=True)
|
.annotate(arr=ArrayAgg("module_id", distinct=True))
|
||||||
),
|
.values("arr")
|
||||||
),
|
),
|
||||||
Value([], output_field=ArrayField(UUIDField())),
|
Value([], output_field=ArrayField(UUIDField())),
|
||||||
),
|
),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue