From d6db23e96b720611d30deabe30b993cbb975f698 Mon Sep 17 00:00:00 2001 From: Jose Buitron Date: Thu, 26 Sep 2024 10:12:12 -0500 Subject: [PATCH 1/3] refactor: Optimized visualization config query --- .../graphql/schema/shared_resources_mixin.py | 5 +++ .../graphql/schema/visualization_config.py | 43 ++++++++++++++----- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/terraso_backend/apps/graphql/schema/shared_resources_mixin.py b/terraso_backend/apps/graphql/schema/shared_resources_mixin.py index e6912a3e8..716689316 100644 --- a/terraso_backend/apps/graphql/schema/shared_resources_mixin.py +++ b/terraso_backend/apps/graphql/schema/shared_resources_mixin.py @@ -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( diff --git a/terraso_backend/apps/graphql/schema/visualization_config.py b/terraso_backend/apps/graphql/schema/visualization_config.py index a5a3e955f..13b09428d 100644 --- a/terraso_backend/apps/graphql/schema/visualization_config.py +++ b/terraso_backend/apps/graphql/schema/visualization_config.py @@ -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 ( @@ -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 @@ -98,7 +100,6 @@ class Meta: "configuration", "created_by", "created_at", - "data_entry", "mapbox_tileset_id", "mapbox_tileset_status", ) @@ -130,16 +131,36 @@ 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.defer("configuration") + .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: From 8ffd930a82504573f325ff7841b93da2339577f3 Mon Sep 17 00:00:00 2001 From: Jose Buitron Date: Thu, 26 Sep 2024 10:19:29 -0500 Subject: [PATCH 2/3] fix: Removed defered configuration fetch --- terraso_backend/apps/graphql/schema/visualization_config.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/terraso_backend/apps/graphql/schema/visualization_config.py b/terraso_backend/apps/graphql/schema/visualization_config.py index 13b09428d..5c471f868 100644 --- a/terraso_backend/apps/graphql/schema/visualization_config.py +++ b/terraso_backend/apps/graphql/schema/visualization_config.py @@ -132,8 +132,7 @@ def get_queryset(cls, queryset, info): ).values("id") ) return ( - queryset.defer("configuration") - .prefetch_related( + queryset.prefetch_related( Prefetch( "data_entry", queryset=DataEntry.objects.prefetch_related( From 5f86e2038c0e7d8a25a5fe33babf952c5d36d8b4 Mon Sep 17 00:00:00 2001 From: Jose Buitron Date: Thu, 26 Sep 2024 10:26:18 -0500 Subject: [PATCH 3/3] fix: Schema update --- terraso_backend/apps/graphql/schema/schema.graphql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/terraso_backend/apps/graphql/schema/schema.graphql b/terraso_backend/apps/graphql/schema/schema.graphql index 52a4ae751..1337dbcd8 100644 --- a/terraso_backend/apps/graphql/schema/schema.graphql +++ b/terraso_backend/apps/graphql/schema/schema.graphql @@ -671,8 +671,8 @@ type VisualizationConfigNode implements Node { createdBy: UserNode mapboxTilesetId: String mapboxTilesetStatus: SharedDataVisualizationConfigMapboxTilesetStatusChoices! - dataEntry: DataEntryNode! owner: OwnerNode + dataEntry: DataEntryNode } """ @@ -691,6 +691,8 @@ enum SharedDataVisualizationConfigMapboxTilesetStatusChoices { READY } +union OwnerNode = GroupNode | LandscapeNode + type DataEntryNode implements Node { createdAt: DateTime! name: String! @@ -742,8 +744,6 @@ type VisualizationConfigNodeEdge { cursor: String! } -union OwnerNode = GroupNode | LandscapeNode - union TargetNode = GroupNode | LandscapeNode type GroupNodeConnection {