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-1798: Merge subscription dashboard into metrics dashboard (#1822)
Athulba95ab67961e
diff --git a/backend/dashboard_metrics/README.md b/backend/dashboard_metrics/README.mdindex 5e16b398a..fc40e9f55 100644--- a/backend/dashboard_metrics/README.md+++ b/backend/dashboard_metrics/README.md@@ -588,23 +588,26 @@ Users toggle metrics via Filter dropdown.| Token count | `SUM(usage.total_tokens)` per execution || Time ago | Relative time from `workflow_file_execution.created_at` |-### LLM Usage Tab+### Usage by Deployment Tab-#### LLM Usage by Workflow (table)+#### Usage by Deployment (table with sub-tabs)-- **Component**: `LLMUsageTable` → `frontend/src/components/metrics-dashboard/LLMUsageTable.jsx`-- **Hook**: `useWorkflowTokenUsage(startDate, endDate)`-- **API**: `GET /metrics/workflow-token-usage/?start_date=...&end_date=...`-- **DB**: `usage` table (live query, `usage_type='llm'`, grouped by `workflow_id`)+- **Component**: `DeploymentUsageTable` → `frontend/src/components/metrics-dashboard/LLMUsageTable.jsx`+- **Hook**: `useDeploymentUsage(deploymentType, startDate, endDate)`+- **API**: `GET /metrics/workflow-token-usage/?start_date=...&end_date=...&deployment_type=API`+- **DB**: `workflow_execution` → deployment table → `usage` (3-table join per type)++Sub-tabs: API, ETL, TASK, WF (each fetched independently with per-tab caching)| Column | Source ||---|---|-| Workflow | `workflow.workflow_name` (resolved via `usage.workflow_id` FK) |+| Deployment | Name from `api_deployment.display_name`, `pipeline.pipeline_name`, or `workflow.workflow_name` || Tokens | `SUM(usage.total_tokens)` || LLM Calls | `COUNT(usage.id)` |+| Executions | `COUNT(DISTINCT workflow_execution.id)` || Cost | `SUM(usage.cost_in_dollars)` |-Sorted by tokens descending. Only rows with non-empty `workflow_id` appear.+Sorted by tokens descending.### Data Hooksdiff --git a/backend/dashboard_metrics/serializers.py b/backend/dashboard_metrics/serializers.pyindex 844a8c173..602f031cd 100644--- a/backend/dashboard_metrics/serializers.py+++ b/backend/dashboard_metrics/serializers.py@@ -4,6 +4,7 @@ from datetime import timedeltafrom django.utils import timezonefrom rest_framework import serializers+from workflow_manager.execution.enum import ExecutionEntityfrom .models import EventMetricsHourly@@ -72,6 +73,35 @@ class MetricsQuerySerializer(serializers.Serializer):return attrs+class DeploymentUsageQuerySerializer(MetricsQuerySerializer):+ """Serializer for deployment usage query parameters."""++ DEPLOYMENT_TYPE_CHOICES = [(e.value, e.name) for e in ExecutionEntity]++ deployment_type = serializers.ChoiceField(+ choices=DEPLOYMENT_TYPE_CHOICES,+ default=ExecutionEntity.API.value,+ help_text="Deployment type filter: API, ETL, TASK, or WF.",+ )++ def validate(self, attrs):+ """Validate and enforce 30-day max range for deployment queries."""+ attrs = super().validate(attrs)++ # Enforce 30-day max range for deployment usage queries+ max_range = timedelta(days=30)+ if attrs["end_date"] - attrs["start_date"] > max_range:+ attrs["start_date"] = attrs["end_date"] - max_range+ attrs["range_truncated"] = True+ else:+ attrs["range_truncated"] = False++ # Normalize deployment_type to uppercase+ attrs["deployment_type"] = attrs["deployment_type"].upper()++ return attrs++class EventMetricsHourlySerializer(serializers.ModelSerializer):"""Model serializer for EventMetricsHourly records."""diff --git a/backend/dashboard_metrics/services.py b/backend/dashboard_metrics/services.pyindex f2a743784..e382f6261 100644--- a/backend/dashboard_metrics/services.py+++ b/backend/dashboard_metrics/services.py@@ -14,10 +14,11 @@ from typing import Anyfrom account_usage.models import PageUsagefrom account_v2.models import Organizationfrom api_v2.models import APIDeployment-from django.db.models import CharField, Count, OuterRef, Q, Subquery, Sum+from django.db.models import CharField, Count, OuterRef, Subquery, Sumfrom django.db.models.functions import Cast, Coalesce, TruncDay, TruncHour, TruncWeekfrom pipeline_v2.models import Pipelinefrom usage_v2.models import Usage+from workflow_manager.execution.enum import ExecutionEntityfrom workflow_manager.file_execution.models import WorkflowFileExecutionfrom workflow_manager.workflow_v2.models.execution import WorkflowExecutionfrom workflow_manager.workflow_v2.models.workflow import Workflow@@ -695,6 +696,7 @@ class MetricsQueryService:results.append({"id": str(execution.id),+ "execution_id": str(execution.workflow_execution.id),"type": exec_type,"file_name": execution.file_name,"status": execution.status,@@ -734,67 +736,259 @@ class MetricsQueryService:return results@staticmethod- def get_workflow_token_usage(- organization_id: str,+ def _get_deployment_names(+ deployment_type: str,+ ) -> tuple[dict[str, str], dict[str, Any]]:+ """Get deployment ID->name mapping and execution filter for a type.++ Uses the default model manager which auto-filters by the current+ user's organization via DefaultOrganizationManagerMixin.++ Returns:+ (dep_names, exec_filter) or ({}, {}) if type is invalid.+ """+ if deployment_type == ExecutionEntity.API.value:+ dep_list = list(APIDeployment.objects.all().values_list("id", "display_name"))+ exec_filter = {"pipeline_id__in": [d[0] for d in dep_list]}+ elif deployment_type in (+ ExecutionEntity.ETL.value,+ ExecutionEntity.TASK.value,+ ):+ dep_list = list(+ Pipeline.objects.filter(+ pipeline_type=deployment_type,+ ).values_list("id", "pipeline_name")+ )+ exec_filter = {"pipeline_id__in": [d[0] for d in dep_list]}+ elif deployment_type == ExecutionEntity.WORKFLOW.value:+ dep_list = list(Workflow.objects.all().values_list("id", "workflow_name"))+ exec_filter = {+ "pipeline_id__isnull": True,+ "workflow_id__in": [d[0] for d in dep_list],+ }+ else:+ return {}, {}++ dep_names = {str(dep_id): name for dep_id, name in dep_list}+ return dep_names, exec_filter++ @staticmethod+ def _build_exec_stats(+ exec_rows: list[dict], dep_field: str+ ) -> tuple[dict[str, str], dict[str, dict[str, Any]]]:+ """Build exec->deployment mapping and per-deployment stats."""+ exec_to_dep: dict[str, str] = {}+ dep_exec_stats: dict[str, dict[str, Any]] = {}+ status_counters = {+ ExecutionStatus.COMPLETED.value: "completed",+ ExecutionStatus.ERROR.value: "failed",+ }++ def _default_stats() -> dict[str, Any]:+ return {+ "total": 0,+ "completed": 0,+ "failed": 0,+ "first_execution_at": None,+ "last_execution_at": None,+ }++ for e in exec_rows:+ exec_id = str(e["id"])+ dep_id = str(e[dep_field])+ exec_to_dep[exec_id] = dep_id++ stats = dep_exec_stats.setdefault(dep_id, _default_stats())+ stats["total"] += 1++ counter_key = status_counters.get(str(e["status"] or ""))+ if counter_key:+ stats[counter_key] += 1++ ts = e["created_at"]+ if not ts:+ continue+ first = stats["first_execution_at"]+ last = stats["last_execution_at"]+ stats["first_execution_at"] = ts if first is None else min(first, ts)+ stats["last_execution_at"] = ts if last is None else max(last, ts)++ return exec_to_dep, dep_exec_stats++ @staticmethod+ def _get_pages_by_deployment(+ exec_rows: list[dict], exec_to_dep: dict[str, str]+ ) -> dict[str, int]:+ """Query pages processed, aggregated by deployment.++ Two queries:+ 1. WorkflowFileExecution → map file_exec_id to workflow_execution_id+ 2. PageUsage → aggregate pages by file_exec_id, then map to deployment+ """+ exec_ids = [e["id"] for e in exec_rows]+ if not exec_ids:+ return {}++ # Map file_execution_id → workflow_execution_id+ file_exec_map: dict[str, str] = {}+ for fid, weid in WorkflowFileExecution.objects.filter(+ workflow_execution_id__in=exec_ids,+ ).values_list("id", "workflow_execution_id"):+ file_exec_map[str(fid)] = str(weid)++ if not file_exec_map:+ return {}++ page_rows = (+ PageUsage.objects.filter(+ run_id__in=list(file_exec_map.keys()),+ )+ .values("run_id")+ .annotate(pages=Sum("pages_processed"))+ )++ dep_pages: dict[str, int] = {}+ for row in page_rows:+ we_id = file_exec_map.get(row["run_id"])+ dep_id = exec_to_dep.get(we_id) if we_id else None+ if dep_id:+ dep_pages[dep_id] = dep_pages.get(dep_id, 0) + (row["pages"] or 0)+ return dep_pages++ @staticmethod+ def _aggregate_usage_by_deployment(+ usage_rows: list[dict], exec_to_dep: dict[str, str]+ ) -> dict[str, dict[str, Any]]:+ """Aggregate LLM token usage by deployment."""+ dep_agg: dict[str, dict[str, Any]] = {}+ for row in usage_rows:+ dep_id = exec_to_dep.get(row["execution_id"])+ if not dep_id:+ continue+ if dep_id not in dep_agg:+ dep_agg[dep_id] = {+ "total_tokens": 0,+ "total_cost": 0,+ "call_count": 0,+ }+ agg = dep_agg[dep_id]+ agg["total_tokens"] += row["total_tokens"] or 0+ agg["total_cost"] += row["total_cost"] or 0+ agg["call_count"] += row["call_count"] or 0+ return dep_agg++ @staticmethod+ def get_deployment_usage(+ deployment_type: str,start_date: datetime,end_date: datetime,) -> list[dict[str, Any]]:- """Get per-workflow LLM token usage breakdown.+ """Get LLM usage grouped by deployment.++ Joins workflow_execution -> deployment table -> usage to aggregate+ token usage per deployment. Supports 4 deployment types:+ - API: workflow_execution.pipeline_id -> api_deployment+ - ETL: workflow_execution.pipeline_id -> pipeline (type=ETL)+ - TASK: workflow_execution.pipeline_id -> pipeline (type=TASK)+ - WF: workflow_execution with no pipeline (direct workflow runs)- Aggregates total_tokens and cost_in_dollars grouped by workflow,- with workflow name resolved from the Workflow model.+ Also includes execution status counts, pages processed (via+ page_usage), and execution date range per deployment.Args:- organization_id: Organization UUID string+ deployment_type: One of API, ETL, TASK, WFstart_date: Start of date rangeend_date: End of date rangeReturns:- List of dicts with workflow_id, workflow_name, total_tokens,- total_cost, and call_count, ordered by total_tokens descending.+ List of dicts ordered by total_tokens descending."""- # Query Usage grouped by workflow_id- usage_qs = (+ # Step 1: Get deployment names and execution filter+ dep_names, exec_filter = MetricsQueryService._get_deployment_names(+ deployment_type+ )+ if not dep_names:+ return []++ # Step 2: Get executions with status and timestamp+ dep_field = (+ "workflow_id"+ if deployment_type == ExecutionEntity.WORKFLOW.value+ else "pipeline_id"+ )+ # exec_filter already scopes to org-owned deployment IDs+ exec_rows = list(+ WorkflowExecution.objects.filter(+ created_at__gte=start_date,+ created_at__lte=end_date,+ **exec_filter,+ ).values("id", dep_field, "status", "created_at")+ )+ if not exec_rows:+ return []++ # Step 3: Build exec->deployment mapping and stats+ exec_to_dep, dep_exec_stats = MetricsQueryService._build_exec_stats(+ exec_rows, dep_field+ )++ # Step 4: Query LLM usage+ # Use _get_usage_queryset() to bypass DefaultOrganizationManagerMixin+ # which auto-filters by UserContext and can return empty results.+ usage_rows = list(_get_usage_queryset().filter(- organization_id=organization_id,+ execution_id__in=list(exec_to_dep.keys()),usage_type="llm",created_at__gte=start_date,created_at__lte=end_date,)- .exclude(Q(workflow_id__isnull=True) | Q(workflow_id=""))- .values("workflow_id")+ .values("execution_id").annotate(total_tokens=Sum("total_tokens"),total_cost=Sum("cost_in_dollars"),call_count=Count("id"),)- .order_by("-total_tokens"))- # Evaluate queryset once to avoid double DB hit- usage_rows = list(usage_qs)+ # Step 5: Query pages and aggregate usage+ dep_pages = MetricsQueryService._get_pages_by_deployment(exec_rows, exec_to_dep)+ dep_agg = MetricsQueryService._aggregate_usage_by_deployment(+ usage_rows, exec_to_dep+ )- # Resolve workflow names using _base_manager to bypass- # DefaultOrganizationManagerMixin (UserContext is None in Celery tasks)- workflow_ids = [row["workflow_id"] for row in usage_rows]- workflow_names = {}- if workflow_ids:- workflows = Workflow._base_manager.filter(id__in=workflow_ids).values(- "id", "workflow_name"- )- workflow_names = {str(w["id"]): w["workflow_name"] for w in workflows}+ # Ensure all deployments with executions appear in results+ empty_agg = {"total_tokens": 0, "total_cost": 0, "call_count": 0}+ for dep_id in dep_exec_stats:+ dep_agg.setdefault(dep_id, dict(empty_agg))+ # Step 6: Build results sorted by total_tokens descendingresults = []- for row in usage_rows:- wf_id = row["workflow_id"]+ for dep_id, agg in sorted(+ dep_agg.items(), key=lambda x: x[1]["total_tokens"], reverse=True+ ):+ stats = dep_exec_stats.get(dep_id, {})results.append({- "workflow_id": str(wf_id),- "workflow_name": workflow_names.get(str(wf_id), "Unknown"),- "total_tokens": row["total_tokens"] or 0,- "total_cost": round(row["total_cost"] or 0, 4),- "call_count": row["call_count"] or 0,+ "deployment_id": dep_id,+ "deployment_name": dep_names.get(dep_id, "Unknown"),+ "total_tokens": agg["total_tokens"],+ "total_cost": round(agg["total_cost"], 4),+ "call_count": agg["call_count"],+ "execution_count": stats.get("total", 0),+ "completed_executions": stats.get("completed", 0),+ "failed_executions": stats.get("failed", 0),+ "total_pages_processed": dep_pages.get(dep_id, 0),+ "first_execution_at": (+ stats["first_execution_at"].isoformat()+ if stats.get("first_execution_at")+ else None+ ),+ "last_execution_at": (+ stats["last_execution_at"].isoformat()+ if stats.get("last_execution_at")+ else None+ ),})diff --git a/backend/dashboard_metrics/views.py b/backend/dashboard_metrics/views.pyindex 8e40a0c90..adb086dbf 100644--- a/backend/dashboard_metrics/views.py+++ b/backend/dashboard_metrics/views.py@@ -34,6 +34,7 @@ from .models import (MetricType,)from .serializers import (+ DeploymentUsageQuerySerializer,EventMetricsHourlySerializer,MetricsQuerySerializer,)@@ -863,30 +864,28 @@ class DashboardMetricsViewSet(viewsets.ReadOnlyModelViewSet):@action(detail=False, methods=["get"], url_path="workflow-token-usage")@cache_metrics_response(endpoint="workflow_token_usage")def workflow_token_usage(self, request: Request) -> Response:- """Get per-workflow LLM token usage breakdown.+ """Get LLM token usage grouped by deployment.- Returns token usage and cost aggregated per workflow for the- given date range. Cached for 1 hour; pass ?refresh=true to- bypass cache.+ Returns token usage and cost aggregated per deployment for the+ given date range and deployment type. Cached for 1 hour;+ pass ?refresh=true to bypass cache.Query Parameters:start_date: Start of date range (ISO 8601)end_date: End of date range (ISO 8601)+ deployment_type: One of API, ETL, TASK, WF (default: API)refresh: If "true", bypass cache and fetch fresh dataReturns:- 200: List of workflows with token usage- 500: Error occurred+ 200: List of deployments with token usage+ 400: Invalid parameters"""- query_serializer = MetricsQuerySerializer(data=request.query_params)- query_serializer.is_valid(raise_exception=True)- params = query_serializer.validated_data+ serializer = DeploymentUsageQuerySerializer(data=request.query_params)+ serializer.is_valid(raise_exception=True)+ params = serializer.validated_data- organization = self._get_organization()- org_id = str(organization.id)-- data = MetricsQueryService.get_workflow_token_usage(- org_id,+ data = MetricsQueryService.get_deployment_usage(+ params["deployment_type"],params["start_date"],params["end_date"],)@@ -894,7 +893,9 @@ class DashboardMetricsViewSet(viewsets.ReadOnlyModelViewSet):{"start_date": params["start_date"].isoformat(),"end_date": params["end_date"].isoformat(),- "workflows": data,+ "deployment_type": params["deployment_type"],+ "range_truncated": params["range_truncated"],+ "deployments": data,})diff --git a/frontend/src/components/logging/detailed-logs/DetailedLogs.jsx b/frontend/src/components/logging/detailed-logs/DetailedLogs.jsxindex 3522d0cd4..00723b0dd 100644--- a/frontend/src/components/logging/detailed-logs/DetailedLogs.jsx+++ b/frontend/src/components/logging/detailed-logs/DetailedLogs.jsx@@ -1,4 +1,5 @@import {+ ArrowLeftOutlined,CalendarOutlined,ClockCircleOutlined,CloseCircleFilled,@@ -23,11 +24,12 @@ import {} from "antd";import PropTypes from "prop-types";import { useEffect, useRef, useState } from "react";-import { useParams } from "react-router-dom";+import { useLocation, useNavigate, useParams } from "react-router-dom";import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate";import { useExceptionHandler } from "../../../hooks/useExceptionHandler";import { useAlertStore } from "../../../store/alert-store";+import { useSessionStore } from "../../../store/session-store";import "./DetailedLogs.css";import {formattedDateTime,@@ -91,8 +93,12 @@ const DetailedLogs = () => {const axiosPrivate = useAxiosPrivate();const { setAlertDetails } = useAlertStore();const handleException = useExceptionHandler();+ const navigate = useNavigate();+ const location = useLocation();+ const { sessionDetails } = useSessionStore();const { getUrl } = useRequestUrl();const copyToClipboard = useCopyToClipboard();+ const cameFromDashboard = location.state?.from === "dashboard";const [executionDetails, setExecutionDetails] = useState();const [executionFiles, setExecutionFiles] = useState();@@ -454,7 +460,19 @@ const DetailedLogs = () => {<div className="detailed-logs-container"><div className="detailed-logs-header"><Typography.Title className="logs-title" level={4}>- {type} Execution {id}+ <Button+ type="text"+ shape="circle"+ icon={<ArrowLeftOutlined />}+ onClick={() =>+ navigate(+ cameFromDashboard+ ? `/${sessionDetails?.orgName}/dashboard`+ : `/${sessionDetails?.orgName}/logs`,+ )+ }+ />+ {type} Execution ID {id}<ButtonclassName="copy-btn-outlined"icon={<CopyOutlined />}diff --git a/frontend/src/components/metrics-dashboard/LLMUsageTable.jsx b/frontend/src/components/metrics-dashboard/LLMUsageTable.jsxindex 997123c67..114228b8c 100644--- a/frontend/src/components/metrics-dashboard/LLMUsageTable.jsx+++ b/frontend/src/components/metrics-dashboard/LLMUsageTable.jsx@@ -1,6 +1,26 @@-import { InfoCircleOutlined, ReloadOutlined } from "@ant-design/icons";-import { Button, Card, Empty, Spin, Table, Tooltip, Typography } from "antd";+import {+ CheckCircleOutlined,+ CloseCircleOutlined,+ InfoCircleOutlined,+ ReloadOutlined,+} from "@ant-design/icons";+import {+ Alert,+ Button,+ Card,+ Empty,+ Spin,+ Table,+ Tabs,+ Tag,+ Tooltip,+ Typography,+} from "antd";import PropTypes from "prop-types";+import { useState } from "react";++import { ApiDeployments, ETLIcon, Task, Workflows } from "../../assets/index";+import { useDeploymentUsage } from "../../hooks/useMetricsData";import "./MetricsDashboard.css";@@ -8,9 +28,9 @@ const { Text } = Typography;const columns = [{- title: "Workflow",- dataIndex: "workflow_name",- key: "workflow_name",+ title: "Deployment",+ dataIndex: "deployment_name",+ key: "deployment_name",ellipsis: true,render: (text) => <Text strong>{text}</Text>,},@@ -18,8 +38,8 @@ const columns = [title: (<span>Tokens{" "}- <Tooltip title="Total LLM tokens consumed by this workflow">- <InfoCircleOutlined style={{ color: "#8c8c8c", fontSize: 12 }} />+ <Tooltip title="Total LLM tokens consumed by this deployment">+ <InfoCircleOutlined className="llm-usage-info-icon" /></Tooltip></span>),@@ -28,7 +48,7 @@ const columns = [sorter: (a, b) => a.total_tokens - b.total_tokens,defaultSortOrder: "descend",render: (value) => (value || 0).toLocaleString(),- width: 140,+ width: 130,},{title: "LLM Calls",@@ -36,7 +56,47 @@ const columns = [key: "call_count",sorter: (a, b) => a.call_count - b.call_count,render: (value) => (value || 0).toLocaleString(),- width: 120,+ width: 100,+ },+ {+ title: "Executions",+ dataIndex: "execution_count",+ key: "execution_count",+ sorter: (a, b) => a.execution_count - b.execution_count,+ render: (_, record) => {+ const total = record.execution_count || 0;+ const completed = record.completed_executions || 0;+ const failed = record.failed_executions || 0;+ return (+ <span>+ {total.toLocaleString()}{" "}+ {completed > 0 && (+ <Tooltip title={`${completed} completed`}>+ <Tag color="success" className="llm-usage-tag-compact">+ <CheckCircleOutlined /> {completed}+ </Tag>+ </Tooltip>+ )}+ {failed > 0 && (+ <Tooltip title={`${failed} failed`}>+ <Tag color="error">+ <CloseCircleOutlined /> {failed}+ </Tag>+ </Tooltip>+ )}+ </span>+ );+ },+ width: 180,+ },+ {+ title: "Pages",+ dataIndex: "total_pages_processed",+ key: "total_pages_processed",+ sorter: (a, b) =>+ (a.total_pages_processed || 0) - (b.total_pages_processed || 0),+ render: (value) => (value || 0).toLocaleString(),+ width: 100,},{title: "Cost",@@ -44,93 +104,151 @@ const columns = [key: "total_cost",sorter: (a, b) => a.total_cost - b.total_cost,render: (value) => `$${(value || 0).toFixed(4)}`,- width: 120,+ width: 110,+ },+ {+ title: "Last Run",+ dataIndex: "last_execution_at",+ key: "last_execution_at",+ render: (value) => (value ? value.split("T")[0] : "-"),+ width: 110,},];-function LLMUsageTable({ data, loading, onRefresh }) {- const refreshButton = onRefresh ? (+function DeploymentUsageTable({ startDate, endDate }) {+ const [activeType, setActiveType] = useState("API");++ const { data, loading, error, refetch } = useDeploymentUsage(+ activeType,+ startDate,+ endDate,+ );++ const handleTabChange = (key) => {+ setActiveType(key);+ };++ const deploymentTabs = [+ {+ key: "API",+ label: "API Deployments",+ icon: (+ <ApiDeployments+ className={`log-tab-icon ${activeType === "API" ? "active" : ""}`}+ />+ ),+ },+ {+ key: "ETL",+ label: "ETL Pipelines",+ icon: (+ <ETLIcon+ className={`log-tab-icon ${activeType === "ETL" ? "active" : ""}`}+ />+ ),+ },+ {+ key: "TASK",+ label: "Task Pipelines",+ icon: (+ <Task+ className={`log-tab-icon ${activeType === "TASK" ? "active" : ""}`}+ />+ ),+ },+ {+ key: "WF",+ label: "Workflows",+ icon: (+ <Workflows+ className={`log-tab-icon ${activeType === "WF" ? "active" : ""}`}+ />+ ),+ },+ ];++ const refreshButton = (<Tooltip title="Refresh (bypasses cache)"><Buttonicon={<ReloadOutlined />}size="small"- onClick={onRefresh}+ onClick={refetch}loading={loading}/></Tooltip>- ) : null;+ );- if (loading) {- return (- <Card className="metrics-chart-card llm-usage-card">- <div className="llm-usage-header">- <Text strong className="llm-usage-title">- LLM Usage by Workflow- </Text>- {refreshButton}- </div>+ const renderContent = () => {+ if (loading) {+ return (<div className="metrics-loading"><Spin /></div>- </Card>- );- }+ );+ }++ if (error) {+ return (+ <Alert+ message="Failed to load usage data"+ description={error}+ type="error"+ showIcon+ className="llm-usage-error-alert"+ />+ );+ }++ if (!data?.deployments?.length) {+ return <Empty description="No LLM usage data for this period" />;+ }- if (!data?.workflows?.length) {return (- <Card className="metrics-chart-card llm-usage-card">- <div className="llm-usage-header">- <Text strong className="llm-usage-title">- LLM Usage by Workflow- </Text>- {refreshButton}- </div>- <Empty description="No LLM usage data for this period" />- </Card>+ <Table+ dataSource={data.deployments}+ columns={columns}+ rowKey="deployment_id"+ size="middle"+ pagination={false}+ scroll={{ x: 900, y: 400 }}+ className="llm-usage-table"+ />);- }+ };return (<Card className="metrics-chart-card llm-usage-card"><div className="llm-usage-header"><Text strong className="llm-usage-title">- LLM Usage by Workflow+ Usage by Deployment</Text>{refreshButton}</div>- <Table- dataSource={data.workflows}- columns={columns}- rowKey="workflow_id"- size="middle"- pagination={false}- scroll={{ y: 400 }}- className="llm-usage-table"+ <Tabs+ items={deploymentTabs}+ activeKey={activeType}+ onChange={handleTabChange}+ size="small"+ className="deployment-type-tabs"/>+ {data?.range_truncated && (+ <Text type="secondary" className="llm-usage-subtitle">+ Date range was limited to the last 30 days+ </Text>+ )}+ {renderContent()}</Card>);}-LLMUsageTable.propTypes = {- data: PropTypes.shape({- workflows: PropTypes.arrayOf(- PropTypes.shape({- workflow_id: PropTypes.string.isRequired,- workflow_name: PropTypes.string.isRequired,- total_tokens: PropTypes.number,- total_cost: PropTypes.number,- call_count: PropTypes.number,- }),- ),- }),- loading: PropTypes.bool,- onRefresh: PropTypes.func,+DeploymentUsageTable.propTypes = {+ startDate: PropTypes.string,+ endDate: PropTypes.string,};-LLMUsageTable.defaultProps = {- data: null,- loading: false,- onRefresh: null,+DeploymentUsageTable.defaultProps = {+ startDate: null,+ endDate: null,};-export { LLMUsageTable };+export { DeploymentUsageTable };diff --git a/frontend/src/components/metrics-dashboard/MetricsChart.jsx b/frontend/src/components/metrics-dashboard/MetricsChart.jsxindex 855656d40..80dad597e 100644--- a/frontend/src/components/metrics-dashboard/MetricsChart.jsx+++ b/frontend/src/components/metrics-dashboard/MetricsChart.jsx@@ -72,7 +72,13 @@ function formatValue(value) {* @return {string} Human-readable metric name*/function formatMetricName(metric) {- return metric.replace(/_/g, " ").replace(/\b\w/g, (l) => l.toUpperCase());+ return metric+ .replaceAll("_", " ")+ .replaceAll(/\b\w/g, (l) => l.toUpperCase())+ .replaceAll("Api", "API")+ .replaceAll("Etl", "ETL")+ .replaceAll("Llm", "LLM")+ .replaceAll("Hitl", "HITL");}/**@@ -497,13 +503,13 @@ function HITLChart({ data, loading }) {/><BardataKey="hitl_reviews"- name="hitl_reviews"+ name="HITL Reviews"fill={HITL_COLORS.hitl_reviews}radius={[4, 4, 0, 0]}/><BardataKey="hitl_completions"- name="hitl_completions"+ name="HITL Completions"fill={HITL_COLORS.hitl_completions}radius={[4, 4, 0, 0]}/>diff --git a/frontend/src/components/metrics-dashboard/MetricsDashboard.css b/frontend/src/components/metrics-dashboard/MetricsDashboard.cssindex 36ec49c8e..ed7e8e4f1 100644--- a/frontend/src/components/metrics-dashboard/MetricsDashboard.css+++ b/frontend/src/components/metrics-dashboard/MetricsDashboard.css@@ -157,6 +157,39 @@padding: 40px;}+/* Dashboard Empty State */+.metrics-empty-state {+ display: flex;+ flex-direction: column;+ align-items: center;+ justify-content: center;+ padding: 80px 24px;+ text-align: center;+}++.metrics-empty-state-icon {+ width: 120px;+ height: 120px;+ margin-bottom: 24px;+ opacity: 0.6;+}++.metrics-empty-state-title {+ margin-bottom: 8px !important;+ color: #262626;+}++.metrics-empty-state-text {+ font-size: 14px;+ line-height: 22px;+ max-width: 360px;+ margin-bottom: 24px;+}++.metrics-empty-state-actions {+ margin-top: 8px;+}+/* Chart Components */.metrics-chart-card {height: 100%;@@ -228,10 +261,19 @@}.recent-activity-item {- padding: 10px 0 !important;+ padding: 10px 24px !important;border-bottom: 1px solid #f0f0f0;}+.recent-activity-clickable {+ cursor: pointer;+ transition: background-color 0.2s;+}++.recent-activity-clickable:hover {+ background-color: #f5f5f5;+}+.recent-activity-item:last-child {border-bottom: none;}@@ -278,6 +320,43 @@font-size: 12px;}+/* Subscription Usage Tab */+.subscription-usage-tab {+ max-width: 600px;+ margin: 0 auto;+}++.subscription-plan-card {+ text-align: center;+ padding: 20px;+ background: #e6f4ff;+ border: 1px solid #91caff;+ border-radius: 8px;+ max-width: 400px;+ margin: 0 auto;+}++.subscription-plan-name {+ font-size: 16px;+ font-weight: 600;+ color: #262626;+ margin-bottom: 4px;+}++.subscription-plan-pages {+ font-size: 14px;+ color: #595959;+}++.subscription-overdue-tag {+ margin-bottom: 12px;+}++/* Deployment Type Tabs */+.deployment-type-tabs {+ padding-left: 24px;+}+/* LLM Usage Table */.llm-usage-card .ant-card-body {padding: 0;@@ -296,6 +375,10 @@color: #262626;}+.llm-usage-table {+ padding: 0 24px;+}+.llm-usage-table .ant-table-thead > tr > th {background: #fafafa;color: #595959;@@ -316,3 +399,21 @@.llm-usage-table .ant-table-tbody > tr > td:not(:first-child) {font-variant-numeric: tabular-nums;}++.llm-usage-tag-compact {+ margin-inline-end: 2px;+}++.llm-usage-error-alert {+ margin: 16px 24px;+}++.llm-usage-subtitle {+ font-size: 12px;+ padding: 0 24px;+}++.llm-usage-info-icon {+ color: #8c8c8c;+ font-size: 12px;+}diff --git a/frontend/src/components/metrics-dashboard/MetricsDashboard.jsx b/frontend/src/components/metrics-dashboard/MetricsDashboard.jsxindex 17f390b37..41bcc2fff 100644--- a/frontend/src/components/metrics-dashboard/MetricsDashboard.jsx+++ b/frontend/src/components/metrics-dashboard/MetricsDashboard.jsx@@ -1,8 +1,9 @@import {- BarChartOutlined,+ CreditCardOutlined,DashboardOutlined,FileSearchOutlined,ReloadOutlined,+ RocketOutlined,SlackOutlined,ThunderboltOutlined,} from "@ant-design/icons";@@ -17,14 +18,18 @@ import {Typography,} from "antd";import dayjs from "dayjs";-import { useCallback, useMemo, useState } from "react";+import { useCallback, useEffect, useMemo, useState } from "react";+import { useNavigate } from "react-router-dom";+import { EmptyPlaceholder } from "../../assets";+import { evictExpiredCache } from "../../helpers/metricsCache";import {useMetricsOverview,useRecentActivity,- useWorkflowTokenUsage,+ useSubscriptionUsage,} from "../../hooks/useMetricsData";-import { LLMUsageTable } from "./LLMUsageTable";+import { useSessionStore } from "../../store/session-store";+import { DeploymentUsageTable } from "./LLMUsageTable";import { HITLChart, PagesChart, TrendAnalysisChart } from "./MetricsChart";import { MetricsSummary } from "./MetricsSummary";import { RecentActivity } from "./RecentActivity";@@ -34,28 +39,53 @@ import "./MetricsDashboard.css";// Cloud-only: Plan banner with subscription detailslet PlanBanner;try {- PlanBanner =- require("../../plugins/unstract-subscription/components/PlanBanner.jsx").PlanBanner;+ const mod = await import(+ "../../plugins/unstract-subscription/components/PlanBanner.jsx"+ );+ PlanBanner = mod.PlanBanner;} catch {// Plugin unavailable - no banner on OSS}-const { Title } = Typography;+// Cloud-only: Subscription usage tab+let SubscriptionUsageTab;+try {+ const mod = await import(+ "../../plugins/unstract-subscription/components/SubscriptionUsageTab.jsx"+ );+ SubscriptionUsageTab = mod.SubscriptionUsageTab;+} catch {+ // Plugin unavailable - no subscription tab on OSS+}+const { RangePicker } = DatePicker;function MetricsDashboard() {+ const navigate = useNavigate();+ const { sessionDetails } = useSessionStore();+ const orgName = sessionDetails?.orgName;++ // Evict expired cache entries on mount+ useEffect(() => {+ evictExpiredCache();+ }, []);++ // Stabilise "now" to start-of-minute so remounts within the same+ // minute produce identical ISO strings → same cache keys → cache hits.+ const [stableNow] = useState(() => dayjs().startOf("minute"));+// Date range state (default: last 30 days)- const [dateRange, setDateRange] = useState([- dayjs().subtract(30, "day"),- dayjs(),+ const [dateRange, setDateRange] = useState(() => [+ stableNow.subtract(30, "day"),+ stableNow,]);// Fixed 30-day range for activity charts (independent of date picker)const chartStart = useMemo(- () => dayjs().subtract(30, "day").toISOString(),- [],+ () => stableNow.subtract(30, "day").toISOString(),+ [stableNow],);- const chartEnd = useMemo(() => dayjs().toISOString(), []);+ const chartEnd = useMemo(() => stableNow.toISOString(), [stableNow]);// API hooks - pass date range to overview (for summary cards)const {@@ -82,15 +112,15 @@ function MetricsDashboard() {refetch: refetchActivity,} = useRecentActivity(5);- // Per-workflow LLM token usage (uses same date range as summary cards)+ // Subscription usage (cloud-only, returns null on OSS)const {- data: tokenUsageData,- loading: tokenUsageLoading,- refetch: refetchTokenUsage,- } = useWorkflowTokenUsage(- dateRange[0]?.toISOString(),- dateRange[1]?.toISOString(),- );+ data: subscriptionData,+ loading: subscriptionLoading,+ refetch: refetchSubscription,+ } = useSubscriptionUsage();++ // Active tab (controls date picker visibility)+ const [activeTab, setActiveTab] = useState("overview");// Handle date range changeconst handleDateChange = useCallback((dates) => {@@ -104,8 +134,21 @@ function MetricsDashboard() {refetchOverview();refetchChart();refetchActivity();- refetchTokenUsage();- }, [refetchOverview, refetchChart, refetchActivity, refetchTokenUsage]);+ refetchSubscription();+ }, [refetchOverview, refetchChart, refetchActivity, refetchSubscription]);++ // Check if all data sources are empty (no activity at all)+ const isDataLoading = overviewLoading || chartLoading || activityLoading;+ const hasNoData = useMemo(() => {+ if (isDataLoading) {+ return false;+ }+ const hasOverview =+ overviewData?.totals?.some((m) => m.total_value > 0) || false;+ const hasChart = chartData?.daily_trend?.length > 0 || false;+ const hasActivity = activityData?.activity?.length > 0 || false;+ return !hasOverview && !hasChart && !hasActivity;+ }, [isDataLoading, overviewData, chartData, activityData]);const tabItems = [{@@ -115,7 +158,32 @@ function MetricsDashboard() {<DashboardOutlined /> Overview</span>),- children: (+ children: hasNoData ? (+ <div className="metrics-empty-state">+ <EmptyPlaceholder className="metrics-empty-state-icon" />+ <Typography.Title level={4} className="metrics-empty-state-title">+ No activity yet+ </Typography.Title>+ <Typography.Text+ type="secondary"+ className="metrics-empty-state-text"+ >+ Run a workflow or API deployment to see your metrics here.+ </Typography.Text>+ <Space className="metrics-empty-state-actions">+ <Button+ type="primary"+ icon={<RocketOutlined />}+ onClick={() => navigate(`/${orgName}/workflows`)}+ >+ Create Workflow+ </Button>+ <Button onClick={() => navigate(`/${orgName}/tools`)}>+ Open Prompt Studio+ </Button>+ </Space>+ </div>+ ) : (<><Row gutter={[16, 16]}><Col xs={24}>@@ -141,16 +209,15 @@ function MetricsDashboard() {key: "llm-usage",label: (<span>- <ThunderboltOutlined /> LLM Usage+ <ThunderboltOutlined /> Usage by Deployment</span>),children: (<Row gutter={[16, 16]}><Col xs={24}>- <LLMUsageTable- data={tokenUsageData}- loading={tokenUsageLoading}- onRefresh={refetchTokenUsage}+ <DeploymentUsageTable+ startDate={dateRange[0]?.toISOString()}+ endDate={dateRange[1]?.toISOString()}/></Col></Row>@@ -158,14 +225,40 @@ function MetricsDashboard() {},];+ // Cloud-only: add subscription usage tab+ if (SubscriptionUsageTab) {+ tabItems.push({+ key: "subscription",+ label: (+ <span>+ <CreditCardOutlined /> Subscription+ </span>+ ),+ children: (+ <SubscriptionUsageTab+ data={subscriptionData}+ loading={subscriptionLoading}+ overviewData={overviewData}+ overviewLoading={overviewLoading}+ />+ ),+ });+ }+return (<div className="metrics-dashboard"><div className="metrics-topbar"><div className="metrics-topbar-left">- <BarChartOutlined className="metrics-topbar-icon" />- <Title level={4} style={{ margin: 0 }}>+ <Typography+ style={{+ fontWeight: 600,+ fontSize: "16px",+ display: "inline",+ lineHeight: "24px",+ }}+ >Dashboard- </Title>+ </Typography></div><Space className="metrics-topbar-right">@@ -200,7 +293,7 @@ function MetricsDashboard() {</Space></div>- {PlanBanner && <PlanBanner />}+ {PlanBanner && subscriptionData && <PlanBanner />}{overviewError && (<Alert@@ -215,30 +308,38 @@ function MetricsDashboard() {<Tabsitems={tabItems}- defaultActiveKey="overview"+ activeKey={activeTab}+ onChange={setActiveTab}tabBarExtraContent={<Space>- <RangePicker- value={dateRange}- onChange={handleDateChange}- disabledDate={(current) => current && current > dayjs()}- allowClear={false}- size="middle"- presets={[- {- label: "Last 7 Days",- value: [dayjs().subtract(7, "day"), dayjs()],- },- {- label: "Last 30 Days",- value: [dayjs().subtract(30, "day"), dayjs()],- },- {- label: "Last 90 Days",- value: [dayjs().subtract(90, "day"), dayjs()],- },- ]}- />+ {activeTab !== "subscription" && (+ <RangePicker+ value={dateRange}+ onChange={handleDateChange}+ disabledDate={(current) => current && current > dayjs()}+ allowClear={false}+ size="middle"+ presets={[+ {+ label: "Last 7 Days",+ value: [dayjs().subtract(7, "day"), dayjs()],+ },+ {+ label: "Last 30 Days",+ value: [dayjs().subtract(30, "day"), dayjs()],+ },+ // 90-day preset only for overview (deployment usage capped at 30 days)+ ...(activeTab === "llm-usage"+ ? []+ : [+ {+ label: "Last 90 Days",+ value: [dayjs().subtract(90, "day"), dayjs()],+ },+ ]),+ ]}+ />+ )}<Button icon={<ReloadOutlined />} onClick={handleRefresh} /></Space>}diff --git a/frontend/src/components/metrics-dashboard/RecentActivity.jsx b/frontend/src/components/metrics-dashboard/RecentActivity.jsxindex 874c5e360..f8bbb3864 100644--- a/frontend/src/components/metrics-dashboard/RecentActivity.jsx+++ b/frontend/src/components/metrics-dashboard/RecentActivity.jsx@@ -12,6 +12,8 @@ import { Card, Empty, List, Spin, Tag, Tooltip, Typography } from "antd";import dayjs from "dayjs";import relativeTime from "dayjs/plugin/relativeTime";import PropTypes from "prop-types";+import { useNavigate } from "react-router-dom";+import { useSessionStore } from "../../store/session-store";import "./MetricsDashboard.css";@@ -49,16 +51,19 @@ const TYPE_CONFIG = {label: "ETL Pipeline",icon: <BranchesOutlined />,color: "#722ed1",+ logType: "ETL",},api: {label: "API Request",icon: <ApiOutlined />,color: "#1890ff",+ logType: "API",},workflow: {label: "Workflow",icon: <PlayCircleOutlined />,color: "#52c41a",+ logType: "WF",},};@@ -69,6 +74,18 @@ const TYPE_CONFIG = {* @return {JSX.Element} The rendered recent activity component.*/function RecentActivity({ data, loading }) {+ const navigate = useNavigate();+ const { sessionDetails } = useSessionStore();+ const orgName = sessionDetails?.orgName;++ const handleActivityClick = (item) => {+ if (!item.execution_id || !orgName) return;+ const typeConfig = TYPE_CONFIG[item.type] || TYPE_CONFIG.workflow;+ navigate(`/${orgName}/logs/${typeConfig.logType}/${item.execution_id}`, {+ state: { from: "dashboard" },+ });+ };+if (loading) {return (<Card title="Recent Activity" className="metrics-chart-card">@@ -100,7 +117,18 @@ function RecentActivity({ data, loading }) {const typeConfig = TYPE_CONFIG[item.type] || TYPE_CONFIG.workflow;return (- <List.Item className="recent-activity-item">+ <List.Item+ className="recent-activity-item recent-activity-clickable"+ tabIndex={0}+ role="button"+ onClick={() => handleActivityClick(item)}+ onKeyDown={(e) => {+ if (e.key === "Enter" || e.key === " ") {+ e.preventDefault();+ handleActivityClick(item);+ }+ }}+ ><div className="recent-activity-content"><div className="recent-activity-header"><span@@ -142,6 +170,7 @@ RecentActivity.propTypes = {activity: PropTypes.arrayOf(PropTypes.shape({id: PropTypes.string.isRequired,+ execution_id: PropTypes.string,type: PropTypes.oneOf(["etl", "api", "workflow"]).isRequired,file_name: PropTypes.string.isRequired,status: PropTypes.string.isRequired,diff --git a/frontend/src/components/metrics-dashboard/index.js b/frontend/src/components/metrics-dashboard/index.jsindex 4893ecedb..3ed2d3fdf 100644--- a/frontend/src/components/metrics-dashboard/index.js+++ b/frontend/src/components/metrics-dashboard/index.js@@ -1,4 +1,4 @@-export { LLMUsageTable } from "./LLMUsageTable";+export { DeploymentUsageTable } from "./LLMUsageTable";export { PagesChart, TrendAnalysisChart } from "./MetricsChart";export { MetricsDashboard } from "./MetricsDashboard";export { MetricsSummary } from "./MetricsSummary";diff --git a/frontend/src/components/navigations/side-nav-bar/SideNavBar.jsx b/frontend/src/components/navigations/side-nav-bar/SideNavBar.jsxindex 74f6c467b..346efa579 100644--- a/frontend/src/components/navigations/side-nav-bar/SideNavBar.jsx+++ b/frontend/src/components/navigations/side-nav-bar/SideNavBar.jsx@@ -57,7 +57,6 @@ try {let unstractSubscriptionPlan;let unstractSubscriptionPlanStore;-let dashboardSideMenuItem;let UNSTRACT_SUBSCRIPTION_PLANS;try {unstractSubscriptionPlanStore = await import(@@ -66,7 +65,6 @@ try {const unstractSubscriptionConstants = await import("../../../plugins/unstract-subscription/helper/constants");- dashboardSideMenuItem = unstractSubscriptionConstants?.dashboardSideMenuItem;UNSTRACT_SUBSCRIPTION_PLANS =unstractSubscriptionConstants?.UNSTRACT_SUBSCRIPTION_PLANS;} catch {@@ -452,19 +450,14 @@ const SideNavBar = ({ collapsed, setCollapsed }) => {},];- if (dashboardSideMenuItem) {- unstractMenuItems[1].subMenu.unshift(dashboardSideMenuItem(orgName));- }-- // Add metrics dashboard menu item (available for both OSS and cloud)+ // Dashboard menu item (available for both OSS and cloud)unstractMenuItems[1].subMenu.unshift({id: 2.0,title: "Dashboard",- tag: "New",description: "View platform usage metrics and analytics",image: DashboardIcon,- path: `/${orgName}/metrics`,- active: globalThis.location.pathname.startsWith(`/${orgName}/metrics`),+ path: `/${orgName}/dashboard`,+ active: globalThis.location.pathname.startsWith(`/${orgName}/dashboard`),});// If selectedProduct is verticals and menu is null, don't show any sidebar itemsdiff --git a/frontend/src/helpers/metricsCache.js b/frontend/src/helpers/metricsCache.jsindex 6d182c942..2acf1ff56 100644--- a/frontend/src/helpers/metricsCache.js+++ b/frontend/src/helpers/metricsCache.js@@ -24,8 +24,18 @@ const CACHE_TTL = {*/function buildCacheKey(endpoint, params) {const sortedKeys = Object.keys(params).sort((a, b) => a.localeCompare(b));- const paramStr = sortedKeys.map((k) => `${k}=${params[k]}`).join("&");- // Use simple encoding to avoid btoa issues with unicode+ const paramStr = sortedKeys+ .map((k) => {+ let v = params[k];+ // Truncate ISO datetime strings to the minute so that+ // "2026-03-03T15:21:47.123Z" and "2026-03-03T15:21:02.456Z"+ // produce the same cache key within a 1-minute window.+ if (typeof v === "string" && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(v)) {+ v = v.slice(0, 16); // "2026-03-03T15:21"+ }+ return `${k}=${v}`;+ })+ .join("&");const hash = paramStr ? `_${encodeURIComponent(paramStr)}` : "";return `${CACHE_PREFIX}${endpoint}${hash}`;}@@ -85,6 +95,31 @@ function setCache(endpoint, params = {}, data) {}}+/**+ * Remove expired metrics cache entries.+ * Call on dashboard mount to prevent localStorage bloat from old date ranges.+ */+function evictExpiredCache() {+ try {+ const keys = Object.keys(localStorage).filter((k) =>+ k.startsWith(CACHE_PREFIX),+ );+ keys.forEach((k) => {+ try {+ const { expiry } = JSON.parse(localStorage.getItem(k));+ if (!expiry || Date.now() > expiry) {+ localStorage.removeItem(k);+ }+ } catch {+ // Malformed cache entry — remove it+ localStorage.removeItem(k);+ }+ });+ } catch (error) {+ console.warn("Cache eviction error:", error);+ }+}+/*** Clear all metrics cache entries.* Useful when user manually refreshes or on logout.@@ -130,4 +165,11 @@ function getCacheInfo(endpoint, params = {}) {}}-export { getCached, setCache, clearMetricsCache, getCacheInfo, CACHE_TTL };+export {+ getCached,+ setCache,+ clearMetricsCache,+ evictExpiredCache,+ getCacheInfo,+ CACHE_TTL,+};diff --git a/frontend/src/hooks/useMetricsData.js b/frontend/src/hooks/useMetricsData.jsindex 7b16aade3..f12d6c177 100644--- a/frontend/src/hooks/useMetricsData.js+++ b/frontend/src/hooks/useMetricsData.js@@ -76,7 +76,7 @@ function useMetricsOverview(startDate = null, endDate = null) {setLoading(false);setError(null);}- }, [orgId, startDate, endDate]); // eslint-disable-line+ }, [orgId, startDate, endDate]); // eslint-disable-line react-hooks/exhaustive-deps// refetch bypasses cacheconst refetch = useCallback(() => fetchOverview(true), [fetchOverview]);@@ -127,21 +127,22 @@ function useRecentActivity(limit = 10) {setLoading(false);setError(null);}- }, [orgId, limit]); // eslint-disable-line+ }, [orgId, limit]); // eslint-disable-line react-hooks/exhaustive-depsreturn { data, loading, error, refetch: fetchActivity };}/**- * Hook for fetching per-workflow LLM token usage.- * Uses localStorage caching with 1-hour TTL. Supports manual refresh- * that bypasses both frontend and backend caches.+ * Hook for fetching LLM token usage grouped by deployment type.+ * Uses localStorage caching with 1-hour TTL per deployment type.+ * Supports manual refresh that bypasses both frontend and backend caches.*+ * @param {string} deploymentType - One of "API", "ETL", "TASK", "WF"* @param {string} startDate - Start date (ISO 8601)* @param {string} endDate - End date (ISO 8601)* @return {Object} { data, loading, error, refetch }*/-function useWorkflowTokenUsage(startDate, endDate) {+function useDeploymentUsage(deploymentType, startDate, endDate) {const [data, setData] = useState(null);const [loading, setLoading] = useState(true);const [error, setError] = useState(null);@@ -151,17 +152,18 @@ function useWorkflowTokenUsage(startDate, endDate) {const fetchUsage = useCallback(async (skipCache = false) => {- if (!orgId || !startDate || !endDate) {+ if (!orgId || !deploymentType || !startDate || !endDate) {setLoading(false);return;}setError(null);- const cacheParams = { org: orgId, startDate, endDate };+ const cacheKey = `deployment_usage_${deploymentType}`;+ const cacheParams = { org: orgId, deploymentType, startDate, endDate };// Check cache first (unless skip requested)if (!skipCache) {- const cached = getCached("workflow_token_usage", cacheParams);+ const cached = getCached(cacheKey, cacheParams);if (cached) {setData(cached);setLoading(false);@@ -175,33 +177,34 @@ function useWorkflowTokenUsage(startDate, endDate) {const params = new URLSearchParams();params.append("start_date", startDate);params.append("end_date", endDate);+ params.append("deployment_type", deploymentType);if (skipCache) {params.append("refresh", "true");}const url = `/api/v1/unstract/${orgId}/metrics/workflow-token-usage/?${params}`;const response = await axiosPrivate.get(url);setData(response.data);- setCache("workflow_token_usage", cacheParams, response.data);+ setCache(cacheKey, cacheParams, response.data);} catch (err) {setError(- err.response?.data?.message || "Failed to fetch workflow token usage",+ err.response?.data?.message || "Failed to fetch deployment usage",);} finally {setLoading(false);}},- [axiosPrivate, orgId, startDate, endDate],+ [axiosPrivate, orgId, deploymentType, startDate, endDate],);useEffect(() => {- if (orgId && startDate && endDate) {+ if (orgId && deploymentType && startDate && endDate) {fetchUsage();} else {setData(null);setLoading(false);setError(null);}- }, [orgId, startDate, endDate]); // eslint-disable-line+ }, [orgId, deploymentType, startDate, endDate]); // eslint-disable-line react-hooks/exhaustive-deps// refetch bypasses both frontend and backend cachesconst refetch = useCallback(() => fetchUsage(true), [fetchUsage]);@@ -209,4 +212,77 @@ function useWorkflowTokenUsage(startDate, endDate) {return { data, loading, error, refetch };}-export { useMetricsOverview, useRecentActivity, useWorkflowTokenUsage };+/**+ * Hook for fetching subscription usage data (cloud-only).+ * Calls /subscription/usage/ and /subscription/usage/daily/ endpoints.+ * Returns null gracefully on OSS where these endpoints don't exist.+ *+ * @return {Object} { data, loading, error, refetch }+ */+function useSubscriptionUsage() {+ const [data, setData] = useState(null);+ const [loading, setLoading] = useState(true);+ const [error, setError] = useState(null);+ const axiosPrivate = useAxiosPrivate();+ const { sessionDetails } = useSessionStore();+ const orgId = sessionDetails?.orgId;++ const fetchUsage = useCallback(async () => {+ if (!orgId) {+ setLoading(false);+ return;+ }++ setLoading(true);+ setError(null);++ try {+ const baseUrl = `/api/v1/unstract/${orgId}`;+ const [totalRes, dailyRes] = await Promise.all([+ axiosPrivate.get(`${baseUrl}/subscription/usage/`),+ axiosPrivate.get(`${baseUrl}/subscription/usage/daily/`),+ ]);++ const totalPages = totalRes.data?.usage_data?.pages_processed || 0;+ const dailyUsage = (dailyRes.data?.usage_data?.usage || []).map(+ (item) => ({+ key: item.record_date,+ pagesExtracted: item.pages_processed,+ date: item.record_date,+ }),+ );++ setData({ totalPages, dailyUsage });+ } catch (err) {+ // 404 = endpoints don't exist (OSS) — not an error, just no data+ if (err.response?.status === 404) {+ setData(null);+ } else {+ setError(+ err.response?.data?.message || "Failed to fetch subscription usage",+ );+ }+ } finally {+ setLoading(false);+ }+ }, [axiosPrivate, orgId]);++ useEffect(() => {+ if (orgId) {+ fetchUsage();+ } else {+ setData(null);+ setLoading(false);+ setError(null);+ }+ }, [orgId]); // eslint-disable-line react-hooks/exhaustive-deps++ return { data, loading, error, refetch: fetchUsage };+}++export {+ useDeploymentUsage,+ useMetricsOverview,+ useRecentActivity,+ useSubscriptionUsage,+};diff --git a/frontend/src/routes/useMainAppRoutes.js b/frontend/src/routes/useMainAppRoutes.jsindex 360d6359e..9ec8bb96e 100644--- a/frontend/src/routes/useMainAppRoutes.js+++ b/frontend/src/routes/useMainAppRoutes.js@@ -36,7 +36,6 @@ let ManualReviewPage;let SimpleManualReviewPage;let ReviewLayout;let Manage;-let UnstractUsagePage;let UnstractSubscriptionPage;let UnstractSubscriptionCheck;let AgenticPromptStudio;@@ -116,10 +115,6 @@ try {"../plugins/unstract-subscription/pages/UnstractSubscriptionPage.jsx");UnstractSubscriptionPage = mod1.UnstractSubscriptionPage;- const mod2 = await import(- "../plugins/unstract-subscription/pages/UnstractUsagePage.jsx"- );- UnstractUsagePage = mod2.UnstractUsagePage;const mod3 = await import("../plugins/unstract-subscription/components/UnstractSubscriptionCheck.jsx");@@ -140,9 +135,7 @@ function useMainAppRoutes() {</Route>)}<Route path=":orgName" element={<PageLayout />}>- {UnstractUsagePage && (- <Route path="dashboard" element={<UnstractUsagePage />} />- )}+ <Route path="dashboard" element={<MetricsDashboardPage />} />{UnstractSubscriptionPage && (<Route element={<RequireAdmin />}><Route path="pricing" element={<UnstractSubscriptionPage />} />@@ -188,7 +181,6 @@ function useMainAppRoutes() {)}<Route path="logs" element={<LogsPage />} /><Route path="logs/:type/:id/" element={<LogsPage />} />- <Route path="metrics" element={<MetricsDashboardPage />} /><Routepath="settings/llms"element={<ToolsSettingsPage type="llm" />}