feat(shipping-notice): 添加识别定义和规则集管理功能,支持客户和站点的可配置识别规则

This commit is contained in:
hugo committed 2026-07-20 19:28:17 +08:00
1 parent 88408490d7
commit 34d489a2d1
167 files changed
+10365 -7978

No files matched your search

+106 -17
View File
@@ -888,6 +888,57 @@ def _summarize_inventory_batch_results(results: list[dict[str, Any]]) -> dict[st
}
def _configuration_test_callback_payload(run_no: str, results: list) -> dict:
"""
将 batch 导出结果收敛为库存站点测试专用回调载荷。
核心逻辑:配置测试只测单站点,取首个 success 的 file_path;无库存是有效业务结果,单独上报 empty。
必须回调 /rpa/inventory-configuration-test-ready(站点停用时生产 batch 接口会报 unknown_or_disabled_source)。
Args:
run_no: 测试运行号(CONFIG-...)。
results: Worker 各 source 导出结果列表。
Returns:
dict: report_test_export 所需的 run_no / status / file_path 或 error_message。
"""
success_item = next(
(
item
for item in results
if isinstance(item, dict) and _normalize_inventory_batch_item_status(item) == "success"
),
None,
)
if success_item is not None:
return {
"run_no": run_no,
"status": "success",
"file_path": str(success_item.get("file_path") or ""),
}
empty_item = next(
(
item for item in results
if isinstance(item, dict) and str(item.get("skip_reason") or "") == "no_inventory_data"
),
None,
)
if empty_item is not None:
return {
"run_no": run_no,
"status": "empty",
"message": str(empty_item.get("message") or "库存查询成功但当前无库存数据")[:500],
}
first = next((item for item in results if isinstance(item, dict)), {})
return {
"run_no": run_no,
"status": "failed",
"error_message": str(
first.get("error_message") or first.get("message") or "测试导出失败"
)[:500],
}
def _handle_inventory_batch_trigger(trigger_path: Path, drop_dir: Path, processed_dir: Path) -> None:
body = json.loads(trigger_path.read_text(encoding="utf-8"))
run_no = str(body.get("run_no") or trigger_path.stem).strip()
@@ -902,10 +953,28 @@ def _handle_inventory_batch_trigger(trigger_path: Path, drop_dir: Path, processe
mode = "mock" if mock else "browser"
results: list[dict[str, Any]] = []
def report_configuration_test_progress(source: dict) -> None:
"""实际操作站点前回传领取状态;进度回调故障不能中断库存导出。"""
if str(body.get("trigger_type") or "").strip() != "configuration_test_batch":
return
validation_run_no = str(source.get("validation_run_no") or "").strip()
if not validation_run_no:
return
try:
_post_callback("/api/shipping-notice/rpa/inventory-configuration-test-batch-progress", {
"run_no": run_no,
"validation_batch_id": body.get("validation_batch_id"),
"validation_run_no": validation_run_no,
"stage": "rpa_exporting",
})
except Exception as exc:
print(f"[inventory-batch] progress callback failed {validation_run_no}: {exc}", file=sys.stderr)
if mock:
for source in sources:
if not isinstance(source, dict):
continue
report_configuration_test_progress(source)
task_stub = run_no
out_file = _write_sample_inventory(drop_dir, task_stub)
results.append({
@@ -915,6 +984,7 @@ def _handle_inventory_batch_trigger(trigger_path: Path, drop_dir: Path, processe
"success": True,
"file_path": str(out_file),
"task_no": task_no or None,
"validation_run_no": source.get("validation_run_no"),
})
else:
# 一个 Profile 对应一个独立浏览器登录态;同 Driver 的不同账号绝不能共用 session。
@@ -934,6 +1004,7 @@ def _handle_inventory_batch_trigger(trigger_path: Path, drop_dir: Path, processe
code = str(source.get("source_code") or "")
wh = str(source.get("warehouse_id") or "")
try:
report_configuration_test_progress(source)
_validate_inventory_source_for_rpa(source)
skip_navigation = False
if site == "dsv360":
@@ -944,17 +1015,17 @@ def _handle_inventory_batch_trigger(trigger_path: Path, drop_dir: Path, processe
_reset_crane_export_entry(page, entry_url)
download = export_fn(page, body, source, skip_navigation=skip_navigation) if site == "dsv360" else export_fn(page, body, source)
out_file = _save_batch_download(download, drop_dir, run_no, source)
results.append({"source_code": code, "warehouse_id": wh, "status": "success", "success": True, "file_path": str(out_file), "task_no": task_no or None})
results.append({"source_code": code, "warehouse_id": wh, "status": "success", "success": True, "file_path": str(out_file), "task_no": task_no or None, "validation_run_no": source.get("validation_run_no")})
print(f"[inventory-batch] {site} export done {code}/{wh} -> {out_file.name}")
except 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)})
results.append({"source_code": code, "warehouse_id": wh, "status": "skipped", "success": False, "skipped": True, "skip_reason": "no_inventory_data", "message": str(exc), "validation_run_no": source.get("validation_run_no")})
print(f"[inventory-batch] {site} export skipped {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)})
results.append({"source_code": code, "warehouse_id": wh, "status": "failed", "success": False, "error_code": "rpa_export_failed", "error_message": str(exc), "validation_run_no": source.get("validation_run_no")})
print(f"[inventory-batch] {site} export failed {code}/{wh}: {exc}", file=sys.stderr)
except Exception as exc:
for source in site_sources:
results.append({"source_code": source.get("source_code"), "warehouse_id": source.get("warehouse_id"), "status": "failed", "success": False, "error_code": "rpa_profile_failed", "error_message": str(exc)})
results.append({"source_code": source.get("source_code"), "warehouse_id": source.get("warehouse_id"), "status": "failed", "success": False, "error_code": "rpa_profile_failed", "error_message": str(exc), "validation_run_no": source.get("validation_run_no")})
finally:
if context is not None:
context.close()
@@ -965,22 +1036,40 @@ def _handle_inventory_batch_trigger(trigger_path: Path, drop_dir: Path, processe
try:
batch_summary = _summarize_inventory_batch_results(results)
_post_callback("/api/shipping-notice/rpa/inventory-batch-ready", {
"run_no": run_no,
"status": batch_summary["status"],
"success": batch_summary["success"],
"has_success": batch_summary["has_success"],
"success_count": batch_summary["success_count"],
"skipped_count": batch_summary["skipped_count"],
"failed_count": batch_summary["failed_count"],
"rpa_mode": mode,
"trigger_type": body.get("trigger_type"),
"schedule_run_id": body.get("schedule_run_id"),
"results": results,
})
trigger_type = str(body.get("trigger_type") or "").strip()
# [STEP] 配置测试必须走专用回调;停用站点走生产 batch 会得到 unknown_or_disabled_source
if trigger_type == "configuration_test":
callback_path = "/api/shipping-notice/rpa/inventory-configuration-test-ready"
callback_payload = _configuration_test_callback_payload(run_no, results)
elif trigger_type == "configuration_test_batch":
callback_path = "/api/shipping-notice/rpa/inventory-configuration-test-batch-ready"
callback_payload = {
"run_no": run_no,
"validation_batch_id": body.get("validation_batch_id"),
"batch_no": body.get("batch_no"),
"rpa_mode": mode,
"results": results,
}
else:
callback_path = "/api/shipping-notice/rpa/inventory-batch-ready"
callback_payload = {
"run_no": run_no,
"status": batch_summary["status"],
"success": batch_summary["success"],
"has_success": batch_summary["has_success"],
"success_count": batch_summary["success_count"],
"skipped_count": batch_summary["skipped_count"],
"failed_count": batch_summary["failed_count"],
"rpa_mode": mode,
"trigger_type": body.get("trigger_type"),
"schedule_run_id": body.get("schedule_run_id"),
"results": results,
}
_post_callback(callback_path, callback_payload)
_move_trigger(trigger_path, processed_dir, "processed")
print(
f"[inventory-batch] callback done run={run_no} "
f"path={callback_path} "
f"status={batch_summary['status']} "
f"success={batch_summary['success_count']} "
f"skipped={batch_summary['skipped_count']} "