diff --git a/src/app/(dashboard)/dashboard/settings/components/CacheStatsCard.tsx b/src/app/(dashboard)/dashboard/settings/components/CacheStatsCard.tsx index 7876a67fdf..4798fbba2e 100644 --- a/src/app/(dashboard)/dashboard/settings/components/CacheStatsCard.tsx +++ b/src/app/(dashboard)/dashboard/settings/components/CacheStatsCard.tsx @@ -1,6 +1,6 @@ "use client"; -import { useState, useEffect } from "react"; +import { useState, useEffect, useCallback } from "react"; import { Card } from "@/shared/components"; import { useTranslations } from "next-intl"; @@ -33,19 +33,26 @@ interface CacheMetrics { lastUpdated: string; } +const REFRESH_INTERVAL_MS = 10_000; +const REFRESH_INTERVAL_SECONDS = REFRESH_INTERVAL_MS / 1000; + export default function CacheStatsCard() { const [metrics, setMetrics] = useState(null); const [resetting, setResetting] = useState(false); - const t = useTranslations("settings"); + const t = useTranslations("cache"); - const fetchMetrics = () => { + const fetchMetrics = useCallback(() => { fetch("/api/settings/cache-metrics") .then((r) => r.json()) .then(setMetrics) .catch(() => {}); - }; + }, []); - useEffect(fetchMetrics, []); + useEffect(() => { + void fetchMetrics(); + const id = setInterval(() => void fetchMetrics(), REFRESH_INTERVAL_MS); + return () => clearInterval(id); + }, [fetchMetrics]); const handleReset = async () => { setResetting(true); @@ -73,15 +80,20 @@ export default function CacheStatsCard() { > insights -

Prompt Cache Metrics

+

{t("cacheMetrics")}

+ +
+ + {t("autoRefresh", { seconds: REFRESH_INTERVAL_SECONDS })} + +
- {metrics ? ( @@ -89,11 +101,11 @@ export default function CacheStatsCard() { {/* Overview Stats */}
-

Total Requests

+

{t("totalRequests")}

{metrics.totalRequests}

-

With Cache Control

+

{t("withCacheControl")}

{metrics.requestsWithCacheControl}

@@ -103,19 +115,19 @@ export default function CacheStatsCard() { {/* Token Stats */}
-

Input Tokens

+

{t("inputTokens")}

{metrics.totalInputTokens.toLocaleString()}

-

Cached Tokens (Read)

+

{t("cachedTokensRead")}

{metrics.totalCachedTokens.toLocaleString()}

-

Cache Creation (Write)

+

{t("cacheCreationWrite")}

{metrics.totalCacheCreationTokens.toLocaleString()}

@@ -126,8 +138,8 @@ export default function CacheStatsCard() {
-

Cache Reuse Ratio

-

Cached tokens / Total input tokens

+

{t("cacheReuseRatio")}

+

{t("cacheReuseRatioDesc")}

{cacheHitRate.toFixed(1)}%

@@ -143,13 +155,13 @@ export default function CacheStatsCard() { {/* Savings */}
-

Tokens Saved

+

{t("tokensSaved")}

{metrics.tokensSaved.toLocaleString()}

-

Est. Cost Saved

+

{t("estCostSaved")}

${metrics.estimatedCostSaved.toFixed(4)}

@@ -159,7 +171,7 @@ export default function CacheStatsCard() { {/* By Provider */} {Object.keys(metrics.byProvider).length > 0 && (
-

By Provider

+

{t("byProvider")}

{Object.entries(metrics.byProvider).map(([provider, stats]) => { const providerCacheRate = @@ -171,17 +183,19 @@ export default function CacheStatsCard() { >
{provider} - {stats.requests} reqs + + {stats.requests} {t("requestsShort")} +
- - In: {stats.inputTokens.toLocaleString()} + + {t("inputShort")}: {stats.inputTokens.toLocaleString()} - - Cached: {stats.cachedTokens.toLocaleString()} + + {t("cachedShort")}: {stats.cachedTokens.toLocaleString()} - - Write: {stats.cacheCreationTokens.toLocaleString()} + + {t("writeShort")}: {stats.cacheCreationTokens.toLocaleString()} {providerCacheRate.toFixed(0)}% @@ -195,7 +209,7 @@ export default function CacheStatsCard() { )}
) : ( -

Loading cache metrics...

+

{t("loading")}

)}
diff --git a/src/i18n/messages/en.json b/src/i18n/messages/en.json index 43463d7cba..0a1ce25ac6 100644 --- a/src/i18n/messages/en.json +++ b/src/i18n/messages/en.json @@ -2937,6 +2937,19 @@ "cacheHitRate": "Cache Hit Rate", "cachedTokens": "Cached Tokens", "cacheCreationTokens": "Cache Creation Tokens", + "cacheMetrics": "Prompt Cache Metrics", + "withCacheControl": "With Cache Control", + "cachedTokensRead": "Cached Tokens (Read)", + "cacheCreationWrite": "Cache Creation (Write)", + "cacheReuseRatio": "Cache Reuse Ratio", + "cacheReuseRatioDesc": "Cached tokens / Total input tokens", + "estCostSaved": "Est. Cost Saved", + "requestsShort": "reqs", + "inputShort": "In", + "cachedShort": "Cached", + "writeShort": "Write", + "resetting": "Resetting...", + "resetMetrics": "Reset Metrics", "byProvider": "Breakdown by Provider", "provider": "Provider", "requests": "Requests",