mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
fix: optimize cycles endpoint for sql (#4065)
This commit is contained in:
parent
599809dd2e
commit
7452e40134
@ -1,56 +1,56 @@
|
|||||||
# Python imports
|
# Python imports
|
||||||
import json
|
import json
|
||||||
|
|
||||||
# Django imports
|
|
||||||
from django.db.models import (
|
|
||||||
Func,
|
|
||||||
F,
|
|
||||||
Q,
|
|
||||||
Exists,
|
|
||||||
OuterRef,
|
|
||||||
Count,
|
|
||||||
Prefetch,
|
|
||||||
Case,
|
|
||||||
When,
|
|
||||||
Value,
|
|
||||||
CharField,
|
|
||||||
Subquery,
|
|
||||||
IntegerField,
|
|
||||||
)
|
|
||||||
from django.utils import timezone
|
|
||||||
from django.contrib.postgres.aggregates import ArrayAgg
|
from django.contrib.postgres.aggregates import ArrayAgg
|
||||||
from django.contrib.postgres.fields import ArrayField
|
from django.contrib.postgres.fields import ArrayField
|
||||||
from django.db.models import UUIDField
|
|
||||||
|
# Django imports
|
||||||
|
from django.db.models import (
|
||||||
|
Case,
|
||||||
|
CharField,
|
||||||
|
Count,
|
||||||
|
Exists,
|
||||||
|
F,
|
||||||
|
Func,
|
||||||
|
OuterRef,
|
||||||
|
Prefetch,
|
||||||
|
Q,
|
||||||
|
UUIDField,
|
||||||
|
Value,
|
||||||
|
When,
|
||||||
|
)
|
||||||
from django.db.models.functions import Coalesce
|
from django.db.models.functions import Coalesce
|
||||||
|
from django.utils import timezone
|
||||||
|
from rest_framework import status
|
||||||
|
|
||||||
# Third party imports
|
# Third party imports
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status
|
|
||||||
|
|
||||||
# Module imports
|
|
||||||
from .. import BaseViewSet, BaseAPIView, WebhookMixin
|
|
||||||
from plane.app.serializers import (
|
|
||||||
CycleSerializer,
|
|
||||||
CycleFavoriteSerializer,
|
|
||||||
CycleWriteSerializer,
|
|
||||||
CycleUserPropertiesSerializer,
|
|
||||||
)
|
|
||||||
from plane.app.permissions import (
|
from plane.app.permissions import (
|
||||||
ProjectEntityPermission,
|
ProjectEntityPermission,
|
||||||
ProjectLitePermission,
|
ProjectLitePermission,
|
||||||
)
|
)
|
||||||
from plane.db.models import (
|
from plane.app.serializers import (
|
||||||
User,
|
CycleFavoriteSerializer,
|
||||||
Cycle,
|
CycleSerializer,
|
||||||
CycleIssue,
|
CycleUserPropertiesSerializer,
|
||||||
Issue,
|
CycleWriteSerializer,
|
||||||
CycleFavorite,
|
|
||||||
Label,
|
|
||||||
CycleUserProperties,
|
|
||||||
)
|
)
|
||||||
from plane.bgtasks.issue_activites_task import issue_activity
|
from plane.bgtasks.issue_activites_task import issue_activity
|
||||||
|
from plane.db.models import (
|
||||||
|
Cycle,
|
||||||
|
CycleFavorite,
|
||||||
|
CycleIssue,
|
||||||
|
CycleUserProperties,
|
||||||
|
Issue,
|
||||||
|
Label,
|
||||||
|
User,
|
||||||
|
)
|
||||||
from plane.utils.analytics_plot import burndown_plot
|
from plane.utils.analytics_plot import burndown_plot
|
||||||
|
|
||||||
|
# Module imports
|
||||||
|
from .. import BaseAPIView, BaseViewSet, WebhookMixin
|
||||||
|
|
||||||
|
|
||||||
class CycleViewSet(WebhookMixin, BaseViewSet):
|
class CycleViewSet(WebhookMixin, BaseViewSet):
|
||||||
serializer_class = CycleSerializer
|
serializer_class = CycleSerializer
|
||||||
@ -73,60 +73,6 @@ class CycleViewSet(WebhookMixin, BaseViewSet):
|
|||||||
project_id=self.kwargs.get("project_id"),
|
project_id=self.kwargs.get("project_id"),
|
||||||
workspace__slug=self.kwargs.get("slug"),
|
workspace__slug=self.kwargs.get("slug"),
|
||||||
)
|
)
|
||||||
cancelled_issues = (
|
|
||||||
Issue.issue_objects.filter(
|
|
||||||
state__group="cancelled",
|
|
||||||
issue_cycle__cycle_id=OuterRef("pk"),
|
|
||||||
)
|
|
||||||
.values("issue_cycle__cycle_id")
|
|
||||||
.annotate(cnt=Count("pk"))
|
|
||||||
.values("cnt")
|
|
||||||
)
|
|
||||||
completed_issues = (
|
|
||||||
Issue.issue_objects.filter(
|
|
||||||
state__group="completed",
|
|
||||||
issue_cycle__cycle_id=OuterRef("pk"),
|
|
||||||
)
|
|
||||||
.values("issue_cycle__cycle_id")
|
|
||||||
.annotate(cnt=Count("pk"))
|
|
||||||
.values("cnt")
|
|
||||||
)
|
|
||||||
started_issues = (
|
|
||||||
Issue.issue_objects.filter(
|
|
||||||
state__group="started",
|
|
||||||
issue_cycle__cycle_id=OuterRef("pk"),
|
|
||||||
)
|
|
||||||
.values("issue_cycle__cycle_id")
|
|
||||||
.annotate(cnt=Count("pk"))
|
|
||||||
.values("cnt")
|
|
||||||
)
|
|
||||||
unstarted_issues = (
|
|
||||||
Issue.issue_objects.filter(
|
|
||||||
state__group="unstarted",
|
|
||||||
issue_cycle__cycle_id=OuterRef("pk"),
|
|
||||||
)
|
|
||||||
.values("issue_cycle__cycle_id")
|
|
||||||
.annotate(cnt=Count("pk"))
|
|
||||||
.values("cnt")
|
|
||||||
)
|
|
||||||
backlog_issues = (
|
|
||||||
Issue.issue_objects.filter(
|
|
||||||
state__group="backlog",
|
|
||||||
issue_cycle__cycle_id=OuterRef("pk"),
|
|
||||||
)
|
|
||||||
.values("issue_cycle__cycle_id")
|
|
||||||
.annotate(cnt=Count("pk"))
|
|
||||||
.values("cnt")
|
|
||||||
)
|
|
||||||
total_issues = (
|
|
||||||
Issue.issue_objects.filter(
|
|
||||||
issue_cycle__cycle_id=OuterRef("pk"),
|
|
||||||
)
|
|
||||||
.values("issue_cycle__cycle_id")
|
|
||||||
.annotate(cnt=Count("pk"))
|
|
||||||
.values("cnt")
|
|
||||||
)
|
|
||||||
|
|
||||||
return self.filter_queryset(
|
return self.filter_queryset(
|
||||||
super()
|
super()
|
||||||
.get_queryset()
|
.get_queryset()
|
||||||
@ -156,39 +102,62 @@ class CycleViewSet(WebhookMixin, BaseViewSet):
|
|||||||
)
|
)
|
||||||
.annotate(is_favorite=Exists(favorite_subquery))
|
.annotate(is_favorite=Exists(favorite_subquery))
|
||||||
.annotate(
|
.annotate(
|
||||||
completed_issues=Coalesce(
|
total_issues=Count(
|
||||||
Subquery(completed_issues[:1]),
|
"issue_cycle",
|
||||||
Value(0, output_field=IntegerField()),
|
filter=Q(
|
||||||
|
issue_cycle__issue__archived_at__isnull=True,
|
||||||
|
issue_cycle__issue__is_draft=False,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
cancelled_issues=Coalesce(
|
completed_issues=Count(
|
||||||
Subquery(cancelled_issues[:1]),
|
"issue_cycle__issue__state__group",
|
||||||
Value(0, output_field=IntegerField()),
|
filter=Q(
|
||||||
|
issue_cycle__issue__state__group="completed",
|
||||||
|
issue_cycle__issue__archived_at__isnull=True,
|
||||||
|
issue_cycle__issue__is_draft=False,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
started_issues=Coalesce(
|
cancelled_issues=Count(
|
||||||
Subquery(started_issues[:1]),
|
"issue_cycle__issue__state__group",
|
||||||
Value(0, output_field=IntegerField()),
|
filter=Q(
|
||||||
|
issue_cycle__issue__state__group="cancelled",
|
||||||
|
issue_cycle__issue__archived_at__isnull=True,
|
||||||
|
issue_cycle__issue__is_draft=False,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
unstarted_issues=Coalesce(
|
started_issues=Count(
|
||||||
Subquery(unstarted_issues[:1]),
|
"issue_cycle__issue__state__group",
|
||||||
Value(0, output_field=IntegerField()),
|
filter=Q(
|
||||||
|
issue_cycle__issue__state__group="started",
|
||||||
|
issue_cycle__issue__archived_at__isnull=True,
|
||||||
|
issue_cycle__issue__is_draft=False,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
backlog_issues=Coalesce(
|
unstarted_issues=Count(
|
||||||
Subquery(backlog_issues[:1]),
|
"issue_cycle__issue__state__group",
|
||||||
Value(0, output_field=IntegerField()),
|
filter=Q(
|
||||||
|
issue_cycle__issue__state__group="unstarted",
|
||||||
|
issue_cycle__issue__archived_at__isnull=True,
|
||||||
|
issue_cycle__issue__is_draft=False,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
total_issues=Coalesce(
|
backlog_issues=Count(
|
||||||
Subquery(total_issues[:1]),
|
"issue_cycle__issue__state__group",
|
||||||
Value(0, output_field=IntegerField()),
|
filter=Q(
|
||||||
|
issue_cycle__issue__state__group="backlog",
|
||||||
|
issue_cycle__issue__archived_at__isnull=True,
|
||||||
|
issue_cycle__issue__is_draft=False,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
@ -217,9 +186,6 @@ class CycleViewSet(WebhookMixin, BaseViewSet):
|
|||||||
distinct=True,
|
distinct=True,
|
||||||
filter=~Q(
|
filter=~Q(
|
||||||
issue_cycle__issue__assignees__id__isnull=True
|
issue_cycle__issue__assignees__id__isnull=True
|
||||||
)
|
|
||||||
& Q(
|
|
||||||
issue_cycle__issue__assignees__member_project__is_active=True
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Value([], output_field=ArrayField(UUIDField())),
|
Value([], output_field=ArrayField(UUIDField())),
|
||||||
@ -384,8 +350,8 @@ class CycleViewSet(WebhookMixin, BaseViewSet):
|
|||||||
"external_id",
|
"external_id",
|
||||||
"progress_snapshot",
|
"progress_snapshot",
|
||||||
# meta fields
|
# meta fields
|
||||||
"total_issues",
|
|
||||||
"is_favorite",
|
"is_favorite",
|
||||||
|
"total_issues",
|
||||||
"cancelled_issues",
|
"cancelled_issues",
|
||||||
"completed_issues",
|
"completed_issues",
|
||||||
"started_issues",
|
"started_issues",
|
||||||
|
Loading…
Reference in New Issue
Block a user