sync: 同步 monkey-ai@weiyuan(ddc5b9443) 的 web/ 到 ui-optimization

- 合入 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)
This commit is contained in:
qiubaishen
2026-11-18 10:22:02 +08:00
parent 737c353d84
commit db3dba1e96
157 changed files with 29960 additions and 1197 deletions
@@ -0,0 +1,379 @@
/*
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, { useMemo, useState, useEffect } from 'react';
import { Button, Empty, Segmented, Skeleton, theme } from 'antd';
import { VChart } from '@visactor/react-vchart';
import { Info } from 'lucide-react';
import { RefreshCw } from '@/components/common/icons/ActionIcons';
import { ConsoleDrawer } from '@/components/console/ConsolePrimitives';
import useOrgCacheHourly from '../../hooks/analytics/useOrgCacheHourly';
import { useIsMobile } from '../../hooks/common/useIsMobile';
// 图表序列色属于专用图表(VChart)配色,统计块沿用同色以便与图例对应。
// 千分位缩写。线上单小时 cache_read 能到十亿量级,不缩写轴标签会糊成一片。
const abbr = (n) => {
const v = Number(n) || 0;
const abs = Math.abs(v);
if (abs >= 1e9) return `${(v / 1e9).toFixed(2)}B`;
if (abs >= 1e6) return `${(v / 1e6).toFixed(2)}M`;
if (abs >= 1e3) return `${(v / 1e3).toFixed(2)}K`;
return `${v}`;
};
const money = (n) => {
const v = Number(n) || 0;
if (Math.abs(v) >= 1000) return `$${(v / 1000).toFixed(2)}K`;
return `$${v.toFixed(2)}`;
};
// unix 秒 → 本地「MM-DD HH:00」。后端返回的是小时桶起点,整点整分,不做四舍五入。
const hourLabel = (unixSec) => {
const d = new Date((Number(unixSec) || 0) * 1000);
const p = (x) => String(x).padStart(2, '0');
return `${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:00`;
};
const SERIES_COLORS = {
input: '#3b82f6',
output: '#10b981',
cacheCreation: '#f59e0b',
cacheRead: '#8b5cf6',
hitRate: '#ef4444',
};
const StatBlock = ({ label, value, sub, color }) => (
<div className='flex-1 min-w-[110px]'>
<div className='text-xs text-[var(--text-secondary)] mb-1'>{label}</div>
<div
className='text-lg font-semibold'
style={color ? { color } : undefined}
>
{value}
</div>
{sub ? (
<div className='text-xs text-[var(--text-secondary)] mt-0.5'>{sub}</div>
) : null}
</div>
);
/**
* 单企业逐小时缓存命中率报表抽屉。
*
* 数据源是企业小时聚合表,不是 logs —— 口径与成本理由见
* model/analytics_org_cache.go 顶部注释。
*/
const OrgCacheHourlySheet = ({ visible, org, onClose, t }) => {
const isMobile = useIsMobile();
const { token } = theme.useToken();
const [hours, setHours] = useState(24);
// 关闭后重开默认回到 24h:上次选了 7d 不该粘到下一家企业上。
useEffect(() => {
if (!visible) setHours(24);
}, [visible]);
const orgId = visible ? org?.org_id : null;
const { data, loading, error, refresh } = useOrgCacheHourly(orgId, hours);
const points = useMemo(() => data?.points || [], [data]);
const totals = data?.totals || null;
// 全窗口零流量:补出来的点都是 0,画出来是一条贴底的直线,不如直接给空态。
const hasTraffic = points.some((p) => (p.requests || 0) > 0);
const spec = useMemo(() => {
if (!points.length || !hasTraffic) return null;
const inputKey = t('输入');
const outputKey = t('输出');
const creationKey = t('写缓存');
const readKey = t('读缓存');
const stacked = [];
const rateRows = [];
points.forEach((p) => {
const label = hourLabel(p.hour);
stacked.push(
{ hour: label, kind: inputKey, value: p.input || 0 },
{ hour: label, kind: outputKey, value: p.output || 0 },
{ hour: label, kind: creationKey, value: p.cache_creation || 0 },
{ hour: label, kind: readKey, value: p.cache_read || 0 },
);
rateRows.push({
hour: label,
// 该小时无流量时后端给 null;这里原样传下去并让序列 invalidType='break',
// 折线在空洞处断开,而不是掉到 0% 被误读成命中率暴跌。
hit_rate:
p.hit_rate === null || p.hit_rate === undefined ? null : p.hit_rate,
actual_cost: p.actual_cost || 0,
requests: p.requests || 0,
});
});
const rateKey = t('缓存命中率');
return {
type: 'common',
data: [
{ id: 'tokens', values: stacked },
{ id: 'rate', values: rateRows },
],
series: [
{
type: 'bar',
id: 'tokens',
dataIndex: 0,
xField: 'hour',
yField: 'value',
seriesField: 'kind',
stack: true,
bar: { style: { cornerRadius: [0, 0, 0, 0] } },
tooltip: {
visible: true,
mark: {
content: [
{
key: (datum) => datum?.kind,
value: (datum) => `${abbr(datum?.value)} tokens`,
},
],
},
},
},
{
type: 'line',
id: 'rate',
name: rateKey,
dataIndex: 1,
xField: 'hour',
yField: 'hit_rate',
// 空洞断线,不要 connectNulls 把两段无关的曲线连起来
invalidType: 'break',
point: {
visible: true,
size: 3,
style: { fill: SERIES_COLORS.hitRate },
},
line: { style: { stroke: SERIES_COLORS.hitRate, lineWidth: 2 } },
tooltip: {
visible: true,
mark: {
content: [
{
key: () => rateKey,
value: (datum) =>
datum?.hit_rate === null || datum?.hit_rate === undefined
? t('无流量')
: `${Number(datum.hit_rate).toFixed(1)}%`,
},
{
key: () => t('实际消费'),
value: (datum) => money(datum?.actual_cost),
},
{
key: () => t('请求数'),
value: (datum) => (datum?.requests || 0).toLocaleString(),
},
],
},
},
},
],
color: [
SERIES_COLORS.input,
SERIES_COLORS.output,
SERIES_COLORS.cacheCreation,
SERIES_COLORS.cacheRead,
],
legends: {
visible: true,
orient: 'top',
position: 'middle',
padding: { top: 0, bottom: 12, left: 0, right: 0 },
layoutType: 'normal',
},
tooltip: { visible: true, dimension: { visible: true } },
crosshair: { xField: { visible: true, line: { visible: true } } },
axes: [
{
orient: 'bottom',
// 7d = 168 个桶,标签必须自动抽稀,否则重叠成黑条
label: { style: { angle: -45 }, autoLimit: true, autoHide: true },
},
{
orient: 'left',
title: { visible: true, text: t('Token 数') },
label: { formatMethod: (v) => abbr(v) },
seriesIndex: [0],
},
{
orient: 'right',
title: { visible: true, text: t('命中率 (%)') },
// 固定 0~100:让曲线的高低在不同企业、不同窗口之间可比。
// 自适应量程会把 76%~87% 的波动拉成满屏振荡,像出了故障。
min: 0,
max: 100,
label: { formatMethod: (v) => `${Math.round(v)}%` },
seriesIndex: [1],
},
],
};
}, [points, hasTraffic, t]);
const orgTitle = org?.org_name || (org ? `${t('企业')} ${org.org_id}` : '');
return (
<ConsoleDrawer
open={visible}
onClose={onClose}
size={isMobile ? '100%' : 820}
title={
<div className='flex items-center gap-2 flex-wrap'>
<span className='font-semibold'>{t('缓存命中率明细')}</span>
<span className='text-sm font-normal text-[var(--text-secondary)]'>
{orgTitle}
</span>
{org?.org_id ? (
<span className='text-xs font-normal text-[var(--text-secondary)]'>
#{org.org_id}
</span>
) : null}
</div>
}
>
<div className='flex items-center justify-between gap-3 flex-wrap mb-4'>
<Segmented
value={hours}
onChange={setHours}
options={[
{ label: t('近 24 小时'), value: 24 },
{ label: t('近 7 天'), value: 24 * 7 },
]}
/>
<Button
icon={<RefreshCw size={16} />}
onClick={refresh}
loading={loading}
>
{t('刷新')}
</Button>
</div>
{totals ? (
<div
className='flex flex-wrap gap-4 px-4 py-3 mb-4 rounded-lg'
style={{ background: token.colorFillAlter }}
>
<StatBlock
label={t('缓存命中率')}
value={
totals.hit_rate === null || totals.hit_rate === undefined
? '--'
: `${Number(totals.hit_rate).toFixed(2)}%`
}
sub={t('按 token 加权')}
color={SERIES_COLORS.hitRate}
/>
<StatBlock label={t('输入')} value={abbr(totals.input)} />
<StatBlock label={t('输出')} value={abbr(totals.output)} />
<StatBlock
label={t('写缓存')}
value={abbr(totals.cache_creation)}
color={SERIES_COLORS.cacheCreation}
/>
<StatBlock
label={t('读缓存')}
value={abbr(totals.cache_read)}
color={SERIES_COLORS.cacheRead}
/>
<StatBlock
label={t('实际消费')}
value={money(totals.actual_cost)}
sub={t('{{n}} 次请求', {
n: (totals.requests || 0).toLocaleString(),
})}
/>
</div>
) : null}
{loading && !points.length ? (
<Skeleton active title={false} paragraph={{ rows: 8 }} />
) : error ? (
<Empty
image={Empty.PRESENTED_IMAGE_SIMPLE}
className='py-12'
description={
<>
<div className='text-[var(--text-primary)]'>{t('加载失败')}</div>
<div>{error}</div>
</>
}
/>
) : spec ? (
<VChart
spec={spec}
option={{ mode: 'desktop-browser' }}
style={{ height: 360 }}
/>
) : (
<Empty
image={Empty.PRESENTED_IMAGE_SIMPLE}
className='py-12'
description={
<>
<div className='text-[var(--text-primary)]'>
{t('该时间段内没有流量')}
</div>
<div>{t('所选窗口内这家企业没有产生可统计的请求')}</div>
</>
}
/>
)}
{/* 口径脚注:不写清楚的话,运营会拿这张图去对账单,然后发现对不上。 */}
<div
className='mt-4 px-3 py-2.5 text-xs text-[var(--text-secondary)] leading-relaxed rounded-md'
style={{ background: token.colorFillAlter }}
>
<div className='flex items-start gap-1.5'>
<Info aria-hidden className='w-3.5 h-3.5 mt-0.5 flex-shrink-0' />
<div>
<div>
{t(
'命中率 = 读缓存 ÷(输入 + 写缓存 + 读缓存)。输出 token 与缓存无关,不计入分母。',
)}
</div>
<div>
{t(
'数据来自企业小时聚合表,按自然小时切桶;最后一个小时尚未走完,数字仍在累加。',
)}
</div>
<div>
{t(
'「实际消费」为本平台实际计费金额;标准价对比待官方价目表配置完成后再上。',
)}
</div>
</div>
</div>
</div>
</ConsoleDrawer>
);
};
export { abbr as formatTokenAbbr, money as formatUsd, hourLabel };
export default OrgCacheHourlySheet;