Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

UN-2871 [FEATURE] Log sharing across shared workflows/deployments (#1580)

Rahul Johny89d4d6b5a152
diff --git a/backend/workflow_manager/execution/filter.py b/backend/workflow_manager/execution/filter.py
index ab09f107a..6664e3f8a 100644
--- a/backend/workflow_manager/execution/filter.py
+++ b/backend/workflow_manager/execution/filter.py
@@ -1,12 +1,12 @@
from api_v2.models import APIDeployment
-from django.db.models import QuerySet
+from django.db.models import Exists, OuterRef, QuerySet
from django_filters import rest_framework as filters
from pipeline_v2.models import Pipeline
from utils.date import DateRangePresets, DateTimeProcessor
from workflow_manager.execution.enum import ExecutionEntity
from workflow_manager.workflow_v2.enums import ExecutionStatus
-from workflow_manager.workflow_v2.models import Workflow, WorkflowExecution
+from workflow_manager.workflow_v2.models import WorkflowExecution
class ExecutionFilter(filters.FilterSet):
@@ -36,27 +36,40 @@ class ExecutionFilter(filters.FilterSet):
def filter_execution_entity(
self, queryset: QuerySet, name: str, value: str
) -> QuerySet:
+ """Filter executions by entity type efficiently.
+
+ The queryset is already filtered by user permissions via get_queryset(),
+ so we use EXISTS subqueries to check entity type without fetching all IDs.
+ This is more efficient than values_list() for large datasets.
+ """
if value == ExecutionEntity.API.value:
+ # Filter for API deployments using EXISTS for efficiency
return queryset.filter(
- pipeline_id__in=APIDeployment.objects.values_list("id", flat=True)
+ Exists(APIDeployment.objects.filter(id=OuterRef("pipeline_id")))
)
elif value == ExecutionEntity.ETL.value:
+ # Filter for ETL pipelines using EXISTS
return queryset.filter(
- pipeline_id__in=Pipeline.objects.filter(
- pipeline_type=Pipeline.PipelineType.ETL
- ).values_list("id", flat=True)
+ Exists(
+ Pipeline.objects.filter(
+ id=OuterRef("pipeline_id"),
+ pipeline_type=Pipeline.PipelineType.ETL,
+ )
+ )
)
elif value == ExecutionEntity.TASK.value:
+ # Filter for TASK pipelines using EXISTS
return queryset.filter(
- pipeline_id__in=Pipeline.objects.filter(
- pipeline_type=Pipeline.PipelineType.TASK
- ).values_list("id", flat=True)
+ Exists(
+ Pipeline.objects.filter(
+ id=OuterRef("pipeline_id"),
+ pipeline_type=Pipeline.PipelineType.TASK,
+ )
+ )
)
elif value == ExecutionEntity.WORKFLOW.value:
- return queryset.filter(
- pipeline_id=None,
- workflow_id__in=Workflow.objects.values_list("id", flat=True),
- )
+ # Filter for workflow-level executions (no pipeline)
+ return queryset.filter(pipeline_id__isnull=True)
return queryset
def filter_by_date_range(self, queryset: QuerySet, name: str, value: str) -> QuerySet:
diff --git a/backend/workflow_manager/execution/views/execution.py b/backend/workflow_manager/execution/views/execution.py
index 9df253a49..5363ef508 100644
--- a/backend/workflow_manager/execution/views/execution.py
+++ b/backend/workflow_manager/execution/views/execution.py
@@ -38,7 +38,6 @@ class ExecutionViewSet(viewsets.ReadOnlyModelViewSet):
ordering_fields = ["created_at", "execution_time"]
ordering = ["-created_at"]
filterset_class = ExecutionFilter
- queryset = WorkflowExecution.objects.all()
def get_queryset(self):
# Use the custom manager method to filter executions for the current user
diff --git a/backend/workflow_manager/workflow_v2/models/execution.py b/backend/workflow_manager/workflow_v2/models/execution.py
index f06baa6d6..9ce0187bb 100644
--- a/backend/workflow_manager/workflow_v2/models/execution.py
+++ b/backend/workflow_manager/workflow_v2/models/execution.py
@@ -5,7 +5,7 @@ from datetime import timedelta
from api_v2.models import APIDeployment
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
-from django.db.models import QuerySet, Sum
+from django.db.models import Q, QuerySet, Sum
from pipeline_v2.models import Pipeline
from tags.models import Tag
from usage_v2.constants import UsageKeys
@@ -28,8 +28,17 @@ class WorkflowExecutionManager(models.Manager):
"""Custom manager for WorkflowExecution model to handle user-specific filtering."""
def for_user(self, user) -> QuerySet:
- """Filter user's workflow executions.
- Show those belonging to workflows created by the specified user.
+ """Filter user's workflow executions with proper access control.
+
+ Returns executions where the user has access to:
+ - The workflow (created by user OR shared with user) AND/OR
+ - The pipeline/API deployment (created by user OR shared with user)
+
+ This handles independent sharing scenarios:
+ 1. Workflow shared but not API deployment -> User can see workflow-only executions
+ 2. API deployment shared but not workflow -> User can see those API executions
+ 3. Both shared -> User can see all executions
+ 4. Neither shared -> User cannot see executions
Args:
user: The user to filter executions for
@@ -37,8 +46,36 @@ class WorkflowExecutionManager(models.Manager):
Returns:
QuerySet of executions that the user has permission to access
"""
- # Return executions where the workflow's created_by matches the user
- return self.filter(workflow__created_by=user)
+ # Filter for workflow access
+ workflow_filter = Q(workflow__created_by=user) | Q(workflow__shared_users=user)
+
+ # Filter for API deployments the user can access
+ api_filter = Q(
+ pipeline_id__in=models.Subquery(
+ APIDeployment.objects.filter(
+ Q(created_by=user) | Q(shared_users=user)
+ ).values("id")
+ )
+ )
+
+ # Filter for Pipelines the user can access
+ pipeline_filter = Q(
+ pipeline_id__in=models.Subquery(
+ Pipeline.objects.filter(Q(created_by=user) | Q(shared_users=user)).values(
+ "id"
+ )
+ )
+ )
+
+ # Combine deployment filters
+ deployment_filter = api_filter | pipeline_filter
+
+ # User can see executions if they have access to:
+ # 1. The workflow AND execution has no pipeline (workflow-level execution)
+ # 2. The pipeline/API deployment (regardless of workflow access)
+ final_filter = (workflow_filter & Q(pipeline_id__isnull=True)) | deployment_filter
+
+ return self.filter(final_filter).distinct()
def clean_invalid_workflows(self):
"""Remove execution records with invalid workflow references.