/** * Compression Analytics Tab * * Shows compression request stats from call_logs (request_type = 'compression'), * mode breakdown, provider breakdown, and cost/token savings summary. */ "use client"; import { useEffect, useState } from "react"; import { useTranslations } from "next-intl"; interface CompressionAnalyticsSummary { totalRequests: number; totalTokensSaved: number; avgSavingsPct: number; avgDurationMs: number; byMode: Record; byProvider: Record; last24h: Array<{ hour: string; count: number; tokensSaved: number }>; validationFallbacks: number; realUsage: { requestsWithReceipts: number; promptTokens: number; completionTokens: number; totalTokens: number; cacheReadTokens: number; cacheWriteTokens: number; estimatedUsdSaved: number; bySource: Record; }; } function StatCard({ icon, label, value, sub, }: { icon: string; label: string; value: string | number; sub?: string; }) { return (
{icon} {label}
{value}
{sub &&
{sub}
}
); } function ModeBar({ mode, count, total, tokensSaved, }: { mode: string; count: number; total: number; tokensSaved: number; }) { const pct = total > 0 ? Math.round((count / total) * 100) : 0; return (
{mode} {count} requests · {tokensSaved.toLocaleString()} tokens saved
{pct}%
); } function ProviderBar({ provider, count, total, tokensSaved, }: { provider: string; count: number; total: number; tokensSaved: number; }) { const pct = total > 0 ? Math.round((count / total) * 100) : 0; return (
{provider} {count} requests · {tokensSaved.toLocaleString()} tokens saved
{pct}%
); } export default function CompressionAnalyticsTab() { const t = useTranslations("analytics"); const [stats, setStats] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [since, setSince] = useState<"24h" | "7d" | "30d" | "all">("24h"); useEffect(() => { fetch(`/api/analytics/compression?since=${since}`) .then((r) => r.json()) .then((d) => { setStats(d); setLoading(false); }) .catch((e) => { setError(e.message); setLoading(false); }); }, [since]); if (loading) { return (
progress_activity Loading compression analytics…
); } if (error || !stats) { return (
compress {error || "No compression data yet."}

Compression requests will appear here after the first request via /v1/chat/completions with compression enabled.

); } const modes = Object.entries(stats.byMode).sort(([, a], [, b]) => b.count - a.count); const providers = Object.entries(stats.byProvider).sort(([, a], [, b]) => b.count - a.count); // Calculate max tokens for hourly chart scaling const maxTokensPerHour = Math.max(...stats.last24h.map((h) => h.tokensSaved), 1); const maxCountPerHour = Math.max(...stats.last24h.map((h) => h.count), 1); return (
{/* Time Range Selector */}
{(["24h", "7d", "30d", "all"] as const).map((range) => ( ))}
{/* KPI Cards */}
{stats.realUsage.requestsWithReceipts > 0 && (

receipt_long Real Usage Receipts

{t("compressionAnalyticsPromptTokens")}
{stats.realUsage.promptTokens.toLocaleString()}
{t("compressionAnalyticsCompletionTokens")}
{stats.realUsage.completionTokens.toLocaleString()}
{t("compressionAnalyticsTotalTokens")}
{stats.realUsage.totalTokens.toLocaleString()}
{t("compressionAnalyticsCacheTokens")}
{( (stats.realUsage.cacheReadTokens ?? 0) + (stats.realUsage.cacheWriteTokens ?? 0) ).toLocaleString()}
Sources
{Object.entries(stats.realUsage.bySource) .map(([source, count]) => `${source}: ${count}`) .join(", ")}
)} {/* Mode Breakdown */} {modes.length > 0 && (

tune Mode Breakdown

{modes.map(([mode, data]) => ( ))}
)} {/* Provider Breakdown */} {providers.length > 0 && (

hub Provider Breakdown

{providers.map(([prov, data]) => ( ))}
)} {/* Last 24h Hourly Chart (CSS-only height-based bars) */} {stats.last24h.length > 0 && (

show_chart Last 24 Hours (Activity)

{stats.last24h.map((entry, idx) => { const countPct = (entry.count / maxCountPerHour) * 100; const tokenPct = (entry.tokensSaved / maxTokensPerHour) * 100; return (
{entry.count}
{entry.hour.substring(11, 13)}
); })}
Max requests/hour: {maxCountPerHour}
Max tokens/hour: {maxTokensPerHour.toLocaleString()}
)} {/* Empty state */} {stats.totalRequests === 0 && (
compress

{t("compressionAnalyticsNoDataYet")}

Use POST /v1/chat/completions with compression configuration to start tracking compression analytics.

)} {/* Info note */}
info Compression analytics: Token savings tracked per mode (off, lite, standard, aggressive, ultra, RTK, stacked), engine, compression combo, and provider. Hover over charts for details. Use the time selector to view different time periods.
); }