From f5c9f0c13c2905b06e27a6a1c0a5fc417f0d2fae Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Sat, 17 Oct 2026 08:51:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=96=E6=9E=90=E4=BB=BB=E5=8A=A1=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=B6=E5=8F=96=E5=80=BC=E8=A7=82=E5=AF=9F=E8=8C=83?= =?UTF-8?q?=E5=9B=B4=EF=BC=8C=E5=AF=B9=E8=AF=9D=E6=A1=86=E7=BB=99=E5=87=BA?= =?UTF-8?q?=E3=80=8C=E5=8F=96=E5=80=BC=E8=A7=82=E5=AF=9F=E3=80=8D=EF=BC=9B?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E7=BB=B4=E6=8A=A4=E6=8C=91=E4=B8=8D=E5=87=BA?= =?UTF-8?q?=E8=A6=81=E8=A7=82=E5=AF=9F=E7=9A=84=E8=A1=A8=E6=97=B6=E6=8A=8A?= =?UTF-8?q?=E5=8E=9F=E5=9B=A0=E8=AE=B0=E8=BF=9B=E8=A7=A6=E5=8F=91=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 验收:取值观察只能经定时维护触发——POST /catalog/profile-jobs 的 scope 只收 needed、all,可剖析任务本来就支持 observe(只观察、不剖析)。另外 12 小时内观察过的表不再观察时,任务以 0 张表结束,原因只在剖析任务的 notice 里,从定时维护触发时触发记录(ScheduleFire.inputs)看不出来。 后端: - ProfileJobIn.scope 加 observe(maintenance 是定时维护用的,接口照旧不收)。挑不出表时照旧 409 profile_nothing, 说法是 NOTHING_TO_OBSERVE(「最近 12 小时内都观察过了」)。 - catalog_profile_jobs.nothing_reason:按此刻的表结构和目录一张表都挑不出来时交回 ProfileNothing 的原话;排在 起草后面时说不准,返回 None。 - source_maintenance._start:建出 0 张表的剖析任务、这次触发又没有探查(开始时表结构不会变)、也没排在起草后面 (目录不会变)时,判断得出任务会以 0 张表结束,把原因记进 inputs.skipped(同一套 NOT_RUN 说法进 reason)。 任务照建、触发照旧是 started:每小时一次的维护不至于一天二十几次「没能发起」。同一次触发里有探查、或者排在 起草后面时判断不了,保持现状,写进了模块说明。 前端:剖析对话框的范围里加「取值观察:有数据目录的 N 张表」(说法照维护设置里「取值观察」那一步:码值分布、 最新业务日期、按日行数和行数,和同口径的历史比出取值变化;最近 12 小时内观察过的这次不再观察),建任务带 scope: observe。张数按清单上有数据目录、不是视图的表算(isObservable,profile.ts),观察到期没有由服务端判, 所以是上限。挑不出表时说「没有要观察的表」和服务端原话,不给「改选全部表」(那是剖析,不是观察)。文案进 PROFILE_TEXT。 测试:test_catalog_observe 新增一条(接口建 observe 任务、只做观察检查,再建一次 409 profile_nothing,maintenance 422);test_source_maintenance 新增一条(只选取值观察,12 小时内再触发:任务 0 张表,inputs.skipped 和 reason 写明 原因)。去掉修复回跑两条都挂。check-catalog-profile 跟上:范围多一项 observe:1,说法核对;新一节建取值观察任务 (请求体 {scope: observe})、都观察过时的「没有要观察的表」且没有「改选全部表」。tsc、check-copy 通过, check-catalog-profile 204 项全过。 --- backend/app/api/catalog.py | 5 +- backend/app/data/catalog_profile_jobs.py | 17 ++++++- backend/app/data/source_maintenance.py | 10 +++- backend/tests/test_catalog_observe.py | 21 ++++++++ backend/tests/test_source_maintenance.py | 26 ++++++++++ frontend/src/api/client.ts | 2 +- frontend/src/lib/terms.ts | 7 +++ frontend/src/pages/catalog/ProfileDialog.tsx | 39 +++++++++------ frontend/src/pages/catalog/profile.ts | 16 ++++-- frontend/src/pages/catalog/profileJob.ts | 2 +- scripts/check-catalog-profile.mjs | 51 +++++++++++++++++++- 11 files changed, 170 insertions(+), 26 deletions(-) diff --git a/backend/app/api/catalog.py b/backend/app/api/catalog.py index 0526ca13..8810486c 100644 --- a/backend/app/api/catalog.py +++ b/backend/app/api/catalog.py @@ -114,8 +114,9 @@ GUIDANCE_NOT_TEXT = "起草要求要是一段文字(要清除起草要求请 class ProfileJobIn(BaseModel): #: 要剖析的表(范围 selected,按给的顺序去重);最多 500 张(超了 422,说明是中文) tables: list[str] | None = None - #: 不指定表时的范围:needed 需要剖析的表(缺省)、all 全部表(都不含视图、没有可剖析的项的表) - scope: Literal["needed", "all"] | None = None + #: 不指定表时的范围:needed 需要剖析的表(缺省)、all 全部表(都不含视图、没有可剖析的项的表)、observe 取值观察(只观察 + #: 有数据目录、观察到期的表,不剖析;见 data/catalog_profile_observe)。maintenance 是定时维护用的,这里不收 + scope: Literal["needed", "all", "observe"] | None = None async def _resolved(session: AsyncSession, source_id: str) -> tuple[DataSource, Any]: diff --git a/backend/app/data/catalog_profile_jobs.py b/backend/app/data/catalog_profile_jobs.py index e24d2c19..37a4b097 100644 --- a/backend/app/data/catalog_profile_jobs.py +++ b/backend/app/data/catalog_profile_jobs.py @@ -692,6 +692,18 @@ async def _behind_draft(session: AsyncSession, source_id: str) -> bool: return catalog_jobs.sync_drafting(source_id) or await catalog_jobs.active_job(session, source_id) is not None +async def nothing_reason(session: AsyncSession, row: Any, source: Any, scope: str) -> str | None: + """按此刻的表结构和目录,这个范围一张表都挑不出来时为什么(ProfileNothing 的原话);挑得出、或者排在起草后面(开始时按 + 起草后的目录再挑,现在说不准)返回 None。定时维护建出 0 张表的任务时用它把原因记进触发记录。""" + if await _behind_draft(session, row.id): + return None + try: + await _pick(session, row, source, tables=None, scope=scope) + except ProfileNothing as e: + return str(e) + return None + + async def create_job(session: AsyncSession, row: Any, source: Any, *, tables: list[str] | None, scope: str, settings: ProfileSettings, actor: str | None, retry_of: str | None = None, trigger: str = "manual", fire_at: str | None = None, schedule_id: str | None = None, @@ -1161,5 +1173,6 @@ __all__ = ["INTERRUPTED_ERROR", "KEEP_JOBS", "MAX_TABLES", "NOTHING_AT_ALL", "NO "NOTHING_TO_OBSERVE", "NothingToRetry", "OBSERVING_SCOPES", "SCOPES", "task_modes", "trigger_of", "ProfileBusy", "ProfileFinished", "ProfileJobs", "ProfileNothing", "THEN_PROFILE", "active_job", "busy_message", "cancel", "create_job", "due_reason", "ensure_free", "job_brief", "job_dict", "job_out", - "last_detail", "last_facts", "last_profiles", "outside_of", "latest_job", "load", "load_task", "manager", "prune", "public_last", - "retry_tables", "startup", "task_detail", "task_row", "task_rows", "unsaved_error", "waiting_for"] + "last_detail", "last_facts", "last_profiles", "outside_of", "latest_job", "load", "load_task", "manager", + "nothing_reason", "prune", "public_last", "retry_tables", "startup", "task_detail", "task_row", "task_rows", + "unsaved_error", "waiting_for"] diff --git a/backend/app/data/source_maintenance.py b/backend/app/data/source_maintenance.py index a2bba31e..aa8597bf 100644 --- a/backend/app/data/source_maintenance.py +++ b/backend/app/data/source_maintenance.py @@ -13,7 +13,10 @@ observe 取值观察,可多选,没设过是三步都做。 **到点**(fire,持着 scheduler 的 launch_lock):按顺序建探查任务 → 剖析任务(范围 needed / observe / maintenance,按选的 步骤),任务上记 trigger=schedule 和 fire_at,靠 SourceLanes 串行:剖析排在同一次触发的探查后面(等探查进了同源队才建剖析 -任务),开始时按那时的表结构和目录挑表(挑出 0 张也照建,任务以 0 张表结束、提醒里写明为什么)。预算、止损照剖析设置。 +任务),开始时按那时的表结构和目录挑表(挑出 0 张也照建,任务以 0 张表结束、提醒里写明为什么)。建任务时就判断得出会是 0 张 +的(这次没有探查、也没排在起草后面,开始时表结构和目录都不会变——比如有数据目录的表都在 12 小时内观察过),原因另记进触发 +记录的 inputs.skipped 和 reason;同一次触发里有探查、或者排在起草后面时说不准(探查完、起草完可能就有了),保持现状,只在 +任务的提醒里写。预算、止损照剖析设置。 照实记没执行的:数据源不存在、已停用、是上传表格或记录表时整次不执行(failed_to_start);剖析没开时剖析、观察两步不执行、 探查照做;上一次的维护(或别的探查、剖析)还没做完时这次跳过(skipped_overlap)。每次触发记下建了哪几个任务、哪几步没执行、 为什么(触发记录的 inputs:{steps, jobs, skipped})。 @@ -221,6 +224,11 @@ async def _start(source_id: str, steps: list[str], *, actor: str, fire_at: str, table_versions.SnapshotMissing) as e: skipped += [{"step": s, "reason": str(e)} for s in wanted] return jobs, skipped + if not tasks and introspect_id is None and (why := await catalog_profile_jobs.nothing_reason( + session, row, view, scope)): + # 建任务时就挑不出表、这次又没有探查(开始时表结构不会变)、也没排在起草后面(目录不会变):任务会以 0 张表 + # 结束,原因照实记进触发记录,不只在剖析任务的提醒里。任务照建(触发照旧是 started) + skipped += [{"step": s, "reason": why} for s in wanted] catalog_profile_jobs.manager.start(job.id) jobs.append({"step": "+".join(wanted), "kind": "profile", "id": job.id, "scope": scope, "tables": len(tasks)}) return jobs, skipped diff --git a/backend/tests/test_catalog_observe.py b/backend/tests/test_catalog_observe.py index 76abfad0..689cfe74 100644 --- a/backend/tests/test_catalog_observe.py +++ b/backend/tests/test_catalog_observe.py @@ -271,6 +271,27 @@ def test_observation_today_is_the_server_local_date(monkeypatch): time.tzset() +async def test_the_profile_jobs_api_takes_the_observe_scope(client, shop): + """取值观察以前只能经定时维护触发:POST /catalog/profile-jobs 的 scope 只收 needed、all(下一轮验收)。现在也收 observe: + 只观察、不剖析;12 小时内都观察过时 409 profile_nothing,说清为什么。""" + sid, _ = shop + r = await client.post(f"/api/datasources/{sid}/catalog/profile-jobs", json={"scope": "observe"}) + assert r.status_code == 202, r.text + job = r.json()["job"] + assert job["scope"] == "observe" + await catalog_profile_jobs.manager.wait(job["id"], timeout=60) + async with SessionLocal() as session: + [task] = (await session.execute(select(CatalogProfileTask).where( + CatalogProfileTask.job_id == job["id"]))).scalars() + assert {c["kind"] for c in task.checks} == set(observe.OBSERVE_KINDS), "只观察:不做关系、码值、日期的剖析" + assert {o.kind for o in await _obs(sid)} == set(observe.OBSERVE_KINDS) + again = await client.post(f"/api/datasources/{sid}/catalog/profile-jobs", json={"scope": "observe"}) + assert again.status_code == 409 and again.json()["code"] == "profile_nothing" + assert f"{observe.OBSERVE_EVERY_H} 小时内都观察过" in again.json()["detail"] + bad = await client.post(f"/api/datasources/{sid}/catalog/profile-jobs", json={"scope": "maintenance"}) + assert bad.status_code == 422, "maintenance 是定时维护用的,接口不收" + + class _FakeCx: """只看拼出来的 SQL:大表、业务日期没有索引。""" diff --git a/backend/tests/test_source_maintenance.py b/backend/tests/test_source_maintenance.py index 468c580c..73f76413 100644 --- a/backend/tests/test_source_maintenance.py +++ b/backend/tests/test_source_maintenance.py @@ -111,6 +111,32 @@ async def test_fire_builds_introspect_then_profile_in_order(client, tmp_path, ma await drop_source(sid) +async def test_observing_again_within_the_window_records_why_in_the_fire(client, tmp_path, made): + """只选取值观察:12 小时内观察过的表不再观察,任务以 0 张表结束。建任务时就判断得出(这次没有探查、也没排在起草后面, + 开始时表结构、目录不会变):原因记进触发记录的 inputs.skipped 和 reason(下一轮验收:以前只在剖析任务的提醒里)。 + 任务照建(触发照旧是 started,不当成没能发起)。""" + sid = await _source(client, tmp_path / "c.db") + try: + async with SessionLocal() as session: + row = await session.get(DataSource, sid) + row.options = sm.with_steps(row.options, ["observe"], actor="测试员") + await session.commit() + schedule_id = await _schedule(sid) + made.append(schedule_id) + at = utcnow().replace(microsecond=0) + first = await service.fire_once(schedule_id, at, "manual") + assert first.status == "started" and not first.inputs.get("skipped"), first.inputs + await _wait_jobs(first) + second = await service.fire_once(schedule_id, at + timedelta(seconds=1), "manual") + await _wait_jobs(second) + assert second.status == "started" and [j["tables"] for j in second.inputs["jobs"]] == [0] + [skipped] = second.inputs["skipped"] + assert skipped["step"] == "observe" and "12 小时内都观察过" in skipped["reason"], skipped + assert "取值观察没有执行" in second.reason + finally: + await drop_source(sid) + + async def test_profile_off_records_steps_not_run_and_still_introspects(client, tmp_path, made): sid = await _source(client, tmp_path / "b.db", profile=False) try: diff --git a/frontend/src/api/client.ts b/frontend/src/api/client.ts index 63dc10ed..13c722d2 100644 --- a/frontend/src/api/client.ts +++ b/frontend/src/api/client.ts @@ -530,7 +530,7 @@ export const api = { * 409 带机读码:profile_disabled、datasource_inactive、schema_missing、snapshot_tampered、profile_busy(这个源已有排队、 * 进行中的剖析任务,detail 写明进行到第几张)、profile_nothing(按范围挑出 0 张表,detail 写明原因和建议) */ - create: (sourceId: string, body: { tables?: string[]; scope?: 'needed' | 'all' }) => + create: (sourceId: string, body: { tables?: string[]; scope?: 'needed' | 'all' | 'observe' }) => post<{ job: CatalogProfileJob }>(`${catalogBase(sourceId)}/profile-jobs`, body), /** 排队或进行中的那个剖析任务;没有为 null */ active: (sourceId: string, opts?: RequestOptions) => diff --git a/frontend/src/lib/terms.ts b/frontend/src/lib/terms.ts index a1143961..cd426a7a 100644 --- a/frontend/src/lib/terms.ts +++ b/frontend/src/lib/terms.ts @@ -3035,6 +3035,11 @@ export const PROFILE_TEXT = { scopeNeededNone: '没有需要剖析的表:都剖析过、上次都做完了,也没有新的待核实关联关系和新的码值、日期列', scopeAll: (n: number) => `全部 ${formatNumber(n)} 张表`, scopeAllHint: '有可剖析项的表,不含视图', + /** 取值观察(范围 observe):只观察、不剖析。张数是有数据目录的表,其中最近观察过的服务端这次不观察 */ + scopeObserve: (n: number) => `取值观察:有数据目录的 ${formatNumber(n)} 张表`, + scopeObserveHint: (hours: number) => '只观察、不剖析:码值分布、最新业务日期、按日行数和行数,和同口径的历史比出取值变化,' + + `列在「变化」里。最近 ${formatNumber(hours)} 小时内观察过的表这次不再观察`, + scopeObserveNone: '还没有有数据目录的表:取值观察只看有数据目录的表,请先起草数据目录', /** 已选、筛选结果里有剖析不了的表:不交给服务端,写明有几张 */ scopeLeftOut: (n: number) => `另有 ${formatNumber(n)} 张不剖析(视图、表结构中已没有,或没有可剖析的项)`, scopeEmpty: '这个范围里没有可剖析的表', @@ -3230,6 +3235,7 @@ export const PROFILE_TEXT = { busy: '这个数据源已有剖析任务', tampered: '数据文件核对未通过', nothing: '没有可剖析的表', + nothingObserve: '没有要观察的表', other: '无法开始剖析', } as Record, blockedNext: { @@ -3239,6 +3245,7 @@ export const PROFILE_TEXT = { busy: '同一个数据源同一时间只有一个剖析任务:可以查看它的进度,等它结束后再开始新的剖析。', tampered: '请重新上传表格,或联系管理员核对数据文件。', nothing: '可以改选「全部表」,或在表清单中选定要剖析的表。', + nothingObserve: '观察过的表要过一段时间才再观察;要剖析,可以回到上一步改选「需要剖析的表」或「全部表」。', other: '请稍后重试。', } as Record, busyAt: (who: string | null, done: number, total: number) => diff --git a/frontend/src/pages/catalog/ProfileDialog.tsx b/frontend/src/pages/catalog/ProfileDialog.tsx index 24d253dc..5eba1092 100644 --- a/frontend/src/pages/catalog/ProfileDialog.tsx +++ b/frontend/src/pages/catalog/ProfileDialog.tsx @@ -20,8 +20,8 @@ import { } from '../../lib/terms' import { STATUS_TONE, StatusChip } from './parts' import { - PROFILE_MAX_TABLES, PROFILE_RETRYABLE, UNFINISHED_REASONS, coverageText, fillKey, isProfilable, profileBlockOf, profileScopeTables, - secondsText, sizeText, spanText, + PROFILE_MAX_TABLES, PROFILE_OBSERVE_EVERY_H, PROFILE_RETRYABLE, UNFINISHED_REASONS, coverageText, fillKey, isProfilable, profileBlockOf, + profileScopeTables, secondsText, sizeText, spanText, } from './profile' import type { ProfileBlock } from './profile' import { isJobActive } from './draftJob' @@ -32,16 +32,18 @@ import type { ProfileStartBody } from './profileJob' // 数据目录页的「数据剖析」。剖析是服务端的后台任务(profileJob.ts):这里选范围、看预算、建任务;之后显示页面跟着的 // 那个任务(进行中的逐表进度、结束后的结果),任务本身的状态在页面上,关掉对话框照常进行。照起草对话框的结构: // -// 开始前:范围(已选 / 当前筛选 / 需要剖析的表(默认)/ 全部表,个数按清单上每行的 profile_due、profile_checks 算)、按表写的 -// 预算和这次最多约几条查询、醒目一句「只读查询、一条一条发、可随时停止」;同一个数据源有起草任务时按钮写「排队剖析」。 +// 开始前:范围(已选 / 当前筛选 / 需要剖析的表(默认)/ 全部表 / 取值观察,个数按清单上每行的 profile_due、profile_checks、 +// 有没有数据目录算;取值观察只观察不剖析,最近观察过的服务端这次不观察,张数是上限)、按表写的预算和这次最多约几条查询、 +// 醒目一句「只读查询、一条一条发、可随时停止」;同一个数据源有起草任务时按钮写「排队剖析」。 // 进行中:几张 / 共几张、已发几条查询、用时,按表数的真进度条,停止;逐表一行(状态、各类检查的进度、查询数、用时、正在做 // 哪一项),点一行展开这张表的检查清单、发现和发过的查询。 // 结束后:汇总(各状态几张、核实了什么)、止损和停止的原因照实写、按「有发现 / 有跳过 / 失败」筛、重试没做完的表;码值的 // 「填写含义」照旧(填完回到这里,接着下一列)。 -// 开始不了(409):每种情况一句说法和下一步(未开启就地打开剖析设置;正忙写清是谁的、到哪了,可以去看它的进度)。 +// 开始不了(409):每种情况一句说法和下一步(未开启就地打开剖析设置;正忙写清是谁的、到哪了,可以去看它的进度;取值观察 +// 挑不出表时说「没有要观察的表」,不给「改选全部表」)。 // =========================================================================== -type Scope = 'selected' | 'filtered' | 'needed' | 'all' +type Scope = 'selected' | 'filtered' | 'needed' | 'all' | 'observe' type Filter = 'all' | 'findings' | 'skipped' | 'failed' const FILTERS: readonly Filter[] = ['all', 'findings', 'skipped', 'failed'] @@ -114,6 +116,10 @@ export function ProfileDialog({ tables: needed, leftOut: 0, body: { scope: 'needed' } }) const all = profileScopeTables(rows, 'all') out.push({ key: 'all', label: PT.scopeAll(all.length), hint: PT.scopeAllHint, tables: all, leftOut: 0, body: { scope: 'all' } }) + const watched = profileScopeTables(rows, 'observe') + out.push({ key: 'observe', label: PT.scopeObserve(watched.length), + hint: watched.length ? PT.scopeObserveHint(PROFILE_OBSERVE_EVERY_H) : PT.scopeObserveNone, tables: watched, leftOut: 0, + body: { scope: 'observe' } }) return out }, [rows, visible, selected, filtered]) const [scope, setScope] = useState(() => { @@ -260,8 +266,9 @@ export function ProfileDialog({
{scopes.map((s) => { const over = !!s.body.tables && s.tables.length > PROFILE_MAX_TABLES - const note = over ? PT.scopeTooMany(PROFILE_MAX_TABLES) : !s.tables.length && s.key !== 'needed' ? PT.scopeEmpty - : s.leftOut ? PT.scopeLeftOut(s.leftOut) : s.hint + const note = over ? PT.scopeTooMany(PROFILE_MAX_TABLES) + : !s.tables.length && s.key !== 'needed' && s.key !== 'observe' ? PT.scopeEmpty + : s.leftOut ? PT.scopeLeftOut(s.leftOut) : s.hint return (