"use client"; import { useEffect, useMemo, useState } from "react"; import { RefreshCcw, TrendingUp, TrendingDown, Target, Activity } from "lucide-react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { opsApi } from "@/lib/ops-api"; import type { SystemStatusPayload } from "@/types/ops"; import { CHART_TOOLTIP_STYLE } from "@/lib/chart-utils"; import Link from "next/link"; import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, ComposedChart, Line, Legend, Cell, LabelList, } from "recharts"; function StatRow({ label, value }: { label: string; value: React.ReactNode }) { return (
{label} {value}
); } function KpiCard({ label, value, sub, icon: Icon, color }: { label: string; value: string | number; sub?: string; icon: React.ComponentType<{ className?: string }>; color: string; }) { return (
{value}
{label}
{sub ?
{sub}
: null}
); } interface CityAccuracy { city_id: string; name: string; deb?: { hit_rate: number; mae: number; total_days: number; details_str: string; } | null; mu?: { mae: number; hit_rate: number; brier_score: number | null; total_days: number; details_str: string; } | null; } const CHART_COLORS = { green: "#22c55e", yellow: "#eab308", red: "#ef4444", blue: "#3b82f6", cyan: "#06b6d4", purple: "#a855f7", slate: "#64748b", emerald: "#10b981", amber: "#f59e0b", rose: "#f43f5e", }; function hitColor(hitRate: number) { if (hitRate >= 80) return CHART_COLORS.green; if (hitRate >= 60) return CHART_COLORS.yellow; return CHART_COLORS.red; } function maeColor(mae: number) { if (mae <= 1.5) return CHART_COLORS.green; if (mae <= 2.5) return CHART_COLORS.yellow; return CHART_COLORS.red; } function brierColor(score: number) { if (score <= 0.1) return CHART_COLORS.green; if (score <= 0.25) return CHART_COLORS.yellow; return CHART_COLORS.red; } export function TrainingPageClient() { const [loading, setLoading] = useState(true); const [status, setStatus] = useState(null); const [accuracy, setAccuracy] = useState(null); const load = async () => { setLoading(true); try { const [s, accData] = await Promise.all([ opsApi.systemStatus() as Promise, opsApi.trainingAccuracy().catch(() => ({ accuracy: [] as CityAccuracy[] })), ]); setStatus(s); setAccuracy((accData as { accuracy: CityAccuracy[] }).accuracy ?? []); } catch { /* */ } setLoading(false); }; useEffect(() => { void load(); }, []); const kpis = useMemo(() => { if (!accuracy?.length) return null; const debCities = accuracy.filter((c) => c.deb); const avgHit = debCities.reduce((s, c) => s + (c.deb?.hit_rate ?? 0), 0) / debCities.length; const avgMae = debCities.reduce((s, c) => s + (c.deb?.mae ?? 0), 0) / debCities.length; const best = debCities.reduce((a, b) => ((a.deb?.hit_rate ?? 0) > (b.deb?.hit_rate ?? 0) ? a : b)); const worst = debCities.reduce((a, b) => ((a.deb?.mae ?? 0) > (b.deb?.mae ?? 0) ? a : b)); return { avgHit, avgMae, best, worst }; }, [accuracy]); const debChartData = useMemo(() => { if (!accuracy?.length) return []; return accuracy .filter((c) => c.deb && c.deb.total_days >= 5) .sort((a, b) => (b.deb?.hit_rate ?? 0) - (a.deb?.hit_rate ?? 0)) .map((c) => ({ name: c.name, cityId: c.city_id, hitRate: Number((c.deb?.hit_rate ?? 0).toFixed(1)), mae: Number((c.deb?.mae ?? 0).toFixed(1)), days: c.deb?.total_days ?? 0, })); }, [accuracy]); const muChartData = useMemo(() => { if (!accuracy?.length) return []; return accuracy .filter((c) => c.mu && c.mu.total_days >= 5 && c.mu.brier_score !== null) .sort((a, b) => ((a.mu?.brier_score ?? 1) - (b.mu?.brier_score ?? 1))) .map((c) => ({ name: c.name, cityId: c.city_id, brierScore: Number((c.mu?.brier_score ?? 0).toFixed(4)), hitRate: Number((c.mu?.hit_rate ?? 0).toFixed(1)), mae: Number((c.mu?.mae ?? 0).toFixed(1)), days: c.mu?.total_days ?? 0, })); }, [accuracy]); if (loading) return
加载中...
; if (!status) return
加载失败
; const td = status.training_data; const truth = td?.truth_records; const features = td?.training_features; const coverage = td?.city_coverage; const modelCities = td?.model_cities; return (

训练数据

{/* Data volume KPIs */}
真值记录 训练特征 城市覆盖
{/* Accuracy KPI row */} {kpis ? (
) : null} {/* DEB Accuracy Charts */} {debChartData.length > 0 ? (
DEB 命中率 by 城市
[`${Number(value).toFixed(1)}%`, "命中率"]} /> {debChartData.map((entry, i) => ( ))} `${Number(v)}%`} />
DEB MAE by 城市
[`${Number(value).toFixed(1)}°`, "MAE"]} /> {debChartData.map((entry, i) => ( ))} `${Number(v)}°`} />
) : null} {/* Mu Probability Charts */} {muChartData.length > 0 ? (
概率 μ Brier Score by 城市
[Number(value).toFixed(4), "Brier Score"]} /> {muChartData.map((entry, i) => ( ))} Number(v).toFixed(3)} />
概率 μ 命中率 by 城市
[`${Number(value).toFixed(1)}%`, "命中率"]} /> {muChartData.map((entry, i) => ( ))} `${Number(v)}%`} />
) : null} {/* City coverage */} {modelCities ? (
最强城市 {modelCities.strongest?.length ? (
    {modelCities.strongest.map((c, i) => (
  • {c.city} 真值:{c.truth_rows ?? "—"} 特征:{c.feature_rows ?? "—"}
  • ))}
) : 无数据}
覆盖缺口 {modelCities.gaps?.length ? (
{modelCities.gaps.map((c) => ( {c} ))}
) : 无缺口}
) : null} {/* Detail table */} 模型融合与预测准确率详情
{accuracy && accuracy.length > 0 ? ( accuracy.map((row) => ( )) ) : ( )}
城市 DEB 命中 DEB MAE DEB 天数 μ 命中 μ MAE Brier μ 天数
{row.name} {row.city_id} {row.deb ? ( = 80 ? "bg-green-500/15 text-green-400" : row.deb.hit_rate >= 60 ? "bg-yellow-500/15 text-yellow-400" : "bg-red-500/15 text-red-400" }`}> {row.deb.hit_rate.toFixed(0)}% ) : ( )} {row.deb ? `${row.deb.mae.toFixed(1)}°` : "—"} {row.deb ? row.deb.total_days : "—"} {row.mu ? ( = 80 ? "bg-green-500/15 text-green-400" : row.mu.hit_rate >= 60 ? "bg-yellow-500/15 text-yellow-400" : "bg-red-500/15 text-red-400" }`}> {row.mu.hit_rate.toFixed(0)}% ) : ( )} {row.mu ? `${row.mu.mae.toFixed(1)}°` : "—"} {row.mu && row.mu.brier_score !== null ? ( {row.mu.brier_score.toFixed(3)} ) : ( "—" )} {row.mu ? row.mu.total_days : "—"}
无有效准确率记录
真值历史浏览

按城市和日期筛选查看历史真值记录。

打开真值历史 →
); }