From 544d44bdc219fff1f8bc70704c5625d3f227266b Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Tue, 6 Oct 2026 11:56:31 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=90=E8=A1=8C=E5=AF=B9=E6=AF=94=E7=9A=84?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=9F=BA=E5=87=86=E5=92=8C=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=9A=84=E8=87=AA=E5=8A=A8=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=85=B1=E7=94=A8=E4=B8=80=E4=B8=AA=E6=8C=91=E6=B3=95=EF=BC=9A?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E3=80=81=E5=8F=96=E6=B6=88=E7=9A=84=E4=B8=8D?= =?UTF-8?q?=E5=BD=93=E5=9F=BA=E5=87=86=EF=BC=8C=E6=8C=89=E5=8F=91=E8=B5=B7?= =?UTF-8?q?=E5=85=88=E5=90=8E=E5=8F=96=E4=B8=8A=E4=B8=80=E6=AC=A1=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E7=9A=84=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 运行详情「更多」里的「与上一次对比」原先走 default_base:任何终态都算、按结束时间排,上一次失败时拿失败的那次当基准; 触发历史里同名的入口用的是自动对比的 _previous(同一定时任务里成功结束并封存的最近一次、按发起先后)。两处拿到的基准 不同,计数也对不上。 - api/compare.py 新增 pick_base:比这次先发起的运行里成功结束并封存的最近一次,同时给出是不是紧挨着的上一次。定时任务 发起的先找同一定时任务,没有再退回同一工作流、同一运行类别(同样只挑成功的、按发起先后);schedule_only 时不退 - default_base 与自动对比(scheduler/auto_compare.py,原 _previous 删掉)都调它;找不到基准的说明改成「上一次成功的运行」 - 测试:上一次失败、取消时默认基准跳过它们,和自动对比挑的是同一次;按发起先后而非结束时间(含停在审批上的这次); 之前没有成功的运行时回 404 --- backend/app/api/compare.py | 82 ++++++++++++++++--------- backend/app/scheduler/auto_compare.py | 33 +++------- backend/tests/test_compare_api.py | 62 ++++++++++++++++--- backend/tests/test_scheduler_compare.py | 25 +++++++- 4 files changed, 137 insertions(+), 65 deletions(-) diff --git a/backend/app/api/compare.py b/backend/app/api/compare.py index 6d5a7b2b..a1f36123 100644 --- a/backend/app/api/compare.py +++ b/backend/app/api/compare.py @@ -1,9 +1,10 @@ """运行对比:同一个工作流的两次运行按「变化三分法」对照——数据变了、口径变了、还是模板 / SQL 变了(engine/compare.py)。 -GET /api/runs/{run_id}/compare/base:默认拿哪一次来比,{base_run_id, reason, base}。这次运行是定时任务发起的,取同一 -定时任务、结束得比它早的最近一次终态运行(reason: same_schedule);不是、或者同一任务还没有更早的,取同一工作流、 -同一运行类别、结束得比它早的最近一次终态运行(reason: same_workflow)。只挑封存过的:没封存的拿来也比不了。 -找不到 404。 +GET /api/runs/{run_id}/compare/base:默认拿哪一次来比,{base_run_id, reason, base}。规则在 pick_base,定时任务结束后 +的自动对比(scheduler/auto_compare.py)用的是同一个函数——触发历史里的「与上一次对比」和运行详情里的同名入口必须拿到 +同一个基准,计数才对得上。这次运行是定时任务发起的,取同一定时任务、同一工作流、比它先发起的运行里成功结束并封存的 +最近一次(reason: same_schedule);不是、或者同一任务之前没有成功的,取同一工作流、同一运行类别里同样条件的最近一次 +(reason: same_workflow)。找不到 404。 GET /api/runs/{run_id}/compare?base={base_id}:{target, base, summary, template, metrics, queries, notes},每个字段 的形状见 engine/compare.py。两次运行都必须已结束、封存且封存核对一致,封存判定照搬证据接口(api/evidence._sealed): @@ -15,16 +16,16 @@ GET /api/runs/{run_id}/compare?base={base_id}:{target, base, summary, template 结果里。读工件、比 SQL、重算都在线程里做:快照大的时候不挡住同一进程里别的请求。 路径挂在 /api/runs/{run_id}/… 下面;这个路由登记在 runs.router 之前,以后加 /api/runs/compare 这类固定路径也不会 -被 /{run_id} 吃掉。定时任务结束后自动对比(期 4)直接调 default_base、compare_runs。 +被 /{run_id} 吃掉。定时任务结束后自动对比(期 4)直接调 pick_base、compare_runs。 """ from __future__ import annotations import asyncio from collections.abc import Callable -from typing import Any +from typing import Any, NamedTuple from fastapi import APIRouter -from sqlalchemy import select +from sqlalchemy import func, select from app.api.coded import CodedHTTPException from app.api.evidence import _TERMINAL, RUN_NOT_FOUND, _Masks, _Sealed, _sealed, _sealed_cards @@ -51,8 +52,8 @@ UNSEALED_TEXT = "{who}尚未结束并封存:对比只用封存的运行记录 #: 已经结束、只是没有封存清单(封存上线前结束的、结束时没封上的):再等也等不来封存,不能说「运行结束后才能对比」 NO_MANIFEST_TEXT = "{who}没有封存清单,不能用于对比" SEAL_BROKEN_TEXT = "{who}的封存核对不一致:封存后有事件被修改、删除或插入,不能用于对比。可在记录页点「核对」查看核对结果" -NO_BASE_TEXT = ("没有找到可以对比的上一次运行:需要同一工作流、同一运行类别(定时任务发起的优先找同一定时任务)、" - "更早结束并已封存的运行") +NO_BASE_TEXT = ("没有找到可以对比的上一次成功的运行:需要同一工作流、同一运行类别(定时任务发起的优先找同一定时任务)、" + "比这次先发起、成功结束并已封存的运行") TARGET = "这次运行" BASE = "作为基准的运行" @@ -63,8 +64,7 @@ async def compare_base(run_id: str) -> dict[str, Any]: found = await default_base(run_id) if found is None: raise CodedHTTPException(404, NO_BASE_TEXT, NO_BASE) - run, reason = found - return {"base_run_id": run.id, "reason": reason, "base": run_ref(run)} + return {"base_run_id": found.run.id, "reason": found.reason, "base": run_ref(found.run)} @router.get("/{run_id}/compare") @@ -75,29 +75,53 @@ async def compare_with(run_id: str, base: str | None = None) -> dict[str, Any]: return await compare_runs(run_id, base.strip()) -async def default_base(run_id: str) -> tuple[Run, str] | None: - """(默认的基准运行, 怎么选的);找不到返回 None,这次运行本身不存在 404。 +class Pick(NamedTuple): + """挑出来的基准:哪一次、怎么选的(same_schedule | same_workflow)、是不是紧挨着的上一次(同一范围里两次之间没有 + 隔着别的运行——失败、取消、还没结束的都算隔着)。""" + run: Run + reason: str + previous: bool - 「早」按结束时间比:这次运行还没结束(还在跑、停在审批)的按开始时间。只挑终态、封存过的运行——没封存的拿来 - 对比只会得到 409。定时任务发起的先找同一定时任务(同一个工作流)的;它是这个任务的第一次时,退回同一工作流、 - 同一运行类别。没挂在已保存的工作流上的运行(画布上没保存就运行的图)没有可比的。""" + +async def default_base(run_id: str) -> Pick | None: + """这次运行默认拿哪一次来比(pick_base);找不到返回 None,这次运行本身不存在 404。""" async with SessionLocal() as session: run = await session.get(Run, run_id) if run is None: raise CodedHTTPException(404, "运行记录不存在", RUN_NOT_FOUND) - if not run.workflow_id: - return None - before = run.finished_at or run.started_at or run.created_at - query = (select(Run) - .where(Run.id != run.id, Run.workflow_id == run.workflow_id, Run.status.in_(_TERMINAL), - Run.manifest_hash.is_not(None), Run.finished_at.is_not(None), Run.finished_at < before) - .order_by(Run.finished_at.desc(), Run.created_at.desc()).limit(1)) - if run.schedule_id: - found = (await session.execute(query.where(Run.schedule_id == run.schedule_id))).scalars().first() - if found is not None: - return found, "same_schedule" - found = (await session.execute(query.where(Run.run_class == run.run_class))).scalars().first() - return (found, "same_workflow") if found is not None else None + return await pick_base(session, run) + + +async def pick_base(session: Any, target: Run, *, schedule_only: bool = False) -> Pick | None: + """target 的基准:比它先发起的运行里成功结束并封存的最近一次。运行详情「与上一次对比」(default_base)和定时任务 + 结束后的自动对比(scheduler/auto_compare.py,schedule_only=True)都调这一个函数:两个同名入口拿到的必须是同一个 + 基准,不然计数对不上。 + + - 定时任务发起的先在同一定时任务里找(same_schedule);它是这个任务的第一次、或者之前的都没成功时,退回同一工作流、 + 同一运行类别(same_workflow)。schedule_only 时不退:自动对比要比的是「同一定时任务的上一次」,别的运行不算 + - 只挑成功结束并封存的:失败、取消的运行口径卡常常没执行,拿它当基准每个指标都成了「新增」「移除」;没封存的 + 拿来对比只会得到 409 + - 按发起先后挑(created_at),不按结束时间:老的失败运行事后被接着运行完,结束得比后一次晚,它的上一次仍是它前面 + 那次;这次还没结束(还在运行、停在审批)的也一样按发起先后 + - 都只看同一个工作流:没挂在已保存的工作流上的运行(画布上没保存就运行的图)没有可比的,定时任务改过工作流的, + 之前那个工作流的运行也不算""" + if not target.workflow_id: + return None + scopes = [("same_schedule", Run.schedule_id == target.schedule_id)] if target.schedule_id else [] + if not schedule_only: + scopes.append(("same_workflow", Run.run_class == target.run_class)) + for reason, scope in scopes: + earlier = (scope, Run.workflow_id == target.workflow_id, Run.id != target.id, + Run.created_at < target.created_at) + base = (await session.execute( + select(Run).where(*earlier, Run.status == "succeeded", Run.manifest_hash.is_not(None)) + .order_by(Run.created_at.desc()).limit(1))).scalars().first() + if base is None: + continue + between = await session.scalar(select(func.count()).select_from(Run).where( + *earlier, Run.created_at > base.created_at)) + return Pick(base, reason, not between) + return None async def compare_runs(run_id: str, base_id: str) -> dict[str, Any]: diff --git a/backend/app/scheduler/auto_compare.py b/backend/app/scheduler/auto_compare.py index 83a209b4..575b0cfd 100644 --- a/backend/app/scheduler/auto_compare.py +++ b/backend/app/scheduler/auto_compare.py @@ -6,10 +6,11 @@ - 比哪些:成功结束、封存完的运行。失败、取消的运行指标不全(口径卡常常没执行),和上一次比只会得出「移除 N 个」, 收到通知的人会以为报告删了指标 -- 跟谁比:同一定时任务、同一工作流、比这次先发起的运行里成功结束并封存的最近一次(_previous)。失败的那次不当基准: - 拿它当基准,每个指标都成了「新增」。按发起先后挑,不按结束时间:老的失败运行事后被接着跑完,结束得比后一次晚, - 它的上一次仍是它前面那次。紧挨着的上一次不是成功的(失败、取消、还没结束)时 previous 为 False,通知和触发历史 - 写明比的是哪一次 +- 跟谁比:同一定时任务、同一工作流、比这次先发起的运行里成功结束并封存的最近一次。规则在对比接口的 pick_base, + 运行详情「与上一次对比」的默认基准用的是同一个函数:触发历史和运行详情里两个同名的入口必须比同一对运行。失败的那次 + 不当基准:拿它当基准,每个指标都成了「新增」。按发起先后挑,不按结束时间:老的失败运行事后被接着跑完,结束得比 + 后一次晚,它的上一次仍是它前面那次。紧挨着的上一次不是成功的(失败、取消、还没结束)时 previous 为 False,通知和 + 触发历史写明比的是哪一次。这个任务的第一次不比(不像运行详情那样退回同一工作流的别的运行:那不是「上一次」) - 和封存绑定:运行的封存要盖住它最后一条结束事件才算封存完(sealed_now)。runner 先写终态、再另起会话封存,失败后 接着跑完的运行在两步之间还带着失败时的清单哈希,拿它去比只比得到旧封存范围里的事件。结果里记下两边的清单哈希: 哪一边后来变了,触发历史就不再给这份对比(api/schedules._fire_outs);对比途中变了,结果不记 @@ -125,7 +126,8 @@ async def compare_fire(fire_id: str, run_id: str) -> dict[str, Any] | None: async def _compare(fire_id: str, run_id: str) -> dict[str, Any] | None: - # 现成的对比函数在用到时才导入:api/compare.py 连带证据、推演接口,调度模块被导入时不必一起加载 + # 挑基准、对比都用对比接口现成的函数,在用到时才导入:api/compare.py 连带证据、推演接口,调度模块被导入时不必 + # 一起加载 from app.api import compare as compare_api # 挑基准、核对封存、看有没有比过都只是读库,不占对比的空位:前面排着慢的对比时,第一次运行这类没什么可比的 @@ -136,10 +138,10 @@ async def _compare(fire_id: str, run_id: str) -> dict[str, Any] | None: if target is None or target.status != "succeeded" or not target.schedule_id or not target.workflow_id \ or not await sealed_now(session, target): return None - found = await _previous(session, target) + found = await compare_api.pick_base(session, target, schedule_only=True) if found is None: return None - base, previous = found + base, previous = found.run, found.previous if not await sealed_now(session, base): # 基准正好处在写完终态、还没封存的那一刻,或者封存没补上就停了机:拿它比只比得到旧封存里的事件 logger.warning("作为基准的运行 %s 还没有封存到它的结束事件,运行 %s 这次不比", base.id, run_id) @@ -167,23 +169,6 @@ async def _compare(fire_id: str, run_id: str) -> dict[str, Any] | None: return _copy(brief) -async def _previous(session: Any, target: Run) -> tuple[Run, bool] | None: - """(基准, 是不是紧挨着的上一次):同一定时任务、同一工作流、比这次先发起的运行里成功结束并封存的最近一次。 - - 按发起先后挑(created_at),不按结束时间;同一工作流才比得了(任务改过工作流的,之前那个工作流的运行不算)。 - 中间隔着别的运行(失败、取消、还没结束的)时第二项为 False。""" - earlier = (Run.schedule_id == target.schedule_id, Run.workflow_id == target.workflow_id, Run.id != target.id, - Run.created_at < target.created_at) - base = (await session.execute( - select(Run).where(*earlier, Run.status == "succeeded", Run.manifest_hash.is_not(None)) - .order_by(Run.created_at.desc()).limit(1))).scalars().first() - if base is None: - return None - between = await session.scalar(select(func.count()).select_from(Run).where( - *earlier, Run.created_at > base.created_at)) - return base, not between - - def _slots() -> asyncio.Semaphore: loop = asyncio.get_running_loop() slots = _SLOTS.get(loop) diff --git a/backend/tests/test_compare_api.py b/backend/tests/test_compare_api.py index eda3efdf..2257f070 100644 --- a/backend/tests/test_compare_api.py +++ b/backend/tests/test_compare_api.py @@ -6,7 +6,8 @@ 同一次运行回 422;运行不存在 404 - 对比只读:两次运行的 last_seq、事件、工件引用、工件文件一样不多 - 遮罩列(查询当时记下的、数据源现在设的)的值不出现在结果里 -- 默认基准:同一定时任务优先,否则同一工作流、同一运行类别;只挑更早结束、封存过的终态运行 +- 默认基准:同一定时任务优先,否则同一工作流、同一运行类别;只挑比这次先发起、成功结束并封存的运行,按发起先后取 + 最近一次(失败、取消的跳过,和定时任务的自动对比挑的是同一次,见 test_scheduler_compare.py) - 真跑:同一条 SQL 数据变了是 data_only;只是代进去的日期不同是 params_only,图一样 """ from __future__ import annotations @@ -236,14 +237,15 @@ async def test_masked_columns_never_leave_the_server(client): async def bare(workflow_id, finished, *, status="succeeded", run_class="formal", schedule_id=None, sealed=True, - resumed=False) -> str: + resumed=False, created=None) -> str: """只为挑基准用的运行记录:挑基准只看运行记录上的几列。resumed:结束、封存过又被接着运行(状态已回到运行中, - 结束时间和封存还没清掉),只能靠状态把它挡在外面。""" + 结束时间和封存还没清掉),只能靠状态把它挡在外面。created:发起时间,默认在结束前 3 分钟(发起先后和结束先后 + 一致);要让两者错开的用例另给。""" done = status in ("succeeded", "failed", "cancelled") or resumed async with SessionLocal() as session: run = Run(workflow_id=workflow_id, workflow_name="区域周报", status=status, run_class=run_class, - schedule_id=schedule_id, started_at=finished - timedelta(minutes=2), - finished_at=finished if done else None, + schedule_id=schedule_id, created_at=created or finished - timedelta(minutes=3), + started_at=finished - timedelta(minutes=2), finished_at=finished if done else None, manifest_hash="ab" * 32 if sealed else None, manifest_seq=1 if sealed else None) session.add(run) await session.commit() @@ -279,20 +281,60 @@ async def test_the_default_base_skips_what_cannot_be_compared(client): await bare(workflow, T0 + 2 * day, status="running", resumed=True) # 结束过、又被接着运行 await bare(workflow, T0 + 3 * day, run_class="exploratory") # 别的运行类别 await bare(await new_workflow("另一个周报"), T0 + 4 * day) # 别的工作流 + await bare(workflow, T0 + 4 * day, status="failed") # 失败的(封存过) + await bare(workflow, T0 + 4 * day + timedelta(hours=1), status="cancelled") # 取消的(封存过) current = await bare(workflow, T0 + 5 * day) - await bare(workflow, T0 + 6 * day) # 比它晚结束 + await bare(workflow, T0 + 6 * day) # 比它晚发起 body = await get(client, f"/api/runs/{current}/compare/base") assert (body["base_run_id"], body["reason"]) == (good, "same_workflow") - # 失败、取消的终态运行也算(封存过) - failed = await bare(workflow, T0 + 5 * day - timedelta(hours=1), status="failed") - assert (await get(client, f"/api/runs/{current}/compare/base"))["base_run_id"] == failed # 第一次运行、没保存的工作流:没有可比的 body = await get(client, f"/api/runs/{good}/compare/base", 404) - assert body["code"] == "compare_no_base" and "上一次运行" in body["detail"] + assert body["code"] == "compare_no_base" and "上一次成功的运行" in body["detail"] await get(client, f"/api/runs/{await bare(None, T0)}/compare/base", 404) await get(client, "/api/runs/no-such-run/compare/base", 404) +async def test_the_default_base_is_the_last_successful_run(client): + """失败、取消的不当默认基准:拿它当基准,口径卡没执行的指标都成了「新增」「移除」。同一定时任务里跳过它们,取再往前 + 成功的那一次(和定时任务的自动对比同一个规则);同一工作流的退路也一样;之前一次成功的都没有就是没有基准""" + workflow = await new_workflow() + day = timedelta(days=1) + first = await bare(workflow, T0, schedule_id="sched-weekly") + await bare(workflow, T0 + day, schedule_id="sched-weekly", status="failed") + await bare(workflow, T0 + 2 * day, schedule_id="sched-weekly", status="cancelled") + current = await bare(workflow, T0 + 3 * day, schedule_id="sched-weekly") + body = await get(client, f"/api/runs/{current}/compare/base") + assert (body["base_run_id"], body["reason"]) == (first, "same_schedule") + # 手动发起的:同一工作流、同一运行类别里成功的最近一次(定时的也算),中间失败的跳过 + await bare(workflow, T0 + 4 * day, status="failed") + manual = await bare(workflow, T0 + 5 * day) + body = await get(client, f"/api/runs/{manual}/compare/base") + assert (body["base_run_id"], body["reason"]) == (current, "same_workflow") + # 之前只有失败、取消的:没有基准,原因写明要成功结束的 + lonely = await new_workflow("门店日报") + await bare(lonely, T0, status="failed") + await bare(lonely, T0 + day, status="cancelled", schedule_id="sched-daily") + alone = await bare(lonely, T0 + 2 * day, schedule_id="sched-daily") + body = await get(client, f"/api/runs/{alone}/compare/base", 404) + assert body["code"] == "compare_no_base" and "成功结束" in body["detail"] + + +async def test_the_default_base_follows_launch_order(client): + """按发起先后挑,不按结束时间:先发起、后来才结束的那次(失败后被接着运行完)仍排在这次前面;比这次晚发起的, + 结束得再早也不算。这次还没结束(停在审批上)的也按发起先后""" + workflow = await new_workflow() + t = datetime(2026, 9, 1, 1, 0, tzinfo=timezone.utc) + hour, day = timedelta(hours=1), timedelta(days=1) + await bare(workflow, T0, created=t) + resumed = await bare(workflow, T0 + 10 * day, created=t + hour) # 比这次先发起,结束得最晚 + current = await bare(workflow, T0 + 5 * day, created=t + 2 * hour) + later = await bare(workflow, T0 + 4 * day, created=t + 3 * hour) # 比这次晚发起,结束得比它早 + body = await get(client, f"/api/runs/{current}/compare/base") + assert (body["base_run_id"], body["reason"]) == (resumed, "same_workflow") + waiting = await bare(workflow, T0 + 20 * day, status="interrupted", created=t + 4 * hour) + assert (await get(client, f"/api/runs/{waiting}/compare/base"))["base_run_id"] == later + + # -------------------------------------------------------------------------- # 真跑:虚构的 SQLite 库,两次运行之间改了数、换了日期参数 # -------------------------------------------------------------------------- diff --git a/backend/tests/test_scheduler_compare.py b/backend/tests/test_scheduler_compare.py index 3c9af745..5ffc5f98 100644 --- a/backend/tests/test_scheduler_compare.py +++ b/backend/tests/test_scheduler_compare.py @@ -4,7 +4,7 @@ - 基准是同一定时任务、同一工作流、比这次先发起的运行里成功结束并封存的最近一次:同一工作流更近的手动运行、别的 定时任务的运行都不算;失败的那一次不当基准(拿它当基准每个指标都成了「新增」);按发起先后挑,不按结束时间—— 老的失败运行事后被接着跑完,结束得比后一次晚,它的上一次仍是它前面那次。紧挨着的上一次不是成功的时,通知和触发 - 历史写明比的是哪一次 + 历史写明比的是哪一次。运行详情「与上一次对比」的默认基准(api/compare.py 的 pick_base)和这里挑的是同一次 - 整份结果连同两边的清单哈希存成工件(kind=run_compare,不挂在运行名下、不进运行的工件列表),工件 id 记进触发 记录的 compare_artifact。这个任务的第一次(只有同一工作流别的运行可比)、还没封存时不比;对比出错(基准的封存核对 不一致、意外的异常)只记日志,通知照常推送、不带对比 @@ -320,9 +320,12 @@ async def test_after_a_failed_run_the_base_is_the_last_successful_one(recorder, assert (body["compare"]["base_run_id"], body["compare"]["previous"]) == (first, False) items = {f["id"]: f for f in (await client.get(f"/api/schedules/{sid}/fires")).json()["items"]} assert items[fire_id]["compare"] == {"base_run_id": first, "summary": SUMMARY, "previous": False} + # 运行详情「与上一次对比」的默认基准挑的是同一次(同一个函数):两个同名入口比的是同一对运行、计数一致 + base = (await client.get(f"/api/runs/{target_id}/compare/base")).json() + assert (base["base_run_id"], base["reason"]) == (first, "same_schedule") -async def test_base_follows_launch_order_not_finish_time(recorder): +async def test_base_follows_launch_order_not_finish_time(recorder, client): """第 2 周失败、第 3 周照常成功之后,有人把第 2 周接着跑完:它结束得比第 3 周晚,但它的上一次仍是第 1 周。以前 按结束时间挑,拿后一次触发的第 3 周当基准,「与上一次相比」的变化方向是反的。第 3 周已经比过的那一份不受影响""" workflow = await new_workflow() @@ -342,6 +345,24 @@ async def test_base_follows_launch_order_not_finish_time(recorder): stored = load((await fire_row(second_fire)).compare_artifact) assert stored["result"]["base"]["id"] == first and stored["previous"] is True assert (await fire_row(third_fire)).compare_artifact == third_artifact + # 运行详情里的默认基准同样按发起先后:接着跑完的第 2 周,上一次仍是第 1 周 + base = (await client.get(f"/api/runs/{second}/compare/base")).json() + assert (base["base_run_id"], base["reason"]) == (first, "same_schedule") + + +async def test_without_an_earlier_success_there_is_no_base(recorder, client): + """同一定时任务之前只有失败的那次:不自动对比;运行详情里的默认基准也不拿它(同一工作流、同一类别里也没有成功的), + 回 404 说清找不到,不给一个比出来全是「新增」的基准""" + workflow = await new_workflow() + sid = await new_schedule(workflow, url=None) + failed = await persist(failed_week("r1"), workflow, finished=T0, schedule_id=sid, status="failed") + await fire_for(sid, failed, T0 - timedelta(minutes=2), notified="failed") + target_id = await persist(week("r2", NEXT_ROWS), workflow, finished=T0 + WEEK, schedule_id=sid) + fire_id = await fire_for(sid, target_id, T0 + WEEK - timedelta(minutes=2)) + await webhook.notify_due() + assert (await fire_row(fire_id)).compare_artifact is None + resp = await client.get(f"/api/runs/{target_id}/compare/base") + assert resp.status_code == 404 and resp.json()["code"] == "compare_no_base", resp.text async def test_first_run_of_a_schedule_has_no_compare(recorder, client):