perf(wiki): subgraph API and interactive exploration for large graphs

Wikis with tens of thousands of pages used to crash the browser trying
to render the entire link graph at once (30MB+ JSON, 100k+ SVG elements).
This change moves the graph viewer from "fetch everything, render
everything" to "fetch a slice, expand on demand" with several matching
UX improvements.

Backend

* GET /wiki/graph now accepts `mode` (overview | ego), `center`, `depth`,
  `types`, `limit` query params. Default is overview top-500 by
  link_count, capped at 2000. Response includes a Meta object so the
  frontend can render a truncation hint and drive the UI.
* Pure helper `computeGraphSubset` extracted for testability; six unit
  tests cover overview truncation / type filter / ego BFS / missing
  center error.
* WikiLintService passes Limit=0 (uncapped) so link integrity checks
  still walk every page.
* Repo `Search` adds CASE-based relevance ranking (title 4, slug 3,
  summary 2, content 1) so full-text results put the most obvious
  matches first instead of whatever was updated most recently.

Frontend

* Graph viewer fetches overview on entry; ego pivot on double-click,
  search, wiki-link click, URL ?slug=, global issues jump.
* Shift+click or hover ⊕ button blooms neighbors onto the current
  canvas additively. Bloom tracks generations and LRU-evicts oldest
  when total exceeds 1500 nodes; ego center / selected node / latest
  anchor are always protected.
* "Grow frontier (N)" legend action expands every dashed-ring node in
  one click (6-way concurrency) while skipping Index/Log super-nodes
  that would otherwise dump the whole wiki onto the canvas.
* Node dashed expansion ring + drawer "X/Y neighbors shown" hint tell
  users which nodes still have neighbors to load.
* Search dropdown uses remote full-text search (debounced, sequence-
  numbered to drop stale responses) and falls back to the overview
  top-500 snapshot when the input is empty.
* Type filters now round-trip to the server so top-N is always
  computed from the user's active type set rather than hiding nodes
  client-side and shrinking the view.
* Status card replaces the cramped "centered on X · N hops · M nodes"
  line with a structured focus/overview summary, resolving slugs to
  page titles.
* Help popover in the legend lists every canvas shortcut.

