import { formatIdReference, formatPercent } from '../../helpers/safeDisplay'; import React, { useState, useCallback } from 'react'; import { Badge, Empty, Progress, Skeleton, Tag, Tooltip, theme } from 'antd'; import { ArrowDown } from 'lucide-react'; import { ConsoleCard, ConsoleTable, } from '@/components/console/ConsolePrimitives'; import OrgCacheHourlySheet from './OrgCacheHourlySheet'; // 并发占用比对应的色阶:接近上限提前变黄,打满变红。 const WARN_RATIO = 0.8; // 缓存命中率色阶。90% 是与客户约定的 KPI 线(严格口径),所以绿/黄的分界放在这里; // 70% 以下基本意味着会话结构出了问题(深轮首触、子代理扇出),标红提示排查。 const CACHE_GOOD = 90; const CACHE_WARN = 70; // 色阶取主题语义 token(成功 / 警告 / 错误 / 主色),阈值不变 const cacheRateColor = (rate, token) => { if (rate >= CACHE_GOOD) return token.colorSuccess; if (rate >= CACHE_WARN) return token.colorWarning; return token.colorError; }; const concurrencyColor = (ratio, token) => { if (ratio >= 1) return token.colorError; if (ratio >= WARN_RATIO) return token.colorWarning; return token.colorPrimary; }; // 可点击的表头:点一下换排序维度,始终降序(后端只支持降序 TOP-N)。 const SortableTitle = ({ label, field, sortBy, onSort }) => { const active = sortBy === field; return ( onSort(field)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); onSort(field); } }} > {label} ); }; const OrgRealtimeUsage = ({ data, loading, sortBy, onSortChange, limit = 10, title, icon, t, }) => { const { token } = theme.useToken(); const items = data?.items || []; const windowSeconds = data?.window_seconds || 60; // 抽屉状态放在这里而不是提到页面级:它完全由本表的行点击驱动, // 页面不需要知道它的存在。 const [detailOrg, setDetailOrg] = useState(null); const closeDetail = useCallback(() => setDetailOrg(null), []); const columns = [ { title: t('企业'), dataIndex: 'org_name', key: 'org_name', width: 220, fixed: 'left', render: (text, record) => { const orgReference = formatIdReference(record.org_id); return (
{text || `${t('企业')} ${orgReference}`} {orgReference}
); }, }, { title: ( ), dataIndex: 'concurrency', key: 'concurrency', width: 220, render: (val, record) => { const current = val || 0; const limit = record.max_concurrency || 0; // 0 表示未配置上限:不画进度条(没有分母,画出来就是误导)。 if (limit <= 0) { return (
{current.toLocaleString()} / {t('不限制')}
); } const ratio = current / limit; const color = concurrencyColor(ratio, token); return (
{current.toLocaleString()} / {limit.toLocaleString()} {record.rejected > 0 && ( {t('限流中')} )}
); }, }, { title: ( ), dataIndex: 'rpm', key: 'rpm', width: 140, align: 'right', render: (val) => ( {(val || 0).toLocaleString()} ), }, { title: ( ), dataIndex: 'tpm', key: 'tpm', width: 180, align: 'right', render: (val) => ( {(val || 0).toLocaleString()} ), }, { // 这一列不可排序:后端的 TOP-N 是按内存计数器(并发/RPM/TPM)排的, // 命中率是取完 TOP-N 之后再补的落库数据,没有「按命中率取前 N 名」这回事。 title: ( {t('1h 缓存命中率')} ), dataIndex: 'cache_hit_rate', key: 'cache_hit_rate', width: 170, align: 'right', render: (val, record) => { // 分母为 0 = 这一小时没有可统计的 token,不是「命中率 0%」。 // 两者混为一谈会让刚接入的企业看起来像缓存全丢。 if (!record.cache_tokens) { return ( -- ); } const rate = val || 0; return ( setDetailOrg(record)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); setDetailOrg(record); } }} > {formatPercent(rate, 1)} › ); }, }, ]; return (
{icon} {title} {t('取前 {{limit}} 名', { limit })}
{data && data.enforced === false && ( {t('仅观测')} )} {data?.degraded ? ( {t('单实例口径')} } /> ) : ( {t('实时 · 近 {{seconds}} 秒滚动', { seconds: windowSeconds, })} } /> )}
} styles={{ body: { padding: 0 } }} > {loading && items.length === 0 ? (
) : ( <> ), }} /> {/* 口径脚注:并发/RPM 只盖同步转发,TPM 还滞后一个请求时长。 不写出来的话,运营会拿这张表去对账单,然后发现对不上。 */}
{t( '口径:仅统计 /v1、/v1beta 同步转发接口;Midjourney、Suno、视频与异步图片任务提交后在后台执行,不计入本表。', )}{' '} {t( 'RPM 含被限流拒绝的请求;TPM 在请求结束时才计入,长流式请求会在结束的那一秒形成毛刺。', )}{' '} {t( '缓存命中率的窗口是近 1 小时(读企业小时聚合表),与左侧 60 秒窗口的指标不同步。', )}
)}
); }; export default OrgRealtimeUsage;