数据目录阶段 4A 影响面:新增接口列出引用某张表的已发布和受管模板,区分直接引用和可能涉及、合并查询按输入追溯,表详情和保存纠正后都显示

This commit is contained in:
ThearchyHelios
2026-10-03 11:52:49 +08:00
parent 1c806fa3a0
commit 958e096e56
11 changed files with 741 additions and 6 deletions
+4 -1
View File
@@ -1,5 +1,5 @@
import type {
ActivateSnapshotBody, ActivateSnapshotOut, AiPreview, CatalogDetail, CatalogDraftOut, CatalogList, CatalogNotes,
ActivateSnapshotBody, ActivateSnapshotOut, AiPreview, CatalogDetail, CatalogDraftOut, CatalogImpact, CatalogList, CatalogNotes,
CatalogPatchPreview, CatalogPatchSubmit, CatalogReviewAction, CommitOut, CurrentRecipe, EditPreview, EditRequest, ImportRecord,
ManifestOut, PurgeRawBody, PurgeRawOut, QuestionAnswer, Recipe, RedraftRulesOut, RemovePeriodBody, RemovePeriodOut,
RevokeAcceptanceBody, RevokeAcceptanceOut, SnapshotOut, Staging,
@@ -669,6 +669,9 @@ export const api = {
/** 单项审阅。path:表级项写字段名(grain);列级项 columns.<列名>.<字段>;关系 relations.<编号> */
review: (sourceId: string, table: string, body: { path: string; action: CatalogReviewAction; if_version: number }) =>
post<CatalogDetail>(`/datasources/${encodeURIComponent(sourceId)}/catalog/${encodeURIComponent(table)}/review`, body),
/** 影响面:引用这张表的已发布、受管模板(直接引用 / 可能涉及,合并查询按输入追溯) */
impact: (sourceId: string, table: string, opts?: RequestOptions) =>
get<CatalogImpact>(`/datasources/${encodeURIComponent(sourceId)}/catalog/${encodeURIComponent(table)}/impact`, opts),
/** 目录修改提案的预览:对着当前目录重算改前、改后,不写库(保存遇到 409 后重新载入用) */
patchPreview: (sourceId: string, table: string, changes: CatalogPatchSubmit[]) =>
post<CatalogPatchPreview>(
+122
View File
@@ -0,0 +1,122 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { Link } from 'react-router-dom'
import { GitBranch } from 'lucide-react'
import clsx from 'clsx'
import { api } from '../api/client'
import { humanizeError } from '../lib/errors'
import { CATALOG_IMPACT_TEXT as T, WORKFLOW_STATUS_LABEL } from '../lib/terms'
import type { CatalogImpact, CatalogImpactNode, CatalogImpactTemplate } from '../types'
import { Skeleton } from './ui'
// ===========================================================================
// 影响面:哪些已发布、受管的模板引用这张表(GET /catalog/{table}/impact)。数据目录的表详情里一栏,助手的
// 「建议更新数据目录」卡片保存之后也列一遍——改了目录,人要知道哪些正式出具的数可能跟着变。
//
// 「直接引用」和「可能涉及」分开写:前者 SQL 里写着这张表,后者是 Agent 运行时自己写 SQL,静态看不出。
// 合并查询写明经由哪个输入。模板名点进去是画布(定位到那个节点)。
// ===========================================================================
const SHOWN = 5
export function CatalogImpactList({ sourceId, table, refreshKey, compact = false }: {
sourceId: string
table: string
/** 变了就重新统计(表详情传目录版本:保存之后刷新) */
refreshKey?: number | string
/** 卡片里的紧凑版:只列前几个模板,节点收成一行 */
compact?: boolean
}) {
const [data, setData] = useState<CatalogImpact | null>(null)
const [error, setError] = useState<unknown>(null)
const [all, setAll] = useState(false)
const seq = useRef(0)
const load = useCallback(() => {
const mine = ++seq.current
setError(null)
api.dataCatalog.impact(sourceId, table)
.then((d) => { if (mine === seq.current) setData(d) })
.catch((e) => { if (mine === seq.current) setError(e) })
}, [sourceId, table])
useEffect(() => {
setData(null)
setAll(false)
load()
}, [load, refreshKey])
if (error && !data) {
return (
<p className="flex flex-wrap items-center gap-2 text-2xs text-[var(--err)]" data-catalog-impact="error">
{T.error}:{humanizeError(error).title}
<button type="button" className="rounded px-1 text-dim hover:bg-hover hover:text-fg" onClick={load}>{T.retry}</button>
</p>
)
}
if (!data) return <div data-catalog-impact="loading"><Skeleton rows={compact ? 1 : 2} height={compact ? 12 : 20} gap={6} /></div>
const list = data.templates
if (!list.length) {
return (
<p className={clsx('text-faint', compact ? 'text-2xs' : 'rounded-lg border px-3 py-4 text-xs')} data-catalog-impact="0">
{compact ? T.afterSaveNone : T.none}
</p>
)
}
const shown = all || !compact ? list : list.slice(0, SHOWN)
return (
<div data-catalog-impact={list.length}>
{compact && <p className="mb-1 text-2xs text-dim">{T.afterSave(list.length)}</p>}
<ul className={clsx(compact ? 'space-y-0.5' : 'divide-y divide-[var(--hairline)] rounded-lg border bg-panel')}>
{shown.map((t) => <TemplateRow key={t.workflow_id} t={t} compact={compact} />)}
</ul>
{compact && list.length > shown.length && (
<button type="button" className="mt-0.5 rounded px-1 text-2xs text-dim hover:bg-hover hover:text-fg" onClick={() => setAll(true)}>
{T.more(list.length - shown.length)}
</button>
)}
</div>
)
}
function ImpactBadge({ impact }: { impact: 'direct' | 'possible' }) {
return (
<span className={clsx('chip shrink-0', impact === 'direct' ? 'text-fg' : 'text-faint')} title={T.impactHint[impact]}
style={impact === 'direct' ? { borderColor: 'var(--st-waiting)', color: 'var(--st-waiting)' } : undefined}
data-impact={impact}>
{T.impact[impact]}
</span>
)
}
function nodeText(n: CatalogImpactNode): string {
const who = n.member ? `「${n.label}」${T.member(n.member)}` : `「${n.label}」`
const via = n.via?.length ? `(${T.via(n.via.map((v) => `「${v.label}」`).join('、'))})` : ''
return `${who}${via}`
}
function TemplateRow({ t, compact }: { t: CatalogImpactTemplate; compact: boolean }) {
const first = t.nodes.find((n) => n.impact === t.impact) ?? t.nodes[0]
const href = `/studio/${encodeURIComponent(t.workflow_id)}${first ? `?focus=${encodeURIComponent(first.node_id)}` : ''}`
return (
<li className={clsx('min-w-0', compact ? 'text-2xs' : 'px-3 py-2 text-xs')} data-impact-template={t.workflow_id} data-impact-level={t.impact}>
<div className="flex min-w-0 flex-wrap items-center gap-x-2 gap-y-0.5">
<GitBranch size={compact ? 10 : 11} className="shrink-0 text-faint" aria-hidden />
<Link to={href} className="min-w-0 truncate font-medium hover:underline">{t.name}</Link>
<span className="mono shrink-0 text-2xs text-faint">{T.version(t.version)}</span>
<span className="shrink-0 text-2xs text-faint">{WORKFLOW_STATUS_LABEL[t.level] ?? t.level}</span>
<ImpactBadge impact={t.impact} />
</div>
{!compact && (
<ul className="mt-1 space-y-0.5 pl-[19px] text-2xs text-dim">
{t.nodes.map((n) => (
<li key={`${n.node_id}:${n.member ?? ''}`} className="flex min-w-0 items-baseline gap-1.5" data-impact-node={n.node_id}>
<span className="min-w-0 break-words">{nodeText(n)}</span>
<span className={clsx('shrink-0', n.impact === 'direct' ? 'text-dim' : 'text-faint')}>{T.impact[n.impact]}</span>
</li>
))}
</ul>
)}
</li>
)
}
+24
View File
@@ -2187,3 +2187,27 @@ export const CATALOG_PATCH_TEXT = {
whereRelationNew: (to: string) => `新增指向 ${to} 的关联关系`,
sourceOf: (source: string) => `数据源「${source}」`,
}
/**
* 影响面:哪些已发布、受管的模板引用这张表。按每个模板当前的已发布版本统计(正式运行用的是它)。
* 「直接引用」:查询的 SQL 里写着这张表;「可能涉及」:Agent 绑定了这个数据源的查询工具,SQL 运行时才生成。
*/
export const CATALOG_IMPACT_TEXT = {
title: '引用这张表的模板',
hint: '按每个模板当前的已发布版本统计',
none: '没有已发布或受管的模板引用这张表',
count: (n: number) => `${formatNumber(n)} 个模板`,
impact: { direct: '直接引用', possible: '可能涉及' } as Record<'direct' | 'possible', string>,
impactHint: {
direct: '查询的 SQL 中写着这张表',
possible: 'Agent 绑定了这个数据源的查询工具,SQL 在运行时生成,可能用到这张表',
} as Record<'direct' | 'possible', string>,
version: (v: number) => `v${v}`,
via: (labels: string) => `经由输入${labels}`,
member: (name: string) => `成员「${name}」`,
afterSave: (n: number) => `${formatNumber(n)} 个已发布模板引用这张表,下次正式运行时会提示数据目录有变化:`,
afterSaveNone: '没有已发布或受管的模板引用这张表',
more: (n: number) => `另有 ${formatNumber(n)} 个`,
error: '无法统计引用这张表的模板',
retry: '重试',
}
+11 -2
View File
@@ -11,9 +11,10 @@ import { DeleteButton, EmptyState, ErrorState, Notice, Skeleton, Spinner, confir
import { useLeaveGuard } from '../../lib/leave'
import { formatDateTime, formatNumber } from '../../lib/format'
import {
CATALOG_CARDINALITY_LABEL, CATALOG_KIND_HINT, CATALOG_KIND_LABEL, CATALOG_TABLE_FIELD_HINT, CATALOG_TABLE_FIELD_LABEL,
CATALOG_TEXT as CT,
CATALOG_CARDINALITY_LABEL, CATALOG_IMPACT_TEXT, CATALOG_KIND_HINT, CATALOG_KIND_LABEL, CATALOG_TABLE_FIELD_HINT,
CATALOG_TABLE_FIELD_LABEL, CATALOG_TEXT as CT,
} from '../../lib/terms'
import { CatalogImpactList } from '../../components/CatalogImpact'
import { CountBadges, ItemMark, StatusChip, StatusLegend, SystemNotesNotice } from './parts'
import type { ReviewTarget } from './parts'
import { ColumnTable, Value } from './ColumnTable'
@@ -382,6 +383,14 @@ export function TableDetail({ sourceId, table, rows, drafted, prev, next, onOpen
onChange={setRelations} />
: <RelationList relations={notes.relations ?? []} busy={!!busy} targetOf={targetOf} onOpen={onOpen} onReview={review} />}
</section>
<section aria-labelledby="catalog-sec-impact" data-catalog-section="impact">
<div className="mb-2 flex flex-wrap items-baseline gap-2">
<h3 id="catalog-sec-impact" className="text-xs font-semibold">{CATALOG_IMPACT_TEXT.title}</h3>
<span className="text-2xs text-faint">{CATALOG_IMPACT_TEXT.hint}</span>
</div>
<CatalogImpactList sourceId={sourceId} table={detail.table_name} refreshKey={detail.version} />
</section>
</div>
</div>
+3 -1
View File
@@ -4,6 +4,7 @@ import { BookMarked, Check, ExternalLink, RotateCw } from 'lucide-react'
import clsx from 'clsx'
import { ApiError, api } from '../api/client'
import { Notice, Spinner, toast } from '../components/ui'
import { CatalogImpactList } from '../components/CatalogImpact'
import { CATALOG_PATCH_TEXT as T, CATALOG_STATUS_LABEL } from '../lib/terms'
import type { CatalogPatchChange } from '../types'
import {
@@ -15,7 +16,7 @@ import {
// 改后和理由,人点「保存到数据目录」才写入——服务端从不自动写。
//
// 保存带着提案对照的版本:别人在这之后改过这张表就 409,卡片写明「这张表刚被修改过」,「重新载入」按最新的
// 目录重算改前和改后(预览接口,只读),人看过再保存。保存成功后给到数据目录那张表的入口。
// 目录重算改前和改后(预览接口,只读),人看过再保存。保存成功后列出受影响的模板(影响面),给到数据目录那张表的入口。
//
// 卡片的结局(已保存、已忽略、重新载入后的内容)记在模块级的表里:画布右栏在对话层和运行层之间切换会卸载
// 卡片,回来时不能又变回「未保存」,再点一次只会撞上 409。
@@ -151,6 +152,7 @@ export function CatalogPatchCard({ patch, turnId, dense = false }: {
{T.open} <ExternalLink size={10} aria-hidden />
</Link>
</p>
{saved && <CatalogImpactList sourceId={patch.sourceId} table={patch.table} compact />}
</div>
)
: (
+31
View File
@@ -2739,3 +2739,34 @@ export interface CatalogPatchPreview {
/** 不合法的项的说明(目录刚被改过、列已删除……) */
problems: string[]
}
/** 影响面里模板的一个节点(服务端 catalog_impact.template_refs) */
export interface CatalogImpactNode {
node_id: string
label: string
type: string
/** direct:SQL 里写着这张表;possible:Agent 运行时自己写 SQL,可能用到 */
impact: 'direct' | 'possible'
/** 合并查询:经由哪些输入(别名 → 节点) */
via?: { node_id: string; label: string; alias: string }[]
/** 协作节点:绑定了查询工具的成员 */
member?: string
}
/** 引用这张表的一个已发布或受管模板(按它当前的已发布版本算) */
export interface CatalogImpactTemplate {
workflow_id: string
name: string
/** 当前的已发布版本 */
version: number
level: 'published' | 'governed'
impact: 'direct' | 'possible'
nodes: CatalogImpactNode[]
}
/** GET /datasources/{id}/catalog/{table}/impact */
export interface CatalogImpact {
table: string
/** 直接引用的在前 */
templates: CatalogImpactTemplate[]
}