Skip to content

PRINTFILM: AI short-video marketing and AI short-drama creation platform

feat(admin): refine dashboard charts, user stats, and layout

yi83d52614c460
diff --git a/admin/src/index.css b/admin/src/index.css
index 9abea16..3c69f39 100644
--- a/admin/src/index.css
+++ b/admin/src/index.css
@@ -1543,6 +1543,136 @@ textarea.admin-control {
background: linear-gradient(145deg, #f4ecdc 0%, #ebe0ca 48%, #e2d4b8 100%);
}
+.admin-dashboard-kpi.tone-rose {
+ background: linear-gradient(145deg, #fde8ea 0%, #f8d4d8 48%, #f0c0c8 100%);
+}
+
+.admin-dashboard-kpi.tone-mint {
+ background: linear-gradient(145deg, #dff5ee 0%, #c8ebdf 48%, #b0e0d2 100%);
+}
+
+.admin-dashboard-kpi.tone-slate {
+ background: linear-gradient(145deg, #e8edf0 0%, #d8e0e6 48%, #c8d2da 100%);
+}
+
+.admin-dashboard-kpi-grid--secondary {
+ margin-top: -4px;
+}
+
+.admin-dashboard-insight-grid {
+ display: grid;
+ gap: 10px;
+}
+
+.admin-dashboard-insight-grid.cols-2 {
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+}
+
+.admin-dashboard-insight-grid.cols-3 {
+ grid-template-columns: repeat(3, minmax(0, 1fr));
+}
+
+.admin-dashboard-insight-grid.cols-4 {
+ grid-template-columns: repeat(4, minmax(0, 1fr));
+}
+
+.admin-dashboard-insight-grid.cols-6 {
+ grid-template-columns: repeat(6, minmax(0, 1fr));
+}
+
+.admin-dashboard-insight {
+ display: flex;
+ align-items: flex-start;
+ gap: 10px;
+ min-height: 78px;
+ padding: 12px;
+ border-radius: 14px;
+ border: 1px solid rgba(255, 255, 255, 0.55);
+ box-shadow: 0 6px 18px rgba(18, 26, 22, 0.05);
+}
+
+.admin-dashboard-insight.tone-teal {
+ background: linear-gradient(145deg, #e8f6f0 0%, #d8eee4 100%);
+}
+
+.admin-dashboard-insight.tone-blue {
+ background: linear-gradient(145deg, #e8f1fc 0%, #d8e6f8 100%);
+}
+
+.admin-dashboard-insight.tone-purple {
+ background: linear-gradient(145deg, #f0eaf8 0%, #e4d8f0 100%);
+}
+
+.admin-dashboard-insight.tone-sand {
+ background: linear-gradient(145deg, #f8f2e8 0%, #efe4d4 100%);
+}
+
+.admin-dashboard-insight.tone-rose {
+ background: linear-gradient(145deg, #fcecee 0%, #f5dde1 100%);
+}
+
+.admin-dashboard-insight.tone-mint {
+ background: linear-gradient(145deg, #e4f6ef 0%, #d4ede4 100%);
+}
+
+.admin-dashboard-insight.tone-slate {
+ background: linear-gradient(145deg, #eef2f5 0%, #e2e8ed 100%);
+}
+
+.admin-dashboard-insight-icon {
+ display: grid;
+ place-items: center;
+ width: 34px;
+ height: 34px;
+ border-radius: 10px;
+ background: rgba(255, 255, 255, 0.55);
+ color: var(--admin-forest-deep);
+ flex-shrink: 0;
+}
+
+.admin-dashboard-insight-body {
+ min-width: 0;
+ flex: 1;
+}
+
+.admin-dashboard-insight-label {
+ font-size: 12px;
+ color: rgba(22, 40, 32, 0.62);
+}
+
+.admin-dashboard-insight-value {
+ margin-top: 4px;
+ font-family: var(--font-display);
+ font-size: 1.15rem;
+ font-weight: 700;
+ line-height: 1.1;
+ color: #142820;
+ font-variant-numeric: tabular-nums;
+}
+
+.admin-dashboard-insight-hint {
+ margin-top: 4px;
+ font-size: 11px;
+ color: rgba(22, 40, 32, 0.52);
+}
+
+.admin-dashboard-mini-card-head {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ margin-bottom: 8px;
+}
+
+.admin-dashboard-mini-card-icon {
+ display: grid;
+ place-items: center;
+ width: 30px;
+ height: 30px;
+ border-radius: 9px;
+ background: var(--admin-accent-soft);
+ color: var(--admin-forest);
+}
+
.admin-dashboard-kpi-icon {
position: absolute;
top: 14px;
@@ -1776,6 +1906,10 @@ textarea.admin-control {
gap: 12px;
}
+.admin-dashboard-body--full {
+ grid-column: 1 / -1;
+}
+
.admin-modal {
gap: 0 !important;
padding: 0 !important;
@@ -2242,7 +2376,7 @@ textarea.admin-control {
grid-template-columns: minmax(0, 1.6fr) minmax(240px, 1fr);
gap: 12px;
margin-top: 12px;
- align-items: stretch;
+ align-items: start;
}
.admin-dashboard-chart-main,
@@ -2250,15 +2384,21 @@ textarea.admin-control {
min-height: 0;
}
-.admin-dashboard-chart-main .admin-section-body,
-.admin-dashboard-chart-side .admin-section-body {
- min-height: 300px;
+.admin-dashboard-chart-stack {
+ display: flex;
+ flex-direction: column;
+ gap: 10px;
+ min-height: 0;
+}
+
+.admin-dashboard-chart-stack .admin-section {
+ min-height: 0;
}
.admin-chart-wrap {
width: 100%;
- height: 300px;
- min-height: 300px;
+ height: auto;
+ min-height: 0;
}
.admin-chart-wrap--compact {
@@ -2266,7 +2406,7 @@ textarea.admin-control {
flex-direction: column;
gap: 8px;
height: auto;
- min-height: 280px;
+ min-height: 0;
}
.admin-chart-empty {
@@ -2320,6 +2460,11 @@ textarea.admin-control {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
+ .admin-dashboard-insight-grid.cols-4,
+ .admin-dashboard-insight-grid.cols-6 {
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ }
+
.admin-dashboard-section-tabs {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
@@ -2370,6 +2515,13 @@ textarea.admin-control {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
+ .admin-dashboard-insight-grid.cols-2,
+ .admin-dashboard-insight-grid.cols-3,
+ .admin-dashboard-insight-grid.cols-4,
+ .admin-dashboard-insight-grid.cols-6 {
+ grid-template-columns: 1fr;
+ }
+
.admin-chip-filter-list.is-segment {
flex-wrap: wrap;
border-radius: 12px;
diff --git a/admin/src/pages/DashboardPage.tsx b/admin/src/pages/DashboardPage.tsx
index 801d82a..c37da56 100644
--- a/admin/src/pages/DashboardPage.tsx
+++ b/admin/src/pages/DashboardPage.tsx
@@ -29,8 +29,14 @@ import {
type DashboardFilterState,
} from "@/pages/dashboard/DashboardFilters";
import { DashboardKpiCard } from "@/pages/dashboard/DashboardKpiCard";
+import { DashboardInsightGrid } from "@/pages/dashboard/DashboardInsightGrid";
+import { DashboardPeriodKpis } from "@/pages/dashboard/DashboardPeriodKpis";
import { DashboardSectionTabs, type DashboardSection } from "@/pages/dashboard/DashboardSectionTabs";
+import { buildDomainInsights } from "@/pages/dashboard/dashboardInsightMaps";
+import { buildFinanceInsights, buildProjectInsights } from "@/pages/dashboard/dashboardSectionInsights";
+import { sumDailyUsage } from "@/pages/dashboard/dashboardMetrics";
import { UsageDistributionChart } from "@/pages/dashboard/UsageDistributionChart";
+import { TopUsersRankingChart } from "@/pages/dashboard/TopUsersRankingChart";
import { UsageTrendChart } from "@/pages/dashboard/UsageTrendChart";
type OrderRes = { items: AdminOrder[]; meta: PageMeta };
@@ -127,6 +133,11 @@ export function DashboardPage() {
const topUsers = stats?.top_users_by_charge ?? [];
const rangeLabel = dashboardRangeLabel(filters.days);
const showUsageFilters = section === "overview" || section === "usage";
+ const domainInsights = buildDomainInsights(byDomain, filters.metric, domainChartLabel);
+ const financeInsights = buildFinanceInsights(stats, upstreamUsage);
+ const projectInsights = buildProjectInsights(stats, sumDailyUsage(daily));
+ const metricHint =
+ filters.metric === "cost" ? "上游成本" : filters.metric === "calls" ? "调用次数" : "扣费金额";
return (
<div className="admin-page admin-dashboard-page">
@@ -166,6 +177,7 @@ export function DashboardPage() {
<DashboardSectionTabs value={section} onChange={setSection} />
{showUsageFilters ? <DashboardFilters value={filters} onChange={setFilters} /> : null}
+ {showUsageFilters ? <DashboardPeriodKpis stats={stats} filters={filters} loading={loading} /> : null}
{section === "overview" ? (
<>
@@ -181,7 +193,7 @@ export function DashboardPage() {
<PageSection
title="能力分布"
- description={rangeLabel}
+ description={`${rangeLabel} · ${metricHint}`}
bodyClassName="!pt-2"
className="admin-dashboard-chart-side admin-dashboard-glass min-h-0"
>
@@ -194,55 +206,40 @@ export function DashboardPage() {
</PageSection>
</div>
- <div className="admin-dashboard-mini-row">
- <PageSection
- title="今日已付"
- bodyClassName="!pt-0"
- className="admin-dashboard-glass min-h-0"
- >
- <div className="admin-dashboard-mini-metric">
- {kpiReady ? `¥${fenToYuan(stats!.order_paid_today_fen)}` : kpiPlaceholder}
- </div>
- <p className="admin-dashboard-mini-desc">今日充值到账</p>
- </PageSection>
+ <PageSection
+ title={`用户消费 TOP3(${rangeLabel})`}
+ actions={
+ <Link to="/orders?tab=usage" className="admin-link">
+ 更多 →
+ </Link>
+ }
+ bodyClassName="!pt-2"
+ className="admin-dashboard-glass min-h-0"
+ >
+ <TopUsersRankingChart users={topUsers.slice(0, 3)} metric="charge" />
+ </PageSection>
+
+ <div className="admin-dashboard-charts">
<PageSection
- title="今日调用"
- bodyClassName="!pt-0"
- className="admin-dashboard-glass min-h-0"
+ title="领域分布"
+ description={`${rangeLabel} · ${metricHint}`}
+ bodyClassName="!pt-2"
+ className="admin-dashboard-chart-main admin-dashboard-glass min-h-0"
>
- <div className="admin-dashboard-mini-metric">
- {kpiReady ? stats!.usage_calls_today ?? 0 : kpiPlaceholder}
- </div>
- <p className="admin-dashboard-mini-desc">
- {kpiReady ? `本月 ${stats!.usage_calls_month ?? 0} 次` : "调用次数"}
- </p>
+ <UsageDistributionChart
+ data={byDomain}
+ metric={filters.metric}
+ labelForKey={domainChartLabel}
+ variant="bar"
+ />
</PageSection>
<PageSection
- title={`用户消费 TOP3(${rangeLabel})`}
- actions={
- <Link to="/orders?tab=usage" className="admin-link">
- 更多 →
- </Link>
- }
- bodyClassName="!pt-0"
- className="admin-dashboard-glass min-h-0 admin-dashboard-mini-wide"
+ title="领域洞察"
+ description={`${rangeLabel} · ${metricHint}`}
+ bodyClassName="!pt-2"
+ className="admin-dashboard-chart-side admin-dashboard-glass min-h-0"
>
- <div className="admin-dashboard-rank-list">
- {topUsers.length === 0 ? (
- <div className="admin-chart-empty !min-h-[120px]">暂无排行</div>
- ) : (
- topUsers.slice(0, 3).map((u, idx) => (
- <div key={u.user_id} className="admin-dashboard-rank-item">
- <span className="admin-dashboard-rank-no">{idx + 1}</span>
- <div className="min-w-0 flex-1">
- <div className="truncate text-sm">{u.email ?? `用户 ${u.user_id}`}</div>
- <div className="text-[11px] text-[var(--admin-muted)]">{u.calls} 次调用</div>
- </div>
- <span className="admin-dashboard-rank-amount">¥{fenToYuan(u.charge_fen)}</span>
- </div>
- ))
- )}
- </div>
+ <DashboardInsightGrid items={domainInsights} columns={2} />
</PageSection>
</div>
</>
@@ -262,7 +259,7 @@ export function DashboardPage() {
<PageSection
title="能力分布"
- description={rangeLabel}
+ description={`${rangeLabel} · ${metricHint}`}
bodyClassName="!pt-2"
className="admin-dashboard-chart-side admin-dashboard-glass min-h-0"
>
@@ -275,8 +272,13 @@ export function DashboardPage() {
</PageSection>
</div>
- <div className="admin-dashboard-body">
- <PageSection title="领域分布" description={rangeLabel} bodyClassName="!pt-2" className="admin-dashboard-glass min-h-0">
+ <div className="admin-dashboard-charts">
+ <PageSection
+ title="领域分布"
+ description={`${rangeLabel} · ${metricHint}`}
+ bodyClassName="!pt-2"
+ className="admin-dashboard-chart-main admin-dashboard-glass min-h-0"
+ >
<UsageDistributionChart
data={byDomain}
metric={filters.metric}
@@ -284,7 +286,6 @@ export function DashboardPage() {
variant="bar"
/>
</PageSection>
-
<PageSection
title={`用户消费排行(${rangeLabel})`}
actions={
@@ -292,44 +293,11 @@ export function DashboardPage() {
用量明细 →
</Link>
}
- bodyClassName="!pt-0"
- className="admin-dashboard-glass min-h-0"
+ description={metricHint}
+ bodyClassName="!pt-2"
+ className="admin-dashboard-chart-side admin-dashboard-glass min-h-0"
>
- <div className="admin-table-wrap">
- <table>
- <thead>
- <tr>
- <th>#</th>
- <th>用户</th>
- <th>调用</th>
- <th>扣费</th>
- </tr>
- </thead>
- <tbody>
- {topUsers.length === 0 ? (
- <tr>
- <td colSpan={4} className="!text-center text-[var(--admin-muted)]">
- 暂无排行
- </td>
- </tr>
- ) : (
- topUsers.map((u, idx) => (
- <tr key={u.user_id}>
- <td>{idx + 1}</td>
- <td>
- <AdminEntityLink kind="user" id={u.user_id} label={u.email ?? undefined} />
- <div className="font-mono text-[11px] text-[var(--admin-muted)]">
- ID {u.user_id}
- </div>
- </td>
- <td>{u.calls}</td>
- <td>¥{fenToYuan(u.charge_fen)}</td>
- </tr>
- ))
- )}
- </tbody>
- </table>
- </div>
+ <TopUsersRankingChart users={topUsers} metric={filters.metric} />
</PageSection>
</div>
</>
@@ -337,6 +305,20 @@ export function DashboardPage() {
{section === "finance" ? (
<div className="admin-dashboard-body admin-dashboard-body--finance">
+ <PageSection
+ title="财务概览"
+ description="充值、扣费、成本与毛利"
+ actions={
+ <Link to="/finance" className="admin-link">
+ 财务列表 →
+ </Link>
+ }
+ bodyClassName="!pt-2"
+ className="admin-dashboard-glass min-h-0 admin-dashboard-body--full"
+ >
+ <DashboardInsightGrid items={financeInsights} columns={3} />
+ </PageSection>
+
<PageSection
title="Seedance 官方用量对照"
description={
@@ -443,6 +425,15 @@ export function DashboardPage() {
{section === "projects" ? (
<>
+ <PageSection
+ title="运维概览"
+ description="项目规模、调用与状态分布"
+ bodyClassName="!pt-2"
+ className="admin-dashboard-glass min-h-0"
+ >
+ <DashboardInsightGrid items={projectInsights} columns={4} />
+ </PageSection>
+
<div className="admin-dashboard-project-row">
<PageSection
title="AI短视频项目状态"
diff --git a/admin/src/pages/UsersPage.tsx b/admin/src/pages/UsersPage.tsx
index ef23c8a..344694f 100644
--- a/admin/src/pages/UsersPage.tsx
+++ b/admin/src/pages/UsersPage.tsx
@@ -68,12 +68,18 @@ export function UsersPage() {
useEffect(() => {
void load();
- void api<AdminStats>("/api/admin/stats?days=7")
- .then(setStats)
- .catch(() => undefined);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [page, pageSize]);
+ useEffect(() => {
+ void api<AdminStats>("/api/admin/stats?days=7")
+ .then(setStats)
+ .catch((err) => {
+ setStats(null);
+ toast.error(err instanceof Error ? err.message : "统计加载失败");
+ });
+ }, []);
+
useEffect(() => {
if (!userDetail.id || !data?.items) return;
const hit = data.items.find((u) => u.id === userDetail.id);
@@ -161,12 +167,12 @@ export function UsersPage() {
{ label: "总用户数", value: stats?.user_count ?? data?.meta.total ?? (loading ? "…" : "—") },
{
label: "本月调用",
- value: stats?.usage_calls_month ?? (loading ? "…" : "—"),
+ value: stats != null ? stats.usage_calls_month ?? 0 : loading ? "…" : "—",
hint: stats ? `今日 ${stats.usage_calls_today ?? 0} 次` : undefined,
},
{
label: "累计调用",
- value: stats?.usage_calls_total ?? (loading ? "…" : "—"),
+ value: stats != null ? stats.usage_calls_total ?? 0 : loading ? "…" : "—",
},
]}
/>
diff --git a/admin/src/pages/dashboard/DashboardInsightGrid.tsx b/admin/src/pages/dashboard/DashboardInsightGrid.tsx
new file mode 100644
index 0000000..f8cefef
--- /dev/null
+++ b/admin/src/pages/dashboard/DashboardInsightGrid.tsx
@@ -0,0 +1,48 @@
+import type { LucideIcon } from "lucide-react";
+import type { ReactNode } from "react";
+import { cn } from "@/lib/utils";
+
+export type DashboardInsightTone = "teal" | "blue" | "purple" | "sand" | "rose" | "mint" | "slate";
+
+export type DashboardInsightItem = {
+ key: string;
+ label: string;
+ value: ReactNode;
+ hint?: string;
+ icon: LucideIcon;
+ tone?: DashboardInsightTone;
+};
+
+type DashboardInsightGridProps = {
+ items: DashboardInsightItem[];
+ columns?: 2 | 3 | 4 | 6;
+ className?: string;
+};
+
+/** 仪表盘图标洞察格:能力/领域/周期指标 */
+export function DashboardInsightGrid({ items, columns = 4, className }: DashboardInsightGridProps) {
+ if (items.length === 0) {
+ return <div className="admin-chart-empty !min-h-[88px]">暂无数据</div>;
+ }
+
+ return (
+ <div className={cn("admin-dashboard-insight-grid", `cols-${columns}`, className)}>
+ {items.map((item) => {
+ const Icon = item.icon;
+ const tone = item.tone ?? "teal";
+ return (
+ <div key={item.key} className={cn("admin-dashboard-insight", `tone-${tone}`)}>
+ <div className="admin-dashboard-insight-icon">
+ <Icon className="h-4 w-4" />
+ </div>
+ <div className="admin-dashboard-insight-body">
+ <div className="admin-dashboard-insight-label">{item.label}</div>
+ <div className="admin-dashboard-insight-value">{item.value}</div>
+ {item.hint ? <div className="admin-dashboard-insight-hint">{item.hint}</div> : null}
+ </div>
+ </div>
+ );
+ })}
+ </div>
+ );
+}
diff --git a/admin/src/pages/dashboard/DashboardKpiCard.tsx b/admin/src/pages/dashboard/DashboardKpiCard.tsx
index 16b0cae..0d66718 100644
--- a/admin/src/pages/dashboard/DashboardKpiCard.tsx
+++ b/admin/src/pages/dashboard/DashboardKpiCard.tsx
@@ -2,7 +2,7 @@ import type { LucideIcon } from "lucide-react";
import type { ReactNode } from "react";
import { cn } from "@/lib/utils";
-type DashboardKpiTone = "teal" | "blue" | "purple" | "sand";
+type DashboardKpiTone = "teal" | "blue" | "purple" | "sand" | "rose" | "mint" | "slate";
type DashboardKpiCardProps = {
label: string;
diff --git a/admin/src/pages/dashboard/DashboardPeriodKpis.tsx b/admin/src/pages/dashboard/DashboardPeriodKpis.tsx
new file mode 100644
index 0000000..852024c
--- /dev/null
+++ b/admin/src/pages/dashboard/DashboardPeriodKpis.tsx
@@ -0,0 +1,56 @@
+import { Activity, Film, Percent, TrendingUp } from "lucide-react";
+import type { AdminStats } from "@/api/client";
+import { fenToYuan } from "@/lib/utils";
+import { dashboardRangeLabel, type DashboardFilterState } from "@/pages/dashboard/DashboardFilters";
+import { DashboardKpiCard } from "@/pages/dashboard/DashboardKpiCard";
+import { calcProfitFen, sumDailyUsage } from "@/pages/dashboard/dashboardMetrics";
+
+type DashboardPeriodKpisProps = {
+ stats: AdminStats | null;
+ filters: DashboardFilterState;
+ loading: boolean;
+};
+
+/** 第二行 KPI:随筛选时间窗变化的调用/扣费/毛利/项目规模 */
+export function DashboardPeriodKpis({ stats, filters, loading }: DashboardPeriodKpisProps) {
+ const placeholder = loading ? "…" : "—";
+ const rangeLabel = dashboardRangeLabel(filters.days);
+ const period = sumDailyUsage(stats?.daily_usage ?? []);
+ const profitFen = calcProfitFen(period.charge_fen, period.cost_fen);
+ const profitPct =
+ period.charge_fen > 0 ? `${((profitFen / period.charge_fen) * 100).toFixed(1)}%` : undefined;
+
+ return (
+ <div className="admin-dashboard-kpi-grid admin-dashboard-kpi-grid--secondary">
+ <DashboardKpiCard
+ label={`${rangeLabel}调用`}
+ value={stats ? period.calls.toLocaleString() : placeholder}
+ hint={stats ? `累计 ${stats.usage_calls_total ?? 0} 次` : "调用次数"}
+ icon={Activity}
+ tone="mint"
+ />
+ <DashboardKpiCard
+ label={`${rangeLabel}扣费`}
+ value={stats ? `¥${fenToYuan(period.charge_fen)}` : placeholder}
+ hint={stats ? `本月 ¥${fenToYuan(stats.usage_charge_month_fen ?? 0)}` : "用户扣费"}
+ icon={TrendingUp}
+ tone="blue"
+ />
+ <DashboardKpiCard
+ label={`${rangeLabel}毛利`}
+ value={stats ? `¥${fenToYuan(profitFen)}` : placeholder}
+ hint={stats ? `成本 ¥${fenToYuan(period.cost_fen)}` : "扣费减成本"}
+ icon={Percent}
+ tone="rose"
+ trend={profitPct ? `毛利率 ${profitPct}` : undefined}
+ />
+ <DashboardKpiCard
+ label="漫剧项目"
+ value={stats ? stats.drama_project_count ?? 0 : placeholder}
+ hint={stats ? `用户 ${stats.user_count}` : "项目规模"}
+ icon={Film}
+ tone="slate"
+ />
+ </div>
+ );
+}
diff --git a/admin/src/pages/dashboard/TopUsersRankingChart.tsx b/admin/src/pages/dashboard/TopUsersRankingChart.tsx
new file mode 100644
index 0000000..c430f7e
--- /dev/null
+++ b/admin/src/pages/dashboard/TopUsersRankingChart.tsx
@@ -0,0 +1,46 @@
+import type { AdminTopUser, AdminUsageBucket } from "@/api/client";
+import type { DashboardMetric } from "@/pages/dashboard/DashboardFilters";
+import { UsageDistributionChart } from "@/pages/dashboard/UsageDistributionChart";
+
+/** 用户排行转为柱状图数据桶 */
+export function topUsersToBuckets(users: AdminTopUser[]): AdminUsageBucket[] {
+ return users.map((user) => ({
+ key: String(user.user_id),
+ calls: user.calls,
+ charge_fen: user.charge_fen,
+ cost_fen: 0,
+ }));
+}
+
+/** 柱状图 Y 轴用户简称 */
+export function topUserChartLabel(userId: string, users: AdminTopUser[]): string {
+ const user = users.find((item) => String(item.user_id) === userId);
+ const email = user?.email ?? "";
+ const local = email.split("@")[0]?.trim();
+ if (local) return local.length > 12 ? `${local.slice(0, 11)}…` : local;
+ return `ID ${userId}`;
+}
+
+type TopUsersRankingChartProps = {
+ users: AdminTopUser[];
+ metric: DashboardMetric;
+};
+
+/** 用户消费排行:与领域分布一致的横向柱状图 */
+export function TopUsersRankingChart({ users, metric }: TopUsersRankingChartProps) {
+ const rows = topUsersToBuckets(users);
+ if (rows.length === 0) {
+ return <div className="admin-chart-empty">暂无排行</div>;
+ }
+
+ return (
+ <UsageDistributionChart
+ data={rows}
+ metric={metric}
+ labelForKey={(key) => topUserChartLabel(key, users)}
+ variant="bar"
+ chartHeight={Math.max(220, rows.length * 36)}
+ yAxisWidth={96}
+ />
+ );
+}
diff --git a/admin/src/pages/dashboard/UsageDistributionChart.tsx b/admin/src/pages/dashboard/UsageDistributionChart.tsx
index 91c6440..f98ba1d 100644
--- a/admin/src/pages/dashboard/UsageDistributionChart.tsx
+++ b/admin/src/pages/dashboard/UsageDistributionChart.tsx
@@ -27,6 +27,8 @@ type UsageDistributionChartProps = {
metric: DashboardMetric;
labelForKey: (key: string) => string;
variant?: "bar" | "donut";
+ chartHeight?: number;
+ yAxisWidth?: number;
};
function readMetric(row: AdminUsageBucket, metric: DashboardMetric): number {
@@ -46,6 +48,8 @@ export function UsageDistributionChart({
metric,
labelForKey,
variant = "bar",
+ chartHeight = 220,
+ yAxisWidth = 72,
}: UsageDistributionChartProps) {
const chartData = data
.map((row) => ({
@@ -109,13 +113,13 @@ export function UsageDistributionChart({
return (
<div className="admin-chart-wrap admin-chart-wrap--compact">
- <ResponsiveContainer width="100%" height={220}>
+ <ResponsiveContainer width="100%" height={chartHeight}>
<BarChart data={chartData} layout="vertical" margin={{ top: 0, right: 8, left: 0, bottom: 0 }}>
<XAxis type="number" hide />
<YAxis
type="category"
dataKey="name"
- width={72}
+ width={yAxisWidth}
tick={{ fill: "var(--admin-muted)", fontSize: 11 }}
axisLine={false}
tickLine={false}
diff --git a/admin/src/pages/dashboard/UsageTrendChart.tsx b/admin/src/pages/dashboard/UsageTrendChart.tsx
index d91402b..94f7150 100644
--- a/admin/src/pages/dashboard/UsageTrendChart.tsx
+++ b/admin/src/pages/dashboard/UsageTrendChart.tsx
@@ -52,7 +52,7 @@ export function UsageTrendChart({ data, metric }: UsageTrendChartProps) {
return (
<div className="admin-chart-wrap">
- <ResponsiveContainer width="100%" height={280}>
+ <ResponsiveContainer width="100%" height={240}>
<AreaChart data={chartData} margin={{ top: 8, right: 8, left: 0, bottom: 0 }}>
<defs>
<linearGradient id="usageTrendFill" x1="0" y1="0" x2="0" y2="1">
diff --git a/admin/src/pages/dashboard/dashboardInsightMaps.tsx b/admin/src/pages/dashboard/dashboardInsightMaps.tsx
new file mode 100644
index 0000000..2f6e3cd
--- /dev/null
+++ b/admin/src/pages/dashboard/dashboardInsightMaps.tsx
@@ -0,0 +1,81 @@
+import {
+ Clapperboard,
+ Film,
+ Image,
+ MessageSquareText,
+ Mic,
+ Palette,
+ Video,
+ Webhook,
+ Wrench,
+ type LucideIcon,
+} from "lucide-react";
+import type { AdminUsageBucket } from "@/api/client";
+import type { DashboardMetric } from "@/pages/dashboard/DashboardFilters";
+import type { DashboardInsightItem, DashboardInsightTone } from "@/pages/dashboard/DashboardInsightGrid";
+import { formatDashboardMetric, readBucketMetric } from "@/pages/dashboard/dashboardMetrics";
+
+const CAPABILITY_META: Record<string, { label: string; icon: LucideIcon; tone: DashboardInsightTone }> = {
+ llm: { label: "LLM 文本", icon: MessageSquareText, tone: "purple" },
+ image: { label: "生图", icon: Image, tone: "blue" },
+ video: { label: "视频", icon: Video, tone: "teal" },
+ tts: { label: "配音", icon: Mic, tone: "sand" },
+ unknown: { label: "其他", icon: Wrench, tone: "slate" },
+};
+
+const DOMAIN_META: Record<string, { label: string; icon: LucideIcon; tone: DashboardInsightTone }> = {
+ drama: { label: "漫剧", icon: Film, tone: "teal" },
+ kepu: { label: "AI短视频", icon: Clapperboard, tone: "blue" },
+ api: { label: "开放 API", icon: Webhook, tone: "purple" },
+ tools: { label: "工具", icon: Wrench, tone: "sand" },
+ studio: { label: "工作室", icon: Palette, tone: "mint" },
+ unknown: { label: "其他", icon: Wrench, tone: "slate" },
+};
+
+function buildInsightItems(
+ rows: AdminUsageBucket[],
+ metric: DashboardMetric,
+ metaMap: Record<string, { label: string; icon: LucideIcon; tone: DashboardInsightTone }>,
+ labelForKey?: (key: string) => string,
+): DashboardInsightItem[] {
+ const prepared = [...rows]
+ .map((row) => ({
+ row,
+ value: readBucketMetric(row, metric),
+ }))
+ .filter((item) => item.value > 0);
+ const total = prepared.reduce((sum, item) => sum + item.value, 0);
+
+ return prepared
+ .sort((a, b) => b.value - a.value)
+ .map(({ row, value }) => {
+ const meta = metaMap[row.key] ?? metaMap.unknown;
+ const sharePct = total > 0 ? ((value / total) * 100).toFixed(1) : null;
+ const shareHint = sharePct ? `占比 ${sharePct}%` : undefined;
+ return {
+ key: row.key,
+ label: labelForKey?.(row.key) ?? meta.label,
+ value: formatDashboardMetric(value, metric),
+ hint: [shareHint, `${row.calls.toLocaleString()} 次调用`].filter(Boolean).join(" · "),
+ icon: meta.icon,
+ tone: meta.tone,
+ };
+ });
+}
+
+/** 能力分布洞察卡片 */
+export function buildCapabilityInsights(
+ rows: AdminUsageBucket[],
+ metric: DashboardMetric,
+): DashboardInsightItem[] {
+ return buildInsightItems(rows, metric, CAPABILITY_META);
+}
+
+/** 领域分布洞察卡片 */
+export function buildDomainInsights(
+ rows: AdminUsageBucket[],
+ metric: DashboardMetric,
+ labelForKey: (key: string) => string,
+): DashboardInsightItem[] {
+ return buildInsightItems(rows, metric, DOMAIN_META, labelForKey);
+}
diff --git a/admin/src/pages/dashboard/dashboardMetrics.ts b/admin/src/pages/dashboard/dashboardMetrics.ts
new file mode 100644
index 0000000..58085ea
--- /dev/null
+++ b/admin/src/pages/dashboard/dashboardMetrics.ts
@@ -0,0 +1,46 @@
+import type { AdminDailyUsage, AdminStats, AdminUsageBucket } from "@/api/client";
+import type { DashboardMetric } from "@/pages/dashboard/DashboardFilters";
+import { fenToYuan } from "@/lib/utils";
+
+/** 时间窗内日趋势汇总 */
+export function sumDailyUsage(daily: AdminDailyUsage[]) {
+ return daily.reduce(
+ (acc, row) => ({
+ calls: acc.calls + row.calls,
+ charge_fen: acc.charge_fen + row.charge_fen,
+ cost_fen: acc.cost_fen + (row.cost_fen ?? 0),
+ }),
+ { calls: 0, charge_fen: 0, cost_fen: 0 },
+ );
+}
+
+/** 按当前图表指标读取桶数值 */
+export function readBucketMetric(row: AdminUsageBucket, metric: DashboardMetric): number {
+ if (metric === "cost") return row.cost_fen ?? 0;
+ if (metric === "calls") return row.calls;
+ return row.charge_fen;
+}
+
+/** 格式化指标展示值 */
+export function formatDashboardMetric(value: number, metric: DashboardMetric): string {
+ if (metric === "calls") return value.toLocaleString();
+ return `¥${fenToYuan(value)}`;
+}
+
+/** 估算毛利(扣费 - 成本) */
+export function calcProfitFen(chargeFen: number, costFen: number): number {
+ return chargeFen - costFen;
+}
+
+/** 科普项目总数(各状态之和) */
+export function sumProjectStatuses(counts: Record<string, number> | undefined): number {
+ return Object.values(counts ?? {}).reduce((sum, n) => sum + n, 0);
+}
+
+/** 漫剧 + 科普项目规模摘要 */
+export function projectScaleHint(stats: AdminStats | null): string | undefined {
+ if (!stats) return undefined;
+ const kepu = sumProjectStatuses(stats.project_status_counts);
+ const drama = stats.drama_project_count ?? 0;
+ return `科普 ${kepu} · 漫剧 ${drama}`;
+}
diff --git a/admin/src/pages/dashboard/dashboardSectionInsights.tsx b/admin/src/pages/dashboard/dashboardSectionInsights.tsx
new file mode 100644
index 0000000..c4a8c0a
--- /dev/null
+++ b/admin/src/pages/dashboard/dashboardSectionInsights.tsx
@@ -0,0 +1,161 @@
+import {
+ Activity,
+ Banknote,
+ CheckCircle2,
+ CircleDollarSign,
+ Clapperboard,
+ Film,
+ Layers,
+ Percent,
+ TrendingDown,
+ TrendingUp,
+ Wallet,
+ Zap,
+ type LucideIcon,
+} from "lucide-react";
+import type { AdminStats, AdminUpstreamUsage } from "@/api/client";
+import type { DashboardInsightItem } from "@/pages/dashboard/DashboardInsightGrid";
+import { calcProfitFen, sumDailyUsage, sumProjectStatuses } from "@/pages/dashboard/dashboardMetrics";
+import { fenToYuan } from "@/lib/utils";
+import { projectStatusLabel } from "@/lib/statusLabels";
+
+/** 财务账单 Tab 图标指标 */
+export function buildFinanceInsights(
+ stats: AdminStats | null,
+ upstream: AdminUpstreamUsage | null,
+): DashboardInsightItem[] {
+ if (!stats) return [];
+
+ const monthCharge = stats.usage_charge_month_fen ?? 0;
+ const monthCost = stats.usage_cost_month_fen ?? 0;
+ const profitFen = calcProfitFen(monthCharge, monthCost);
+ const recent = (upstream?.series ?? []).slice(-7);
+ const localCost7 = recent.reduce((sum, row) => sum + row.local_cost_fen, 0);
+ const officialCost7 = recent.reduce((sum, row) => sum + row.official_cost_fen, 0);
+ const delta7 = localCost7 - officialCost7;
+
+ const items: DashboardInsightItem[] = [
+ {
+ key: "paid-total",
+ label: "累计充值",
+ value: `¥${fenToYuan(stats.order_paid_total_fen)}`,
+ hint: `今日 ¥${fenToYuan(stats.order_paid_today_fen)}`,
+ icon: Banknote,
+ tone: "blue",
+ },
+ {
+ key: "charge-month",
+ label: "本月扣费",
+ value: `¥${fenToYuan(monthCharge)}`,
+ hint: `今日 ¥${fenToYuan(stats.usage_charge_today_fen ?? 0)}`,
+ icon: Zap,
+ tone: "purple",
+ },
+ {
+ key: "cost-month",
+ label: "本月成本",
+ value: `¥${fenToYuan(monthCost)}`,
+ hint: `今日 ¥${fenToYuan(stats.usage_cost_today_fen ?? 0)}`,
+ icon: Wallet,
+ tone: "sand",
+ },
+ {
+ key: "profit-month",
+ label: "本月毛利",
+ value: `¥${fenToYuan(profitFen)}`,
+ hint: monthCharge > 0 ? `毛利率 ${((profitFen / monthCharge) * 100).toFixed(1)}%` : undefined,
+ icon: Percent,
+ tone: profitFen >= 0 ? "mint" : "rose",
+ },
+ ];
+
+ if (upstream?.configured && officialCost7 > 0) {
+ items.push({
+ key: "upstream-delta",
+ label: "近 7 日成本差额",
+ value: `¥${fenToYuan(delta7)}`,
+ hint: `本地 ¥${fenToYuan(localCost7)} / 官方 ¥${fenToYuan(officialCost7)}`,
+ icon: delta7 >= 0 ? TrendingUp : TrendingDown,
+ tone: delta7 >= 0 ? "teal" : "rose",
+ });
+ }
+
+ items.push({
+ key: "paid-today",
+ label: "今日到账",
+ value: `¥${fenToYuan(stats.order_paid_today_fen)}`,
+ hint: "充值订单",
+ icon: CircleDollarSign,
+ tone: "teal",
+ });
+
+ return items;
+}
+
+const STATUS_META: Record<string, { icon: LucideIcon; tone: DashboardInsightItem["tone"] }> = {
+ DONE: { icon: CheckCircle2, tone: "mint" },
+ DRAFT: { icon: Layers, tone: "slate" },
+ FAILED: { icon: TrendingDown, tone: "rose" },
+ SCRIPTING: { icon: Clapperboard, tone: "blue" },
+ IMAGING: { icon: Film, tone: "purple" },
+ VIDEOING: { icon: Activity, tone: "teal" },
+};
+
+/** 项目运维 Tab 图标指标 */
+export function buildProjectInsights(stats: AdminStats | null, periodDaily: ReturnType<typeof sumDailyUsage>): DashboardInsightItem[] {
+ if (!stats) return [];
+
+ const kepuTotal = sumProjectStatuses(stats.project_status_counts);
+ const items: DashboardInsightItem[] = [
+ {
+ key: "kepu-total",
+ label: "科普项目",
+ value: kepuTotal,
+ hint: `漫剧 ${stats.drama_project_count ?? 0} 部`,
+ icon: Clapperboard,
+ tone: "blue",
+ },
+ {
+ key: "drama-total",
+ label: "漫剧项目",
+ value: stats.drama_project_count ?? 0,
+ hint: "全站项目",
+ icon: Film,
+ tone: "teal",
+ },
+ {
+ key: "calls-today",
+ label: "今日调用",
+ value: (stats.usage_calls_today ?? 0).toLocaleString(),
+ hint: `本月 ${stats.usage_calls_month ?? 0} 次`,
+ icon: Activity,
+ tone: "mint",
+ },
+ {
+ key: "calls-total",
+ label: "累计调用",
+ value: (stats.usage_calls_total ?? 0).toLocaleString(),
+ hint: `近窗 ${periodDaily.calls.toLocaleString()} 次`,
+ icon: Layers,
+ tone: "slate",
+ },
+ ];
+
+ const statusEntries = Object.entries(stats.project_status_counts ?? {})
+ .sort((a, b) => b[1] - a[1])
+ .slice(0, 4);
+
+ for (const [status, count] of statusEntries) {
+ const meta = STATUS_META[status] ?? { icon: Layers, tone: "sand" as const };
+ items.push({
+ key: `status-${status}`,
+ label: projectStatusLabel(status),
+ value: count,
+ hint: kepuTotal > 0 ? `占科普 ${((count / kepuTotal) * 100).toFixed(1)}%` : undefined,
+ icon: meta.icon,
+ tone: meta.tone,
+ });
+ }
+
+ return items;
+}