LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
[MISC] Fix N+1 query patterns in execution log views (#1693)
Chandrasekharan M48e4a0146039
diff --git a/backend/workflow_manager/file_execution/serializers.py b/backend/workflow_manager/file_execution/serializers.pyindex 38d44b961..a40f80b04 100644--- a/backend/workflow_manager/file_execution/serializers.py+++ b/backend/workflow_manager/file_execution/serializers.py@@ -35,13 +35,7 @@ class FileCentricExecutionSerializer(serializers.ModelSerializer):elif obj.status == ExecutionStatus.ERROR.value:return obj.execution_error- latest_log = (- obj.execution_logs.exclude(data__level__in=["DEBUG", "WARN"])- .order_by("-event_time")- .first()- )- return (- latest_log.data["log"]- if latest_log and "log" in latest_log.data- else self.DEFAULT_STATUS_MSG- )+ # Use pre-annotated field from viewset queryset to avoid N+1 queries+ if hasattr(obj, "latest_log_data") and obj.latest_log_data:+ return obj.latest_log_data.get("log", self.DEFAULT_STATUS_MSG)+ return self.DEFAULT_STATUS_MSGdiff --git a/backend/workflow_manager/file_execution/views.py b/backend/workflow_manager/file_execution/views.pyindex 8e3ef9607..be012a6c1 100644--- a/backend/workflow_manager/file_execution/views.py+++ b/backend/workflow_manager/file_execution/views.py@@ -1,3 +1,4 @@+from django.db.models import OuterRef, Subqueryfrom django_filters.rest_framework import DjangoFilterBackendfrom rest_framework import viewsetsfrom rest_framework.filters import OrderingFilter@@ -9,6 +10,7 @@ from workflow_manager.file_execution.models import (WorkflowFileExecution as FileExecution,)from workflow_manager.file_execution.serializers import FileCentricExecutionSerializer+from workflow_manager.workflow_v2.models.execution_log import ExecutionLogclass FileCentricExecutionViewSet(viewsets.ReadOnlyModelViewSet):@@ -22,4 +24,16 @@ class FileCentricExecutionViewSet(viewsets.ReadOnlyModelViewSet):def get_queryset(self):execution_id = self.kwargs.get("pk")- return FileExecution.objects.filter(workflow_execution_id=execution_id)++ # Subquery to get latest non-DEBUG/WARN log data per file execution+ # Avoids N+1 queries when serializing status_msg+ latest_log_subquery = (+ ExecutionLog.objects.filter(file_execution=OuterRef("pk"))+ .exclude(data__level__in=["DEBUG", "WARN"])+ .order_by("-event_time")+ .values("data")[:1]+ )++ return FileExecution.objects.filter(workflow_execution_id=execution_id).annotate(+ latest_log_data=Subquery(latest_log_subquery)+ )diff --git a/backend/workflow_manager/workflow_v2/execution_log_internal_views.py b/backend/workflow_manager/workflow_v2/execution_log_internal_views.pyindex dd839eb55..708376dcc 100644--- a/backend/workflow_manager/workflow_v2/execution_log_internal_views.py+++ b/backend/workflow_manager/workflow_v2/execution_log_internal_views.py@@ -35,7 +35,9 @@ class GetWorkflowExecutionsByIdsAPIView(APIView):try:execution_ids = request.data.get("execution_ids", [])- executions = WorkflowExecution.objects.filter(id__in=execution_ids)+ executions = WorkflowExecution.objects.filter(+ id__in=execution_ids+ ).select_related("workflow")execution_data = {}for execution in executions:@@ -76,7 +78,7 @@ class GetFileExecutionsByIdsAPIView(APIView):file_executions = WorkflowFileExecution.objects.filter(id__in=file_execution_ids- )+ ).select_related("workflow_execution")file_execution_data = {}for file_execution in file_executions: