/* 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 . For commercial licensing, please contact support@quantumnous.com */ import { formatIdReference } from '../../helpers/safeDisplay'; import { formatUserReference } from '../../helpers/safeDisplay'; import React, { useCallback, useEffect, useRef, useState } from 'react'; import { Button, SideSheet, Spin, Table, Tag, Tooltip, Typography, } from '@/components/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 {t('中间跳')}; return ( {t(o.label)} ); }; 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 ( {t('无渠道尝试记录')} ); } const success = outcome === 1 || outcome === 2 || outcome === 6; return (
{hops.map((hop, i) => { const last = i === hops.length - 1; const failed = Boolean(hop.error_code || hop.status_code) || (last && !success); return ( {i > 0 && }
{t('第 {{n}} 跳', { n: i + 1 })} ·{' '} {hop.channel_name || formatIdReference(hop.channel_id)} {failed ? ( {hop.status_code ? `HTTP ${hop.status_code}` : ''} {hop.error_code ? ` ${hop.error_code}` : ''} {!hop.status_code && !hop.error_code ? t('失败') : ''} ) : ( {t('成功')} )}
); })}
); }; /** 明细:按 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) => {fmtTs(v, true)}, }, { title: t('结局'), dataIndex: 'outcome', width: 120, render: (v) => , }, { title: t('责任方'), dataIndex: 'fault_side', width: 110, render: (v) => faultLabel(v, scope, t), }, { title: isOrg ? t('成员') : t('用户'), dataIndex: 'username', width: 130, render: (v, row) => ( {v || formatUserReference('', row.user_id)} ), }, ...(!isOrg ? [ { title: t('企业'), dataIndex: 'org_name', width: 130, render: (v, row) => row.organization_id ? ( {v || formatIdReference(row.organization_id)} ) : ( {t('个人')} ), }, ] : []), { title: t('模型'), dataIndex: 'model_name', width: 170, render: (v) => ( {v || '-'} ), }, ...(!isOrg ? [ { title: t('最终渠道'), dataIndex: 'channel_name', width: 140, render: (v, row) => ( {row.channel_id ? v || formatIdReference(row.channel_id) : '-'} ), }, ] : []), { title: t('状态码 / 错误码'), dataIndex: 'error_code', width: 190, render: (v, row) => row.status_code || v ? ( {row.status_code || ''}{' '} {v === 'service_error' ? t('服务侧错误') : v || ''} ) : ( '-' ), }, { title: t('尝试次数'), dataIndex: 'attempt_count', width: 120, align: 'right', render: (v) => {v || '-'}, }, { title: t('耗时'), dataIndex: 'use_time', width: 90, align: 'right', render: (v) => ( {v ? fmtMs(v * 1000) : '-'} ), }, { title: t('费用'), dataIndex: 'quota', width: 100, align: 'right', render: (v) => ( {v ? renderQuota(v) : '-'} ), }, ...(!isOrg ? [ { title: '', dataIndex: 'op', width: 90, fixed: isMobile ? undefined : 'right', render: (_v, row) => row.request_id ? ( ) : null, }, ] : []), ]; const expandRowRender = (row) => (
{!isOrg && (
{t('兜底链路')}
)} {!isOrg && row.content && (
{t('错误信息')}
{row.content}
)}
{row.request_id && ( {t('请求 ID')}:{' '} {row.request_id} )} {row.token_name && ( {t('密钥')}: {row.token_name} )} {!isOrg && row.group && ( {t('分组')}: {row.group} )} {!isOrg && row.upstream_model_name && ( {t('上游模型')}: {row.upstream_model_name} )} {!isOrg && row.fallback_depth > 0 && ( {t('兜底深度')}: {row.fallback_depth} )}
); return (
{t('请求明细')} {t('默认只列失败与兜底请求;在筛选栏选择「结局」可查看其他请求')}
{error ? (
{qualityErrorText(t, error)}
) : ( <> true} scroll={{ x: isOrg ? 1100 : 1500 }} empty={
{t('没有符合条件的请求')}
} /> {nextCursor && (
{moreError && ( {qualityErrorText(t, moreError)} )}
)} )} setTrace(null)} > {trace?.error ? (
{qualityErrorText(t, trace.error)}
) : (
{t('请求 ID')}:{' '} {trace?.row?.request_id}
{(trace?.items || []).map((row) => (
{fmtTs(row.created_at, true)} {row.type === 5 ? t('错误日志') : t('消费日志')} {row.channel_name || (row.channel_id ? formatIdReference(row.channel_id) : '')} {(row.status_code || row.error_code) && ( {row.status_code || ''} {row.error_code || ''} )} {row.quota > 0 && ( {renderQuota(row.quota)} )}
{row.content && (
{row.content}
)}
))} {!traceLoading && (trace?.items || []).length === 0 && !trace?.error && (
{t('未找到该请求的日志')}
)} {(trace?.items || []).length > 0 && (
{t('共 {{n}} 条日志', { n: fmtInt(trace.items.length) })}
)}
)}
); }; export default QualityDetail;