import { useMemo } from 'react' import { useQuery } from '@tanstack/react-query' import { AlertTriangle, HeartPulse, Timer } from 'lucide-react' import { useTranslation } from 'react-i18next' import { cn } from '@/lib/utils' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table' import { GroupBadge } from '@/components/group-badge' import { getPerfMetrics } from '@/features/performance-metrics/api' import { formatLatency, formatThroughput, formatUptimePct, } from '@/features/performance-metrics/lib/format' import type { PerformanceGroup } from '@/features/performance-metrics/types' import { type UptimeDayPoint } from '../lib/mock-stats' import type { PricingModel } from '../types' import { LatencyTrendChart, UptimeTrendChart } from './model-details-charts' import { UptimeSparkline } from './model-details-uptime-sparkline' function StatCard(props: { icon: React.ComponentType<{ className?: string }> label: string value: React.ReactNode hint?: string intent?: 'default' | 'warning' | 'success' }) { const Icon = props.icon const intent = props.intent ?? 'default' return (
{props.label} {props.value} {props.hint && ( {props.hint} )}
) } type PerformanceRow = { group: string avg_ttft_ms: number avg_latency_ms: number success_rate: number avg_tps: number } function toLatencySeries(groups: PerformanceGroup[]) { const byTs = new Map() for (const group of groups) { for (const point of group.series) { if (point.avg_ttft_ms <= 0) continue const current = byTs.get(point.ts) ?? [] current.push(point.avg_ttft_ms) byTs.set(point.ts, current) } } return Array.from(byTs.entries()) .sort(([a], [b]) => a - b) .map(([ts, values]) => ({ timestamp: new Date(ts * 1000).toISOString(), group: 'latency', ttft_ms: Math.round( values.reduce((sum, value) => sum + value, 0) / values.length ), })) } function toUptimeSeries(groups: PerformanceGroup[]): UptimeDayPoint[] { const byTs = new Map() for (const group of groups) { for (const point of group.series) { const current = byTs.get(point.ts) ?? { rates: [], incidents: 0 } if (Number.isFinite(point.success_rate)) { current.rates.push(point.success_rate) if (point.success_rate < 100) current.incidents += 1 } byTs.set(point.ts, current) } } return Array.from(byTs.entries()) .sort(([a], [b]) => a - b) .map(([ts, value]) => { const uptime = value.rates.length > 0 ? value.rates.reduce((sum, rate) => sum + rate, 0) / value.rates.length : 0 return { date: new Date(ts * 1000).toISOString(), uptime_pct: Math.round(uptime * 100) / 100, incidents: value.incidents, outage_minutes: 0, } }) } function toGroupUptimeSeries(group: PerformanceGroup): UptimeDayPoint[] { return group.series.map((point) => ({ date: new Date(point.ts * 1000).toISOString(), uptime_pct: Math.round(point.success_rate * 100) / 100, incidents: point.success_rate < 100 ? 1 : 0, outage_minutes: 0, })) } function average( rows: PerformanceRow[], field: 'avg_ttft_ms' | 'avg_latency_ms' ) { const values = rows.map((row) => row[field]).filter((value) => value > 0) if (values.length === 0) return 0 return Math.round( values.reduce((sum, value) => sum + value, 0) / values.length ) } export function ModelDetailsPerformance(props: { model: PricingModel }) { const { t } = useTranslation() const metricsQuery = useQuery({ queryKey: ['perf-metrics', props.model.model_name], queryFn: () => getPerfMetrics(props.model.model_name, 24), staleTime: 60 * 1000, }) const groups = useMemo( () => metricsQuery.data?.data.groups ?? [], [metricsQuery.data] ) const performances = useMemo( () => groups.map((group) => ({ group: group.group, avg_ttft_ms: group.avg_ttft_ms, avg_latency_ms: group.avg_latency_ms, success_rate: group.success_rate, avg_tps: group.avg_tps, })), [groups] ) const latencySeries = useMemo(() => toLatencySeries(groups), [groups]) const uptimeSeries = useMemo(() => toUptimeSeries(groups), [groups]) const uptimeByGroup = useMemo>(() => { const map: Record = {} for (const group of groups) { map[group.group] = toGroupUptimeSeries(group) } return map }, [groups]) if (metricsQuery.isLoading || performances.length === 0) { return (
{t('Performance data is not yet available for this model.')}
) } const tpsValues = performances .map((p) => p.avg_tps) .filter((value) => value > 0) const avgTps = tpsValues.length > 0 ? tpsValues.reduce((sum, value) => sum + value, 0) / tpsValues.length : 0 const avgLatency = average(performances, 'avg_latency_ms') const successRates = performances .map((perf) => perf.success_rate) .filter((value) => Number.isFinite(value)) const successRate = successRates.length > 0 ? successRates.reduce((sum, value) => sum + value, 0) / successRates.length : 0 const incidentCount = uptimeSeries.reduce((s, p) => s + p.incidents, 0) let intent: 'default' | 'warning' | 'success' = 'warning' if (successRate >= 99.9) { intent = 'success' } else if (successRate >= 99) { intent = 'default' } const headerCellClass = 'text-muted-foreground py-2 text-[10px] font-medium tracking-wider uppercase' return (
0 ? t('{{count}} incidents in the last 24 hours', { count: incidentCount, }) : t('No incidents in the last 24 hours') } intent={intent} />
{t('Group')} TPS {t('Average TTFT')} {t('Average latency')} {t('Success rate')} {performances.map((perf) => ( {formatThroughput(perf.avg_tps)} {formatLatency(perf.avg_ttft_ms)} {formatLatency(perf.avg_latency_ms)} ))}
0 ? t( 'Request success rate; {{incidents}} incident buckets in the last 24 hours', { incidents: incidentCount, } ) : t('Request success rate sampled over the last 24 hours') } accent={ incidentCount > 0 ? ( {t('{{count}} incidents', { count: incidentCount, })} ) : null } />
) } function SectionHeader(props: { icon: React.ComponentType<{ className?: string }> title: string description?: string accent?: React.ReactNode }) { const Icon = props.icon return (
{props.title}
{props.description && (

{props.description}

)}
{props.accent && (
{props.accent}
)}
) }