Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,497 @@
|
||||
/*
|
||||
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, { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import {
|
||||
Button,
|
||||
SideSheet,
|
||||
Spin,
|
||||
Table,
|
||||
Tag,
|
||||
Tooltip,
|
||||
Typography,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import { ChevronRight, ListTree, Route } from 'lucide-react';
|
||||
import { renderQuota } from '../../helpers/render';
|
||||
import { useIsMobile } from '../../hooks/common/useIsMobile';
|
||||
import {
|
||||
FAULTS,
|
||||
OUTCOMES,
|
||||
fmtInt,
|
||||
fmtMs,
|
||||
fmtTs,
|
||||
outcomeTagColor,
|
||||
qualityErrorText,
|
||||
} from './qualityConstants';
|
||||
|
||||
const OutcomeTag = ({ value, t }) => {
|
||||
const o = OUTCOMES.find((x) => x.value === value);
|
||||
if (!o) return <Tag size='small'>{t('中间跳')}</Tag>;
|
||||
return (
|
||||
<Tag size='small' color={outcomeTagColor[value]}>
|
||||
{t(o.label)}
|
||||
</Tag>
|
||||
);
|
||||
};
|
||||
|
||||
const faultLabel = (value, scope, t) => {
|
||||
const f = FAULTS.find((x) => x.value === value);
|
||||
if (!f) return '-';
|
||||
return t(scope === 'org' ? f.orgLabel : f.label);
|
||||
};
|
||||
|
||||
/** 兜底链路:逐跳展示渠道与该跳错误 */
|
||||
export const HopChain = ({ hops, outcome, t }) => {
|
||||
if (!hops || hops.length === 0) {
|
||||
return (
|
||||
<span className='text-xs text-[var(--text-secondary)]'>
|
||||
{t('无渠道尝试记录')}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
const success = outcome === 1 || outcome === 2 || outcome === 6;
|
||||
return (
|
||||
<div className='quality-hops'>
|
||||
{hops.map((hop, i) => {
|
||||
const last = i === hops.length - 1;
|
||||
const failed =
|
||||
Boolean(hop.error_code || hop.status_code) || (last && !success);
|
||||
return (
|
||||
<React.Fragment key={`${hop.seq}-${i}`}>
|
||||
{i > 0 && <ChevronRight size={14} className='quality-hop-arrow' />}
|
||||
<div
|
||||
className='quality-hop'
|
||||
data-failed={failed}
|
||||
data-ok={last && success && !failed}
|
||||
>
|
||||
<span className='font-medium truncate'>
|
||||
{t('第 {{n}} 跳', { n: i + 1 })} ·{' '}
|
||||
{hop.channel_name || `#${hop.channel_id}`}
|
||||
</span>
|
||||
{failed ? (
|
||||
<span className='quality-mono'>
|
||||
{hop.status_code ? `HTTP ${hop.status_code}` : ''}
|
||||
{hop.error_code ? ` ${hop.error_code}` : ''}
|
||||
{!hop.status_code && !hop.error_code ? t('失败') : ''}
|
||||
</span>
|
||||
) : (
|
||||
<span className='text-[var(--semi-color-success)]'>
|
||||
{t('成功')}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</React.Fragment>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
/** 明细:按 id 游标翻页(不做 COUNT,大表友好) */
|
||||
const QualityDetail = ({ quality, t }) => {
|
||||
const { scope, get, params } = quality;
|
||||
const isOrg = scope === 'org';
|
||||
const isMobile = useIsMobile();
|
||||
const [items, setItems] = useState([]);
|
||||
const [nextCursor, setNextCursor] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [reloading, setReloading] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [moreError, setMoreError] = useState('');
|
||||
const [trace, setTrace] = useState(null);
|
||||
const [traceLoading, setTraceLoading] = useState(false);
|
||||
const seq = useRef(0);
|
||||
|
||||
const load = useCallback(
|
||||
(cursor) => {
|
||||
const current = ++seq.current;
|
||||
const firstPage = !cursor;
|
||||
setLoading(true);
|
||||
// 翻页失败只在"加载更多"处提示,不清掉已加载的列表;筛选变化(第一页)时遮罩旧数据
|
||||
setReloading(firstPage);
|
||||
setMoreError('');
|
||||
if (firstPage) setError('');
|
||||
get('/detail', { ...params, cursor: cursor || undefined, limit: 20 })
|
||||
.then((d) => {
|
||||
if (current !== seq.current) return;
|
||||
setItems((prev) =>
|
||||
firstPage ? d.items || [] : [...prev, ...(d.items || [])],
|
||||
);
|
||||
setNextCursor(d.next_cursor || '');
|
||||
})
|
||||
.catch((e) => {
|
||||
if (current !== seq.current) return;
|
||||
if (firstPage) {
|
||||
setItems([]);
|
||||
setNextCursor('');
|
||||
setError(e.code || 'query_failed');
|
||||
} else {
|
||||
setMoreError(e.code || 'query_failed');
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
if (current !== seq.current) return;
|
||||
setLoading(false);
|
||||
setReloading(false);
|
||||
});
|
||||
},
|
||||
[get, params],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
load('');
|
||||
}, [load]);
|
||||
|
||||
const openTrace = (row) => {
|
||||
if (!row.request_id) return;
|
||||
setTrace({ row, items: [] });
|
||||
setTraceLoading(true);
|
||||
get('/trace', {
|
||||
request_id: row.request_id,
|
||||
user_id: row.user_id,
|
||||
created_at: row.created_at,
|
||||
})
|
||||
.then((d) => setTrace({ row, items: d.items || [] }))
|
||||
.catch((e) =>
|
||||
setTrace({ row, items: [], error: e.code || 'query_failed' }),
|
||||
)
|
||||
.finally(() => setTraceLoading(false));
|
||||
};
|
||||
|
||||
const columns = [
|
||||
{
|
||||
title: t('时间'),
|
||||
dataIndex: 'created_at',
|
||||
width: 130,
|
||||
render: (v) => <span className='quality-num'>{fmtTs(v, true)}</span>,
|
||||
},
|
||||
{
|
||||
title: t('结局'),
|
||||
dataIndex: 'outcome',
|
||||
width: 120,
|
||||
render: (v) => <OutcomeTag value={v} t={t} />,
|
||||
},
|
||||
{
|
||||
title: t('责任方'),
|
||||
dataIndex: 'fault_side',
|
||||
width: 110,
|
||||
render: (v) => faultLabel(v, scope, t),
|
||||
},
|
||||
{
|
||||
title: isOrg ? t('成员') : t('用户'),
|
||||
dataIndex: 'username',
|
||||
width: 130,
|
||||
render: (v, row) => (
|
||||
<span className='block truncate max-w-[130px]' title={v}>
|
||||
{v || `#${row.user_id}`}
|
||||
</span>
|
||||
),
|
||||
},
|
||||
...(!isOrg
|
||||
? [
|
||||
{
|
||||
title: t('企业'),
|
||||
dataIndex: 'org_name',
|
||||
width: 130,
|
||||
render: (v, row) =>
|
||||
row.organization_id ? (
|
||||
<span className='block truncate max-w-[130px]' title={v}>
|
||||
{v || `#${row.organization_id}`}
|
||||
</span>
|
||||
) : (
|
||||
<span className='text-[var(--text-secondary)]'>
|
||||
{t('个人')}
|
||||
</span>
|
||||
),
|
||||
},
|
||||
]
|
||||
: []),
|
||||
{
|
||||
title: t('模型'),
|
||||
dataIndex: 'model_name',
|
||||
width: 170,
|
||||
render: (v) => (
|
||||
<span className='block truncate max-w-[170px]' title={v}>
|
||||
{v || '-'}
|
||||
</span>
|
||||
),
|
||||
},
|
||||
...(!isOrg
|
||||
? [
|
||||
{
|
||||
title: t('最终渠道'),
|
||||
dataIndex: 'channel_name',
|
||||
width: 140,
|
||||
render: (v, row) => (
|
||||
<span className='block truncate max-w-[140px]' title={v}>
|
||||
{row.channel_id ? v || `#${row.channel_id}` : '-'}
|
||||
</span>
|
||||
),
|
||||
},
|
||||
]
|
||||
: []),
|
||||
{
|
||||
title: t('状态码 / 错误码'),
|
||||
dataIndex: 'error_code',
|
||||
width: 190,
|
||||
render: (v, row) =>
|
||||
row.status_code || v ? (
|
||||
<span className='quality-mono'>
|
||||
{row.status_code || ''}{' '}
|
||||
{v === 'service_error' ? t('服务侧错误') : v || ''}
|
||||
</span>
|
||||
) : (
|
||||
'-'
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('尝试次数'),
|
||||
dataIndex: 'attempt_count',
|
||||
width: 120,
|
||||
align: 'right',
|
||||
render: (v) => <span className='quality-num'>{v || '-'}</span>,
|
||||
},
|
||||
{
|
||||
title: t('耗时'),
|
||||
dataIndex: 'use_time',
|
||||
width: 90,
|
||||
align: 'right',
|
||||
render: (v) => (
|
||||
<span className='quality-num'>{v ? fmtMs(v * 1000) : '-'}</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('费用'),
|
||||
dataIndex: 'quota',
|
||||
width: 100,
|
||||
align: 'right',
|
||||
render: (v) => (
|
||||
<span className='quality-num'>{v ? renderQuota(v) : '-'}</span>
|
||||
),
|
||||
},
|
||||
...(!isOrg
|
||||
? [
|
||||
{
|
||||
title: '',
|
||||
dataIndex: 'op',
|
||||
width: 90,
|
||||
fixed: isMobile ? undefined : 'right',
|
||||
render: (_v, row) =>
|
||||
row.request_id ? (
|
||||
<Button
|
||||
size='small'
|
||||
theme='borderless'
|
||||
icon={<Route size={14} />}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
openTrace(row);
|
||||
}}
|
||||
>
|
||||
{t('链路')}
|
||||
</Button>
|
||||
) : null,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
];
|
||||
|
||||
const expandRowRender = (row) => (
|
||||
<div className='flex flex-col gap-2 py-1'>
|
||||
{!isOrg && (
|
||||
<div>
|
||||
<div className='mb-1 text-xs text-[var(--text-secondary)]'>
|
||||
{t('兜底链路')}
|
||||
</div>
|
||||
<HopChain hops={row.hops} outcome={row.outcome} t={t} />
|
||||
</div>
|
||||
)}
|
||||
{!isOrg && row.content && (
|
||||
<div>
|
||||
<div className='mb-1 text-xs text-[var(--text-secondary)]'>
|
||||
{t('错误信息')}
|
||||
</div>
|
||||
<Typography.Paragraph
|
||||
className='quality-mono'
|
||||
ellipsis={{ rows: 3, expandable: true, collapsible: true }}
|
||||
>
|
||||
{row.content}
|
||||
</Typography.Paragraph>
|
||||
</div>
|
||||
)}
|
||||
<div className='flex flex-wrap gap-x-6 gap-y-1 text-xs text-[var(--text-secondary)]'>
|
||||
{row.request_id && (
|
||||
<span>
|
||||
{t('请求 ID')}:{' '}
|
||||
<span className='quality-mono'>{row.request_id}</span>
|
||||
</span>
|
||||
)}
|
||||
{row.token_name && (
|
||||
<span>
|
||||
{t('令牌')}: {row.token_name}
|
||||
</span>
|
||||
)}
|
||||
{!isOrg && row.group && (
|
||||
<span>
|
||||
{t('分组')}: {row.group}
|
||||
</span>
|
||||
)}
|
||||
{!isOrg && row.upstream_model_name && (
|
||||
<span>
|
||||
{t('上游模型')}: {row.upstream_model_name}
|
||||
</span>
|
||||
)}
|
||||
{!isOrg && row.fallback_depth > 0 && (
|
||||
<span>
|
||||
{t('兜底深度')}: {row.fallback_depth}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<div className='quality-card'>
|
||||
<div className='quality-card-head'>
|
||||
<span className='quality-card-title'>
|
||||
<ListTree size={16} />
|
||||
{t('请求明细')}
|
||||
<span className='quality-card-sub'>
|
||||
{t('默认只列失败与兜底请求;在筛选栏选择「结局」可查看其他请求')}
|
||||
</span>
|
||||
</span>
|
||||
</div>
|
||||
<div className='quality-card-body'>
|
||||
{error ? (
|
||||
<div className='quality-empty flex flex-col items-center gap-3'>
|
||||
<span>{qualityErrorText(t, error)}</span>
|
||||
<Button size='small' onClick={() => load('')}>
|
||||
{t('重试')}
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<Table
|
||||
size='small'
|
||||
rowKey='id'
|
||||
loading={reloading}
|
||||
columns={columns}
|
||||
dataSource={items}
|
||||
pagination={false}
|
||||
expandedRowRender={expandRowRender}
|
||||
rowExpandable={() => true}
|
||||
scroll={{ x: isOrg ? 1100 : 1500 }}
|
||||
empty={
|
||||
<div className='quality-empty'>{t('没有符合条件的请求')}</div>
|
||||
}
|
||||
/>
|
||||
{nextCursor && (
|
||||
<div className='mt-3 flex flex-col items-center gap-2'>
|
||||
{moreError && (
|
||||
<span className='text-xs text-[var(--semi-color-danger)]'>
|
||||
{qualityErrorText(t, moreError)}
|
||||
</span>
|
||||
)}
|
||||
<Button
|
||||
loading={loading && !reloading}
|
||||
onClick={() => load(nextCursor)}
|
||||
>
|
||||
{moreError ? t('重试') : t('加载更多')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<SideSheet
|
||||
visible={Boolean(trace)}
|
||||
width={isMobile ? '100%' : 720}
|
||||
title={t('请求链路')}
|
||||
onCancel={() => setTrace(null)}
|
||||
>
|
||||
<Spin spinning={traceLoading}>
|
||||
{trace?.error ? (
|
||||
<div className='quality-empty'>
|
||||
{qualityErrorText(t, trace.error)}
|
||||
</div>
|
||||
) : (
|
||||
<div className='flex flex-col gap-3'>
|
||||
<div className='text-xs text-[var(--text-secondary)]'>
|
||||
{t('请求 ID')}:{' '}
|
||||
<span className='quality-mono'>{trace?.row?.request_id}</span>
|
||||
</div>
|
||||
{(trace?.items || []).map((row) => (
|
||||
<div key={row.id} className='quality-card'>
|
||||
<div className='quality-card-body flex flex-col gap-2'>
|
||||
<div className='flex flex-wrap items-center gap-2'>
|
||||
<span className='quality-num text-xs'>
|
||||
{fmtTs(row.created_at, true)}
|
||||
</span>
|
||||
<Tag
|
||||
size='small'
|
||||
color={row.type === 5 ? 'red' : 'green'}
|
||||
>
|
||||
{row.type === 5 ? t('错误日志') : t('消费日志')}
|
||||
</Tag>
|
||||
<OutcomeTag value={row.outcome} t={t} />
|
||||
<span className='text-xs'>
|
||||
{row.channel_name ||
|
||||
(row.channel_id ? `#${row.channel_id}` : '')}
|
||||
</span>
|
||||
{(row.status_code || row.error_code) && (
|
||||
<span className='quality-mono'>
|
||||
{row.status_code || ''} {row.error_code || ''}
|
||||
</span>
|
||||
)}
|
||||
{row.quota > 0 && (
|
||||
<span className='text-xs quality-num'>
|
||||
{renderQuota(row.quota)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<HopChain hops={row.hops} outcome={row.outcome} t={t} />
|
||||
{row.content && (
|
||||
<Tooltip content={row.content}>
|
||||
<div className='quality-mono line-clamp-2'>
|
||||
{row.content}
|
||||
</div>
|
||||
</Tooltip>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{!traceLoading &&
|
||||
(trace?.items || []).length === 0 &&
|
||||
!trace?.error && (
|
||||
<div className='quality-empty'>{t('未找到该请求的日志')}</div>
|
||||
)}
|
||||
{(trace?.items || []).length > 0 && (
|
||||
<div className='text-xs text-[var(--text-secondary)]'>
|
||||
{t('共 {{n}} 条日志', { n: fmtInt(trace.items.length) })}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</Spin>
|
||||
</SideSheet>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default QualityDetail;
|
||||
Reference in New Issue
Block a user