import React, { useState, useEffect, useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { ChevronDown, ChevronRight } from 'lucide-react'; import { getUserLogs, Q } from '../api'; import { useCurrency } from '../context/SiteContext'; function formatTime(unix) { if (!unix) return '-'; const d = new Date(unix * 1000); const pad = (n) => String(n).padStart(2, '0'); return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`; } function getLogOther(otherStr) { if (!otherStr) return null; try { return JSON.parse(otherStr); } catch (e) { return null; } } function formatSitePriceSnapshot(other, symbol, rate, t) { if (!other?.site_billing_mode) return '-'; const formatUSD = (amount) => `${symbol}${(Number(amount || 0) * rate).toFixed(6)}`; if (other.site_billing_mode === 'per_call') { const fixedPrice = Number(other?.site_fixed_price || 0); if (fixedPrice <= 0) return t('按次计费'); return `${t('按次计费')} · ${formatUSD(fixedPrice)} / ${t('pricing.perCallUnit')}`; } const inputPrice = Number(other?.site_input_price || 0); const outputPrice = Number(other?.site_output_price || 0); const parts = [t('按量计费')]; if (inputPrice > 0) parts.push(`${t('输入')} ${formatUSD(inputPrice)} / 1M tokens`); if (outputPrice > 0) parts.push(`${t('输出')} ${formatUSD(outputPrice)} / 1M tokens`); return parts.join(' · '); } function formatProviderSummary(other, t) { if (!other?.provider_name) return []; const items = [{ key: t('供应商'), value: other.provider_name }]; if (other.provider_description) { items.push({ key: t('供应商介绍'), value: other.provider_description }); } return items; } export default function Logs() { const { t } = useTranslation(); const { symbol, rate } = useCurrency(); const [logs, setLogs] = useState([]); const [total, setTotal] = useState(0); const [page, setPage] = useState(1); const [loading, setLoading] = useState(true); const [modelFilter, setModelFilter] = useState(''); const [tokenFilter, setTokenFilter] = useState(''); const [expandedRows, setExpandedRows] = useState({}); const pageSize = 20; const load = useCallback(async () => { setLoading(true); try { const params = { p: page, page_size: pageSize, type: 2 }; // type=2 consume only if (modelFilter) params.model_name = modelFilter; if (tokenFilter) params.token_name = tokenFilter; const res = await getUserLogs(params); if (res.data.success) { setLogs(res.data.data?.items || []); setTotal(res.data.data?.total || 0); } } catch (e) { /* interceptor */ } setLoading(false); }, [page, modelFilter, tokenFilter]); useEffect(() => { load(); }, [load]); const totalPages = Math.max(1, Math.ceil(total / pageSize)); const toggleRow = (logId) => { setExpandedRows(prev => ({ ...prev, [logId]: !prev[logId] })); }; const getExpandData = useCallback((log) => { const other = getLogOther(log.other); if (!other) return []; const data = []; // Cache hits if (other.cache_tokens > 0) { data.push({ key: t('缓存命中 Tokens'), value: other.cache_tokens.toLocaleString() }); } // Cache creation if (other.cache_creation_tokens > 0) { data.push({ key: t('缓存创建 Tokens'), value: other.cache_creation_tokens.toLocaleString() }); } // Request ID if (log.request_id) { data.push({ key: 'Request ID', value: log.request_id }); } // Stream info if (log.is_stream !== undefined) { data.push({ key: t('流式'), value: log.is_stream ? t('是') : t('否') }); } // First response time for streaming if (log.is_stream && other.frt) { const frtSeconds = (parseFloat(other.frt) / 1000.0).toFixed(1); data.push({ key: t('首字时间'), value: `${frtSeconds}s` }); } // Billing source if (other.billing_source === 'subscription') { data.push({ key: t('计费方式'), value: t('订阅抵扣') }); } const providerInfo = formatProviderSummary(other, t); data.push(...providerInfo); if (other.site_billing_mode) { data.push({ key: t('分站定价'), value: formatSitePriceSnapshot(other, symbol, rate, t), }); } return data; }, [rate, symbol, t]); return (
{t('logs.subtitle')}
{t('logs.noLogs')}
| {t('logs.time')} | {t('logs.model')} | {t('logs.token')} | {t('logs.promptTokens')} | {t('logs.completionTokens')} | {t('logs.cost')} | {t('logs.duration')} | |
|---|---|---|---|---|---|---|---|
|
{hasExpandData && (
isExpanded ? |
{formatTime(log.created_at)} | {log.model_name || '-'} | {log.token_name || '-'} | {log.prompt_tokens?.toLocaleString() || '0'} | {log.completion_tokens?.toLocaleString() || '0'} | {log.quota > 0 ? `${symbol}${(log.quota / Q * rate).toFixed(6)}` : '-'} | {log.use_time > 0 ? `${log.use_time}s` : '-'} |
|
{expandData.map((item, idx) => (
{item.key}
{item.value}
))}
|
|||||||
{t('logs.showing', { from: (page - 1) * pageSize + 1, to: Math.min(page * pageSize, total), total })}