i18n entries added for zh-CN / en-US / ko-KR / ru-RU.
This commit is contained in:
wizardchen
2026-05-08 13:29:19 +08:00
committed by lyingbug
parent fc5d16e331
commit df1e9a1bfd
13 changed files with 1962 additions and 137 deletions
+26 -1
View File
@@ -329,6 +329,19 @@ func escapeLikePattern(s string) string {
// Search performs case-insensitive POSIX regex search on wiki pages within a knowledge base.
// The query is interpreted as a PostgreSQL regular expression (via ~*).
//
// Results are ranked by where the query hit, highest-relevance first:
//
// title hit → rank 4 (most obvious intent: user typed what the page is called)
// slug hit → rank 3 (url-like identifiers, direct jump)
// summary hit → rank 2 (short authored abstract)
// content hit → rank 1 (body mention — often surfaces unrelated pages whose
// prose merely mentions the query as trivia)
//
// Without this ranking, a user searching for "王新" on a 4万-page wiki will
// see pages like "华为" or "Index" ahead of the actual 王新 page just
// because they mention 王新 in their body and were updated more recently.
// updated_at stays as the tiebreaker so same-rank ties stay deterministic.
func (r *wikiPageRepository) Search(ctx context.Context, kbID string, query string, limit int) ([]*types.WikiPage, error) {
if limit <= 0 {
limit = 10
@@ -337,12 +350,24 @@ func (r *wikiPageRepository) Search(ctx context.Context, kbID string, query stri
limit = 50
}
// CASE expression is evaluated per-row during SELECT; we order by the
// alias so the DB only computes the rank once. Parameterized four
// times with the same regex to avoid coupling to GORM's positional
// arg rewriting quirks.
rankExpr := "CASE " +
"WHEN title ~* ? THEN 4 " +
"WHEN slug ~* ? THEN 3 " +
"WHEN summary ~* ? THEN 2 " +
"WHEN content ~* ? THEN 1 " +
"ELSE 0 END AS match_rank"
var pages []*types.WikiPage
if err := r.db.WithContext(ctx).
Select("*, "+rankExpr, query, query, query, query).
Where("knowledge_base_id = ? AND (title ~* ? OR content ~* ? OR summary ~* ? OR slug ~* ?)",
kbID, query, query, query, query).
Where("status != ?", "archived").
Order("updated_at DESC").
Order("match_rank DESC, updated_at DESC").
Limit(limit).
Find(&pages).Error; err != nil {
return nil, err
+13 -7
View File
@@ -14,12 +14,12 @@ import (
type WikiLintIssueType string
const (
LintIssueOrphanPage WikiLintIssueType = "orphan_page"
LintIssueBrokenLink WikiLintIssueType = "broken_link"
LintIssueStaleRef WikiLintIssueType = "stale_ref"
LintIssueOrphanPage WikiLintIssueType = "orphan_page"
LintIssueBrokenLink WikiLintIssueType = "broken_link"
LintIssueStaleRef WikiLintIssueType = "stale_ref"
LintIssueMissingCrossRef WikiLintIssueType = "missing_cross_ref"
LintIssueEmptyContent WikiLintIssueType = "empty_content"
LintIssueDuplicateSlug WikiLintIssueType = "duplicate_slug"
LintIssueEmptyContent WikiLintIssueType = "empty_content"
LintIssueDuplicateSlug WikiLintIssueType = "duplicate_slug"
)
// WikiLintIssueSeverity defines the severity of a lint issue
@@ -90,8 +90,14 @@ func (s *WikiLintService) RunLint(ctx context.Context, kbID string) (*WikiLintRe
return nil, fmt.Errorf("get stats: %w", err)
}
// Get graph for link analysis
graph, err := s.wikiService.GetGraph(ctx, kbID)
// Get graph for link analysis. Lint needs the FULL graph to detect
// orphans and broken links across every page, so we pass Limit=0
// which the service treats as "no cap".
graph, err := s.wikiService.GetGraph(ctx, &types.WikiGraphRequest{
KnowledgeBaseID: kbID,
Mode: types.WikiGraphModeOverview,
Limit: 0,
})
if err != nil {
return nil, fmt.Errorf("get graph: %w", err)
}
+230 -21
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"regexp"
"sort"
"strings"
"time"
@@ -258,50 +259,258 @@ func (s *wikiPageService) GetLog(ctx context.Context, kbID string) (*types.WikiP
return page, nil
}
// GetGraph returns the link graph data for visualization
func (s *wikiPageService) GetGraph(ctx context.Context, kbID string) (*types.WikiGraphData, error) {
pages, err := s.repo.ListAll(ctx, kbID)
// GetGraph returns a slice of the wiki link graph for visualization.
//
// Two modes are supported:
//
// - WikiGraphModeOverview (default): returns the top `Limit` pages sorted
// by link_count (in+out), plus every edge that connects two surviving
// nodes. This is what the frontend fetches on the first graph open —
// 4万-page wikis would otherwise ship ~30MB of JSON and crash the
// browser trying to render 100k SVG elements.
//
// - WikiGraphModeEgo: returns the BFS neighborhood of `Center` up to
// `Depth` undirected hops, capped at `Limit` total nodes. The
// frontend uses this to drill down when the user clicks / searches a
// node in the overview.
//
// `Types` is an optional page_type allow-list applied to both the candidate
// node set and (in ego mode) the frontier expansion. Leaving it empty means
// no type filter.
//
// `Limit <= 0` disables the cap entirely and is reserved for internal
// callers like the lint service that need to walk every page. The HTTP
// handler always clamps Limit into a safe range so external traffic can
// never opt out of truncation.
//
// Implementation note: pages are still fetched via repo.ListAll. At 4万
// pages that's ~10MB of rows + deserialization, which is already on the
// expensive side but still tractable and keeps the repository interface
// unchanged. Pushing the filter/top-N down into SQL is a follow-up step
// (cache layer + DB-side projection) — see CLAUDE.md plan.
func (s *wikiPageService) GetGraph(ctx context.Context, req *types.WikiGraphRequest) (*types.WikiGraphData, error) {
if req == nil {
return nil, errors.New("wiki graph request is required")
}
pages, err := s.repo.ListAll(ctx, req.KnowledgeBaseID)
if err != nil {
return nil, err
}
return computeGraphSubset(pages, req)
}
nodeMap := make(map[string]*types.WikiGraphNode)
var edges []types.WikiGraphEdge
// computeGraphSubset is the pure I/O-free core of GetGraph. It takes the
// full page list and a request description and returns the subgraph the
// caller asked for. Extracted from GetGraph so tests can exercise the
// mode/limit/type-filter behavior without plumbing a full repository mock.
func computeGraphSubset(pages []*types.WikiPage, req *types.WikiGraphRequest) (*types.WikiGraphData, error) {
mode := req.Mode
if mode == "" {
mode = types.WikiGraphModeOverview
}
// Build nodes
// Pre-compute link_count and the type allow-list used for candidate
// filtering. We keep the full page list around so ego mode can still
// traverse through neighbors whose type is in the allow-list.
typeAllow := make(map[string]bool, len(req.Types))
for _, t := range req.Types {
if t != "" {
typeAllow[t] = true
}
}
hasTypeFilter := len(typeAllow) > 0
pageBySlug := make(map[string]*types.WikiPage, len(pages))
linkCount := make(map[string]int, len(pages))
for _, p := range pages {
linkCount := len(p.InLinks) + len(p.OutLinks)
nodeMap[p.Slug] = &types.WikiGraphNode{
Slug: p.Slug,
Title: p.Title,
PageType: p.PageType,
LinkCount: linkCount,
pageBySlug[p.Slug] = p
linkCount[p.Slug] = len(p.InLinks) + len(p.OutLinks)
}
// Select the node slug set for the requested slice.
var selected map[string]struct{}
switch mode {
case types.WikiGraphModeEgo:
if req.Center == "" {
return nil, errors.New("ego graph requires a center slug")
}
if _, ok := pageBySlug[req.Center]; !ok {
return nil, fmt.Errorf("ego center slug %q not found", req.Center)
}
depth := req.Depth
if depth < 1 {
depth = 1
}
selected = bfsEgoSlugs(pageBySlug, req.Center, depth, typeAllow, req.Limit)
default:
// overview: keep only type-allowed candidates, sort by link_count desc, cap.
candidates := make([]*types.WikiPage, 0, len(pages))
for _, p := range pages {
if hasTypeFilter && !typeAllow[p.PageType] {
continue
}
candidates = append(candidates, p)
}
sort.SliceStable(candidates, func(i, j int) bool {
li := linkCount[candidates[i].Slug]
lj := linkCount[candidates[j].Slug]
if li != lj {
return li > lj
}
// Stable tiebreaker keeps the API deterministic between calls.
return candidates[i].Slug < candidates[j].Slug
})
if req.Limit > 0 && len(candidates) > req.Limit {
candidates = candidates[:req.Limit]
}
selected = make(map[string]struct{}, len(candidates))
for _, p := range candidates {
selected[p.Slug] = struct{}{}
}
}
// Build edges from outbound links
// Build nodes from the selected set.
nodes := make([]types.WikiGraphNode, 0, len(selected))
for slug := range selected {
p := pageBySlug[slug]
nodes = append(nodes, types.WikiGraphNode{
Slug: p.Slug,
Title: p.Title,
PageType: p.PageType,
LinkCount: linkCount[slug],
})
}
// Deterministic node ordering — the map iteration above is random.
sort.Slice(nodes, func(i, j int) bool {
if nodes[i].LinkCount != nodes[j].LinkCount {
return nodes[i].LinkCount > nodes[j].LinkCount
}
return nodes[i].Slug < nodes[j].Slug
})
// Build edges, keeping only edges whose endpoints both survived selection.
var edges []types.WikiGraphEdge
for _, p := range pages {
if _, ok := selected[p.Slug]; !ok {
continue
}
for _, target := range p.OutLinks {
if _, exists := nodeMap[target]; exists {
edges = append(edges, types.WikiGraphEdge{
Source: p.Slug,
Target: target,
})
if _, ok := selected[target]; !ok {
continue
}
edges = append(edges, types.WikiGraphEdge{
Source: p.Slug,
Target: target,
})
}
}
// total is the count of candidate nodes before truncation — i.e. the
// population the frontend would need to fetch if it asked for the
// whole graph. For overview this respects the type filter; for ego
// it is the total KB page count (the user still sees "X of Y" based
// on the full wiki, not a filtered denominator).
total := len(pages)
if mode == types.WikiGraphModeOverview && hasTypeFilter {
total = 0
for _, p := range pages {
if typeAllow[p.PageType] {
total++
}
}
}
nodes := make([]types.WikiGraphNode, 0, len(nodeMap))
for _, n := range nodeMap {
nodes = append(nodes, *n)
meta := types.WikiGraphMeta{
Mode: mode,
Total: total,
Returned: len(nodes),
Truncated: len(nodes) < total,
}
if mode == types.WikiGraphModeEgo {
meta.Center = req.Center
meta.Depth = req.Depth
if meta.Depth < 1 {
meta.Depth = 1
}
}
return &types.WikiGraphData{
Nodes: nodes,
Edges: edges,
Meta: meta,
}, nil
}
// bfsEgoSlugs computes the undirected BFS neighborhood of `center` up to
// `depth` hops using both inbound and outbound links. Type-filtered pages
// are excluded from the result but are also NOT traversed through — so a
// filter that hides "index" pages will not leak the whole wiki via the
// index. The caller guarantees center exists in pageBySlug.
func bfsEgoSlugs(
pageBySlug map[string]*types.WikiPage,
center string,
depth int,
typeAllow map[string]bool,
limit int,
) map[string]struct{} {
hasTypeFilter := len(typeAllow) > 0
centerPage, ok := pageBySlug[center]
if !ok {
return map[string]struct{}{}
}
// If the center itself fails the type filter we honor the filter and
// return an empty set — the handler will surface Returned=0.
if hasTypeFilter && !typeAllow[centerPage.PageType] {
return map[string]struct{}{}
}
visited := map[string]struct{}{center: {}}
frontier := []string{center}
for hop := 0; hop < depth; hop++ {
if limit > 0 && len(visited) >= limit {
break
}
next := make([]string, 0, len(frontier))
for _, slug := range frontier {
p, ok := pageBySlug[slug]
if !ok {
continue
}
neighbors := make([]string, 0, len(p.OutLinks)+len(p.InLinks))
neighbors = append(neighbors, p.OutLinks...)
neighbors = append(neighbors, p.InLinks...)
for _, nb := range neighbors {
if _, seen := visited[nb]; seen {
continue
}
np, exists := pageBySlug[nb]
if !exists {
continue
}
if hasTypeFilter && !typeAllow[np.PageType] {
continue
}
visited[nb] = struct{}{}
next = append(next, nb)
if limit > 0 && len(visited) >= limit {
break
}
}
if limit > 0 && len(visited) >= limit {
break
}
}
frontier = next
if len(frontier) == 0 {
break
}
}
return visited
}
// GetStats returns aggregate statistics about the wiki
func (s *wikiPageService) GetStats(ctx context.Context, kbID string) (*types.WikiStats, error) {
counts, err := s.repo.CountByType(ctx, kbID)
@@ -128,3 +128,250 @@ func TestRemoveString(t *testing.T) {
t.Errorf("Expected 4 items (nothing removed), got %d", len(result2))
}
}
// makeGraphFixture builds a small synthetic wiki for GetGraph tests.
//
// Edges (directed):
//
// hub -> a, hub -> b, hub -> c, hub -> d
// a -> hub
// b -> hub
// c -> d
// x -> y (isolated 2-node cluster, disconnected from hub)
//
// Page types are chosen so type-filter tests can exclude specific nodes.
func makeGraphFixture() []*types.WikiPage {
return []*types.WikiPage{
{
Slug: "hub",
Title: "Hub",
PageType: types.WikiPageTypeSummary,
OutLinks: types.StringArray{"a", "b", "c", "d"},
InLinks: types.StringArray{"a", "b"},
},
{
Slug: "a",
Title: "A",
PageType: types.WikiPageTypeEntity,
OutLinks: types.StringArray{"hub"},
InLinks: types.StringArray{"hub"},
},
{
Slug: "b",
Title: "B",
PageType: types.WikiPageTypeEntity,
OutLinks: types.StringArray{"hub"},
InLinks: types.StringArray{"hub"},
},
{
Slug: "c",
Title: "C",
PageType: types.WikiPageTypeConcept,
OutLinks: types.StringArray{"d"},
InLinks: types.StringArray{"hub"},
},
{
Slug: "d",
Title: "D",
PageType: types.WikiPageTypeConcept,
OutLinks: types.StringArray{},
InLinks: types.StringArray{"hub", "c"},
},
{
Slug: "x",
Title: "X",
PageType: types.WikiPageTypeEntity,
OutLinks: types.StringArray{"y"},
InLinks: types.StringArray{},
},
{
Slug: "y",
Title: "Y",
PageType: types.WikiPageTypeEntity,
OutLinks: types.StringArray{},
InLinks: types.StringArray{"x"},
},
}
}
func nodeSlugs(data *types.WikiGraphData) map[string]bool {
out := make(map[string]bool, len(data.Nodes))
for _, n := range data.Nodes {
out[n.Slug] = true
}
return out
}
// TestComputeGraphSubset_OverviewTruncatesByLinkCount verifies that overview
// mode returns the most-connected nodes first and reports truncation
// honestly in Meta. At 4万 pages this is the path that must NOT return the
// full graph — the cap is what keeps the response size and frontend
// rendering tractable.
func TestComputeGraphSubset_OverviewTruncatesByLinkCount(t *testing.T) {
pages := makeGraphFixture()
got, err := computeGraphSubset(pages, &types.WikiGraphRequest{
Mode: types.WikiGraphModeOverview,
Limit: 3,
})
if err != nil {
t.Fatalf("computeGraphSubset: %v", err)
}
if len(got.Nodes) != 3 {
t.Errorf("want 3 nodes, got %d (%v)", len(got.Nodes), nodeSlugs(got))
}
slugs := nodeSlugs(got)
// hub has link_count 6 (4 out + 2 in), must survive the cap.
if !slugs["hub"] {
t.Errorf("expected hub to survive the top-3 cap, got %v", slugs)
}
if !got.Meta.Truncated {
t.Errorf("expected Meta.Truncated=true when returned < total")
}
if got.Meta.Total != len(pages) {
t.Errorf("Meta.Total = %d, want %d", got.Meta.Total, len(pages))
}
if got.Meta.Returned != len(got.Nodes) {
t.Errorf("Meta.Returned mismatch: %d vs %d", got.Meta.Returned, len(got.Nodes))
}
// Every returned edge must connect two surviving nodes.
for _, e := range got.Edges {
if !slugs[e.Source] || !slugs[e.Target] {
t.Errorf("edge %s->%s references a non-returned node", e.Source, e.Target)
}
}
}
// TestComputeGraphSubset_OverviewUncapped ensures the Limit<=0 escape hatch
// still works for internal callers (wiki lint) that need every page.
func TestComputeGraphSubset_OverviewUncapped(t *testing.T) {
pages := makeGraphFixture()
got, err := computeGraphSubset(pages, &types.WikiGraphRequest{
Mode: types.WikiGraphModeOverview,
Limit: 0,
})
if err != nil {
t.Fatalf("computeGraphSubset: %v", err)
}
if len(got.Nodes) != len(pages) {
t.Errorf("want %d nodes (uncapped), got %d", len(pages), len(got.Nodes))
}
if got.Meta.Truncated {
t.Errorf("expected Meta.Truncated=false when nothing was dropped")
}
}
// TestComputeGraphSubset_OverviewTypeFilter ensures the type filter applies
// to the candidate set (not just post-hoc), so "total" reflects the
// filter-aware denominator the frontend shows to the user.
func TestComputeGraphSubset_OverviewTypeFilter(t *testing.T) {
pages := makeGraphFixture()
got, err := computeGraphSubset(pages, &types.WikiGraphRequest{
Mode: types.WikiGraphModeOverview,
Types: []string{types.WikiPageTypeEntity},
Limit: 100,
})
if err != nil {
t.Fatalf("computeGraphSubset: %v", err)
}
slugs := nodeSlugs(got)
// Only entity-typed pages: a, b, x, y.
for _, expected := range []string{"a", "b", "x", "y"} {
if !slugs[expected] {
t.Errorf("expected entity %q to be returned, got %v", expected, slugs)
}
}
for _, forbidden := range []string{"hub", "c", "d"} {
if slugs[forbidden] {
t.Errorf("%q should have been filtered out by type, got %v", forbidden, slugs)
}
}
if got.Meta.Total != 4 {
t.Errorf("Meta.Total should equal entity-typed page count (4), got %d", got.Meta.Total)
}
}
// TestComputeGraphSubset_EgoDepth1 checks that depth=1 returns center +
// immediate neighbors only, with no transitive hops.
func TestComputeGraphSubset_EgoDepth1(t *testing.T) {
pages := makeGraphFixture()
got, err := computeGraphSubset(pages, &types.WikiGraphRequest{
Mode: types.WikiGraphModeEgo,
Center: "hub",
Depth: 1,
Limit: 100,
})
if err != nil {
t.Fatalf("computeGraphSubset: %v", err)
}
slugs := nodeSlugs(got)
// hub's direct neighbors: a, b, c, d (out) ∪ a, b (in) = {a, b, c, d} + hub.
want := []string{"hub", "a", "b", "c", "d"}
for _, s := range want {
if !slugs[s] {
t.Errorf("expected %q in ego depth=1 of hub, got %v", s, slugs)
}
}
// x and y are in a disconnected cluster and must not leak in.
if slugs["x"] || slugs["y"] {
t.Errorf("disconnected nodes leaked into ego graph: %v", slugs)
}
if got.Meta.Mode != types.WikiGraphModeEgo || got.Meta.Center != "hub" || got.Meta.Depth != 1 {
t.Errorf("Meta not populated correctly for ego: %+v", got.Meta)
}
}
// TestComputeGraphSubset_EgoDepth2 checks that an extra hop reaches pages
// only accessible through a one-hop neighbor (c -> d becomes reachable from
// "a" at depth 2 because a -> hub -> c -> d ... wait, that's 3 hops. Use
// a clearer case: from "a", depth=2 should reach hub's neighbors.)
func TestComputeGraphSubset_EgoDepth2ExpandsFrontier(t *testing.T) {
pages := makeGraphFixture()
depth1, err := computeGraphSubset(pages, &types.WikiGraphRequest{
Mode: types.WikiGraphModeEgo,
Center: "a",
Depth: 1,
Limit: 100,
})
if err != nil {
t.Fatalf("depth=1: %v", err)
}
depth2, err := computeGraphSubset(pages, &types.WikiGraphRequest{
Mode: types.WikiGraphModeEgo,
Center: "a",
Depth: 2,
Limit: 100,
})
if err != nil {
t.Fatalf("depth=2: %v", err)
}
if len(depth2.Nodes) <= len(depth1.Nodes) {
t.Errorf("depth=2 should expand the frontier beyond depth=1 (%d <= %d)",
len(depth2.Nodes), len(depth1.Nodes))
}
// At depth 2 from "a": a -> hub -> {b, c, d}. So b, c, d must appear.
slugs := nodeSlugs(depth2)
for _, s := range []string{"a", "hub", "b", "c", "d"} {
if !slugs[s] {
t.Errorf("expected %q at depth=2 from a, got %v", s, slugs)
}
}
}
// TestComputeGraphSubset_EgoRejectsMissingCenter ensures we fail fast
// rather than returning an empty graph when the caller points at a
// non-existent slug — an empty result here would look identical to "your
// wiki has no links to that page" and hide the real bug.
func TestComputeGraphSubset_EgoRejectsMissingCenter(t *testing.T) {
pages := makeGraphFixture()
_, err := computeGraphSubset(pages, &types.WikiGraphRequest{
Mode: types.WikiGraphModeEgo,
Center: "does-not-exist",
Depth: 1,
Limit: 100,
})
if err == nil {
t.Fatalf("expected error for missing center slug")
}
}