tokenFactory/web/src/components/dashboard/BentoTrend.jsx

141 lines
5.6 KiB
JavaScript

/*
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 } from 'react';
import { TrendingUp } from 'lucide-react';
import Sparkline from './Sparkline';
import { renderQuota } from '../../helpers';
const formatMD = (d) => {
const m = d.getMonth() + 1;
const day = d.getDate();
return `${m}/${day}`;
};
const BentoTrend = ({
dailyStats = [],
topModels = [],
className = '',
}) => {
const counts = useMemo(() => dailyStats.map((d) => d.count), [dailyStats]);
const costs = useMemo(() => dailyStats.map((d) => d.cost), [dailyStats]);
const totalCalls = counts.reduce((s, v) => s + v, 0);
const totalCost = costs.reduce((s, v) => s + v, 0);
const avg = totalCalls > 0 ? (totalCalls / Math.max(counts.length, 1)).toFixed(1) : '0.0';
const peak = counts.length > 0 ? Math.max(...counts) : 0;
const topMax = topModels[0]?.count || 0;
return (
<div
className={`relative flex flex-col gap-4 rounded-2xl border border-slate-200 bg-white p-6 transition-shadow hover:shadow-md dark:border-white/5 dark:bg-white/[0.02] dark:hover:shadow-black/20 ${className}`}
>
<div className='flex items-center justify-between gap-3'>
<div className='flex items-center gap-3'>
<div className='flex h-10 w-10 items-center justify-center rounded-xl bg-slate-100 text-slate-600 dark:bg-white/5 dark:text-white/70'>
<TrendingUp size={18} />
</div>
<div>
<div className='text-sm font-semibold text-slate-900 dark:text-white'>Token 使用趋势</div>
<div className='mt-0.5 text-[11px] text-slate-400 tabular-nums dark:text-white/40'>
7
<span className='font-semibold text-slate-900 dark:text-white'> {totalCalls}</span>
· 日均{' '}
<span className='font-semibold text-slate-900 dark:text-white'>{avg}</span>
</div>
</div>
</div>
<div className='hidden text-right md:block'>
<div className='text-[10px] uppercase tracking-wider text-slate-400 font-mono dark:text-white/30'>
花费
</div>
<div className='font-mono text-sm font-semibold tabular-nums text-slate-900 dark:text-white'>
{totalCost > 0 ? renderQuota(totalCost) : '—'}
</div>
</div>
</div>
<div className='flex min-h-[80px] flex-1 items-end text-blue-500'>
<Sparkline data={counts} width={400} height={80} />
</div>
<div className='flex items-center justify-between text-[11px]'>
<div className='flex items-center gap-1 tabular-nums'>
{dailyStats.map((d, i) => (
<span
key={i}
className={`w-8 text-center ${
d.count === peak && peak > 0
? 'font-semibold text-slate-900 dark:text-white'
: 'text-slate-300 dark:text-white/30'
}`}
title={`${d.date.getMonth() + 1}${d.date.getDate()}${d.count}`}
>
{formatMD(d.date)}
</span>
))}
</div>
<div className='text-slate-500 tabular-nums dark:text-white/50'>
峰值{' '}
<span className='font-semibold text-slate-900 dark:text-white'>{peak}</span>
</div>
</div>
{topModels.length > 0 && (
<div className='border-t border-slate-100 pt-4 dark:border-white/5'>
<div className='mb-2 flex items-center justify-between text-[11px] uppercase tracking-wider text-slate-400 dark:text-white/40'>
<span>Top 5 模型</span>
<span className='font-mono normal-case tracking-normal'> 30 </span>
</div>
<ul className='space-y-1.5'>
{topModels.map((m, i) => {
const pct = topMax > 0 ? (m.count / topMax) * 100 : 0;
return (
<li
key={m.name + i}
className='flex items-center gap-3 text-xs'
title={m.name}
>
<span className='w-3 shrink-0 text-right font-mono text-[10px] text-slate-300 tabular-nums dark:text-white/30'>
{i + 1}
</span>
<span className='min-w-0 flex-1 truncate text-slate-700 dark:text-white/80'>
{m.name}
</span>
<div className='hidden h-1 w-24 overflow-hidden rounded-full bg-slate-100 sm:block dark:bg-white/5'>
<div
className='h-full rounded-full bg-slate-300 dark:bg-white/30'
style={{ width: `${pct}%` }}
/>
</div>
<span className='w-12 shrink-0 text-right font-mono tabular-nums text-slate-500 dark:text-white/60'>
{m.count}
</span>
</li>
);
})}
</ul>
</div>
)}
</div>
);
};
export default BentoTrend;