feat(catalog): 首次扫描中的来源也出现在「来源与目录」,并显示实时进度

用户反馈:加了钉盘来源后「来源与目录」里看不到。根因不是丢数据,而是列表只列"已经有快照"
的来源 —— 首次扫描要几分钟到几十分钟(钉盘空间 2900+ 个文件夹),这段时间页面上什么都没有。

- 新 Store 方法 ListCatalogScanProgress:已启用 + 还没有 catalog head + 有在跑/排队的
  source.catalog.refresh(三条件同时成立)。进度直接取 worker 写在任务载荷里的同一份计数
  (progress/message/scanned_directories/discovered_directories/scanned_files),不另立口径。
- /api/v1/source-catalog/sources 在既有列表后追加这些行(snapshot_status="scanning" +
  scan_progress/scan_message)。走窄接口按需接线:未接线时安静跳过,不会把旧列表弄坏。
- 前端:左列显示「首次扫描中 44%」,选中后是进度面板(百分比 + 进度条 + 当前文案 +
  已发现文件数),并说明"扫完自动出现目录";有来源在扫描时每 3 秒自动刷新列表,
  扫完那一行自动变成「本地只读快照」。扫描中不拉目录节点、也不显示标注/格式入口。
- 解析器保持严格:snapshot_status 只接受 ready/scanning,scanning 必须带 scan_progress,
  否则整包拒绝 —— 不给"假装 0%"的机会。

测试:postgres 集成测试(真库)覆盖四种不该出现的来源(停用、已有快照、任务类型不符、
历史上已完成的那次任务);前端 parser + 渲染各一条;Go 全绿、前端 192 条 + tsc 干净。

