Skip to content

Commit f444a87

Browse files
committed
feat: 添加实际数据时长计算,优化每分钟Token和请求的显示逻辑
1 parent b7d5896 commit f444a87

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

app/page.tsx

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,38 @@ export default function DashboardPage() {
629629
return [...models].sort((a, b) => b.cost - a.cost);
630630
}, [overviewData]);
631631

632+
// 计算实际数据时长(从最早记录到现在)
633+
const actualTimeSpan = useMemo(() => {
634+
if (!overviewData?.byHour || overviewData.byHour.length === 0) {
635+
return { days: appliedDays, minutes: appliedDays * 24 * 60 };
636+
}
637+
638+
// 找到最早的时间戳
639+
let earliestTime: Date | null = null;
640+
for (const point of overviewData.byHour) {
641+
if (point.timestamp) {
642+
const t = new Date(point.timestamp);
643+
if (Number.isFinite(t.getTime())) {
644+
if (!earliestTime || t < earliestTime) {
645+
earliestTime = t;
646+
}
647+
}
648+
}
649+
}
650+
651+
if (!earliestTime) {
652+
return { days: appliedDays, minutes: appliedDays * 24 * 60 };
653+
}
654+
655+
// 计算从最早记录到现在的时长
656+
const now = new Date();
657+
const diffMs = now.getTime() - earliestTime.getTime();
658+
const diffMinutes = Math.max(1, Math.floor(diffMs / (1000 * 60)));
659+
const diffDays = Math.max(1, diffMinutes / (24 * 60));
660+
661+
return { days: diffDays, minutes: diffMinutes };
662+
}, [overviewData?.byHour, appliedDays]);
663+
632664
const rangeSubtitle = useMemo(() => {
633665
if (rangeMode === "custom" && customStart && customEnd) {
634666
return `${customStart} ~ ${customEnd}(共 ${appliedDays} 天)`;
@@ -1103,7 +1135,7 @@ export default function DashboardPage() {
11031135
<div className={`animate-card-float rounded-2xl p-5 shadow-sm ring-1 transition-all duration-200 ${darkMode ? "bg-gradient-to-br from-emerald-600/20 to-emerald-800/10 ring-emerald-500/30 hover:shadow-lg hover:shadow-emerald-500/20 hover:ring-emerald-500/50" : "bg-emerald-50 ring-emerald-200 hover:shadow-lg hover:ring-emerald-300"}`} style={{ animationDelay: '0.2s' }}>
11041136
<div className="text-sm uppercase tracking-wide text-emerald-400">平均 TPM</div>
11051137
<div className={`mt-3 text-2xl font-bold ${darkMode ? "text-white" : "text-slate-900"}`}>
1106-
{(overviewData.totalTokens / (appliedDays * 24 * 60)).toFixed(2)}
1138+
{(overviewData.totalTokens / actualTimeSpan.minutes).toFixed(2)}
11071139
</div>
11081140
<p className={`mt-2 text-xs ${darkMode ? "text-emerald-300/70" : "text-emerald-600/70"}`}>每分钟Token</p>
11091141
</div>
@@ -1112,7 +1144,7 @@ export default function DashboardPage() {
11121144
<div className={`animate-card-float rounded-2xl p-5 shadow-sm ring-1 transition-all duration-200 ${darkMode ? "bg-gradient-to-br from-blue-600/20 to-blue-800/10 ring-blue-500/30 hover:shadow-lg hover:shadow-blue-500/20 hover:ring-blue-500/50" : "bg-blue-50 ring-blue-200 hover:shadow-lg hover:ring-blue-300"}`} style={{ animationDelay: '0.25s' }}>
11131145
<div className="text-sm uppercase tracking-wide text-blue-400">平均 RPM</div>
11141146
<div className={`mt-3 text-2xl font-bold ${darkMode ? "text-white" : "text-slate-900"}`}>
1115-
{(overviewData.totalRequests / (appliedDays * 24 * 60)).toFixed(2)}
1147+
{(overviewData.totalRequests / actualTimeSpan.minutes).toFixed(2)}
11161148
</div>
11171149
<p className={`mt-2 text-xs ${darkMode ? "text-blue-300/70" : "text-blue-600/70"}`}>每分钟请求</p>
11181150
</div>
@@ -1121,7 +1153,7 @@ export default function DashboardPage() {
11211153
<div className={`animate-card-float rounded-2xl p-5 shadow-sm ring-1 transition-all duration-200 ${darkMode ? "bg-gradient-to-br from-purple-600/20 to-purple-800/10 ring-purple-500/30 hover:shadow-lg hover:shadow-purple-500/20 hover:ring-purple-500/50" : "bg-purple-50 ring-purple-200 hover:shadow-lg hover:ring-purple-300"}`} style={{ animationDelay: '0.3s' }}>
11221154
<div className="text-sm uppercase tracking-wide text-purple-400">日均请求 (RPD)</div>
11231155
<div className={`mt-3 text-2xl font-bold ${darkMode ? "text-white" : "text-slate-900"}`}>
1124-
{formatCompactNumber(Math.round(overviewData.totalRequests / appliedDays))}
1156+
{formatCompactNumber(Math.round(overviewData.totalRequests / actualTimeSpan.days))}
11251157
</div>
11261158
<p className={`mt-2 text-xs ${darkMode ? "text-purple-300/70" : "text-purple-600/70"}`}>每日请求数</p>
11271159
</div>

0 commit comments

Comments
 (0)