合入集成分支(阶段 2A 数据剖析、4B 剖析与 SQL 检查界面及修正)到 4A:两边功能都保留

冲突的解法:
- 后端 api/catalog.py:同时导入 catalog_impact 和 catalog_profile、SnapshotTampered;data/catalog.py:__all__ 取两边的并集重排。
- 2A 放宽了码值校验(含义可以为空,剖析写进来的是「含义待填写」):目录修改提案的码值补充改为含义为空的码不覆盖已有含义、
  只在原来没有这个码时加进去,补了测试;卡片和过程里的码值把空含义写成「含义待填写」,和目录页同一个说法。
- 前端 types.ts、terms.ts:两边各在同一处追加了新段落,按顺序都留下(提案、影响面、目录版本在前,剖析、码值、SQL 检查在后),
  没有重复的键;client.ts、decode.ts、CatalogPage.tsx、TableDetail.tsx 的导入取并集。
- check-catalog.mjs:影响面和剖析、保存数据源三条假路由都留下,各节和最后核对真库数量的逻辑不变。
- 4A 的测试数据源改用 tests/fixtures/sources.py 的 drop_source 删除,顺带作废连接池。
This commit is contained in:
ThearchyHelios
2026-10-03 12:35:47 +08:00
51 changed files with 4803 additions and 147 deletions
+65 -13
View File
@@ -2,7 +2,7 @@ import { useEffect, useId, useMemo, useRef, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import {
AlertTriangle, BookMarked, ChevronRight, Database, EyeOff, FileSpreadsheet, History, Info, KeyRound, Layers, Lock, Plug, Plus,
RefreshCw, Search, Table2, Upload, X,
RefreshCw, ScanSearch, Search, Table2, Upload, X,
} from 'lucide-react'
import clsx from 'clsx'
import { ApiError, api, uploadDecision } from '../api/client'
@@ -21,9 +21,16 @@ import { checkHealth, forgetHealth, healthFromServer, setHealth, useHealth } fro
import type { HealthRecord } from '../lib/health'
import { workflowList, workflowsMentioning } from '../lib/mentions'
import {
CATALOG_TEXT, RAW_STATE_LABEL, RECIPE_ORIGIN_LABEL, RECIPE_TEXT, UPLOAD_CONVERSION_LABEL, UPLOAD_SHAPE_LABEL, UPLOAD_TEXT, VERSIONS_TEXT,
CATALOG_TEXT, PROFILE_TEXT, RAW_STATE_LABEL, RECIPE_ORIGIN_LABEL, RECIPE_TEXT, UPLOAD_CONVERSION_LABEL, UPLOAD_SHAPE_LABEL, UPLOAD_TEXT,
VERSIONS_TEXT,
} from '../lib/terms'
import { useRunClock } from '../run/useRunClock'
import { focusProfileField, ProfileSettingsDialog, ProfileSettingsSection } from './catalog/ProfileSettings'
import type { ProfileErrorKey } from './catalog/ProfileSettings'
import {
hasProfileOption, PROFILE_OPTION, profileFieldOfRejection, profileFormOf, profileOptionOf, profileProblems, profileSettingsOf,
} from './catalog/profile'
import type { ProfileForm } from './catalog/profile'
import { ImportWizard } from './import/ImportWizard'
import type { WizardEntry } from './import/ImportWizard'
import { VersionsDialog } from './import/VersionsDialog'
@@ -285,9 +292,11 @@ function SourceCard({ row, meta, onChange, onRemoved, onEdit, onReupload, onReci
// 传上来的表通常只有一两张,直接摊开;库动辄几十上百个对象,默认收着
const [open, setOpen] = useState(() => uploaded && row.table_count > 0 && row.table_count <= 3)
const [masking, setMasking] = useState(false)
const [profiling, setProfiling] = useState(false)
/** 版本页(当前版本的各期、历史版本、导入记录) */
const [versions, setVersions] = useState(false)
const masks = maskList(row.options?.[MASK_KEY])
const profile = profileSettingsOf(row.options)
const clock = useRunClock(!!busy)
const busySince = useRef(0)
useTicker(60_000)
@@ -525,12 +534,28 @@ function SourceCard({ row, meta, onChange, onRemoved, onEdit, onReupload, onReci
{masks.length ? '修改遮罩列' : '设置遮罩列'}
</button>
)}
{profile.enabled && (
<span className="chip" data-profile-on="" title={PROFILE_TEXT.chipHint(profile.max_queries, profile.query_timeout_s)}>
<ScanSearch size={10} aria-hidden /> {PROFILE_TEXT.chip}
</span>
)}
{uploaded && (
// 数据剖析的设置同理:手工登记的库在编辑框里改
<button type="button" className="text-2xs underline decoration-dotted underline-offset-2 hover:text-dim"
onClick={() => setProfiling(true)} data-edit-profile="">
{PROFILE_TEXT.open}
</button>
)}
{!!row.tools?.length && <span className="mono">{row.tools.join(' · ')}</span>}
</div>
{masking && (
<MaskColumnsDialog row={row} onClose={() => setMasking(false)}
onSaved={(next) => { setMasking(false); onChange(next); syncCatalog() }} />
)}
{profiling && (
<ProfileSettingsDialog row={row} onClose={() => setProfiling(false)}
onSaved={(next) => { setProfiling(false); onChange(next); syncCatalog() }} />
)}
{versions && (
// onChange 是父级的「更新这一行并刷新全局目录」:回滚之后表结构可能变了,检查器、问数据读的目录要跟上
<VersionsDialog source={row} onClose={() => setVersions(false)} onChange={onChange} onReload={onReload} />
@@ -940,11 +965,16 @@ interface SourceForm {
sid: string
/** options.mask_columns:证据面板展示查询原始行时遮掉的列 */
maskColumns: string[]
/** options 里除 schema / service_name / sid / mask_columns 之外的键,全部摊开可编辑 */
/** options.catalog_profile:数据剖析的开关和预算 */
profile: ProfileForm
/** 打开时 options 里已经有剖析设置:没开、也没填时也要照写(关掉之后写回 enabled: false) */
profileStored: boolean
/** options 里除 schema / service_name / sid / mask_columns / catalog_profile 之外的键,全部摊开可编辑 */
advanced: [string, string][]
}
const OWN_OPTION_KEYS = new Set(['schema', 'service_name', 'sid', MASK_KEY])
// 剖析设置是一个对象,不能摊进「高级连接参数」:那里按文字回显,保存时会写成「[object Object]」被服务端拒收
const OWN_OPTION_KEYS = new Set(['schema', 'service_name', 'sid', MASK_KEY, PROFILE_OPTION])
function toForm(source: any): SourceForm {
const o = source.options ?? {}
@@ -966,6 +996,8 @@ function toForm(source: any): SourceForm {
serviceName: o.service_name ?? (oracle ? source.database ?? '' : ''),
sid: o.sid ?? '',
maskColumns: maskList(o[MASK_KEY]),
profile: profileFormOf(o),
profileStored: hasProfileOption(o),
advanced: Object.entries(o)
.filter(([k]) => !OWN_OPTION_KEYS.has(k))
.map(([k, v]) => [k, String(v ?? '')]),
@@ -973,10 +1005,12 @@ function toForm(source: any): SourceForm {
}
function toBody(form: SourceForm, isNew: boolean, wasOracle = true): any {
const options: Record<string, string | string[]> = {}
const options: Record<string, unknown> = {}
for (const [k, v] of form.advanced) if (k.trim() && v.trim()) options[k.trim()] = v.trim()
if (form.schema.trim()) options.schema = form.schema.trim()
if (form.maskColumns.length) options[MASK_KEY] = form.maskColumns
const profile = profileOptionOf(form.profile, form.profileStored)
if (profile) options[PROFILE_OPTION] = profile
const oracle = form.kind === 'oracle'
if (oracle) {
// 只写选中的那一个,另一个不带:两处都有值时引擎听 service_name,
@@ -1005,10 +1039,12 @@ function toBody(form: SourceForm, isNew: boolean, wasOracle = true): any {
}
/**
* 422 的中文 detail 说的是哪一项。眼下只有查询时限按字段拒(后端 query_timeout_problem),
* 认不出的交回 toast
* 422 的中文 detail 说的是哪一项:查询时限(后端 query_timeout_problem);数据剖析的某一项(profile_settings_problem,
* 记为 profile.<键>,它的「单条查询时限」也带「查询时限」四个字,要先认)。认不出的交回 toast
*/
function fieldOfRejection(message: string): string | null {
const profile = profileFieldOfRejection(message)
if (profile) return `profile.${profile}`
if (/查询时限/.test(message)) return 'query_timeout_s'
return null
}
@@ -1019,10 +1055,11 @@ const FIELD_LABEL: Record<string, string> = {
/**
* 决定连到哪个库的那几项(提交体里的)。schema 不在内:它只影响探查哪一片,不影响
* 连不连得上,后端判断测连接结果还作不作数时同样不看它;遮罩列只管面板上怎么显示,也不在内
* 连不连得上,后端判断测连接结果还作不作数时同样不看它;遮罩列只管面板上怎么显示,数据剖析的设置只管剖析,
* 都不在内(服务端拼连接串时同样摘掉这几项)
*/
function connectionOf(body: any): string {
const { schema: _schema, [MASK_KEY]: _mask, ...options } = body.options ?? {}
const { schema: _schema, [MASK_KEY]: _mask, [PROFILE_OPTION]: _profile, ...options } = body.options ?? {}
return JSON.stringify([
body.kind, body.host ?? null, body.port ?? null, body.database ?? null, body.username ?? null,
options, !!body.password,
@@ -1076,6 +1113,12 @@ function SourceEditor({ source, kinds, onClose, onSaved }: {
}
const required = (key: string) => needs.includes(key)
const blocked = missing.length > 0 || !!nameError
// 剖析设置填错只拦保存,不拦测试连接(和连接无关)
const profileBad = Object.keys(profileProblems(form.profile)).length
const saveBlocked = blocked || profileBad > 0
const saveHint = blocked ? `缺少必填项:${missing.join('、') || '标识格式'}` : profileBad ? PROFILE_TEXT.invalid(profileBad) : undefined
const profileError = fieldError?.key.startsWith('profile.')
? { key: fieldError.key.slice('profile.'.length) as ProfileErrorKey, message: fieldError.message } : null
const body = toBody(form, isNew, source.kind === 'oracle')
const sig = JSON.stringify({ ...body, password: form.password })
const dirty = JSON.stringify(form) !== JSON.stringify(initial)
@@ -1097,7 +1140,7 @@ function SourceEditor({ source, kinds, onClose, onSaved }: {
}
const save = async () => {
if (blocked) return
if (saveBlocked) return
setSaving(true)
setFieldError(null)
try {
@@ -1109,7 +1152,10 @@ function SourceEditor({ source, kinds, onClose, onSaved }: {
} catch (e) {
const rejected = e instanceof ApiError && e.status === 422 ? e.message : ''
const key = rejected ? fieldOfRejection(rejected) : null
if (key) {
if (key?.startsWith('profile.')) {
setFieldError({ key, message: rejected })
focusProfileField('ds-profile', key.slice('profile.'.length) as ProfileErrorKey)
} else if (key) {
setFieldError({ key, message: rejected })
setAdvOpen(true)
requestAnimationFrame(() => document.getElementById(`ds-adv-${key}`)?.focus())
@@ -1200,8 +1246,8 @@ function SourceEditor({ source, kinds, onClose, onSaved }: {
title={blocked ? `缺少必填项:${missing.join('、') || '标识格式'}` : '使用当前配置测试连接,不保存'}>
{test.since ? <Spinner size={11} /> : <Plug size={12} aria-hidden />} 测试连接
</button>
<button className="btn btn-primary" disabled={saving || blocked} onClick={() => void save()}
title={blocked ? `缺少必填项:${missing.join('、') || '标识格式'}` : undefined}>
<button className="btn btn-primary" disabled={saving || saveBlocked} onClick={() => void save()}
title={saveHint} data-source-save="">
{saving ? <Spinner size={11} /> : null} 保存
</button>
</>
@@ -1318,6 +1364,12 @@ function SourceEditor({ source, kinds, onClose, onSaved }: {
<MaskColumnsField sourceId={source.id} synced={!!source.table_count} value={form.maskColumns}
onChange={(maskColumns) => set({ maskColumns })} />
<ProfileSettingsSection idPrefix="ds-profile" form={form.profile} serverError={profileError}
onChange={(profile) => {
if (profileError) setFieldError(null)
set({ profile })
}} />
<label className="flex cursor-pointer items-start gap-2 rounded-lg border p-2.5"
style={form.readonly ? undefined : { borderColor: 'var(--warn)', background: 'color-mix(in srgb, var(--warn) 7%, transparent)' }}>
<input type="checkbox" className="mt-0.5" checked={form.readonly}
+7 -2
View File
@@ -14,7 +14,7 @@ import { LineageLayer } from '../canvas/LineageLayer'
import { VersionsSheet } from '../canvas/VersionsSheet'
import { PublishDialog } from '../canvas/PublishDialog'
import { WorkflowPicker, confirmDiscard, createWorkflow, takeDiscarded } from '../canvas/WorkflowPicker'
import { problemsOf, type Problem } from '../canvas/issues'
import { fieldOfIssue, problemsOf, type Problem } from '../canvas/issues'
import { STUDIO_SHORTCUTS, hintOf, studioShortcut, type StudioShortcutId } from '../canvas/shortcuts'
import { AssistantPanel } from '../run/AssistantPanel'
import { copilotProgress } from '../run/Composer'
@@ -620,7 +620,12 @@ export function StudioPage() {
{publishing && workflow && (
<PublishDialog workflow={workflow} onClose={() => setPublishing(false)}
onDone={() => { setPublishing(false); void refresh() }}
onLocate={(id) => { select(id); focusNode(id) }} />
onLocate={(issue) => {
// 和问题面板同一个落点:选中节点、取景,问题落在某一项配置上(调用工具的 SQL)就翻到那一项
if (!issue.node_id) return
const node = useStudio.getState().nodes.find((n) => n.id === issue.node_id)
revealField(issue.node_id, fieldOfIssue(issue, node))
}} />
)}
</div>
)
+135 -5
View File
@@ -1,15 +1,19 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import type { ReactNode } from 'react'
import { useNavigate, useParams } from 'react-router-dom'
import { ArrowLeft, BookMarked, Database, ListFilter as ListFilterIcon, Sparkles } from 'lucide-react'
import { ArrowLeft, BookMarked, Database, ListFilter as ListFilterIcon, ScanSearch, Sparkles } from 'lucide-react'
import clsx from 'clsx'
import { ApiError, api } from '../../api/client'
import type { CatalogDetail, CatalogList, CatalogTableRow } from '../../types'
import { EmptyState, ErrorState, PageHeader, Skeleton, toast } from '../../components/ui'
import { useDatasources, useOnReconnect } from '../../store/catalog'
import { CATALOG_TEXT as CT } from '../../lib/terms'
import { EmptyState, ErrorState, PageHeader, Skeleton, Spinner, toast, useTicker } from '../../components/ui'
import { useCatalog, useDatasources, useOnReconnect } from '../../store/catalog'
import { CATALOG_TEXT as CT, PROFILE_STOP_LABEL, PROFILE_TEXT as PT } from '../../lib/terms'
import { StatusLegend, SystemNotesNotice } from './parts'
import { DraftDialog } from './DraftDialog'
import { ProfileDialog } from './ProfileDialog'
import type { ProfileJob } from './ProfileDialog'
import { ProfileSettingsDialog } from './ProfileSettings'
import { profileBlockOf, profileSettingsOf, secondsText } from './profile'
import { TableDetail } from './TableDetail'
import { TableIndex } from './TableIndex'
import { filterCounts, progressOf, rowFromDetail, usageSummary, visibleRows } from './model'
@@ -65,6 +69,24 @@ export function CatalogPage() {
const [drafting, setDrafting] = useState(false)
/** 起草写进了这些表:详情据此重新载入(编辑中的不动,保存时服务端会用版本号拦下) */
const [drafted, setDrafted] = useState<{ seq: number; tables: Set<string> }>({ seq: 0, tables: new Set() })
const [profiling, setProfiling] = useState(false)
const [profileSettingsOpen, setProfileSettingsOpen] = useState(false)
/** 一次剖析的状态。关掉弹窗剖析照常进行:状态留在页面上,页头有入口,完成后提示并可再打开报告 */
const [job, setJob] = useState<(ProfileJob & { sourceId: string }) | null>(null)
/** 剖析设置刚存过:数据源列表重取回来之前按存下的这份算(预算、开没开) */
const [savedOptions, setSavedOptions] = useState<{ sourceId: string; options: Record<string, unknown> } | null>(null)
/** 从剖析报告点「填写含义」:打开那张表并弹出那一列的码值 */
const [fillCodes, setFillCodes] = useState<{ table: string; column: string; seq: number } | null>(null)
const profilingRef = useRef(profiling)
profilingRef.current = profiling
const alive = useRef(true)
// 开发模式的严格模式会先卸载再挂载一次:挂载时要重新置为真
useEffect(() => {
alive.current = true
return () => { alive.current = false }
}, [])
const sourceRef = useRef(sourceId)
sourceRef.current = sourceId
const rows = useMemo(() => data?.tables ?? [], [data])
const visible = useMemo(() => visibleRows(rows, query, filter, sort), [rows, query, filter, sort])
@@ -87,6 +109,77 @@ export function CatalogPage() {
void load()
}, [load])
const options = savedOptions?.sourceId === sourceId ? savedOptions.options : source?.options
const profileSettings = useMemo(() => profileSettingsOf(options), [options])
const myJob = job?.sourceId === sourceId ? job : null
const jobRunning = myJob?.phase === 'running'
useTicker(1000, jobRunning)
/**
* 剖析:同步请求,做完才返回。期间关掉弹窗不影响;回来时页面已经换了数据源或卸载了,只提示、不再改这一页。
* 写进去的表重新载入清单和正在看的详情(编辑中的不动,同起草)
*/
const startProfile = useCallback(async (tables: string[] | null) => {
const src = sourceId
const startedAt = Date.now()
setJob({ sourceId: src, phase: 'running', startedAt, tables, settings: profileSettings })
let next: ProfileJob
try {
const report = await api.dataCatalog.profile(src, tables ? { tables } : {})
next = { phase: 'done', report, ms: Date.now() - startedAt }
} catch (e) {
next = e instanceof ApiError && e.status === 409
? { phase: 'blocked', kind: profileBlockOf(e.message), message: e.message, tables }
: { phase: 'failed', error: e, tables }
}
const here = alive.current && sourceRef.current === src
if (here) setJob({ ...next, sourceId: src })
if (next.phase === 'done') {
const r = next.report
const touched = r.tables.filter((t) => !t.error && t.added + t.updated + t.removed > 0).map((t) => t.table_name)
if (here) {
if (touched.length) setDrafted((cur) => ({ seq: cur.seq + 1, tables: new Set(touched) }))
void load()
}
// 弹窗开着就看报告;关了(在后台继续)才提示,并给「查看报告」
if (!here || !profilingRef.current) {
const summary = PT.summary(r.queries_used, r.settings.max_queries, r.tables.length)
const text = r.stopped ? PT.toastStopped(PROFILE_STOP_LABEL[r.stopped] ?? r.stopped) : PT.toastDone(summary)
const show = r.stopped ? toast.warn : toast.ok
show(text, here ? { key: `profile:${src}`, duration: 10000, action: { label: PT.viewReport, onClick: () => setProfiling(true) } }
: { key: `profile:${src}` })
}
} else if (!here || !profilingRef.current) {
toast.error(next.phase === 'blocked' ? next.message : next.error, here
? { key: `profile:${src}`, action: { label: PT.viewReport, onClick: () => setProfiling(true) } } : { key: `profile:${src}` })
}
}, [sourceId, profileSettings, load])
const openProfile = useCallback(() => {
// 上一次的结果(报告、被拒)看过了,再点就是重新开始;正在剖析时打开的是进度
setJob((cur) => (cur && cur.phase === 'running' && cur.sourceId === sourceId ? cur : null))
setProfiling(true)
}, [sourceId])
const onProfileSettingsSaved = useCallback((row: any) => {
setProfileSettingsOpen(false)
setSavedOptions({ sourceId, options: row?.options ?? {} })
void useCatalog.getState().reload('datasources')
// 因为没开启被拒的:开了就回到选范围,可以直接开始
setJob((cur) => (cur && cur.phase === 'blocked' && cur.kind === 'disabled' ? null : cur))
}, [sourceId])
const openFromReport = useCallback((t: string) => {
setProfiling(false)
openTable(t)
}, [openTable])
const fillFromReport = useCallback((t: string, column: string) => {
setProfiling(false)
setFillCodes((cur) => ({ table: t, column, seq: (cur?.seq ?? 0) + 1 }))
openTable(t)
}, [openTable])
// 上一张 / 下一张按清单当前的筛选和排序走;正在看的表不在筛选结果里(刚确认完、从关联关系跳过来)时按全部表走
const [prev, next] = useMemo(() => {
if (!table) return [null, null]
@@ -116,6 +209,19 @@ export function CatalogPage() {
<Sparkles size={11} aria-hidden /> {CT.draft}
</button>
)
// 页头的「数据剖析」:剖析进行中换成计时,点开看进度(关掉弹窗剖析照常进行)
const headerProfile = jobRunning && myJob?.phase === 'running'
? (
<button type="button" className="btn btn-sm" onClick={() => setProfiling(true)} title={PT.headerRunningHint}
aria-label={PT.headerRunning(secondsText(Date.now() - myJob.startedAt))} data-catalog-profile="running">
<Spinner size={11} /> <span className="tnum hidden sm:inline">{PT.headerRunning(secondsText(Date.now() - myJob.startedAt))}</span>
</button>
)
: (
<button type="button" className="btn btn-sm" onClick={openProfile} aria-label={PT.action} title={PT.actionHint} data-catalog-profile="">
<ScanSearch size={11} aria-hidden /> <span className="hidden sm:inline">{PT.action}</span>
</button>
)
// 页头的「起草」窄屏只留图标,标题才放得下
const headerDraft = (
<button type="button" className="btn btn-sm" onClick={() => setDrafting(true)} aria-label={CT.draft} title={CT.draft} data-catalog-draft="">
@@ -171,12 +277,13 @@ export function CatalogPage() {
active={table}
onOpen={openTable}
onDraft={() => setDrafting(true)}
onProfile={openProfile}
/>
</aside>
<section className={clsx('min-h-0 min-w-0 flex-1 flex-col lg:flex', table ? 'flex' : 'hidden')} aria-label={table ?? CT.overviewTitle}>
{table
? (
<TableDetail sourceId={sourceId} table={table} rows={rows} drafted={drafted} prev={prev} next={next}
<TableDetail sourceId={sourceId} table={table} rows={rows} drafted={drafted} fillCodes={fillCodes} prev={prev} next={next}
onOpen={openTable} onBack={() => navigate(base)} onDetail={onDetail} />
)
: <Overview rows={rows} empty={empty} systemNotes={data.system_notes} draft={draftButton(true)} onOpen={openTable} />}
@@ -193,6 +300,7 @@ export function CatalogPage() {
subtitle={CT.subtitle}
actions={(
<div className="flex shrink-0 items-center gap-1.5">
{hasTables && headerProfile}
{hasTables && headerDraft}
<button type="button" className="btn btn-sm btn-ghost" onClick={() => navigate(back)} aria-label={CT.back} data-catalog-back="">
<ArrowLeft size={12} aria-hidden /> <span className="hidden sm:inline">{CT.back}</span>
@@ -213,6 +321,28 @@ export function CatalogPage() {
onDrafted={onDrafted}
/>
)}
{profiling && data && (
<ProfileDialog
sourceName={source?.name ?? sourceId}
settings={profileSettings}
rows={rows}
visible={visible}
selected={[...selected]}
filtered={filtered}
job={myJob}
onStart={(tables) => void startProfile(tables)}
onClose={() => setProfiling(false)}
onSettings={() => setProfileSettingsOpen(true)}
onGoSource={() => navigate(back)}
onOpenTable={openFromReport}
onFillCodes={fillFromReport}
onReset={() => setJob(null)}
/>
)}
{profileSettingsOpen && source && (
<ProfileSettingsDialog row={{ id: source.id, name: source.name, options }} onClose={() => setProfileSettingsOpen(false)}
onSaved={onProfileSettingsSaved} />
)}
</div>
)
}
@@ -0,0 +1,79 @@
import { useState } from 'react'
import type { CatalogItem } from '../../types'
import { isComposing, Modal, Spinner } from '../../components/ui'
import { CODES_TEXT as KT } from '../../lib/terms'
import { StatusChip } from './parts'
// ===========================================================================
// 填写码值的含义。数据剖析只知道列里出现过哪些取值(码值候选的含义是空串),含义要人填:逐个码值一个输入框,
// 比在编辑模式里改「码值=含义」的多行文字省事。保存走整份提交,这一项记为人工填写、已确认;
// 暂不清楚的含义可以留空,表格里照旧显示「含义待填写」。
// ===========================================================================
export function CodesDialog({ column, item, saving, onSave, onClose }: {
column: string
item: CatalogItem<Record<string, string>>
saving: boolean
/** 新的码值对照(码值不变,只改含义) */
onSave: (value: Record<string, string>) => void
onClose: () => void
}) {
const [initial] = useState(() => ({ ...item.value }))
const [value, setValue] = useState<Record<string, string>>(initial)
const codes = Object.keys(initial)
const dirty = codes.some((k) => (value[k] ?? '').trim() !== (initial[k] ?? '').trim())
const save = () => {
if (!dirty || saving) return
onSave(Object.fromEntries(codes.map((k) => [k, (value[k] ?? '').trim()])))
}
return (
<Modal open onClose={onClose} dirty={dirty} width={520} title={KT.title(column)}
footer={(
<>
<button type="button" className="btn" onClick={onClose}>{KT.cancel}</button>
<button type="button" className="btn btn-primary" disabled={!dirty || saving} onClick={save} data-codes-save="">
{saving ? <Spinner size={11} /> : null} {KT.save}
</button>
</>
)}>
<div className="space-y-3" data-codes-dialog={column}>
<div className="flex flex-wrap items-center gap-1.5 text-2xs text-faint">
<StatusChip status={item.status} source={item.source} />
</div>
{item.note && (
<div className="rounded border bg-bg px-2 py-1.5 text-2xs leading-relaxed text-dim">
<span className="text-faint">{KT.note}:</span>{item.note}
</div>
)}
<div className="overflow-hidden rounded-lg border">
<table className="w-full border-collapse text-xs">
<thead>
<tr className="border-b bg-elev text-left text-2xs text-faint">
<th scope="col" className="w-[32%] px-3 py-1.5 font-medium">{KT.head.code}</th>
<th scope="col" className="px-2 py-1.5 font-medium">{KT.head.meaning}</th>
</tr>
</thead>
<tbody>
{codes.map((k, i) => (
<tr key={k} className="border-b border-hairline last:border-b-0">
<th scope="row" className="mono px-3 py-1 text-left font-normal [overflow-wrap:anywhere]">
<label htmlFor={`codes-${column}-${i}`}>{k}</label>
</th>
<td className="px-2 py-1">
<input id={`codes-${column}-${i}`} className="field !py-1" value={value[k] ?? ''} placeholder={KT.placeholder}
maxLength={500} autoFocus={i === codes.findIndex((c) => !(initial[c] ?? '').trim())}
onChange={(e) => setValue((v) => ({ ...v, [k]: e.target.value }))}
// 回车保存;输入法组字时的回车是选词,不算
onKeyDown={(e) => { if (e.key === 'Enter' && !isComposing(e)) { e.preventDefault(); save() } }}
data-code={k} />
</td>
</tr>
))}
</tbody>
</table>
</div>
<p className="text-2xs leading-relaxed text-faint">{KT.hint}</p>
</div>
</Modal>
)
}
+39 -16
View File
@@ -1,13 +1,13 @@
import { memo } from 'react'
import type { ReactNode } from 'react'
import { AlertTriangle, CheckCheck, Lock } from 'lucide-react'
import { AlertTriangle, CheckCheck, Lock, PencilLine } from 'lucide-react'
import clsx from 'clsx'
import type {
CatalogColumnNotes, CatalogItem, CatalogMeasure, CatalogReviewAction, CatalogStructureColumn,
} from '../../types'
import { IconButton } from '../../components/ui'
import {
CATALOG_COLUMN_FIELD_LABEL, CATALOG_MEASURE_HINT, CATALOG_MEASURE_LABEL, CATALOG_TEXT as CT,
CATALOG_COLUMN_FIELD_LABEL, CATALOG_MEASURE_HINT, CATALOG_MEASURE_LABEL, CATALOG_TEXT as CT, CODES_TEXT as KT,
} from '../../lib/terms'
import { ItemMark } from './parts'
import type { ReviewTarget } from './parts'
@@ -32,7 +32,7 @@ export interface ColumnModel {
type ColumnTexts = Partial<Record<ColumnField, string>>
export function ColumnTable({ columns, form, initial, problems, busy, systemNotes, onReview, onConfirmColumn, onChange }: {
export function ColumnTable({ columns, form, initial, problems, busy, systemNotes, onReview, onConfirmColumn, onFillCodes, onChange }: {
columns: ColumnModel[]
/** 编辑中:列名 → 字段 → 文字(form 是当前的,initial 是进入编辑时的);不在编辑时为 null */
form: Record<string, ColumnTexts> | null
@@ -44,6 +44,8 @@ export function ColumnTable({ columns, form, initial, problems, busy, systemNote
systemNotes: boolean
onReview: (target: ReviewTarget, action: CatalogReviewAction) => void
onConfirmColumn: (col: string) => void
/** 码值里有含义空着的:打开逐个填写含义的弹窗 */
onFillCodes: (col: string) => void
onChange: (col: string, field: ColumnField, value: string) => void
}) {
return (
@@ -66,7 +68,7 @@ export function ColumnTable({ columns, form, initial, problems, busy, systemNote
<tbody>
{columns.map((c) => (
<ColumnRow key={c.name} col={c} editing={!!form} draft={form?.[c.name]} initial={initial?.[c.name]} problems={problems} busy={busy}
systemNotes={systemNotes} onReview={onReview} onConfirmColumn={onConfirmColumn} onChange={onChange} />
systemNotes={systemNotes} onReview={onReview} onConfirmColumn={onConfirmColumn} onFillCodes={onFillCodes} onChange={onChange} />
))}
</tbody>
</table>
@@ -74,7 +76,7 @@ export function ColumnTable({ columns, form, initial, problems, busy, systemNote
)
}
const ColumnRow = memo(function ColumnRow({ col, editing, draft, initial, problems, busy, systemNotes, onReview, onConfirmColumn, onChange }: {
const ColumnRow = memo(function ColumnRow({ col, editing, draft, initial, problems, busy, systemNotes, onReview, onConfirmColumn, onFillCodes, onChange }: {
col: ColumnModel
editing: boolean
/** 这一列的表单。只有改了这一列时引用才会变,别的行不重画 */
@@ -85,6 +87,7 @@ const ColumnRow = memo(function ColumnRow({ col, editing, draft, initial, proble
systemNotes: boolean
onReview: (target: ReviewTarget, action: CatalogReviewAction) => void
onConfirmColumn: (col: string) => void
onFillCodes: (col: string) => void
onChange: (col: string, field: ColumnField, value: string) => void
}) {
const { name, structure, items } = col
@@ -166,7 +169,10 @@ const ColumnRow = memo(function ColumnRow({ col, editing, draft, initial, proble
)}
{cell(
'codes',
<Value item={codes} render={(v) => <Codes value={v as Record<string, string>} />} />,
<Value item={codes} render={(v) => (
<Codes value={v as Record<string, string>} label={KT.fillLabel(name)}
onFill={codes?.status !== 'rejected' && !busy ? () => onFillCodes(name) : undefined} />
)} />,
<textarea className="field mono !min-h-0 !px-1.5 !py-1 !text-2xs" rows={Math.min(4, Math.max(1, (draft?.codes ?? '').split('\n').length))}
value={draft?.codes ?? ''}
placeholder={codes?.status === 'rejected' ? CT.rejectedPlaceholder(codesInline(codes.value)) : CT.codesPlaceholder}
@@ -183,7 +189,8 @@ const ColumnRow = memo(function ColumnRow({ col, editing, draft, initial, proble
</tr>
)
}, (a, b) => a.col === b.col && a.editing === b.editing && a.draft === b.draft && a.initial === b.initial && a.busy === b.busy
&& a.systemNotes === b.systemNotes && a.onReview === b.onReview && a.onConfirmColumn === b.onConfirmColumn && a.onChange === b.onChange
&& a.systemNotes === b.systemNotes && a.onReview === b.onReview && a.onConfirmColumn === b.onConfirmColumn && a.onFillCodes === b.onFillCodes
&& a.onChange === b.onChange
// 格式问题每次都是新对象:只比这一列的几格
&& COLUMN_FIELDS.every((f) => a.problems[`c:${a.col.name}:${f}`] === b.problems[`c:${b.col.name}:${f}`]))
@@ -195,17 +202,33 @@ export function Value<T>({ item, render }: { item: CatalogItem<T> | undefined; r
return <span className={clsx(item.status === 'rejected' && 'text-faint line-through')}>{render(item.value)}</span>
}
function Codes({ value }: { value: Record<string, string> }) {
/** 含义还空着的码值个数(数据剖析给的码值候选只有取值,含义等人填) */
export const pendingCodes = (v: Record<string, string> | undefined): number =>
Object.values(v ?? {}).filter((x) => !String(x ?? '').trim()).length
function Codes({ value, label, onFill }: { value: Record<string, string>; label: string; onFill?: () => void }) {
const entries = Object.entries(value)
const pending = pendingCodes(value)
return (
<span className="flex flex-wrap gap-1" title={entries.map(([k, v]) => `${k}=${v}`).join('\n')}>
{entries.slice(0, CODES_SHOWN).map(([k, v]) => (
<span key={k} className="inline-flex max-w-full items-center gap-1 rounded border bg-bg px-1 text-2xs">
<span className="mono text-dim">{k}</span>
<span className="truncate">{v}</span>
</span>
))}
{entries.length > CODES_SHOWN && <span className="text-2xs text-faint">+{entries.length - CODES_SHOWN}</span>}
<span className="block">
<span className="flex flex-wrap gap-1" title={entries.map(([k, v]) => `${k}=${v.trim() || KT.pending}`).join('\n')}>
{entries.slice(0, CODES_SHOWN).map(([k, v]) => (
<span key={k} className="inline-flex max-w-full items-center gap-1 rounded border bg-bg px-1 text-2xs" data-code={k}>
<span className="mono text-dim">{k}</span>
{v.trim()
? <span className="truncate">{v}</span>
: <span className="truncate" style={{ color: 'var(--st-waiting)' }} data-code-pending="">{KT.pending}</span>}
</span>
))}
{entries.length > CODES_SHOWN && <span className="text-2xs text-faint">+{entries.length - CODES_SHOWN}</span>}
</span>
{pending > 0 && onFill && (
// 各码值的「含义待填写」已经写在上面,这里只给入口;几个待填写写在悬停里
<button type="button" className="mt-1 inline-flex items-center gap-1 whitespace-nowrap rounded px-1 text-2xs text-dim underline decoration-dotted underline-offset-2 hover:text-fg"
onClick={onFill} aria-label={label} title={KT.pendingN(pending)} data-codes-fill={pending}>
<PencilLine size={10} aria-hidden /> {KT.fill}
</button>
)}
</span>
)
}
@@ -0,0 +1,450 @@
import { useMemo, useState } from 'react'
import type { ReactNode } from 'react'
import { ArrowUpRight, CircleCheck, OctagonX, ScanSearch, ShieldAlert, TriangleAlert } from 'lucide-react'
import clsx from 'clsx'
import type {
CatalogProfileCodesFinding, CatalogProfileDateFinding, CatalogProfileOut, CatalogProfileRelationFinding, CatalogProfileSettings,
CatalogProfileSize, CatalogProfileSkip, CatalogProfileTable, CatalogTableRow,
} from '../../types'
import { ErrorState, Modal, Notice, Spinner, useTicker } from '../../components/ui'
import {
CATALOG_CARDINALITY_LABEL, CATALOG_TEXT as CT, PROFILE_SKIP_KIND_LABEL, PROFILE_SKIP_REASON_LABEL, PROFILE_STOP_LABEL, PROFILE_STOP_NEXT, PROFILE_TEXT as PT,
} from '../../lib/terms'
import { STATUS_TONE, StatusChip } from './parts'
import { PROFILE_DEFAULT_TABLES, PROFILE_MAX_TABLES, coverageText, secondsText } from './profile'
import type { ProfileBlock } from './profile'
// ===========================================================================
// 数据目录页的「数据剖析」。接口是同步的:一次请求做完整个剖析才返回,服务端不支持中途停止,所以这里不给「停止」,
// 进行中只写已用时长和总时长上限,并明说无法中途停止。关掉窗口剖析照常进行(状态在页面上,页头有进度入口),
// 完成后弹提示、可以再打开报告。
//
// 四段:选范围、看预算、确认开始 → 进行中 → 报告(用了几条查询、为什么停、每张表的发现和跳过的项);
// 开始不了(409)时写明是哪一种情况和下一步,未开启的就地打开剖析设置。
// ===========================================================================
/** 一次剖析的状态。放在页面上而不是弹窗里:关掉弹窗剖析照常进行,完成后还能再打开报告 */
export type ProfileJob =
| { phase: 'running'; startedAt: number; tables: string[] | null; settings: CatalogProfileSettings }
| { phase: 'done'; report: CatalogProfileOut; ms: number }
| { phase: 'blocked'; kind: ProfileBlock; message: string; tables: string[] | null }
| { phase: 'failed'; error: unknown; tables: string[] | null }
type Scope = 'selected' | 'filtered' | 'default'
/** 报告里每类最多逐条列几项,其余合成一句 */
const SKIP_SHOWN = 6
const CODES_SHOWN = 12
export function ProfileDialog({
sourceName, settings, rows, visible, selected, filtered, job, onStart, onClose, onSettings, onGoSource, onOpenTable, onFillCodes, onReset,
}: {
sourceName: string
/** 这个数据源此刻的剖析设置(数据源列表里读的) */
settings: CatalogProfileSettings
rows: CatalogTableRow[]
visible: CatalogTableRow[]
selected: string[]
filtered: boolean
/** null:还没开始(选范围) */
job: ProfileJob | null
/** tables 为 null 时由服务端挑表 */
onStart: (tables: string[] | null) => void
onClose: () => void
/** 打开这个数据源的剖析设置 */
onSettings: () => void
/** 去数据源卡片(停用、没有表结构、快照被改动时) */
onGoSource: () => void
onOpenTable: (table: string) => void
onFillCodes: (table: string, column: string) => void
/** 回到选范围 */
onReset: () => void
}) {
const live = useMemo(() => rows.filter((r) => r.in_schema).map((r) => r.table_name), [rows])
const scopes = useMemo(() => {
const liveSet = new Set(live)
const out: { key: Scope; label: string; hint?: string; tables: string[] | null }[] = []
const sel = selected.filter((t) => liveSet.has(t))
if (sel.length) out.push({ key: 'selected', label: PT.scopeSelected(sel.length), tables: sel })
const vis = visible.filter((r) => r.in_schema).map((r) => r.table_name)
if (filtered && vis.length && vis.length !== live.length) out.push({ key: 'filtered', label: PT.scopeFiltered(vis.length), tables: vis })
out.push({ key: 'default', label: PT.scopeDefault, hint: PT.scopeDefaultHint(PROFILE_DEFAULT_TABLES), tables: null })
return out
}, [live, selected, visible, filtered])
const [scope, setScope] = useState<Scope>(() => scopes[0].key)
const picked = scopes.find((s) => s.key === scope) ?? scopes[0]
const tooMany = (picked.tables?.length ?? 0) > PROFILE_MAX_TABLES
const running = job?.phase === 'running'
useTicker(1000, running)
let body: ReactNode
let footer: ReactNode
if (!job) {
body = (
<>
<p className="text-xs leading-relaxed text-dim">{PT.intro}</p>
{!settings.enabled && (
<Notice tone="warn" attr={{ 'data-profile-disabled': '' }}>
<p className="font-medium">{PT.disabledTitle}</p>
<p className="mt-0.5 text-dim">{PT.disabledBody}</p>
<button type="button" className="btn btn-sm mt-2" onClick={onSettings} data-profile-enable="">{PT.enable}</button>
</Notice>
)}
<fieldset>
<legend className="label">{PT.scope}</legend>
<div className="space-y-1.5" role="radiogroup" aria-label={PT.scope}>
{scopes.map((s) => {
const over = (s.tables?.length ?? 0) > PROFILE_MAX_TABLES
return (
<label key={s.key} className={clsx('flex cursor-pointer items-start gap-2 rounded-md border px-3 py-2 text-xs',
scope === s.key ? 'border-[var(--accent)] bg-accent-soft' : 'hover:bg-hover')} data-profile-scope={s.key}>
<input type="radio" className="mt-0.5" name="catalog-profile-scope" checked={scope === s.key} onChange={() => setScope(s.key)} />
<span className="min-w-0">
<span>{s.label}</span>
{(s.hint || over) && (
<span className={clsx('mt-0.5 block text-2xs', over ? 'text-[var(--err)]' : 'text-faint')}>
{over ? PT.scopeTooMany(PROFILE_MAX_TABLES) : s.hint}
</span>
)}
</span>
</label>
)
})}
</div>
</fieldset>
{settings.enabled && <Budget settings={settings} sourceName={sourceName} onSettings={onSettings} />}
<p className="flex items-start gap-1.5 text-2xs leading-relaxed text-faint">
<ShieldAlert size={11} className="mt-px shrink-0" aria-hidden /> {PT.noCancel}
</p>
</>
)
footer = (
<>
<button type="button" className="btn" onClick={onClose}>{CT.cancel}</button>
<button type="button" className="btn btn-primary" disabled={!settings.enabled || tooMany} onClick={() => onStart(picked.tables)}
data-profile-start="">
<ScanSearch size={12} aria-hidden /> {PT.start}
</button>
</>
)
} else if (job.phase === 'running') {
const used = Date.now() - job.startedAt
const max = job.settings.max_total_s * 1000
body = (
<div className="space-y-3" aria-live="polite">
<div className="flex items-center gap-2 text-xs">
<Spinner size={13} />
<span className="font-medium" data-profile-title="">{PT.running}</span>
<span className="flex-1" />
<span className="tnum text-2xs text-faint" data-profile-elapsed={Math.floor(used / 1000)}>
{PT.elapsed(secondsText(used), secondsText(max))}
</span>
</div>
{/* 不是进度:服务端不报进度,这条只量已用时长占总时长上限的多少,剖析最晚在上限处停下 */}
<div className="h-1 overflow-hidden rounded-full bg-hover" role="meter" aria-label={PT.elapsed(secondsText(used), secondsText(max))}
aria-valuemin={0} aria-valuemax={job.settings.max_total_s} aria-valuenow={Math.min(job.settings.max_total_s, Math.floor(used / 1000))}>
<div className="h-full rounded-full bg-[var(--border-strong)] transition-[width] duration-1000 ease-linear"
style={{ width: `${Math.min(100, (used / max) * 100)}%` }} />
</div>
<Notice tone="info" attr={{ 'data-profile-no-cancel': '' }}>{PT.runningHint}</Notice>
<p className="text-2xs leading-relaxed text-faint">{PT.leavePage}</p>
</div>
)
footer = <button type="button" className="btn" onClick={onClose} data-autofocus data-profile-background="">{PT.background}</button>
} else if (job.phase === 'done') {
body = <Report report={job.report} ms={job.ms} onSettings={onSettings} onOpenTable={onOpenTable} onFillCodes={onFillCodes} />
footer = (
<>
<button type="button" className="btn btn-ghost mr-auto" onClick={onReset} data-profile-again="">{PT.again}</button>
<button type="button" className="btn btn-primary" onClick={onClose} data-autofocus data-profile-close="">{PT.close}</button>
</>
)
} else if (job.phase === 'blocked') {
const action = job.kind === 'disabled'
? <button type="button" className="btn btn-sm btn-primary" onClick={onSettings} data-profile-enable="">{PT.enable}</button>
: job.kind === 'busy' || job.kind === 'other'
? <button type="button" className="btn btn-sm" onClick={() => onStart(job.tables)} data-profile-retry="">{PT.retry}</button>
: <button type="button" className="btn btn-sm" onClick={onGoSource} data-profile-go-source="">{PT.goSource}</button>
body = (
<Notice tone={job.kind === 'busy' ? 'info' : 'warn'} attr={{ 'data-profile-blocked': job.kind }}>
<p className="font-medium">{PT.blocked[job.kind]}</p>
<p className="mt-0.5 text-dim">{job.message}</p>
{job.kind !== 'other' && <p className="mt-0.5 text-dim">{PT.blockedNext[job.kind]}</p>}
<div className="mt-2">{action}</div>
</Notice>
)
footer = (
<>
<button type="button" className="btn btn-ghost mr-auto" onClick={onReset} data-profile-back="">{PT.backToSetup}</button>
<button type="button" className="btn" onClick={onClose}>{PT.close}</button>
</>
)
} else {
body = (
<div data-profile-failed="">
<p className="mb-2 text-xs font-medium">{PT.failedTitle}</p>
<ErrorState error={job.error} onRetry={() => onStart(job.tables)} />
</div>
)
footer = (
<>
<button type="button" className="btn btn-ghost mr-auto" onClick={onReset}>{PT.backToSetup}</button>
<button type="button" className="btn" onClick={onClose}>{PT.close}</button>
</>
)
}
return (
<Modal open onClose={onClose} title={PT.title} width={640} footer={footer}>
<div className="space-y-4" data-profile-dialog={job?.phase ?? 'setup'}>{body}</div>
</Modal>
)
}
/** 确认前写明预算:按这个数据源的剖析设置,最多发几条查询、单条和总时长、抽样规模、整表统计的门槛 */
function Budget({ settings: s, sourceName, onSettings }: { settings: CatalogProfileSettings; sourceName: string; onSettings: () => void }) {
const lines = [PT.budgetQueries(s.max_queries, s.query_timeout_s), PT.budgetTotal(s.max_total_s), PT.budgetSample(s.sample_size),
PT.budgetScan(s.max_scan_rows)]
return (
<div className="rounded-lg border px-3 py-2.5" data-profile-budget={`${s.max_queries},${s.query_timeout_s},${s.max_total_s}`}>
<div className="mb-1.5 flex items-center gap-2 text-2xs">
<span className="font-medium text-dim">{PT.budget}</span>
<span className="text-faint">{PT.budgetFrom(sourceName)}</span>
<span className="flex-1" />
<button type="button" className="rounded px-1 text-2xs text-dim underline decoration-dotted underline-offset-2 hover:text-fg"
onClick={onSettings} data-profile-settings-open="">{PT.editSettings}</button>
</div>
<ul className="space-y-0.5 text-xs">
{lines.map((l) => (
<li key={l} className="flex items-start gap-1.5">
<span className="mt-[7px] h-1 w-1 shrink-0 rounded-full bg-[var(--text-faint)]" aria-hidden />
<span className="tnum">{l}</span>
</li>
))}
</ul>
</div>
)
}
// ---------------------------------------------------------------------------
// 报告
// ---------------------------------------------------------------------------
function Report({ report: r, ms, onSettings, onOpenTable, onFillCodes }: {
report: CatalogProfileOut
ms: number
onSettings: () => void
onOpenTable: (table: string) => void
onFillCodes: (table: string, column: string) => void
}) {
const changed = r.total.added + r.total.updated + r.total.removed
const stop = r.stopped
// 一项发现、一项跳过都没有的表合成一行,几十张表时报告不被空卡片撑长
const quiet = r.tables.filter((t) => !t.error && !t.findings.length && !t.skipped.length && !t.date_ranges.length)
const shown = r.tables.filter((t) => !quiet.includes(t))
return (
<div className="space-y-3" data-profile-report={stop ?? 'done'}>
<div className="flex items-start gap-2">
{stop
? <TriangleAlert size={15} className="mt-px shrink-0" style={{ color: 'var(--st-waiting)' }} aria-hidden />
: <CircleCheck size={15} className="mt-px shrink-0" style={{ color: 'var(--st-done)' }} aria-hidden />}
<div className="min-w-0 flex-1">
<p className="text-sm font-medium" data-profile-title="">{stop ? PT.stoppedTitle(PROFILE_STOP_LABEL[stop] ?? stop) : PT.done}</p>
<p className="tnum mt-0.5 text-xs text-dim" data-profile-summary={`${r.queries_used}/${r.settings.max_queries}`}>
{PT.summary(r.queries_used, r.settings.max_queries, r.tables.length)}
<span className="text-faint"> · {secondsText(ms)}</span>
</p>
<p className="tnum mt-0.5 text-xs" data-profile-changes={`${r.total.added},${r.total.updated},${r.total.removed}`}>
{changed ? PT.changes(r.total.added, r.total.updated, r.total.removed) : <span className="text-faint">{PT.noChange}</span>}
</p>
</div>
</div>
{stop && (
<Notice tone="warn" attr={{ 'data-profile-stopped': stop }}>
<p>{PROFILE_STOP_NEXT[stop] ?? ''}</p>
{stop !== 'failed' && (
<button type="button" className="btn btn-sm mt-2" onClick={onSettings} data-profile-settings-open="">{PT.editSettings}</button>
)}
</Notice>
)}
{r.note && <Notice tone="info" attr={{ 'data-profile-note': '' }}>{r.note}</Notice>}
{shown.map((t) => <TableReport key={t.table_name} t={t} onOpenTable={onOpenTable} onFillCodes={onFillCodes} />)}
{quiet.length > 0 && (
<p className="text-2xs leading-relaxed text-faint" data-profile-quiet={quiet.length}>
{PT.noFindings}:<span className="mono">{quiet.map((t) => t.table_name).join('、')}</span>
</p>
)}
</div>
)
}
function sizeText(size: CatalogProfileSize | null): { text: string; hint?: string } | null {
if (!size) return null
if (size.rows != null) return { text: size.method === 'stats' ? PT.rowsStats(size.rows) : PT.rowsCount(size.rows), hint: PT.rowsHint[size.method] }
if (size.at_least != null) return { text: PT.rowsAtLeast(size.at_least), hint: PT.rowsHint.count }
return null
}
function TableReport({ t, onOpenTable, onFillCodes }: {
t: CatalogProfileTable
onOpenTable: (table: string) => void
onFillCodes: (table: string, column: string) => void
}) {
const relations = t.findings.filter((f): f is CatalogProfileRelationFinding => f.kind === 'relation')
const codes = t.findings.filter((f): f is CatalogProfileCodesFinding => f.kind === 'codes')
const date = t.findings.find((f): f is CatalogProfileDateFinding => f.kind === 'business_date')
const size = sizeText(t.row_estimate)
const changed = t.added + t.updated + t.removed
return (
<section className="rounded-lg border" data-profile-table={t.table_name} aria-label={t.table_name}>
<header className="flex flex-wrap items-center gap-x-2 gap-y-0.5 border-b bg-elev px-3 py-1.5">
<h3 className="mono min-w-0 text-xs font-medium [overflow-wrap:anywhere]">{t.table_name}</h3>
<span className="tnum text-2xs text-faint">
{PT.tableQueries(t.queries)}
{size && <span title={size.hint}> · {size.text}</span>}
{changed > 0 && <span> · {PT.changes(t.added, t.updated, t.removed)}</span>}
</span>
<span className="flex-1" />
{!t.error && (
<button type="button" className="inline-flex items-center gap-0.5 rounded px-1 text-2xs text-dim hover:bg-hover hover:text-fg"
onClick={() => onOpenTable(t.table_name)} aria-label={PT.openTableLabel(t.table_name)} data-profile-open={t.table_name}>
{PT.openTable} <ArrowUpRight size={10} aria-hidden />
</button>
)}
</header>
<div className="space-y-2.5 px-3 py-2">
{t.error && (
<p className="flex items-start gap-1.5 text-xs text-[var(--err)]" data-profile-table-error="">
<OctagonX size={12} className="mt-0.5 shrink-0" aria-hidden /> <span>{t.error}</span>
</p>
)}
{relations.length > 0 && (
<Group title={PT.findingRelations}>
{relations.map((f) => <RelationLine key={f.path} f={f} />)}
</Group>
)}
{codes.length > 0 && (
<Group title={PT.findingCodes}>
{codes.map((f) => <CodesLine key={f.path} f={f} onFill={() => onFillCodes(t.table_name, f.column)} />)}
</Group>
)}
{date && (
<Group title={PT.findingDate}>
<li className="text-xs" data-profile-finding="business_date">
<StatusChip status={date.status} className="mr-1.5 align-middle" />
{PT.dateProposal(date.column)}
<span className="tnum text-dim">,{PT.dateSpan(date.min.slice(0, 19), date.max.slice(0, 19))}</span>
</li>
</Group>
)}
{!date && t.date_ranges.length > 0 && (
<Group title={PT.dateRanges}>
{t.date_ranges.map((d) => (
<li key={d.column} className="tnum text-xs text-dim" data-profile-date-range={d.column}>
<span className="mono text-fg">{d.column}</span>:{PT.dateSpan(d.min.slice(0, 19), d.max.slice(0, 19))}
</li>
))}
</Group>
)}
{!t.error && !t.findings.length && !t.date_ranges.length && (
<p className="text-2xs text-faint">{PT.noFindings}</p>
)}
{t.skipped.length > 0 && <Skipped list={t.skipped} />}
</div>
</section>
)
}
function Group({ title, children }: { title: string; children: ReactNode }) {
return (
<div>
<div className="mb-1 text-2xs font-medium text-faint">{title}</div>
<ul className="space-y-1.5">{children}</ul>
</div>
)
}
function RelationLine({ f }: { f: CatalogProfileRelationFinding }) {
const outcome = f.confirmed
? { text: PT.relationConfirmed, tone: 'var(--text-dim)' }
: f.status === 'verified'
? { text: PT.relationVerified, tone: STATUS_TONE.verified }
: { text: `${PT.relationKept}:${f.coverage < 0.95 ? PT.relationLowCoverage : PT.relationNotUnique}`, tone: STATUS_TONE.proposed }
return (
<li className="text-xs" data-profile-finding="relation" data-status={f.status} data-path={f.path}>
<div className="flex flex-wrap items-center gap-x-1.5 gap-y-0.5">
<StatusChip status={f.status} />
<span className="mono min-w-0 [overflow-wrap:anywhere]">{f.target}</span>
</div>
<div className="tnum mt-0.5 text-2xs text-dim">
<span data-profile-coverage="">{PT.coverage(coverageText(f.coverage))}</span>
{' · '}
<span data-profile-cardinality="">{f.cardinality ? CATALOG_CARDINALITY_LABEL[f.cardinality] : PT.cardinalityUnknown}</span>
{' · '}
{PT.sampled(f.sample, f.matched)}
</div>
<div className="mt-0.5 text-2xs" style={{ color: outcome.tone }} data-profile-outcome="">{outcome.text}</div>
</li>
)
}
function CodesLine({ f, onFill }: { f: CatalogProfileCodesFinding; onFill: () => void }) {
// 服务端在 summary 里写了几个含义待填写(别的来源写过的含义会沿用);认不出时按都待填写
const pending = Number(f.summary.match(/(\d+)\s*个含义待填写/)?.[1] ?? (f.summary.includes('待填写') ? f.values.length : 0))
return (
<li className="text-xs" data-profile-finding="codes" data-column={f.column}>
<div className="flex flex-wrap items-center gap-x-1.5 gap-y-0.5">
<StatusChip status={f.status} />
<span className="mono font-medium">{f.column}</span>
<span className="tnum text-2xs text-faint">{PT.codesSummary(f.values.length, f.rows)}</span>
</div>
<div className="mt-1 flex flex-wrap gap-1">
{f.values.slice(0, CODES_SHOWN).map((v) => (
<span key={v.value} className="inline-flex items-center gap-1 rounded border bg-bg px-1 text-2xs">
<span className="mono">{v.value}</span>
<span className="tnum text-faint">{PT.codeRows(v.rows)}</span>
</span>
))}
{f.values.length > CODES_SHOWN && <span className="text-2xs text-faint">+{f.values.length - CODES_SHOWN}</span>}
</div>
<div className="mt-1 flex flex-wrap items-center gap-2">
{pending > 0 && <span className="text-2xs" style={{ color: 'var(--st-waiting)' }} data-codes-pending={pending}>{PT.codesPendingN(pending)}</span>}
<button type="button" className="btn btn-xs" onClick={onFill} aria-label={PT.fillMeaningsLabel(f.column)} data-profile-fill={f.column}>
{PT.fillMeanings}
</button>
</div>
</li>
)
}
/** 跳过的项:原因(短)+ 哪一项 + 服务端给的整句。多了折起来,每类停下的原因只列前几项 */
function Skipped({ list }: { list: CatalogProfileSkip[] }) {
const [all, setAll] = useState(false)
const shown = all ? list : list.slice(0, SKIP_SHOWN)
return (
<div data-profile-skipped={list.length}>
<div className="mb-1 text-2xs font-medium text-faint">{PT.skippedTitle(list.length)}</div>
<ul className="space-y-1">
{shown.map((s, i) => (
<li key={`${s.kind}:${s.target}:${i}`} className="flex items-start gap-1.5 text-2xs" data-profile-skip={s.reason}>
<span className="chip mt-px shrink-0 !px-1.5 !py-0" style={{ color: 'var(--text-dim)' }}>
{PROFILE_SKIP_REASON_LABEL[s.reason] ?? s.reason}
</span>
<span className="min-w-0 leading-relaxed">
<span className="text-faint">{PROFILE_SKIP_KIND_LABEL[s.kind] ?? s.kind} </span>
<span className="mono text-dim [overflow-wrap:anywhere]">{s.target}</span>
<span className="block text-dim">{s.detail}</span>
</span>
</li>
))}
</ul>
{list.length > SKIP_SHOWN && !all && (
<button type="button" className="mt-1 rounded px-1 text-2xs text-dim underline decoration-dotted underline-offset-2 hover:text-fg"
onClick={() => setAll(true)}>
{PT.moreSkipped(list.length - SKIP_SHOWN)}
</button>
)}
</div>
)
}
@@ -0,0 +1,162 @@
import { useState } from 'react'
import { ShieldAlert } from 'lucide-react'
import { ApiError, api } from '../../api/client'
import type { CatalogProfileNumberKey } from '../../types'
import { Field, Modal, Spinner, toast } from '../../components/ui'
import { PROFILE_FIELD_HINT, PROFILE_FIELD_LABEL, PROFILE_FIELD_UNIT, PROFILE_TEXT as PT } from '../../lib/terms'
import {
PROFILE_DEFAULTS, PROFILE_LIMITS, PROFILE_NUMBER_KEYS, hasProfileOption, profileFieldOfRejection, profileFormOf, profileOptionOf,
profileProblems, profileSettingsOfForm, sameProfileForm, withProfileOption,
} from './profile'
import type { ProfileForm } from './profile'
// ===========================================================================
// 数据剖析的设置:开关、五项预算、风险说明。数据源编辑框里是其中一段;导入表格的源没有编辑框,数据目录页在
// 剖析被拒(未开启)时也要就地打开它,所以另有一个只改这一项的弹窗(options 的其余键原样带上)。
// 剖析会对业务库发查询,开关默认关着;开启后五项预算才展开,风险说明写的是此刻填的数。
// ===========================================================================
/** 保存被拒时的落点:某一项,或者整段(认不出是哪一项) */
export type ProfileErrorKey = 'enabled' | CatalogProfileNumberKey | 'section'
export function ProfileSettingsSection({ idPrefix, form, onChange, serverError }: {
/** 输入框 id 的前缀:报错时按 `${idPrefix}-${键}` 聚焦 */
idPrefix: string
form: ProfileForm
onChange: (next: ProfileForm) => void
/** 服务端拒收(422)的那一句,落在对应的项上 */
serverError?: { key: ProfileErrorKey; message: string } | null
}) {
const problems = profileProblems(form)
const errorOf = (k: CatalogProfileNumberKey) => (serverError?.key === k ? serverError.message : problems[k])
// 开关关着时预算收起;但填错的项(或服务端点了名的项)要看得见,不然保存被拦下却找不到原因
const open = form.enabled || Object.keys(problems).length > 0
|| (!!serverError && serverError.key !== 'enabled' && serverError.key !== 'section')
const s = profileSettingsOfForm(form)
const setValue = (k: CatalogProfileNumberKey, v: string) => onChange({ ...form, values: { ...form.values, [k]: v } })
const switchError = serverError && (serverError.key === 'enabled' || serverError.key === 'section') ? serverError.message : null
return (
<fieldset className="rounded-lg border" data-field="catalog_profile" data-profile-settings={form.enabled ? 'on' : 'off'}>
<legend className="sr-only">{PT.section}</legend>
<label className="flex cursor-pointer items-start gap-2 p-2.5">
<input id={`${idPrefix}-enabled`} type="checkbox" className="mt-0.5" checked={form.enabled}
aria-describedby={`${idPrefix}-enabled-hint`} aria-invalid={switchError ? true : undefined}
onChange={(e) => onChange({ ...form, enabled: e.target.checked })} data-profile-enabled="" />
<span className="min-w-0 text-xs">
{PT.section}
<span id={`${idPrefix}-enabled-hint`} className="ml-1.5 text-2xs leading-relaxed text-faint">{PT.enableHint}</span>
</span>
</label>
{switchError && (
<p className="px-2.5 pb-2 text-2xs leading-relaxed text-[var(--err)]" role="alert" data-profile-error="section">{switchError}</p>
)}
{open && (
<div className="space-y-3 border-t px-2.5 py-2.5">
{form.enabled && (
<p className="flex items-start gap-1.5 rounded-md px-2 py-1.5 text-2xs leading-relaxed text-dim" data-profile-risk=""
style={{ background: 'var(--st-waiting-soft)' }}>
<ShieldAlert size={12} className="mt-px shrink-0" style={{ color: 'var(--st-waiting)' }} aria-hidden />
<span>{PT.risk(s.max_queries, s.query_timeout_s, s.max_total_s)}</span>
</p>
)}
<div className="grid gap-3 sm:grid-cols-2">
{PROFILE_NUMBER_KEYS.map((k) => {
const { min, max } = PROFILE_LIMITS[k]
const unit = PROFILE_FIELD_UNIT[k]
const err = errorOf(k)
const id = `${idPrefix}-${k}`
const hint = `${PT.range(min, max, unit, PROFILE_DEFAULTS[k])}${k === 'max_scan_rows' ? `,${PT.scanZero}` : ''}。${PROFILE_FIELD_HINT[k]}`
return (
<Field key={k} htmlFor={id} label={PROFILE_FIELD_LABEL[k]} hint={hint} error={err}>
{(p) => (
<div className="relative" data-profile-field={k}>
<input {...p} className="field tnum pr-8" inputMode={PROFILE_LIMITS[k].integer ? 'numeric' : 'decimal'}
autoComplete="off" spellCheck={false} value={form.values[k]}
placeholder={PT.placeholder(PROFILE_DEFAULTS[k])}
style={err ? { borderColor: 'var(--err)' } : undefined}
onChange={(e) => setValue(k, e.target.value)} />
<span className="pointer-events-none absolute right-2.5 top-1/2 -translate-y-1/2 text-2xs text-faint" aria-hidden>
{unit}
</span>
</div>
)}
</Field>
)
})}
</div>
</div>
)}
</fieldset>
)
}
/** 聚焦保存被拒的那一项(预算收着时先等它展开) */
export function focusProfileField(idPrefix: string, key: ProfileErrorKey) {
const id = key === 'section' ? `${idPrefix}-enabled` : `${idPrefix}-${key}`
requestAnimationFrame(() => requestAnimationFrame(() => {
const el = document.getElementById(id)
el?.scrollIntoView({ block: 'nearest' })
el?.focus({ preventScroll: true })
}))
}
/**
* 只改数据剖析设置的弹窗:导入表格的源(没有编辑框)、数据目录页里剖析被拒时用。
* 保存时 options 的其余键(遮罩列、schema……)原样带上,只换剖析设置这一项
*/
export function ProfileSettingsDialog({ row, onClose, onSaved }: {
row: { id: string; name: string; options?: Record<string, unknown> | null }
onClose: () => void
onSaved: (row: any) => void
}) {
const [initial] = useState(() => profileFormOf(row.options))
const [form, setForm] = useState<ProfileForm>(initial)
const [saving, setSaving] = useState(false)
const [serverError, setServerError] = useState<{ key: ProfileErrorKey; message: string } | null>(null)
const dirty = !sameProfileForm(form, initial)
const problems = Object.keys(profileProblems(form)).length
const idPrefix = `profile-${row.id}`
const save = async () => {
if (!dirty || problems) return
setSaving(true)
setServerError(null)
try {
const options = withProfileOption(row.options, profileOptionOf(form, hasProfileOption(row.options)))
const next = await api.datasources.update(row.id, { options })
onSaved(next)
toast.ok(form.enabled === initial.enabled ? PT.saved(row.name) : form.enabled ? PT.savedOn(row.name) : PT.savedOff(row.name))
} catch (e) {
const key = e instanceof ApiError && e.status === 422 ? profileFieldOfRejection(e.message) : null
if (key) {
setServerError({ key, message: e instanceof ApiError ? e.message : '' })
focusProfileField(idPrefix, key)
} else {
toast.error(e)
}
} finally {
setSaving(false)
}
}
return (
<Modal open onClose={onClose} dirty={dirty} width={560} title={PT.dialogTitle(row.name)}
footer={(
<>
{problems > 0 && <span className="mr-auto text-2xs text-[var(--err)]" data-profile-invalid={problems}>{PT.invalid(problems)}</span>}
<button type="button" className="btn" onClick={onClose}>取消</button>
<button type="button" className="btn btn-primary" disabled={saving || !dirty || problems > 0} onClick={() => void save()}
data-profile-save="">
{saving ? <Spinner size={11} /> : null} 保存
</button>
</>
)}>
<div className="space-y-3" data-profile-settings-dialog={row.id}>
<p className="text-2xs leading-relaxed text-faint">{PT.dialogBody}</p>
<ProfileSettingsSection idPrefix={idPrefix} form={form} serverError={serverError}
onChange={(next) => { setForm(next); setServerError(null) }} />
</div>
</Modal>
)
}
+63 -5
View File
@@ -12,13 +12,15 @@ import { useLeaveGuard } from '../../lib/leave'
import { formatDateTime, formatNumber } from '../../lib/format'
import {
CATALOG_CARDINALITY_LABEL, CATALOG_IMPACT_TEXT, CATALOG_KIND_HINT, CATALOG_KIND_LABEL, CATALOG_TABLE_FIELD_HINT,
CATALOG_TABLE_FIELD_LABEL, CATALOG_TEXT as CT,
CATALOG_TABLE_FIELD_LABEL, CATALOG_TEXT as CT, CODES_TEXT as KT,
} 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'
import { ColumnTable, Value, pendingCodes } from './ColumnTable'
import type { ColumnModel } from './ColumnTable'
import { CodesDialog } from './CodesDialog'
import { coverageText } from './profile'
import {
COLUMN_FIELDS, DATE_KEYS, TABLE_FIELDS, buildNotes, changeCount, columnPath, confirmProposed, countsOf, formFromNotes,
relationDraft, relationPath, tablePath, unknownKeys, validateForm,
@@ -49,13 +51,15 @@ const PROBLEM_TEXT = {
type Editing = { initial: EditForm; form: EditForm }
type Conflict = 'edit' | 'review'
export function TableDetail({ sourceId, table, rows, drafted, prev, next, onOpen, onBack, onDetail }: {
export function TableDetail({ sourceId, table, rows, drafted, fillCodes, prev, next, onOpen, onBack, onDetail }: {
sourceId: string
table: string
/** 全部表:关联关系的目标表能点就跳过去,编辑时给目标表做候选 */
rows: CatalogTableRow[]
/** 起草刚写进了这些表:正在看的表在里面、又没在编辑,就重新载入 */
drafted: { seq: number; tables: Set<string> }
/** 从剖析报告点「填写含义」过来:载入后打开这一列的码值弹窗(seq 让同一列连点两次也会再开) */
fillCodes?: { table: string; column: string; seq: number } | null
prev: string | null
next: string | null
onOpen: (table: string) => void
@@ -73,6 +77,8 @@ export function TableDetail({ sourceId, table, rows, drafted, prev, next, onOpen
const [live, setLive] = useState('')
const [colQuery, setColQuery] = useState('')
const [onlyPending, setOnlyPending] = useState(false)
/** 正在填写哪一列的码值含义 */
const [codesFor, setCodesFor] = useState<string | null>(null)
const loadSeq = useRef(0)
const onDetailRef = useRef(onDetail)
onDetailRef.current = onDetail
@@ -101,6 +107,7 @@ export function TableDetail({ sourceId, table, rows, drafted, prev, next, onOpen
setColQuery('')
setOnlyPending(false)
setLive('')
setCodesFor(null)
void load()
}, [load])
@@ -115,6 +122,15 @@ export function TableDetail({ sourceId, table, rows, drafted, prev, next, onOpen
if (drafted.tables.has(table) && !editingRef.current) void load()
}, [drafted, table, load])
// 剖析报告里点了「填写含义」:这张表载入后打开那一列的码值弹窗。编辑中不打开,免得同一项两处同时改
const fillSeen = useRef(0)
useEffect(() => {
if (!fillCodes || fillCodes.table !== table || fillCodes.seq === fillSeen.current) return
if (!detail || detail.table_name !== table || editingRef.current) return
fillSeen.current = fillCodes.seq
if (detail.notes.columns?.[fillCodes.column]?.codes) setCodesFor(fillCodes.column)
}, [fillCodes, table, detail])
const changes = edit ? changeCount(edit.initial, edit.form) : 0
const problems = useMemo(() => (edit ? validateForm(edit.form, PROBLEM_TEXT) : {}), [edit])
const problemCount = Object.keys(problems).length
@@ -163,6 +179,41 @@ export function TableDetail({ sourceId, table, rows, drafted, prev, next, onOpen
if (done) setLive(CT.confirmedN(n))
}, [sourceId, table, write])
const fillCodesOf = useCallback((col: string) => setCodesFor(col), [])
/**
* 填好的码值含义整份提交:只换这一列码值的值,服务端记为人工填写、已确认。别人刚改过(409)时收起弹窗、挂冲突横幅;
* 其余失败弹窗留着,填的字不丢
*/
const saveCodes = useCallback(async (col: string, value: Record<string, string>) => {
const d = detailRef.current
const item = d?.notes.columns?.[col]?.codes
if (!d || !item) return
const notes = structuredClone(d.notes)
notes.columns![col] = { ...notes.columns![col], codes: { ...item, value } }
setBusy(`codes:${col}`)
setWriteError(null)
try {
const next = await api.dataCatalog.put(sourceId, table, notes, d.version)
setDetail(next)
setConflict(null)
onDetailRef.current(next)
setCodesFor(null)
const filled = Math.max(0, pendingCodes(item.value) - pendingCodes(value))
toast.ok(KT.saved(filled))
setLive(KT.saved(filled))
} catch (e) {
if (e instanceof ApiError && e.status === 409) {
setCodesFor(null)
setConflict('review')
} else {
toast.error(e)
}
} finally {
setBusy('')
}
}, [sourceId, table])
const setColumn = useCallback((col: string, f: ColumnField, v: string) => {
setEdit((e) => e && ({ ...e, form: { ...e.form, columns: { ...e.form.columns, [col]: { ...e.form.columns[col], [f]: v } } } }))
}, [])
@@ -368,7 +419,7 @@ export function TableDetail({ sourceId, table, rows, drafted, prev, next, onOpen
? (
<ColumnTable columns={shownColumns} form={edit?.form.columns ?? null} initial={edit?.initial.columns ?? null}
problems={problems} busy={!!busy} systemNotes={detail.system_notes}
onReview={review} onConfirmColumn={confirmColumn} onChange={setColumn} />
onReview={review} onConfirmColumn={confirmColumn} onFillCodes={fillCodesOf} onChange={setColumn} />
)
: <p className="rounded-lg border px-3 py-6 text-center text-xs text-faint">{CT.noColumns}</p>}
</section>
@@ -394,6 +445,11 @@ export function TableDetail({ sourceId, table, rows, drafted, prev, next, onOpen
</div>
</div>
{codesFor && notes.columns?.[codesFor]?.codes && (
<CodesDialog column={codesFor} item={notes.columns[codesFor]!.codes!} saving={busy === `codes:${codesFor}`}
onSave={(v) => void saveCodes(codesFor, v)} onClose={() => setCodesFor(null)} />
)}
{edit && (
<footer className="flex flex-wrap items-center gap-2 border-t bg-panel px-4 py-2.5" data-catalog-edit-bar="">
<span className="tnum text-xs text-dim" data-catalog-changes={changes}>{changes ? CT.changes(changes) : CT.noChanges}</span>
@@ -597,7 +653,9 @@ function RelationList({ relations, busy, targetOf, onOpen, onReview }: {
<td className={clsx('mono px-2 py-1.5 align-top', rejected && 'text-faint line-through')}>{r.to_columns.join(', ')}</td>
<td className="px-2 py-1.5 align-top">{r.cardinality ? CATALOG_CARDINALITY_LABEL[r.cardinality] : <span className="text-faint">{CT.cardinalityNone}</span>}</td>
<td className="tnum px-2 py-1.5 align-top">
{typeof r.coverage === 'number' ? CT.coverageValue(Math.round(r.coverage * 100)) : <span className="text-faint">—</span>}
{typeof r.coverage === 'number'
? <span data-relation-coverage={r.coverage}>{coverageText(r.coverage)}</span>
: <span className="text-faint">—</span>}
</td>
<td className="px-2 py-1.5 align-top">
<ItemMark target={{ path: relationPath(r.id), where, source: r.source, status: r.status, note: r.note, updated_at: r.updated_at }}
+10 -3
View File
@@ -1,11 +1,11 @@
import { memo, useCallback, useEffect, useRef } from 'react'
import type { KeyboardEvent as ReactKeyboardEvent, MouseEvent as ReactMouseEvent } from 'react'
import { AlertTriangle, Search, Sparkles, X } from 'lucide-react'
import { AlertTriangle, ScanSearch, Search, Sparkles, X } from 'lucide-react'
import clsx from 'clsx'
import type { CatalogTableRow } from '../../types'
import { useRadioGroup } from '../../components/ui'
import { formatNumber } from '../../lib/format'
import { CATALOG_KIND_LABEL, CATALOG_TEXT as CT } from '../../lib/terms'
import { CATALOG_KIND_LABEL, CATALOG_TEXT as CT, PROFILE_TEXT as PT } from '../../lib/terms'
import { CountBadges } from './parts'
import { LIST_FILTERS, LIST_SORTS, progressOf } from './model'
import type { ListFilter, ListSort } from './model'
@@ -17,7 +17,7 @@ import type { ListFilter, ListSort } from './model'
// ===========================================================================
export function TableIndex({
rows, total, counts, query, onQuery, filter, onFilter, sort, onSort, selected, onSelected, active, onOpen, onDraft,
rows, total, counts, query, onQuery, filter, onFilter, sort, onSort, selected, onSelected, active, onOpen, onDraft, onProfile,
}: {
/** 筛选、排序后的行 */
rows: CatalogTableRow[]
@@ -35,6 +35,8 @@ export function TableIndex({
active: string | undefined
onOpen: (table: string) => void
onDraft: () => void
/** 剖析所选(数据剖析弹窗,范围默认是已选的表) */
onProfile?: () => void
}) {
const radio = useRadioGroup(LIST_FILTERS, filter, onFilter)
const listRef = useRef<HTMLUListElement>(null)
@@ -162,6 +164,11 @@ export function TableIndex({
<button type="button" className="btn btn-primary btn-sm" onClick={onDraft} data-catalog-draft-selected="">
<Sparkles size={11} aria-hidden /> {CT.draftSelected(selected.size)}
</button>
{onProfile && (
<button type="button" className="btn btn-sm" onClick={onProfile} data-catalog-profile-selected="">
<ScanSearch size={11} aria-hidden /> {PT.profileSelected(selected.size)}
</button>
)}
</div>
)}
</div>
+6 -3
View File
@@ -205,15 +205,18 @@ function codesText(v: Record<string, string>): string {
return Object.entries(v).map(([k, x]) => `${k}=${x}`).join('\n')
}
/** 码值的文字:每行一个「码值=含义」(全角等号也认)。返回解析结果和第一处错误 */
/**
* 码值的文字:每行一个「码值=含义」(全角等号也认)。含义可以空着(「2=」):数据剖析只知道列里出现过哪些取值,
* 含义等人填,服务端也收空含义。返回解析结果和第一处错误
*/
export function parseCodes(text: string): { value: Record<string, string>; error: { line: number } | { dup: string } | null } {
const value: Record<string, string> = {}
const lines = text.split('\n')
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim()
if (!line) continue
const m = line.match(/^(.+?)\s*[==]\s*(.+)$/)
if (!m) return { value, error: { line: i + 1 } }
const m = line.match(/^(.+?)\s*[==]\s*(.*)$/)
if (!m || !m[1].trim()) return { value, error: { line: i + 1 } }
const code = m[1].trim()
if (code in value) return { value, error: { dup: code } }
value[code] = m[2].trim()
+175
View File
@@ -0,0 +1,175 @@
import type { CatalogProfileNumberKey, CatalogProfileSettings } from '../../types'
import { PROFILE_FIELD_LABEL, PROFILE_TEXT } from '../../lib/terms'
// ===========================================================================
// 数据剖析的设置:数据源 options.catalog_profile 和表单之间的来回转换、校验、保存被拒时认出是哪一项。
// 规则跟着服务端(backend/app/data/catalog_profile.py 的 ProfileSettings、profile_settings、
// profile_settings_problem)走:缺省即关闭;读的时候宽松(某一项不对就用缺省),写的时候严格(超出范围的拒收)。
// ===========================================================================
/** 数据源 options 里剖析设置的键 */
export const PROFILE_OPTION = 'catalog_profile'
export const PROFILE_NUMBER_KEYS: readonly CatalogProfileNumberKey[] = [
'max_queries', 'query_timeout_s', 'sample_size', 'max_scan_rows', 'max_total_s',
]
/** 缺省值(服务端 ProfileSettings 的默认) */
export const PROFILE_DEFAULTS: CatalogProfileSettings = {
enabled: false, max_queries: 60, query_timeout_s: 10, sample_size: 2000, max_scan_rows: 100_000, max_total_s: 120,
}
/** 各数值项的范围(服务端 _NUMBER_FIELDS):下限、上限、是否必须是整数 */
export const PROFILE_LIMITS: Record<CatalogProfileNumberKey, { min: number; max: number; integer: boolean }> = {
max_queries: { min: 1, max: 500, integer: true },
query_timeout_s: { min: 1, max: 60, integer: false },
sample_size: { min: 10, max: 10_000, integer: true },
max_scan_rows: { min: 0, max: 10_000_000, integer: true },
max_total_s: { min: 10, max: 600, integer: false },
}
/** 一个数:数字和数字写成的文字都认(同服务端 _number)。不合规返回 null */
function numberIn(value: unknown, key: CatalogProfileNumberKey): number | null {
if (value == null || typeof value === 'boolean') return null
const text = String(value).trim().replace(/,/g, '')
if (!text) return null
const n = Number(text)
const { min, max, integer } = PROFILE_LIMITS[key]
if (!Number.isFinite(n) || n < min || n > max) return null
if (integer && !Number.isInteger(n)) return null
return n
}
const rawOf = (options: Record<string, unknown> | null | undefined): Record<string, unknown> | null => {
const raw = options?.[PROFILE_OPTION]
return raw && typeof raw === 'object' && !Array.isArray(raw) ? raw as Record<string, unknown> : null
}
/** options → 生效的设置。宽松:某一项不对用缺省;开关只认 true */
export function profileSettingsOf(options: Record<string, unknown> | null | undefined): CatalogProfileSettings {
const raw = rawOf(options)
const out: CatalogProfileSettings = { ...PROFILE_DEFAULTS, enabled: raw?.enabled === true }
if (!raw) return out
for (const key of PROFILE_NUMBER_KEYS) {
const n = numberIn(raw[key], key)
if (n != null) out[key] = n
}
return out
}
// ---------------------------------------------------------------------------
// 表单:数值项是文字(输入时允许写一半),空着表示用缺省
// ---------------------------------------------------------------------------
export interface ProfileForm {
enabled: boolean
values: Record<CatalogProfileNumberKey, string>
}
export function profileFormOf(options: Record<string, unknown> | null | undefined): ProfileForm {
const raw = rawOf(options)
const values = Object.fromEntries(PROFILE_NUMBER_KEYS.map((k) => {
const v = raw?.[k]
return [k, v == null || typeof v === 'object' ? '' : String(v)]
})) as Record<CatalogProfileNumberKey, string>
return { enabled: raw?.enabled === true, values }
}
/** options 里已经有剖析设置这一项 */
export const hasProfileOption = (options: Record<string, unknown> | null | undefined): boolean => !!rawOf(options)
/**
* 表单 → 要写进 options 的设置。原来没有(stored 为假)、现在也没开、一项都没填的不写(不给 options 平白多一个键);
* 其余写开关和填了的项。填的不是数时原样交上去,由服务端拒收并说明是哪一项(表单上已经先拦过一次)
*/
export function profileOptionOf(form: ProfileForm, stored: boolean): Record<string, unknown> | undefined {
const filled = PROFILE_NUMBER_KEYS.filter((k) => form.values[k].trim())
if (!stored && !form.enabled && !filled.length) return undefined
const out: Record<string, unknown> = { enabled: form.enabled }
for (const k of filled) {
const text = form.values[k].trim().replace(/,/g, '')
const n = Number(text)
out[k] = Number.isFinite(n) ? n : form.values[k].trim()
}
return out
}
/** 表单的格式问题:键 → 说明。空对象表示可以保存 */
export function profileProblems(form: ProfileForm): Partial<Record<CatalogProfileNumberKey, string>> {
const out: Partial<Record<CatalogProfileNumberKey, string>> = {}
for (const k of PROFILE_NUMBER_KEYS) {
if (!form.values[k].trim()) continue
if (numberIn(form.values[k], k) == null) {
const { min, max, integer } = PROFILE_LIMITS[k]
out[k] = PROFILE_TEXT.rangeError(min, max, integer)
}
}
return out
}
/** 表单此刻对应的设置:没填、填错的项按缺省(风险说明据此写出「最多 N 条」) */
export function profileSettingsOfForm(form: ProfileForm): CatalogProfileSettings {
const out: CatalogProfileSettings = { ...PROFILE_DEFAULTS, enabled: form.enabled }
for (const k of PROFILE_NUMBER_KEYS) {
const n = numberIn(form.values[k], k)
if (n != null) out[k] = n
}
return out
}
export const sameProfileForm = (a: ProfileForm, b: ProfileForm): boolean =>
a.enabled === b.enabled && PROFILE_NUMBER_KEYS.every((k) => a.values[k] === b.values[k])
/**
* 服务端保存时拒收的 422(一句中文)说的是剖析设置的哪一项:
* 「数据剖析的「查询次数上限」需要填写 1 到 500 之间的整数;当前为「0」」→ max_queries。
* 说的是剖析设置、但认不出哪一项(格式不对、有认不出的项)返回 'section';说的不是剖析设置返回 null
*/
export function profileFieldOfRejection(message: string): 'enabled' | CatalogProfileNumberKey | 'section' | null {
if (!message.includes('数据剖析')) return null
const named = message.match(/「([^」]+)」/)?.[1] ?? ''
for (const k of ['enabled', ...PROFILE_NUMBER_KEYS] as const) {
if (named && named.startsWith(PROFILE_FIELD_LABEL[k])) return k
}
return 'section'
}
// ---------------------------------------------------------------------------
// 剖析:范围、报告里的读数、409 的分类
// ---------------------------------------------------------------------------
/** 不指定表时服务端剖析几张(PROFILE_DEFAULT_TABLES) */
export const PROFILE_DEFAULT_TABLES = 10
/** 一次最多指定几张表(接口 tables 的 max_length) */
export const PROFILE_MAX_TABLES = 200
/** 覆盖率写成百分数:最多一位小数,不到 100% 的不进成 100%(同服务端 _percent) */
export function coverageText(ratio: number): string {
const text = (ratio * 100).toFixed(1).replace(/\.0$/, '')
return text === '100' && ratio < 1 ? '99.9%' : `${text}%`
}
/** 已用时长的读数:「不到 1 秒」「8 秒」「1 分 05 秒」 */
export function secondsText(ms: number): string {
const s = Math.max(0, Math.floor(ms / 1000))
if (s < 1) return '不到 1 秒'
return s < 60 ? `${s} 秒` : `${Math.floor(s / 60)} 分 ${String(s % 60).padStart(2, '0')} 秒`
}
/** 剖析接口回 409 的几种情况(按服务端的原话认:catalog_profile.ensure_enabled、api/catalog.py、engine.SnapshotTampered) */
export type ProfileBlock = 'disabled' | 'inactive' | 'noSchema' | 'busy' | 'tampered' | 'other'
export function profileBlockOf(message: string): ProfileBlock {
if (message.includes('未开启数据剖析')) return 'disabled'
if (message.includes('已停用')) return 'inactive'
if (message.includes('正在进行数据剖析')) return 'busy'
if (message.includes('探查结构')) return 'noSchema'
if (/已拒绝(?:这次)?查询/.test(message)) return 'tampered'
return 'other'
}
/** 设置里有没有剖析设置这一项(options 的其余键原样带上) */
export function withProfileOption(options: Record<string, unknown> | null | undefined, value: Record<string, unknown> | undefined): Record<string, unknown> {
const { [PROFILE_OPTION]: _old, ...rest } = options ?? {}
return value ? { ...rest, [PROFILE_OPTION]: value } : rest
}