面板实测:左列「公司级营销资料管理 · 首次扫描中 44%」,主区「首次扫描中 · 44% /
正在扫描目录:已完成 1540 个文件夹 / 已发现约 4934 份文件」。
This commit is contained in:
姚章浩
2026-10-10 17:04:59 +08:00
parent ed60b20790
commit 1283f65a95
9 changed files with 248 additions and 6 deletions
@@ -0,0 +1,85 @@
package postgres
import (
"context"
"os"
"strings"
"testing"
"github.com/jackc/pgx/v5/pgxpool"
)
// 「正在首次扫描」的来源也必须出现在来源与目录里:进度取自 worker 写在任务载荷里的同一份计数,
// 只有"已启用 + 还没有快照 + 有在跑/排队的 source.catalog.refresh"三者同时成立才算。
func TestCatalogScanProgressShowsOnlySourcesWithoutSnapshot(t *testing.T) {
databaseURL := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
if databaseURL == "" {
t.Skip("TEST_DATABASE_URL is not set")
}
ctx := context.Background()
pool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
if err := ApplyMigrations(ctx, pool); err != nil {
t.Fatal(err)
}
store := NewStore(pool)
// 四个来源:扫描中(无快照)、停用、已有快照、以及"有快照又恰好有一次失败任务"的来源。
if _, err := pool.Exec(ctx, `
INSERT INTO source_connections(id,source_type,name,enabled) VALUES
('conn-scan','dingtalk','公司级营销资料管理',TRUE),
('conn-off','dingtalk','已停用的来源',FALSE),
('conn-head','dingtalk','已完成首次扫描',TRUE);
INSERT INTO jobs(id,type,payload,state,idempotency_key) VALUES
('job-scan-running','source.catalog.refresh',
'{"connection_id":"conn-scan","progress":36,"message":"正在扫描目录:已完成 470 个文件夹,待处理 683 个","scanned_directories":470,"discovered_directories":1153,"scanned_files":1428}'::jsonb,
'running','scan-running'),
('job-scan-old','source.catalog.refresh',
'{"connection_id":"conn-scan","progress":99,"message":"上一次已完成,不该被当成当前进度"}'::jsonb,
'completed','scan-old'),
('job-off-running','source.catalog.refresh',
'{"connection_id":"conn-off","progress":12,"message":"停用来源不该出现"}'::jsonb,
'running','scan-off'),
('job-wrong-type','artifact.fetch',
'{"connection_id":"conn-scan","progress":80,"message":"别的任务类型不该被当成扫描进度"}'::jsonb,
'running','scan-other'),
('job-head-running','source.catalog.refresh',
'{"connection_id":"conn-head","progress":5,"message":"已有快照的来源不再按扫描中展示"}'::jsonb,
'running','scan-head');
INSERT INTO source_catalog_snapshots(id,source_connection_id,snapshot_sha256,source_label,node_count,folder_count,file_count,observed_at,created_at)
VALUES('snap-head','conn-head',repeat('a',64),'已完成首次扫描',1,1,0,now(),now());
INSERT INTO source_catalog_nodes(snapshot_id,node_id,node_type,name,relative_path,source_locator_hash,sort_order,direct_child_count,subtree_file_count,subtree_bytes)
VALUES('snap-head','root-head','folder','已完成首次扫描','',repeat('b',64),0,0,0,0);
INSERT INTO source_catalog_heads(source_connection_id,snapshot_id,activated_at)
VALUES('conn-head','snap-head',now());`); err != nil {
t.Fatal(err)
}
progress, err := store.ListCatalogScanProgress(ctx)
if err != nil {
t.Fatal(err)
}
if len(progress) != 1 {
t.Fatalf("scanning sources = %d (%+v), want only the one without a snapshot", len(progress), progress)
}
item := progress[0]
if item.ConnectionID != "conn-scan" || item.Name != "公司级营销资料管理" {
t.Fatalf("progress item = %+v", item)
}
// 取的是在跑的那次任务,不是历史上已完成的那次。
if item.Progress != 36 {
t.Fatalf("progress = %d, want 36 (the running job, not the completed one)", item.Progress)
}
if item.ScannedDirectories != 470 || item.DiscoveredDirectories != 1153 || item.ScannedFiles != 1428 {
t.Fatalf("counters = %+v", item)
}
if !strings.Contains(item.Message, "470 个文件夹") {
t.Fatalf("message = %q", item.Message)
}
if item.StartedAt.IsZero() {
t.Fatal("started_at should carry the scan start so the page can show a time")
}
}
@@ -12,6 +12,58 @@ import (
var ErrCatalogNodeNotFound = errors.New("catalog node not found")
// ListCatalogScanProgress 列出「已启用、但还没有快照」的来源的首次扫描进度。
// 进度取自 worker 写在任务载荷里的同一份计数(jobs.payload),不另立一套口径:
// 新来源在首次扫描完成前也能出现在「来源与目录」里,而不是等快照落地才出现。
func (s *Store) ListCatalogScanProgress(ctx context.Context) ([]governance.CatalogScanProgress, error) {
rows, err := s.db.Query(ctx, `
SELECT connection.id, connection.source_type, connection.name,
COALESCE(NULLIF(job.payload->>'progress','')::integer,0),
COALESCE(job.payload->>'message',''),
COALESCE(NULLIF(job.payload->>'scanned_directories','')::integer,0),
COALESCE(NULLIF(job.payload->>'discovered_directories','')::integer,0),
COALESCE(NULLIF(job.payload->>'scanned_files','')::integer,0),
job.created_at
FROM source_connections connection
JOIN LATERAL (
SELECT candidate.*
FROM jobs candidate
WHERE candidate.type='source.catalog.refresh'
AND candidate.state IN ('pending','running','retry')
AND candidate.payload->>'connection_id'=connection.id
ORDER BY candidate.created_at DESC
LIMIT 1
) job ON TRUE
LEFT JOIN source_catalog_heads head ON head.source_connection_id=connection.id
WHERE connection.enabled AND head.source_connection_id IS NULL
ORDER BY job.created_at DESC, connection.id`)
if err != nil {
return nil, fmt.Errorf("list catalog scan progress: %w", err)
}
defer rows.Close()
result := make([]governance.CatalogScanProgress, 0)
for rows.Next() {
var item governance.CatalogScanProgress
var sourceType string
if err := rows.Scan(&item.ConnectionID, &sourceType, &item.Name, &item.Progress, &item.Message,
&item.ScannedDirectories, &item.DiscoveredDirectories, &item.ScannedFiles, &item.StartedAt); err != nil {
return nil, fmt.Errorf("scan catalog scan progress: %w", err)
}
item.SourceType = governance.SourceType(sourceType)
if !item.SourceType.Valid() {
return nil, errors.New("invalid catalog scan progress projection")
}
if item.Progress < 0 {
item.Progress = 0
}
if item.Progress > 100 {
item.Progress = 100
}
result = append(result, item)
}
return result, rows.Err()
}
func (s *Store) ListCatalogSources(ctx context.Context) ([]governance.CatalogSource, error) {
rows, err := s.db.Query(ctx, `
SELECT connection.id, connection.source_type, snapshot.source_label, root.node_id,