W4-d 后端:给目录前端的变化、观察、后台任务、定时维护接口补齐界面要的几样;打开 source.changed 的订阅

第 3 波的变化接口只给了 agentlab-change/1 的原样,界面要的几样(能不能做、依据什么、现在怎样)如果让前端自己判,就违背
「界面层不放逻辑」。这一提交由服务端给出,界面只照写:

- 变化列表、单条、处理的回答里每条另带 category(结构 / 取值)、basis_text(依据的口径,照观察时记下的写整表、窗口、块采样、
  按存储顺序的前 N 行)、actions(现在能做哪几个处理动作,做不了的写原因:剖析没开、表结构里没有了、影响面里没有目录项)、
  job(处理时建的任务,界面能点进进度)、影响面里每个目录项现在的状态(清理过、改过的照实)。召回拉的 list_changes 不带这些。
- 列表能按 category 筛(和 kind 一起给时取交集,一种都对不上就是空的,不是不筛),能按 status=handled 看处理过的(最近测到的
  在前:处理过的不再按要不要先处理排)。
- GET /api/datasources/upkeep:数据源列表一次取齐各源要处理的变化条数(卡片角标)和指向它的维护定时(频率、开没开、下一次)。
- GET /{id}/observations:表详情「结构与取值变化」一节的最近几次观察,各带口径;码值的占比、按日行数的合计由服务端算;现在
  被遮罩的列(观察时还没遮的)整组不给,遮罩在读的那一刻再核。
- GET /{id}/background-jobs:这个源进行中和排队的探查、起草、剖析,按同源队的先后排好(05 (g):排队顺序取服务端的,界面
  不自己排),各带 waiting_for。
- GET /{id}/maintenance:维护定时换成定时任务页同一个形状(改频率、开关照样带 version 走 PATCH),最近一次触发带 inputs
  (建了哪些任务、哪几步没执行和原因),另说调度开没开、这个源要不要维护(上传表格、记录表不要)。
- notify:source.changed 打开订阅(只推全局渠道,按源汇总不归哪个人),链接落到目录页的「变化」标签页。
  test_notify_events 里 W4-d 那一行加上;test_source_changes 里原先假设订阅没开的那句改成「没有渠道订阅时推不出去」。

data/source_changes.py(W4-a 的文件)只在末尾加了 available_actions、counts_by_source、list_handled 和 HANDLED;
data/catalog_profile_observe.py 末尾加了 key_text、recent。新测试 tests/test_source_upkeep.py。
This commit is contained in:
ThearchyHelios
2026-10-16 19:31:21 +08:00
parent 45fab21072
commit 238330b9c1
7 changed files with 718 additions and 25 deletions
+81
View File
@@ -524,3 +524,84 @@ __all__ = [
"record", "record_all",
"resolve", "summary", "triggers_recall", "without_paths",
]
# ==========================================================================
# 给界面的(第 4 波 W4-d:审阅页「变化」标签页、数据源列表的角标)
# ==========================================================================
#: 处理过的:解决了的、标为误报的(「变化」标签页的「处理过的」)
HANDLED = ("resolved", "dismissed")
#: 重新起草有用的结构变化:新表、列和键变了(起草按新的结构重写这张表的推断项;人工确认过的项照旧不动)
_REDRAFT_KINDS = frozenset({sd.TABLE_ADDED, sd.COLUMN_ADDED, sd.COLUMN_REMOVED, sd.COLUMN_TYPE, sd.COLUMN_NULLABLE,
sd.KEY, sd.FK, sd.COMMENT})
#: 这张表现在不在表结构里、或者说的是整个源的:起草、剖析都无从做起
_NO_TABLE_KINDS = frozenset({sd.TABLE_REMOVED, sd.TABLE_LEFT_SCOPE, sd.TABLE_REFLECT_FAILED, sd.SCOPE})
NOT_IN_SCHEMA = "表结构里已经没有这张表,先重新探查结构"
NO_CATALOG_ITEMS = "影响面里没有目录项,不需要清理"
def available_actions(row: Any, *, in_schema: bool, profile_enabled: bool, profile_reason: str | None = None,
) -> list[dict[str, Any]]:
"""这条变化现在能做哪几个处理动作(「变化」标签页照它画按钮,界面不自己判):[{action, ok, reason}],按 ACTIONS 的顺序。
已经处理过的(resolved / dismissed)一个都不给。只列对这种变化有意义的动作:加入码值只给「出现了新码值」,重新起草只给
新表和列、键的变化,重新剖析给说的是一张还在的表的变化(注释变了除外);清理目录项只在影响面里有目录项时能点。前置核对
和 handle 一致;建任务时才知道的(起草模型、同源正忙)不在这里判,点了照实 422。ok 为假时 reason 写为什么做不了。"""
if row.status in HANDLED:
return []
kind = row.kind
has_table = bool(row.table_name) and kind not in _NO_TABLE_KINDS
out: list[dict[str, Any]] = []
def put(action: str, reason: str | None = None) -> None:
out.append({"action": action, "ok": reason is None, "reason": reason})
if (row.affects or {}).get("catalog"):
put("clean_catalog", None if _catalog_paths(row) else NO_CATALOG_ITEMS)
if kind == CODES_NEW and row.column_name:
put("add_codes")
if has_table and kind in _REDRAFT_KINDS:
put("redraft", None if in_schema else NOT_IN_SCHEMA)
if has_table and kind != sd.COMMENT:
reason = None if in_schema else NOT_IN_SCHEMA
if reason is None and not profile_enabled:
reason = profile_reason or "这个数据源未开启数据剖析"
put("reprofile", reason)
if row.status == "open":
put("acknowledge")
put("dismiss")
return out
async def counts_by_source(session: AsyncSession, source_ids: Iterable[str] | None = None) -> dict[str, dict[str, Any]]:
"""各源还要人看的变化(同 counts 的形状),一条查询取齐(数据源列表的角标)。没有变化的源不在里面。"""
query = select(SourceChange.source_id, SourceChange.status, SourceChange.severity, func.count()).where(
SourceChange.status.in_(PENDING)).group_by(SourceChange.source_id, SourceChange.status, SourceChange.severity)
if source_ids is not None:
query = query.where(SourceChange.source_id.in_(list(source_ids)))
out: dict[str, dict[str, Any]] = {}
for source_id, status, severity, n in (await session.execute(query)).all():
one = out.setdefault(source_id, {"pending": 0, "open": 0, "by_severity": dict.fromkeys(SEVERITIES, 0)})
one["by_severity"][severity] = one["by_severity"].get(severity, 0) + n
one["pending"] += n
if status == "open":
one["open"] += n
return out
async def list_handled(session: AsyncSession, source_id: str, *, kinds: Iterable[str] | None = None,
table: str | None = None, limit: int = 100, offset: int = 0) -> tuple[list[SourceChange], int]:
"""处理过的变化(解决了的、标为误报的),最近测到的在前:处理过的不再按要不要先处理排。返回 (这一页, 一共几条)。"""
filt = [SourceChange.source_id == source_id, SourceChange.status.in_(HANDLED)]
if kinds:
filt.append(SourceChange.kind.in_(list(kinds)))
if table:
filt.append(SourceChange.table_name == table)
total = int((await session.execute(select(func.count()).select_from(SourceChange).where(*filt))).scalar() or 0)
rows = list((await session.execute(select(SourceChange).where(*filt).order_by(SourceChange.seq.desc())
.offset(offset).limit(limit))).scalars())
return rows, total
__all__ += ["HANDLED", "available_actions", "counts_by_source", "list_handled"]