LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
UN-3248 [FEAT] Expose total_pages_processed in execution API response (#1801)
Praveen Kumar34620029ece2
diff --git a/backend/usage_v2/helper.py b/backend/usage_v2/helper.pyindex 8cefb3b40..0b211514a 100644--- a/backend/usage_v2/helper.py+++ b/backend/usage_v2/helper.py@@ -2,6 +2,7 @@ import loggingfrom datetime import date, datetime, time, timedeltafrom typing import Any+from account_usage.models import PageUsagefrom django.db.models import Count, QuerySet, Sumfrom django.utils import timezonefrom rest_framework.exceptions import APIException, ValidationError@@ -23,6 +24,33 @@ logger = logging.getLogger(__name__)class UsageHelper:+ @staticmethod+ def get_aggregated_pages_processed(+ run_id: str | None = None,+ run_ids: list[str] | None = None,+ ) -> int | None:+ """Retrieve aggregated pages processed for given run_id(s).++ Provide either a single run_id or a list of run_ids.++ Args:+ run_id: Single file execution ID.+ run_ids: List of file execution IDs.++ Returns:+ int | None: Total pages processed, or None if no records found.+ """+ if run_id:+ queryset = PageUsage.objects.filter(run_id=run_id)+ elif run_ids:+ queryset = PageUsage.objects.filter(run_id__in=run_ids)+ else:+ return None+ if not queryset.exists():+ return None+ result = queryset.aggregate(total_pages=Sum("pages_processed"))+ return result.get("total_pages")+@staticmethoddef get_aggregated_token_count(run_id: str) -> dict:"""Retrieve aggregated token counts for the given run_id.diff --git a/backend/workflow_manager/endpoint_v2/destination.py b/backend/workflow_manager/endpoint_v2/destination.pyindex 0e63e129a..093c64b90 100644--- a/backend/workflow_manager/endpoint_v2/destination.py+++ b/backend/workflow_manager/endpoint_v2/destination.py@@ -694,6 +694,9 @@ class DestinationConnector(BaseConnector):# Combine both metadataworkflow_metadata["usage"] = usage_metadata+ workflow_metadata["total_pages_processed"] = (+ UsageHelper.get_aggregated_pages_processed(run_id=file_execution_id)+ )return workflow_metadatadiff --git a/backend/workflow_manager/execution/serializer/execution.py b/backend/workflow_manager/execution/serializer/execution.pyindex 04065cdcc..0f440cc8a 100644--- a/backend/workflow_manager/execution/serializer/execution.py+++ b/backend/workflow_manager/execution/serializer/execution.py@@ -10,6 +10,7 @@ class ExecutionSerializer(serializers.ModelSerializer):pipeline_name = serializers.SerializerMethodField()successful_files = serializers.SerializerMethodField()failed_files = serializers.SerializerMethodField()+ aggregated_total_pages_processed = serializers.SerializerMethodField()execution_time = serializers.ReadOnlyField(source="pretty_execution_time")class Meta:@@ -31,3 +32,7 @@ class ExecutionSerializer(serializers.ModelSerializer):def get_failed_files(self, obj: WorkflowExecution) -> int:"""Return the count of failed executed files"""return obj.file_executions.filter(status=ExecutionStatus.ERROR.value).count()++ def get_aggregated_total_pages_processed(self, obj: WorkflowExecution) -> int | None:+ """Return the total pages processed across all file executions."""+ return obj.aggregated_total_pages_processeddiff --git a/backend/workflow_manager/workflow_v2/models/execution.py b/backend/workflow_manager/workflow_v2/models/execution.pyindex 5c7a8ba3a..0ec5b12dd 100644--- a/backend/workflow_manager/workflow_v2/models/execution.py+++ b/backend/workflow_manager/workflow_v2/models/execution.py@@ -9,6 +9,7 @@ from django.db.models import Q, QuerySet, Sumfrom pipeline_v2.models import Pipelinefrom tags.models import Tagfrom usage_v2.constants import UsageKeys+from usage_v2.helper import UsageHelperfrom usage_v2.models import Usagefrom utils.common_utils import CommonUtilsfrom utils.models.base_model import BaseModel@@ -259,6 +260,22 @@ class WorkflowExecution(BaseModel):return total_cost+ @property+ def aggregated_total_pages_processed(self) -> int | None:+ """Retrieve aggregated total pages processed for this execution.++ Returns:+ int | None: Total pages processed across all file executions,+ or None if no page usage data exists.+ """+ file_execution_ids = list(self.file_executions.values_list("id", flat=True))+ if not file_execution_ids:+ return None++ return UsageHelper.get_aggregated_pages_processed(+ run_ids=[str(fid) for fid in file_execution_ids]+ )+@propertydef is_completed(self) -> bool:return ExecutionStatus.is_completed(self.status)