import { useLocaleContext } from "@arcblock/ux/lib/Locale/context"; import { CallMade, InfoOutlined, TrendingUp } from "@mui/icons-material"; import { Box, Card, CardContent, Grid, styled, Tooltip, Typography } from "@mui/material"; import BigNumber from "bignumber.js"; import prettyMs from "pretty-ms"; import { useEffect, useState } from "react"; import { joinURL } from "ufo"; import Metric from "./components/metric.js"; import formatNumber from "./utils/format-number.ts"; import { origin } from "./utils/index.js"; export interface UsageSummaryProps { title?: string; totalCount?: number; successCount?: number; failCount?: number; totalToken?: number; totalCost?: number; totalDuration?: number; maxLatency?: number; minLatency?: number; avgLatency?: number; llmSuccessCount?: number; llmTotalCount?: number; llmTotalDuration?: number; } interface SummaryCardProps { title: string; value: string; trend?: string; trendDescription?: string; tooltip?: React.ReactNode; showInfoIcon?: boolean; infoTooltip?: string; } export function SummaryCard({ title, value = "-", trend = undefined, trendDescription = undefined, tooltip = undefined, showInfoIcon = false, infoTooltip = undefined, }: SummaryCardProps) { const getTrendColor = (trendStr?: string) => { if (!trendStr) return "text.secondary"; const isPositive = trendStr.startsWith("+"); const isNegative = trendStr.startsWith("-"); if (isPositive) return "success.main"; if (isNegative) return "error.main"; return "text.secondary"; }; return ( {title} {showInfoIcon && ( )} {tooltip ? ( {value || "-"} ) : ( {value || "-"} )} {trend && ( {trend} {trendDescription && ` ${trendDescription}`} )} ); } function UsageSummary({ totalToken = 0, totalCost = 0, totalCount = 0, successCount = 0, title = undefined, totalDuration = 0, llmTotalCount = 0, llmSuccessCount = 0, llmTotalDuration = 0, }: UsageSummaryProps) { const { t, locale } = useLocaleContext(); const metrics = [ { title: t("analytics.totalToken"), value: formatNumber(new BigNumber(totalToken || 0).dp(2).toNumber(), locale), icon: , }, { title: t("analytics.totalCost"), value: `$${Number(totalCost || 0).toFixed(6)}`, icon: , }, { title: t("analytics.totalCount"), value: `${totalCount}${t("analytics.active")} ${successCount}`, icon: , }, { title: t("analytics.totalDuration"), value: prettyMs(totalDuration), icon: , }, { title: t("analytics.llmTotalCount"), value: `${llmTotalCount}${t("analytics.active")} ${llmSuccessCount}`, icon: , }, { title: t("analytics.llmTotalDuration"), value: prettyMs(llmTotalDuration), icon: , }, ]; return (
{title && ( {title} )} {metrics.map(({ icon, ...x }) => ( ))}
); } export default () => { const { t } = useLocaleContext(); const [usageSummary, setUsageSummary] = useState({ totalToken: 0, totalCost: 0, }); useEffect(() => { const abortController = new AbortController(); (async () => { try { const res = await fetch(joinURL(origin, "/api/trace/tree/summary"), { signal: abortController.signal, }); const data = (await res.json()) as UsageSummaryProps; setUsageSummary(data); } catch (error) { if ((error as Error)?.name !== "AbortError") { console.error("Failed to fetch usage summary:", error); } } })(); return () => { abortController.abort(); }; }, []); return ( ); }; const Div = styled(Typography)` .section { display: flex; flex-direction: column; margin-bottom: 64px; .section__header { font-size: 2rem; font-weight: bold; margin-bottom: 24px; } } `;