feat(shipping-notice): 增加 RPA 库存回调处理逻辑,支持跳过无库存情况

This commit is contained in:
hugo committed 2026-07-10 17:21:11 +08:00
1 parent f21216f81e
commit c3f05ba96b
25 files changed
+1619 -81

No files matched your search

+63 -6
View File
@@ -1,10 +1,15 @@
"""
scripts/rpa/dsv_export_batch_smoke.py
用途:本地验证 DSV 360 同一浏览器 session 内连续导出 3 份库存 Excel。
用途:本地验证 DSV 360 同一浏览器 session 内连续批量导出库存(smoke / 联调)。
依赖:Playwright(msedge)、FREIGHT_RPA_DSV_USERNAME/PASSWORD、shipping_inventory_sources.yaml、freight_rpa_worker。
逻辑:复用 worker 批量导出循环(一次登录 + reset 入口),不依赖 HXOPS callback。
Smoke 语义(团队约定):
- 验证目标:**批量导出链路能跑完**(登录 → 导航 → 填参 → View Report → 导出/跳过判定)。
- **不保证**至少导出一个 Excel;全仓无库存(全 skipped)也算通过(退出码 0)。
- 若需“至少真实导出一个 Excel”,设置 FREIGHT_RPA_SMOKE_REQUIRE_EXPORT=true。
场景:
warehouse — 同一 source_code / 同一 3lv 报表,仅 Warehouse 参数变化
source_code — 三个不同 source_code(YAML 中独立配置项)
@@ -13,6 +18,10 @@ scripts/rpa/dsv_export_batch_smoke.py
backend\\venv\\Scripts\\python.exe scripts\\rpa\\dsv_export_batch_smoke.py warehouse
backend\\venv\\Scripts\\python.exe scripts\\rpa\\dsv_export_batch_smoke.py source_code
backend\\venv\\Scripts\\python.exe scripts\\rpa\\dsv_export_batch_smoke.py all
退出码:
0 — summary.status=success(含全 skipped;或至少一条 success)
2 — 无结果、partial_failed、failed;或 REQUIRE_EXPORT=true 且无 success 导出
"""
from __future__ import annotations
@@ -134,18 +143,34 @@ def _run_batch_exports(worker: Any, scenario: str, sources: list[dict[str, Any]]
{
"source_code": code,
"warehouse_id": wh,
"status": "success",
"success": True,
"file_path": str(out_file),
"size_kb": round(size_kb, 1),
}
)
print(f"[batch-smoke] OK {code}/{wh} -> {out_file.name} ({size_kb:.1f} KB)")
except worker.NoInventoryDataError as exc:
results.append(
{
"source_code": code,
"warehouse_id": wh,
"status": "skipped",
"success": False,
"skipped": True,
"skip_reason": "no_inventory_data",
"message": str(exc),
}
)
print(f"[batch-smoke] SKIP {code}/{wh}: {exc}")
except Exception as exc:
results.append(
{
"source_code": code,
"warehouse_id": wh,
"status": "failed",
"success": False,
"error_code": "rpa_export_failed",
"error_message": str(exc),
}
)
@@ -161,14 +186,36 @@ def _run_batch_exports(worker: Any, scenario: str, sources: list[dict[str, Any]]
shutil.rmtree(runtime_profile_dir, ignore_errors=True)
elapsed_s = int(time.monotonic() - started)
ok = sum(1 for item in results if item.get("success"))
summary = worker._summarize_inventory_batch_results(results)
print(
f"[batch-smoke] scenario={scenario} done success={ok}/{len(results)} "
f"[batch-smoke] scenario={scenario} done status={summary['status']} "
f"success={summary['success_count']} skipped={summary['skipped_count']} "
f"failed={summary['failed_count']}/{len(results)} "
f"elapsed={elapsed_s}s drop_dir={drop_dir}"
)
return results
def _resolve_smoke_scenario_exit_code(
*,
results: list[dict[str, Any]],
summary: dict[str, Any],
require_export: bool,
) -> int:
"""
根据 batch summary 与 FREIGHT_RPA_SMOKE_REQUIRE_EXPORT 计算 smoke 退出码。
核心逻辑:默认 status=success 即 0(含全 skipped);require_export 且无 Excel 导出时返回 2。
"""
if not results:
return 2
if summary.get("status") in {"failed", "partial_failed"}:
return 2
if require_export and not summary.get("has_success"):
return 2
return 0
def _run_scenario(worker: Any, scenario: str) -> int:
if scenario == "warehouse":
sources = _build_warehouse_scenario_sources()
@@ -187,9 +234,19 @@ def _run_scenario(worker: Any, scenario: str) -> int:
)
results = _run_batch_exports(worker, scenario, sources, drop_dir)
if not results or not all(item.get("success") for item in results):
return 2
return 0
summary = worker._summarize_inventory_batch_results(results)
require_export = worker._env("FREIGHT_RPA_SMOKE_REQUIRE_EXPORT", "false").lower() == "true"
exit_code = _resolve_smoke_scenario_exit_code(
results=results,
summary=summary,
require_export=require_export,
)
if exit_code == 2 and require_export and not summary.get("has_success") and results:
print(
"[batch-smoke] FREIGHT_RPA_SMOKE_REQUIRE_EXPORT=true 但未导出任何 Excel",
file=sys.stderr,
)
return exit_code
def main() -> int: