a57aae3043
- 合入 weiyuan 新功能前端:分佣 V2 / 伙伴工作台、智能路由、企业资金与分析、官网设置与预约演示等, semi-ui 已全部迁移到 antd 6 / @/components/ui,不重新引入 Semi - 分佣二级菜单与页头去重、伙伴入口探测(企业伙伴走企业中心「企业分佣」)、超管侧栏入口恢复 - 适配层 DatePicker / TimePicker 统一 onChange(Date|Date[]|null, 文本) 契约;生产构建剔除调试日志 - 图片链接刷新:同任务请求复用、后台刷新不弹全局错误、离页停止批量请求 - 恢复 bun.lock(与 package-lock.json 共存);docs 规范与 CHANGELOG 更新 - 停止跟踪 .env.playwright.local(本地登录凭据,已加入 .gitignore)
408 lines
13 KiB
React
408 lines
13 KiB
React
/*
|
||
Copyright (C) 2025 QuantumNous
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Affero General Public License as
|
||
published by the Free Software Foundation, either version 3 of the
|
||
License, or (at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU Affero General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Affero General Public License
|
||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
|
||
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 (
|
||
<span
|
||
role='button'
|
||
tabIndex={0}
|
||
aria-pressed={active}
|
||
className={`inline-flex items-center gap-1 cursor-pointer select-none transition-colors ${
|
||
active
|
||
? 'text-brand font-semibold'
|
||
: 'text-[var(--text-secondary)] hover:text-brand'
|
||
}`}
|
||
onClick={() => onSort(field)}
|
||
onKeyDown={(e) => {
|
||
if (e.key === 'Enter' || e.key === ' ') {
|
||
e.preventDefault();
|
||
onSort(field);
|
||
}
|
||
}}
|
||
>
|
||
{label}
|
||
<ArrowDown
|
||
aria-hidden
|
||
className={`w-3 h-3 ${active ? 'opacity-100' : 'opacity-25'}`}
|
||
/>
|
||
</span>
|
||
);
|
||
};
|
||
|
||
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) => (
|
||
<div className='flex items-center gap-2 min-w-0'>
|
||
<span className='text-sm font-medium truncate' title={text || ''}>
|
||
{text || `${t('企业')} ${record.org_id}`}
|
||
</span>
|
||
<span className='text-xs text-[var(--text-secondary)] flex-shrink-0'>
|
||
#{record.org_id}
|
||
</span>
|
||
</div>
|
||
),
|
||
},
|
||
{
|
||
title: (
|
||
<SortableTitle
|
||
label={t('并发占用')}
|
||
field='concurrency'
|
||
sortBy={sortBy}
|
||
onSort={onSortChange}
|
||
/>
|
||
),
|
||
dataIndex: 'concurrency',
|
||
key: 'concurrency',
|
||
width: 220,
|
||
render: (val, record) => {
|
||
const current = val || 0;
|
||
const limit = record.max_concurrency || 0;
|
||
// 0 表示未配置上限:不画进度条(没有分母,画出来就是误导)。
|
||
if (limit <= 0) {
|
||
return (
|
||
<div className='flex items-center gap-2'>
|
||
<span className='text-sm font-semibold text-[var(--text-primary)]'>
|
||
{current.toLocaleString()}
|
||
</span>
|
||
<span className='text-xs text-[var(--text-secondary)]'>
|
||
/ {t('不限制')}
|
||
</span>
|
||
</div>
|
||
);
|
||
}
|
||
const ratio = current / limit;
|
||
const color = concurrencyColor(ratio, token);
|
||
return (
|
||
<div className='min-w-0'>
|
||
<div className='flex items-center gap-2'>
|
||
<span className='text-sm font-semibold' style={{ color }}>
|
||
{current.toLocaleString()}
|
||
</span>
|
||
<span className='text-xs text-[var(--text-secondary)]'>
|
||
/ {limit.toLocaleString()}
|
||
</span>
|
||
{record.rejected > 0 && (
|
||
<Tooltip
|
||
title={t(
|
||
'近 {{seconds}} 秒内有 {{times}} 次请求因超过并发上限被限流(429)',
|
||
{
|
||
seconds: windowSeconds,
|
||
times: record.rejected,
|
||
},
|
||
)}
|
||
>
|
||
<Tag color='error' variant='filled'>
|
||
{t('限流中')}
|
||
</Tag>
|
||
</Tooltip>
|
||
)}
|
||
</div>
|
||
<Progress
|
||
percent={Math.min(ratio * 100, 100)}
|
||
showInfo={false}
|
||
size='small'
|
||
className='mt-1'
|
||
strokeColor={color}
|
||
/>
|
||
</div>
|
||
);
|
||
},
|
||
},
|
||
{
|
||
title: (
|
||
<SortableTitle
|
||
label='RPM'
|
||
field='rpm'
|
||
sortBy={sortBy}
|
||
onSort={onSortChange}
|
||
/>
|
||
),
|
||
dataIndex: 'rpm',
|
||
key: 'rpm',
|
||
width: 140,
|
||
align: 'right',
|
||
render: (val) => (
|
||
<span className='text-sm font-semibold text-[var(--text-primary)]'>
|
||
{(val || 0).toLocaleString()}
|
||
</span>
|
||
),
|
||
},
|
||
{
|
||
title: (
|
||
<SortableTitle
|
||
label='TPM'
|
||
field='tpm'
|
||
sortBy={sortBy}
|
||
onSort={onSortChange}
|
||
/>
|
||
),
|
||
dataIndex: 'tpm',
|
||
key: 'tpm',
|
||
width: 180,
|
||
align: 'right',
|
||
render: (val) => (
|
||
<span className='text-sm font-semibold text-brand-deep'>
|
||
{(val || 0).toLocaleString()}
|
||
</span>
|
||
),
|
||
},
|
||
{
|
||
// 这一列不可排序:后端的 TOP-N 是按内存计数器(并发/RPM/TPM)排的,
|
||
// 命中率是取完 TOP-N 之后再补的落库数据,没有「按命中率取前 N 名」这回事。
|
||
title: (
|
||
<Tooltip
|
||
title={t(
|
||
'近 1 小时的缓存命中率 = 读缓存 ÷(输入 + 写缓存 + 读缓存)。与左侧 RPM/TPM 的 60 秒窗口不同:命中率在分钟级噪声很大,按 60 秒算每次刷新都会跳。点击数字查看逐小时明细。',
|
||
)}
|
||
>
|
||
<span className='inline-flex items-center gap-1 text-[var(--text-secondary)] border-b border-dashed border-[var(--border-subtle)]'>
|
||
{t('1h 缓存命中率')}
|
||
</span>
|
||
</Tooltip>
|
||
),
|
||
dataIndex: 'cache_hit_rate',
|
||
key: 'cache_hit_rate',
|
||
width: 170,
|
||
align: 'right',
|
||
render: (val, record) => {
|
||
// 分母为 0 = 这一小时没有可统计的 token,不是「命中率 0%」。
|
||
// 两者混为一谈会让刚接入的企业看起来像缓存全丢。
|
||
if (!record.cache_tokens) {
|
||
return (
|
||
<Tooltip
|
||
title={
|
||
data?.cache_degraded
|
||
? t('缓存数据暂时不可用(小时聚合表查询失败)')
|
||
: t('近 1 小时没有可统计的 token')
|
||
}
|
||
>
|
||
<span
|
||
className='text-sm'
|
||
style={{ color: token.colorTextQuaternary }}
|
||
>
|
||
--
|
||
</span>
|
||
</Tooltip>
|
||
);
|
||
}
|
||
const rate = val || 0;
|
||
return (
|
||
<span
|
||
role='button'
|
||
tabIndex={0}
|
||
className='inline-flex items-center justify-end gap-1 cursor-pointer select-none group'
|
||
onClick={() => setDetailOrg(record)}
|
||
onKeyDown={(e) => {
|
||
if (e.key === 'Enter' || e.key === ' ') {
|
||
e.preventDefault();
|
||
setDetailOrg(record);
|
||
}
|
||
}}
|
||
>
|
||
<span
|
||
className='text-sm font-semibold group-hover:underline'
|
||
style={{ color: cacheRateColor(rate, token) }}
|
||
>
|
||
{rate.toFixed(1)}%
|
||
</span>
|
||
<span
|
||
aria-hidden
|
||
className='text-xs text-[var(--text-secondary)] group-hover:text-brand'
|
||
>
|
||
›
|
||
</span>
|
||
</span>
|
||
);
|
||
},
|
||
},
|
||
];
|
||
|
||
return (
|
||
<ConsoleCard
|
||
title={
|
||
<div className='flex items-center justify-between w-full flex-wrap gap-2'>
|
||
<div className='flex items-center gap-2'>
|
||
<span className='text-brand'>{icon}</span>
|
||
<span className='font-semibold'>{title}</span>
|
||
<span className='text-xs font-normal text-[var(--text-secondary)] ml-2'>
|
||
{t('取前 {{limit}} 名', { limit })}
|
||
</span>
|
||
</div>
|
||
<div className='flex items-center gap-3'>
|
||
{data && data.enforced === false && (
|
||
<Tooltip
|
||
title={t(
|
||
'当前只计量不拦截(ORG_CONCURRENCY_LIMIT_ENABLED=false):即使占满上限也不会返回 429',
|
||
)}
|
||
>
|
||
<Tag color='default' variant='filled'>
|
||
{t('仅观测')}
|
||
</Tag>
|
||
</Tooltip>
|
||
)}
|
||
{data?.degraded ? (
|
||
<Tooltip
|
||
title={t(
|
||
'未启用 Redis 或心跳异常,当前仅统计本实例的数据,多节点部署时会偏小',
|
||
)}
|
||
>
|
||
<Badge
|
||
status='warning'
|
||
text={
|
||
<span className='text-xs font-normal'>
|
||
{t('单实例口径')}
|
||
</span>
|
||
}
|
||
/>
|
||
</Tooltip>
|
||
) : (
|
||
<Tooltip
|
||
title={t(
|
||
'统计 {{instances}} 个实例,窗口为近 {{seconds}} 秒滚动',
|
||
{
|
||
instances: data?.instances || 1,
|
||
seconds: windowSeconds,
|
||
},
|
||
)}
|
||
>
|
||
<Badge
|
||
status='processing'
|
||
color={token.colorSuccess}
|
||
text={
|
||
<span className='text-xs font-normal'>
|
||
{t('实时 · 近 {{seconds}} 秒滚动', {
|
||
seconds: windowSeconds,
|
||
})}
|
||
</span>
|
||
}
|
||
/>
|
||
</Tooltip>
|
||
)}
|
||
</div>
|
||
</div>
|
||
}
|
||
styles={{ body: { padding: 0 } }}
|
||
>
|
||
{loading && items.length === 0 ? (
|
||
<div className='px-4 py-6'>
|
||
<Skeleton active title={false} paragraph={{ rows: 5 }} />
|
||
</div>
|
||
) : (
|
||
<>
|
||
<ConsoleTable
|
||
columns={columns}
|
||
dataSource={items}
|
||
pagination={false}
|
||
scroll={{ x: 930 }}
|
||
size='small'
|
||
rowKey='org_id'
|
||
locale={{
|
||
emptyText: (
|
||
<Empty
|
||
image={Empty.PRESENTED_IMAGE_SIMPLE}
|
||
description={t('近 {{seconds}} 秒内没有企业产生请求', {
|
||
seconds: windowSeconds,
|
||
})}
|
||
/>
|
||
),
|
||
}}
|
||
/>
|
||
{/* 口径脚注:并发/RPM 只盖同步转发,TPM 还滞后一个请求时长。
|
||
不写出来的话,运营会拿这张表去对账单,然后发现对不上。 */}
|
||
<div className='px-4 py-2.5 text-xs text-[var(--text-secondary)] leading-relaxed border-t border-[var(--border-subtle)]'>
|
||
{t(
|
||
'口径:仅统计 /v1、/v1beta 同步转发接口;Midjourney、Suno、视频与异步图片任务提交后在后台执行,不计入本表。',
|
||
)}{' '}
|
||
{t(
|
||
'RPM 含被限流拒绝的请求;TPM 在请求结束时才计入,长流式请求会在结束的那一秒形成毛刺。',
|
||
)}{' '}
|
||
{t(
|
||
'缓存命中率的窗口是近 1 小时(读企业小时聚合表),与左侧 60 秒窗口的指标不同步。',
|
||
)}
|
||
</div>
|
||
<OrgCacheHourlySheet
|
||
visible={!!detailOrg}
|
||
org={detailOrg}
|
||
onClose={closeDetail}
|
||
t={t}
|
||
/>
|
||
</>
|
||
)}
|
||
</ConsoleCard>
|
||
);
|
||
};
|
||
|
||
export default OrgRealtimeUsage;
|