OverviewPage.tsx
7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {
Activity, AlertTriangle, Database, FileText, GitBranch, Layers, Search, Sparkles, Upload,
} from "lucide-react";
import { useEffect, useState } from "react";
import { api } from "@/lib/api";
import { formatDuration, formatTime, MODE_INFO, STATUS_TEXT, STATUS_TONE } from "@/lib/utils";
import { Badge, Card, CardHeader, Empty, ErrorNote, Skeleton } from "@/components/ui";
import type { DocumentItem, InfoResponse, JobItem, QueryBrief } from "@/lib/types";
interface Overview {
info: InfoResponse;
documents: DocumentItem[];
jobs: JobItem[];
queries: QueryBrief[];
communities: { id: number; title: string; size: number; summary: string }[];
}
type Activity_ =
| { kind: "job"; at: string | null; job: JobItem }
| { kind: "query"; at: string | null; query: QueryBrief };
/**
* 概览做成一个精简仪表盘:只留「能一眼看懂现状、能立刻点下去做事」的东西。
* 类型分布/社区详情已经在图谱页的筛选面板里更完整地呈现,这里不重复。
*/
export function OverviewPage({ onNavigate }: { onNavigate: (page: string) => void }) {
const [data, setData] = useState<Overview | null>(null);
const [error, setError] = useState<string | null>(null);
const load = () => {
setError(null);
api.overview().then(setData).catch((e) => setError(e instanceof Error ? e.message : "加载失败"));
};
useEffect(load, []);
if (error) return <ErrorNote onRetry={load}>{error}</ErrorNote>;
if (!data) {
return (
<div className="grid gap-3 sm:grid-cols-2 xl:grid-cols-4">
{Array.from({ length: 4 }).map((_, i) => <Skeleton key={i} className="h-28 w-full rounded-xl" />)}
</div>
);
}
const { info, jobs, queries } = data;
const s = info.stats;
const empty = s.entity_count === 0;
const feed: Activity_[] = [
...jobs.map((j): Activity_ => ({ kind: "job", at: j.started_at, job: j })),
...queries.map((q): Activity_ => ({ kind: "query", at: q.created_at, query: q })),
].sort((a, b) => (b.at ?? "").localeCompare(a.at ?? "")).slice(0, 6);
return (
<div className="space-y-3">
{empty && (
<Card className="border-brand/30 bg-brand/5">
<div className="flex flex-col items-start gap-3 p-4 sm:flex-row sm:items-center">
<div className="grid h-10 w-10 shrink-0 place-items-center rounded-xl bg-brand/15">
<Sparkles className="h-5 w-5 text-brand" />
</div>
<div className="min-w-0 flex-1">
<p className="text-[14px] font-semibold">知识库还是空的</p>
<p className="text-xs text-muted">
上传一份业务文档,系统会抽取实体与关系、做实体消歧与社区划分,然后就可以在图谱上浏览、用深挖检索提问。
</p>
</div>
<button className="btn btn-primary shrink-0" onClick={() => onNavigate("documents")}>
<Upload className="h-3.5 w-3.5" />去上传文档
</button>
</div>
</Card>
)}
{/* 核心指标:仪表盘的主体,四块即可,点进去到对应页面 */}
<div className="grid gap-3 sm:grid-cols-2 xl:grid-cols-4">
<MetricCard icon={<FileText className="h-4 w-4" />} label="文档"
value={info.doc_count} hint={`${info.doc_done} 份已完成抽取`}
onClick={() => onNavigate("documents")} />
<MetricCard icon={<Database className="h-4 w-4" />} label="实体"
value={s.entity_count}
hint={`${Object.keys(s.type_dist).length} 种类型 · 平均度 ${s.avg_degree}`}
onClick={() => onNavigate("graph")} />
<MetricCard icon={<GitBranch className="h-4 w-4" />} label="关系"
value={s.relation_count}
hint={`${Object.keys(s.relation_dist).length} 种关系类型`}
onClick={() => onNavigate("graph")} />
<MetricCard icon={<Layers className="h-4 w-4" />} label="社区"
value={s.community_count} hint={`原文片段 ${info.index?.chunks?.count ?? 0} 个`}
onClick={() => onNavigate("graph")} />
</div>
{/* 质量提示:只在有问题时出现 */}
{s.entity_count > 0 && (s.leaf_ratio > 0.3 || s.isolated > 0) && (
<Card className="border-warn/30 bg-warn/5">
<div className="flex items-start gap-2.5 p-3">
<AlertTriangle className="mt-0.5 h-4 w-4 shrink-0 text-warn" />
<div className="min-w-0 text-[13px]">
<p className="font-medium">图谱质量提示</p>
<p className="mt-0.5 text-xs leading-relaxed text-muted">
当前有 <b className="text-ink tabular-nums">{s.leaf}</b> 个度=1 的叶子节点
(占 {(s.leaf_ratio * 100).toFixed(0)}%)
{s.isolated > 0 && <>、<b className="text-ink tabular-nums">{s.isolated}</b> 个孤立点</>}。
可以在「设置」里调高最低置信度,或到图谱页「消歧」标签检查重复实体。
</p>
</div>
</div>
</Card>
)}
{/* 最近动态:任务 + 检索合并成一条时间线,不用两块大卡片 */}
<Card>
<CardHeader title="最近动态" desc={info.queue_depth > 0 ? `队列中还有 ${info.queue_depth} 个任务` : undefined}
right={<Badge tone="bg-ink/5 text-muted"><Activity className="h-3 w-3" />{info.model}</Badge>} />
<div className="divide-y divide-line">
{feed.length === 0 && (
<Empty icon={<Search className="h-7 w-7" />} title="还没有任何动态"
desc="上传文档或去「深挖检索」问一个业务问题。" />
)}
{feed.map((a, i) => a.kind === "job" ? (
<button key={`j${i}`} className="flex w-full items-center gap-2 px-4 py-2 text-left text-[13px] hover:bg-ink/[0.02]"
onClick={() => onNavigate("documents")}>
<Badge tone={STATUS_TONE[a.job.status]}>{STATUS_TEXT[a.job.status] ?? a.job.status}</Badge>
<span className="min-w-0 flex-1 truncate text-[11.5px] text-muted">
#{a.job.id} {a.job.message || a.job.stage || "抽取任务"}
</span>
<span className="shrink-0 text-[11px] text-muted">{formatTime(a.job.started_at)}</span>
</button>
) : (
<button key={`q${i}`} className="flex w-full items-center gap-2 px-4 py-2 text-left text-[13px] hover:bg-ink/[0.02]"
onClick={() => onNavigate("query")}>
<Badge>{MODE_INFO[a.query.mode]?.name ?? a.query.mode}</Badge>
<span className="min-w-0 flex-1 truncate">{a.query.question}</span>
<span className="shrink-0 text-[11px] text-muted">
{formatDuration(a.query.duration_ms)} · {formatTime(a.query.created_at)}
</span>
</button>
))}
</div>
</Card>
</div>
);
}
function MetricCard({ icon, label, value, hint, onClick }: {
icon: React.ReactNode; label: string; value: number; hint: string; onClick: () => void;
}) {
return (
<button className="card p-4 text-left transition-colors hover:border-brand/40" onClick={onClick}>
<div className="flex items-center gap-1.5 text-muted">
{icon}<span className="text-xs font-medium">{label}</span>
</div>
<p className="mt-1.5 text-[26px] font-semibold leading-none tabular-nums">{value.toLocaleString()}</p>
<p className="mt-1.5 text-[11.5px] text-muted">{hint}</p>
</button>
);
}