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

refactor: Optimize maps queries #1471

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions terraso_backend/apps/graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,8 @@ type VisualizationConfigNode implements Node {
createdBy: UserNode
mapboxTilesetId: String
mapboxTilesetStatus: SharedDataVisualizationConfigMapboxTilesetStatusChoices!
dataEntry: DataEntryNode!
owner: OwnerNode
dataEntry: DataEntryNode
}

"""
Expand All @@ -691,6 +691,8 @@ enum SharedDataVisualizationConfigMapboxTilesetStatusChoices {
READY
}

union OwnerNode = GroupNode | LandscapeNode

type DataEntryNode implements Node {
createdAt: DateTime!
name: String!
Expand Down Expand Up @@ -742,8 +744,6 @@ type VisualizationConfigNodeEdge {
cursor: String!
}

union OwnerNode = GroupNode | LandscapeNode

union TargetNode = GroupNode | LandscapeNode

type GroupNodeConnection {
Expand Down
5 changes: 5 additions & 0 deletions terraso_backend/apps/graphql/schema/shared_resources_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class SharedResourcesMixin:
)

def resolve_shared_resources(self, info, **kwargs):
if (
hasattr(self, "_prefetched_objects_cache")
and "shared_resources" in self._prefetched_objects_cache
):
return self.shared_resources
user_pk = getattr(info.context.user, "pk", False)
user_groups_ids = Subquery(
Group.objects.filter(
Expand Down
42 changes: 31 additions & 11 deletions terraso_backend/apps/graphql/schema/visualization_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import structlog
from django.contrib.contenttypes.models import ContentType
from django.db import transaction
from django.db.models import Q, Subquery
from django.db.models import Prefetch, Q, Subquery
from graphene import relay
from graphene_django import DjangoObjectType

from apps.collaboration.models import Membership as CollaborationMembership
from apps.core.gis.mapbox import get_publish_status
from apps.core.models import Group, Landscape
from apps.core.models import Group, Landscape, SharedResource
from apps.graphql.exceptions import GraphQLNotAllowedException
from apps.graphql.schema.data_entries import DataEntryNode
from apps.shared_data.models.data_entries import DataEntry
from apps.shared_data.models.visualization_config import VisualizationConfig
from apps.shared_data.visualization_tileset_tasks import (
Expand Down Expand Up @@ -86,6 +87,7 @@ class Meta:
class VisualizationConfigNode(DjangoObjectType):
id = graphene.ID(source="pk", required=True)
owner = graphene.Field(OwnerNode)
data_entry = graphene.Field(DataEntryNode)

class Meta:
model = VisualizationConfig
Expand All @@ -98,7 +100,6 @@ class Meta:
"configuration",
"created_by",
"created_at",
"data_entry",
"mapbox_tileset_id",
"mapbox_tileset_status",
)
Expand Down Expand Up @@ -130,16 +131,35 @@ def get_queryset(cls, queryset, info):
membership_list__memberships__membership_status=CollaborationMembership.APPROVED,
).values("id")
)
return queryset.filter(
Q(
data_entry__shared_resources__target_object_id__in=user_groups_ids,
data_entry__deleted_at__isnull=True,
return (
queryset.prefetch_related(
Prefetch(
"data_entry",
queryset=DataEntry.objects.prefetch_related(
Prefetch(
"shared_resources",
queryset=SharedResource.objects.prefetch_related("target"),
),
Prefetch("created_by"),
),
),
Prefetch("created_by"),
)
| Q(
data_entry__shared_resources__target_object_id__in=user_landscape_ids,
data_entry__deleted_at__isnull=True,
.filter(
Q(
data_entry__shared_resources__target_object_id__in=user_groups_ids,
data_entry__deleted_at__isnull=True,
)
| Q(
data_entry__shared_resources__target_object_id__in=user_landscape_ids,
data_entry__deleted_at__isnull=True,
)
)
).distinct()
.distinct()
)

def resolve_data_entry(self, info):
return self.data_entry

def resolve_mapbox_tileset_id(self, info):
if self.mapbox_tileset_id is None:
Expand Down
Loading