Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

fix(webui): display server duration for fast API tasks

myhlolia6be4df4bbbf
diff --git a/docs/next/api/parse-jobs.md b/docs/next/api/parse-jobs.md
index b7150f31..152d80ba 100644
--- a/docs/next/api/parse-jobs.md
+++ b/docs/next/api/parse-jobs.md
@@ -254,7 +254,7 @@ OCR 策略和图片分析能力由 `tier` 与服务端实际引擎自动决定
| `page_range` | string | 服务端规范化后的实际解析页码范围。请求未指定时为 `1-{total}`;请求指定时,服务端先按文件总页数展开倒数页码 `rN`,再去重并合并连续页码区间。例如 `1,2,3,r1,r1` 在 10 页文件中规范化为 `1-3,10`。 |
| `status` | string | 文件级状态。 |
| `parse.model_used` | string | 实际模型 ID。仅当该文件 `status="completed"` 时出现。 |
-| `parse.duration_ms` | integer | 单文件从开始解析到结束的耗时,不包含排队时间。仅当该文件 `status="completed"` 时出现。 |
+| `parse.duration_ms` | integer | 单文件从开始处理到输出生成完毕的毫秒耗时,包含解析和结果导出/打包,不包含排队、客户端下载或 WebUI 渲染时间。仅当该文件 `status="completed"` 时出现。 |
| `parse.parser_version` | string | 解析器版本。仅当该文件 `status="completed"` 时出现。 |
| `output_files` | object | 各输出格式对应的 File 引用。仅当该文件 `status="completed"` 时出现。 |
| `error` | object | 文件失败时出现,包含 `code` 和 `message`。仅当该文件 `status="failed"` 时出现。 |
diff --git a/docs/next/cli/mineru-kit-webui.md b/docs/next/cli/mineru-kit-webui.md
index 2653082c..e449150e 100644
--- a/docs/next/cli/mineru-kit-webui.md
+++ b/docs/next/cli/mineru-kit-webui.md
@@ -105,9 +105,9 @@ Gradio 首先发现 `/v1/health` 和 `/v1/tiers`,然后通过 `MinerUApiParser
左下角使用与 3.4.5 一致的两列步骤卡片,展示准备请求、检查服务、提交任务、排队、解析中、下载结果、整理输出和完成。排队状态来自本地任务槽或 V1 的 `queued` 响应,圆点每秒循环更新;不显示前方任务数。每个界面服务仍一次执行一个转换,等待中的会话也会立即显示排队动画。
-收到 V1 的 `running` 后开始解析计时,每 0.1 秒刷新一次;收到任务终态时冻结,下载和整理输出不计入该耗时。最终完成文案保留观察到的解析耗时;直接完成且没有观察到 `running` 的快速任务不显示耗时。界面动画不增加 API 请求,任务轮询仍为每秒一次。清除或更换文件会停止当前会话的状态流并释放本地等待位置,不向 V1 发送远端取消请求。
+收到 V1 的 `running` 后开始解析计时,由浏览器本地刷新;收到任务终态时冻结。API 路径的最终完成文案优先使用响应中已有的 `files[0].parse.duration_ms`,包含服务端解析和结果导出/打包,不包含排队、客户端下载和 WebUI 整理输出,因此即使轮询跳过 `running` 也能显示亚秒耗时。HF 等直接调用解析函数的适配层继续使用实际阶段通知之间的计时;缺少 API 耗时字段时也保留这种计时回退。完成数值按两位小数四舍五入,最低显示 `0.01 秒`。两种路径均不承诺该值等同于日志中的纯 `infer` 耗时。界面动画不增加 API 请求,任务轮询仍为每秒一次。清除或更换文件会停止当前会话的状态流并释放本地等待位置,不向 V1 发送远端取消请求。
-`MinerUApiParser.parse()` 和 `parse_async()` 提供可选的 `status_callback` 参数,接收公开的 `ApiJobStatus`(`queued`、`running`、`completed`、`partial`、`failed`、`canceled`)。回调在任务提交响应及每次轮询响应后同步调用;回调异常只记录日志。这里的 `completed` 表示服务端任务完成,解析方法仍需下载并构造 `ParseResult` 后才返回。
+`MinerUApiParser.parse()` 和 `parse_async()` 提供可选的 `status_callback` 参数,接收公开的 `ApiJobStatus`(`queued`、`running`、`completed`、`partial`、`failed`、`canceled`)。回调在任务提交响应及每次轮询响应后同步调用;回调异常只记录日志。这里的 `completed` 表示服务端任务完成,解析方法仍需下载并构造 `ParseResult` 后才返回。可选的 `duration_callback` 在最终成功响应之后、开始下载之前接收既有文件耗时(浮点毫秒);缺失或非法耗时不触发回调,回调异常不影响解析结果。该值属于本次调用,不存放在共享 parser 实例或 `ParseResult` 中。
## 结果与下载
diff --git a/mineru/kit/gradio/app.py b/mineru/kit/gradio/app.py
index dea6d899..f205d57c 100644
--- a/mineru/kit/gradio/app.py
+++ b/mineru/kit/gradio/app.py
@@ -49,6 +49,7 @@ from .status import (
STATUS_PREPARING_REQUEST,
STATUS_PROCESSING_OUTPUT,
STATUS_QUEUED_LOCALLY,
+ ParseStatusUpdate,
status_html as _status_html,
)
@@ -841,7 +842,7 @@ def build_gradio_app(
selected_tier = "flash"
loop = asyncio.get_running_loop()
- def emit(message: str) -> None:
+ def emit(message: str | ParseStatusUpdate) -> None:
"""在通知发生时记录时间,再由事件循环串行更新会话状态。"""
at = time.monotonic()
loop.call_soon_threadsafe(lambda: run.publish(message, at=at))
@@ -1043,7 +1044,7 @@ def build_gradio_app(
return json.dumps(
{
"run_id": run_id,
- "sequence": run.state.phase_id,
+ "sequence": run.state.sequence,
"ready_at": time.time(),
"outputs": values[:6] + values[7:],
},
diff --git a/mineru/kit/gradio/client.py b/mineru/kit/gradio/client.py
index 29274c48..1195a227 100644
--- a/mineru/kit/gradio/client.py
+++ b/mineru/kit/gradio/client.py
@@ -31,6 +31,7 @@ from .status import (
STATUS_PROCESSING_OUTPUT,
STATUS_QUEUED_ON_SERVER,
STATUS_SUBMITTING_TASK,
+ ParseStatusUpdate,
)
@@ -184,7 +185,7 @@ class V1ArtifactClient:
tier: str,
page_range: str,
ocr_mode: Literal["auto", "txt", "ocr"] = "auto",
- status_callback: Callable[[str], None] | None = None,
+ status_callback: Callable[[str | ParseStatusUpdate], None] | None = None,
) -> ParseResult:
"""通过 V1 API 解析单个文件,并返回带图片/模型输出的 `ParseResult`。"""
page_range = normalize_page_range_input(page_range)
@@ -223,8 +224,14 @@ class V1ArtifactClient:
}
emit(messages[status])
+ def on_job_duration(duration_ms: float) -> None:
+ """复用完成响应的文件耗时,避免快速任务因漏过 running 而丢失最终计时。"""
+ emit(ParseStatusUpdate(STATUS_DOWNLOADING_RESULT, duration_ms))
+
try:
- result = await parser.parse_async(path, page_range=page_range, status_callback=on_job_status)
+ result = await parser.parse_async(
+ path, page_range=page_range, status_callback=on_job_status, duration_callback=on_job_duration
+ )
except Exception as exc:
emit(f"Failed: {exc}")
if isinstance(exc, (FileNotFoundError, V1ArtifactError)):
@@ -266,7 +273,7 @@ class GradioArtifactClient:
tier: str,
page_range: str,
ocr_mode: Literal["auto", "txt", "ocr"] = "auto",
- status_callback: Callable[[str], None] | None = None,
+ status_callback: Callable[[str | ParseStatusUpdate], None] | None = None,
) -> ParseResult:
"""仅向该档位对应的服务提交文件,复用原有状态、取消和产物流程。"""
if tier not in TIERS:
diff --git a/mineru/kit/gradio/conversion.py b/mineru/kit/gradio/conversion.py
index 108b4cb4..f2376734 100644
--- a/mineru/kit/gradio/conversion.py
+++ b/mineru/kit/gradio/conversion.py
@@ -8,7 +8,7 @@ from collections.abc import Callable
from dataclasses import dataclass, field
from typing import Any, ParamSpec, TypeVar
-from .status import STATUS_COMPLETED, STATUS_PREPARING_REQUEST, StatusPanelState
+from .status import STATUS_COMPLETED, STATUS_PREPARING_REQUEST, ParseStatusUpdate, StatusPanelState
P = ParamSpec("P")
T = TypeVar("T")
@@ -31,15 +31,15 @@ class ConversionRun:
self.state = StatusPanelState(run_id=self.run_id)
self.publish(STATUS_PREPARING_REQUEST)
- def publish(self, message: str, *, at: float | None = None) -> None:
- """仅真实阶段变化生成新快照,终态拒绝迟到的后台通知。"""
+ def publish(self, message: str | ParseStatusUpdate, *, at: float | None = None) -> None:
+ """阶段或耗时变化生成新快照,终态拒绝迟到的后台通知。"""
if self.cancelled or self.terminal or not self.state.append(message, at=at):
return
- self.terminal = message == STATUS_COMPLETED or message.startswith("Failed:")
+ self.terminal = self.state.message == STATUS_COMPLETED or self.state.message.startswith("Failed:")
self.snapshot = json.dumps(
{
"run_id": self.run_id,
- "sequence": self.state.phase_id,
+ "sequence": self.state.sequence,
"terminal": self.terminal,
"html": self.state.render(),
},
diff --git a/mineru/kit/gradio/status.py b/mineru/kit/gradio/status.py
index 2bd6ebdd..8165b004 100644
--- a/mineru/kit/gradio/status.py
+++ b/mineru/kit/gradio/status.py
@@ -35,6 +35,14 @@ _MESSAGE_STEPS = {
}
+@dataclass(frozen=True)
+class ParseStatusUpdate:
+ """在现有阶段通知中携带可选的服务端文件处理耗时,单位为毫秒。"""
+
+ message: str
+ duration_ms: float | None = None
+
+
@dataclass
class StatusPanelState:
"""保存一次转换的阶段和单调时钟,不在会话之间共享状态。"""
@@ -43,15 +51,25 @@ class StatusPanelState:
message: str = DEFAULT_STATUS
step_index: int = -1
processing_elapsed: float | None = None
+ server_elapsed: float | None = None
_processing_started: float | None = None
run_id: str = field(default_factory=lambda: uuid.uuid4().hex)
phase_id: int = 0
sequence: int = 0
- def append(self, message: str, *, at: float | None = None) -> bool:
- """接收真实阶段变化;重复通知不会重置解析计时。"""
+ def append(self, message: str | ParseStatusUpdate, *, at: float | None = None) -> bool:
+ """阶段通知维持本地计时;服务端耗时只更新完成数值,不重启同阶段动画。"""
+ duration_changed = False
+ if isinstance(message, ParseStatusUpdate):
+ if message.duration_ms is not None:
+ elapsed = message.duration_ms / 1000
+ duration_changed = elapsed != self.server_elapsed
+ self.server_elapsed = elapsed
+ message = message.message
if not message or message == self.message:
- return False
+ if duration_changed:
+ self.sequence += 1
+ return duration_changed
now = self.clock() if at is None else at
if self._processing_started is not None:
self.processing_elapsed = max(0.0, now - self._processing_started)
@@ -100,8 +118,11 @@ class StatusPanelState:
queue_key = "queued_locally" if self.message == STATUS_QUEUED_LOCALLY else "queued_on_server"
timer_attributes = f' data-mineru-queue-key="{queue_key}"'
elif completed:
- display_elapsed = Decimal(str(self.processing_elapsed or 0.0)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
- latest = f"{STATUS_COMPLETED} ({display_elapsed:.2f}s)"
+ elapsed = self.server_elapsed if self.server_elapsed is not None else self.processing_elapsed
+ if elapsed is not None:
+ # API 优先使用含打包的文件耗时;HF 等直接调用方继续使用阶段间隔,极短任务显示下限为 0.01 秒。
+ display_elapsed = max(Decimal("0.01"), Decimal(str(elapsed)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))
+ latest = f"{STATUS_COMPLETED} ({display_elapsed:.2f}s)"
if self.message == DEFAULT_STATUS:
title = _localized_text("status_idle_title")
latest_html = _localized_text("status_idle_hint")
@@ -136,6 +157,7 @@ __all__ = [
"STATUS_QUEUED_LOCALLY",
"STATUS_QUEUED_ON_SERVER",
"STATUS_SUBMITTING_TASK",
+ "ParseStatusUpdate",
"StatusPanelState",
"status_html",
]
diff --git a/mineru/parser/api_client.py b/mineru/parser/api_client.py
index f9f699d8..70116a50 100644
--- a/mineru/parser/api_client.py
+++ b/mineru/parser/api_client.py
@@ -16,6 +16,7 @@ import io
import ipaddress
import json
import logging
+import math
import os
import random
import re
@@ -66,6 +67,29 @@ def _notify_job_status(job: dict[str, Any], callback: Callable[[ApiJobStatus], N
logger.exception("Parse job status callback failed")
+def _notify_job_duration(job: dict[str, Any], callback: Callable[[float], None] | None) -> None:
+ """下载前通知单文件既有 duration_ms;沿用含导出打包的语义,并隔离缺失值和回调错误。"""
+ if callback is None or job.get("status") not in {"completed", "partial"}:
+ return
+ files = job.get("files")
+ if not isinstance(files, list) or len(files) != 1 or not isinstance(files[0], dict):
+ return
+ info = files[0].get("parse")
+ value = info.get("duration_ms") if isinstance(info, dict) else None
+ if type(value) not in (int, float):
+ return
+ try:
+ duration = float(value)
+ except OverflowError:
+ return
+ if not math.isfinite(duration) or duration < 0:
+ return
+ try:
+ callback(duration)
+ except Exception:
+ logger.exception("Parse job duration callback failed")
+
+
class _APITransportError(Exception):
def __init__(self, *, stage: str, method: _RequestMethod, attempts: int, cause: httpx.TransportError) -> None:
self.stage = stage
@@ -134,8 +158,9 @@ class MinerUApiParser(DocumentParser):
*,
page_range: str = "",
status_callback: Callable[[ApiJobStatus], None] | None = None,
+ duration_callback: Callable[[float], None] | None = None,
) -> ParseResult:
- """同步解析文件;可选回调观察任务状态,服务端完成后仍需下载并构造结果。"""
+ """同步解析;可选 duration_callback 在下载前接收服务端含打包的毫秒耗时。"""
page_range = normalize_page_range_input(page_range)
file_path = Path(path)
if not file_path.exists():
@@ -143,6 +168,7 @@ class MinerUApiParser(DocumentParser):
payload = self._build_payload(self._build_source(file_path), page_range)
job = self._do_parse(payload, status_callback=status_callback)
+ _notify_job_duration(job, duration_callback)
return self._build_result(job, file_path.name)
async def parse_async(
@@ -151,8 +177,9 @@ class MinerUApiParser(DocumentParser):
*,
page_range: str = "",
status_callback: Callable[[ApiJobStatus], None] | None = None,
+ duration_callback: Callable[[float], None] | None = None,
) -> ParseResult:
- """异步解析文件;回调属于本次调用,不在复用的 parser 实例上保存。"""
+ """异步解析;状态和耗时回调均属于本次调用,不在复用的 parser 实例上保存。"""
page_range = normalize_page_range_input(page_range)
file_path = Path(path)
if not file_path.exists():
@@ -160,6 +187,7 @@ class MinerUApiParser(DocumentParser):
payload = self._build_payload(await self._async_build_source(file_path), page_range)
job = await self._async_do_parse(payload, status_callback=status_callback)
+ _notify_job_duration(job, duration_callback)
return await self._async_build_result(job, file_path.name)
def get_usage(self) -> dict[str, Any]:
diff --git a/tests/unittest/test_kit_gradio.py b/tests/unittest/test_kit_gradio.py
index 65e387a8..94f0c585 100644
--- a/tests/unittest/test_kit_gradio.py
+++ b/tests/unittest/test_kit_gradio.py
@@ -39,6 +39,7 @@ from mineru.kit.gradio.client import (
normalize_v1_base_url,
)
from mineru.kit.main import app
+from mineru.kit.gradio.status import STATUS_COMPLETED, ParseStatusUpdate, StatusPanelState
from mineru.parser import api_client as parser_api_client
from mineru.parser import api_server as parser_api_server
from mineru.parser.base import ParseResult
@@ -436,11 +437,17 @@ def test_v1_client_checks_requested_tier_after_first_discovery(tmp_path: Path) -
assert error.value.code == "tier_unavailable"
[email protected]("export_seconds", [0.0, 1.5])
def test_v1_client_full_asgi_upload_job_poll_and_zip(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
+ export_seconds: float,
) -> None:
- """通过真实 V1 ASGI 路由验证上传、任务、轮询、ZIP 与 API Key 链路。"""
+ """贯通真实 V1 与显示层,验证 250ms 解析及可选 1500ms 打包都计入既有文件耗时。"""
+ clock = [10.0]
+ monkeypatch.setattr(
+ parser_api_server, "time", SimpleNamespace(time=parser_api_server.time.time, monotonic=lambda: clock[0])
+ )
source = tmp_path / "demo.pdf"
source.write_bytes(_pdf_bytes())
parse_calls: list[dict[str, Any]] = []
@@ -448,9 +455,19 @@ def test_v1_client_full_asgi_upload_job_poll_and_zip(
async def fake_parse_async(path: str, **kwargs: Any) -> ParseResult:
"""替代模型推理,同时保留 server 的真实 job 和打包流程。"""
parse_calls.append({"path": path, **kwargs})
+ clock[0] += 0.25
return ParseResult(middle_json=_middle_json(with_image=False), _model_output=_model_json())
+ original_zip = parser_api_server._build_self_contained_zip_output
+
+ def export(result: ParseResult) -> bytes:
+ """真实生成 ZIP,仅用确定性时钟模拟结果打包耗时。"""
+ payload = original_zip(result)
+ clock[0] += export_seconds
+ return payload
+
monkeypatch.setattr(parser_api_server, "parse_async", fake_parse_async)
+ monkeypatch.setattr(parser_api_server, "_build_self_contained_zip_output", export)
api = parser_api_server.create_app(
upload_dir=str(tmp_path / "api"),
tier="flash",
@@ -462,7 +479,11 @@ def test_v1_client_full_asgi_upload_job_poll_and_zip(
monkeypatch.setattr(gradio_client, "httpx", proxy)
monkeypatch.setattr(parser_api_client, "httpx", proxy)
client = V1ArtifactClient(api_url="http://testserver", api_key="secret")
- result = asyncio.run(client.parse_file(source, tier="flash", page_range="1"))
+ state = StatusPanelState()
+ result = asyncio.run(client.parse_file(source, tier="flash", page_range="1", status_callback=state.append))
+ state.append(STATUS_COMPLETED)
+ assert state.server_elapsed == pytest.approx(0.25 + export_seconds)
+ assert f"Completed ({0.25 + export_seconds:.2f}s)" in state.render()
paths = [path for _method, path, _authorization in request_log]
assert "/v1/health" in paths
@@ -675,7 +696,9 @@ def test_v1_client_preserves_v1_error_code(monkeypatch: pytest.MonkeyPatch, tmp_
def __init__(self, **_kwargs: Any) -> None:
"""忽略构造参数。"""
- async def parse_async(self, _path: Path, *, page_range: str, status_callback: Any = None) -> ParseResult:
+ async def parse_async(
+ self, _path: Path, *, page_range: str, status_callback: Any = None, duration_callback: Any = None
+ ) -> ParseResult:
"""抛出与正式 API client 一致的错误形态。"""
error = RuntimeError(f"bad range: {page_range}")
error.code = "page_range_invalid" # type: ignore[attr-defined]
@@ -690,7 +713,10 @@ def test_v1_client_preserves_v1_error_code(monkeypatch: pytest.MonkeyPatch, tmp_
@pytest.mark.parametrize("ocr_mode", ["auto", "txt", "ocr"])
-def test_v1_client_parse_uses_api_parser_zip_contract(monkeypatch: pytest.MonkeyPatch, tmp_path: Path, ocr_mode: str) -> None:
[email protected]("observe_running", [False, True])
+def test_v1_client_parse_uses_api_parser_zip_contract(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path, ocr_mode: str, observe_running: bool
+) -> None:
"""验证 Gradio client 固定请求带图片和模型输出的 V1 ZIP 结果。"""
source = tmp_path / "demo.pdf"
source.write_bytes(_pdf_bytes())
@@ -703,16 +729,21 @@ def test_v1_client_parse_uses_api_parser_zip_contract(monkeypatch: pytest.Monkey
"""保存构造参数,供断言 V1 ZIP 契约。"""
calls.update(kwargs)
- async def parse_async(self, path: Path, *, page_range: str, status_callback: Any = None) -> ParseResult:
+ async def parse_async(
+ self, path: Path, *, page_range: str, status_callback: Any = None, duration_callback: Any = None
+ ) -> ParseResult:
"""返回最小解析结果。"""
calls["path"] = path
calls["page_range"] = page_range
assert statuses == ["Preparing request...", "Submitting task..."]
assert status_callback is not None
status_callback("queued")
- status_callback("running")
+ if observe_running:
+ status_callback("running")
status_callback("completed")
assert statuses[-1] == STATUS_DOWNLOADING_RESULT
+ assert duration_callback is not None
+ duration_callback(250)
return ParseResult(middle_json=_middle_json(with_image=False))
monkeypatch.setattr("mineru.kit.gradio.client.MinerUApiParser", FakeParser)
@@ -723,7 +754,7 @@ def test_v1_client_parse_uses_api_parser_zip_contract(monkeypatch: pytest.Monkey
("zip",),
("file_id",),
)
- statuses: list[str] = []
+ statuses: list[str | ParseStatusUpdate] = []
result = asyncio.run(
client.parse_file(source, tier="standard", page_range="1-2", ocr_mode=ocr_mode, status_callback=statuses.append)
)
@@ -734,8 +765,14 @@ def test_v1_client_parse_uses_api_parser_zip_contract(monkeypatch: pytest.Monkey
assert calls["ocr_mode"] == ocr_mode
assert calls["page_range"] == "1-2"
assert statuses[0] == "Preparing request..."
- assert statuses[-1] == STATUS_DOWNLOADING_RESULT
- assert statuses[2:4] == ["Queued on server", "Processing on server..."]
+ assert statuses[-1] == ParseStatusUpdate(STATUS_DOWNLOADING_RESULT, 250)
+ assert statuses[2] == "Queued on server"
+ assert ("Processing on server..." in statuses) == observe_running
+ state = StatusPanelState()
+ for status in statuses:
+ state.append(status)
+ state.append(STATUS_COMPLETED)
+ assert "Completed (0.25s)" in state.render()
def test_managed_local_api_command_uses_kit_entrypoint() -> None:
diff --git a/tests/unittest/test_kit_gradio_status.py b/tests/unittest/test_kit_gradio_status.py
index ecbc39d0..d5b47d0d 100644
--- a/tests/unittest/test_kit_gradio_status.py
+++ b/tests/unittest/test_kit_gradio_status.py
@@ -13,6 +13,7 @@ from mineru.kit.gradio.status import (
STATUS_PROCESSING_OUTPUT,
STATUS_QUEUED_LOCALLY,
STATUS_QUEUED_ON_SERVER,
+ ParseStatusUpdate,
StatusPanelState,
status_html,
)
@@ -59,14 +60,19 @@ def test_queued_status_stays_stable_without_timer_updates(message: str) -> None:
assert f'data-mineru-i18n-en="{message}"' in state.render()
-def test_fast_completion_shows_zero_elapsed_time_and_reset_has_eight_pending_steps() -> None:
- """验证未观察到解析阶段时完成状态显示 0.00 秒,重置后仍保留完整双语步骤。"""
[email protected]("queued", [False, True])
+def test_fast_completion_uses_server_duration_without_running(queued: bool) -> None:
+ """直接完成或轮询漏过 running 时仍显示服务端亚秒耗时,重置后保留完整双语步骤。"""
state = StatusPanelState()
- state.append(STATUS_QUEUED_ON_SERVER)
- state.append(STATUS_DOWNLOADING_RESULT)
+ if queued:
+ state.append(STATUS_QUEUED_ON_SERVER)
+ state.append(ParseStatusUpdate(STATUS_DOWNLOADING_RESULT, 250))
state.append(STATUS_COMPLETED)
- assert 'data-mineru-i18n-en="Completed (0.00s)"' in state.render()
- assert 'data-mineru-i18n-zh="已完成(0.00 秒)"' in state.render()
+ assert state.processing_elapsed is None
+ assert 'data-mineru-i18n-en="Completed (0.25s)"' in state.render()
+ assert 'data-mineru-i18n-zh="已完成(0.25 秒)"' in state.render()
+ assert "data-mineru-processing-start" not in state.render()
+ assert state.render().count("status-step is-done") == 8
idle = status_html()
assert idle.count("status-step is-pending") == 8
assert 'data-mineru-i18n-en="Waiting"' in idle
@@ -76,10 +82,20 @@ def test_fast_completion_shows_zero_elapsed_time_and_reset_has_eight_pending_ste
@pytest.mark.parametrize(
("elapsed", "expected"),
- [(0.0, "0.00"), (0.004, "0.00"), (0.005, "0.01"), (0.04, "0.04"), (0.05, "0.05"), (0.125, "0.13")],
+ [
+ (0.0, "0.01"),
+ (0.004, "0.01"),
+ (0.005, "0.01"),
+ (0.04, "0.04"),
+ (0.05, "0.05"),
+ (0.125, "0.13"),
+ (0.25, "0.25"),
+ (0.999, "1.00"),
+ (1.25, "1.25"),
+ ],
)
def test_completed_duration_uses_decimal_half_up_rounding(elapsed: float, expected: str) -> None:
- """验证完成耗时保留两位小数,并在 0.005 秒等边界按四舍五入显示。"""
+ """直接函数调用的阶段计时保留两位小数,极短任务按 0.01 秒显示下限处理。"""
state = StatusPanelState(clock=lambda: 0.0)
state.append(STATUS_PROCESSING_ON_SERVER, at=0.0)
state.append(STATUS_DOWNLOADING_RESULT, at=elapsed)
@@ -88,6 +104,40 @@ def test_completed_duration_uses_decimal_half_up_rounding(elapsed: float, expect
assert f'data-mineru-i18n-en="Completed ({expected}s)"' in state.render()
[email protected](("duration_ms", "expected"), [(0, "0.01"), (1, "0.01"), (125, "0.13"), (1750, "1.75")])
+def test_server_duration_overrides_observed_polling_time(duration_ms: float, expected: str) -> None:
+ """最终数值来自服务端,长轮询间隔和下载等待不会覆盖它。"""
+ state = StatusPanelState(clock=lambda: 1000.0)
+ state.append(STATUS_PROCESSING_ON_SERVER, at=10.0)
+ state.append(ParseStatusUpdate(STATUS_DOWNLOADING_RESULT, duration_ms), at=20.0)
+ state.append(STATUS_PROCESSING_OUTPUT, at=900.0)
+ state.append(STATUS_COMPLETED, at=1000.0)
+ assert state.processing_elapsed == 10.0
+ assert f"Completed ({expected}s)" in state.render()
+
+
+def test_duration_only_snapshot_preserves_phase_and_rejects_late_updates() -> None:
+ """仅补齐耗时也增加快照序号;重复、终态和取消后的通知不能覆盖当前结果。"""
+ run = ConversionRun("timed")
+ run.publish(STATUS_DOWNLOADING_RESULT)
+ phase, sequence = run.state.phase_id, json.loads(run.snapshot)["sequence"]
+ run.publish(ParseStatusUpdate(STATUS_DOWNLOADING_RESULT, 250))
+ assert run.state.phase_id == phase
+ assert json.loads(run.snapshot)["sequence"] == sequence + 1
+ snapshot = run.snapshot
+ run.publish(ParseStatusUpdate(STATUS_DOWNLOADING_RESULT, 250))
+ assert run.snapshot == snapshot
+ run.publish(STATUS_COMPLETED)
+ final = run.snapshot
+ run.publish(ParseStatusUpdate(STATUS_DOWNLOADING_RESULT, 9000))
+ assert run.snapshot == final
+ assert "Completed (0.25s)" in final
+ canceled = ConversionRun("canceled")
+ canceled.cancel()
+ canceled.publish(ParseStatusUpdate(STATUS_DOWNLOADING_RESULT, 250))
+ assert canceled.state.server_elapsed is None
+
+
@pytest.mark.parametrize("error", ["server task failed", "server task canceled", '<script>alert("x")</script>'])
def test_failure_stops_timer_and_renders_escaped_error(error: str) -> None:
"""验证失败或取消停止计时,并使用红色失败节点展示转义后的消息。"""
diff --git a/tests/unittest/test_parser_api_status.py b/tests/unittest/test_parser_api_status.py
index f2ad75e3..e2dbb2e0 100644
--- a/tests/unittest/test_parser_api_status.py
+++ b/tests/unittest/test_parser_api_status.py
@@ -22,6 +22,7 @@ from mineru.types import MiddleJson
[
("queued", "running", "running", "completed"),
("completed",),
+ ("queued", "completed"),
("queued", "partial"),
("queued", "running", "failed"),
("queued", "canceled"),
@@ -42,6 +43,7 @@ def test_parse_notifies_submission_and_polls_before_result_building(
parser = MinerUApiParser(api_url="http://localhost:8000", tier="flash")
parser._source_features = {"local"}
notifications: list[ApiJobStatus] = []
+ durations: list[float] = []
delays: list[float] = []
replies = iter(statuses)
methods: list[str] = []
@@ -60,7 +62,9 @@ def test_parse_notifies_submission_and_polls_before_result_building(
def respond(request: httpx.Request) -> httpx.Response:
"""只模拟创建和轮询任务,保留正式 HTTP 调用及状态传播路径。"""
methods.append(request.method)
- return httpx.Response(200, json={"job_id": "job_test", "status": next(replies)})
+ return httpx.Response(
+ 200, json={"job_id": "job_test", "status": next(replies), "files": [{"parse": {"duration_ms": 250}}]}
+ )
transport = httpx.MockTransport(respond)
sync_client, async_client = httpx.Client, httpx.AsyncClient
@@ -86,6 +90,7 @@ def test_parse_notifies_submission_and_polls_before_result_building(
def build_result(job: dict[str, Any], file_name: str) -> ParseResult:
"""确认所有状态在开始下载/构建结果之前已经通知。"""
assert notifications == list(statuses)
+ assert durations == ([250.0] if statuses[-1] in {"completed", "partial"} else [])
assert file_name == source.name
api_client._raise_for_terminal_job_error(job)
return result
@@ -104,8 +109,8 @@ def test_parse_notifies_submission_and_polls_before_result_building(
def parse() -> ParseResult:
"""统一执行两个公共入口,确保回调参数一路透传。"""
if use_async:
- return asyncio.run(parser.parse_async(source, status_callback=on_status))
- return parser.parse(source, status_callback=on_status)
+ return asyncio.run(parser.parse_async(source, status_callback=on_status, duration_callback=durations.append))
+ return parser.parse(source, status_callback=on_status, duration_callback=durations.append)
if statuses[-1] in ("failed", "canceled"):
with pytest.raises(Exception, match=f"ended with status {statuses[-1]}"):
@@ -116,3 +121,24 @@ def test_parse_notifies_submission_and_polls_before_result_building(
assert methods == ["POST", *(["GET"] * (len(statuses) - 1))]
assert delays == [1] * (len(statuses) - 1)
assert callback_errors == (["Parse job status callback failed"] * len(statuses) if callback_raises else [])
+
+
[email protected]("value", [None, True, "250", -1, float("nan"), float("inf"), 10**400])
+def test_invalid_or_missing_duration_does_not_invent_zero(value: object) -> None:
+ """旧服务缺失字段或非法数值不伪造零耗时,也不破坏结果下载。"""
+ durations: list[float] = []
+ api_client._notify_job_duration({"status": "completed", "files": [{"parse": {"duration_ms": value}}]}, durations.append)
+ assert durations == []
+
+
+def test_duration_callback_failure_is_isolated(monkeypatch: pytest.MonkeyPatch) -> None:
+ """耗时观察者异常仅写日志,不让已完成的解析任务失败。"""
+ errors: list[str] = []
+ monkeypatch.setattr(api_client.logger, "exception", errors.append)
+
+ def fail(_duration: float) -> None:
+ """模拟显示层的耗时处理异常。"""
+ raise RuntimeError("observer failed")
+
+ api_client._notify_job_duration({"status": "completed", "files": [{"parse": {"duration_ms": 250}}]}, fail)
+ assert errors == ["Parse job duration callback failed"]