清理废弃的shipping xfa ocr automark相关代码和job配置 新增收料报表管理的前端页面、路由和侧边栏入口 新增后端收料报表配置接口、数据库模型和RPA脚本 优化发货通知流程的重试策略,将shipping相关job默认不自动重试 移除废弃的库存源确认相关逻辑,更新流程门禁规则 重构订单模板的值适配器模块,新增坐标OCR值适配器 清理前端shipping菜单和action的废弃选项 更新相关文档和测试用例,调整.gitignore忽略规则
98 lines
3.3 KiB
Python
98 lines
3.3 KiB
Python
"""
|
|
services/shipping_notice/rpa_attempts.py
|
|
|
|
用途:统计任务的 RPA 库存导出与货代上传重试次数,并返回统一上限。
|
|
依赖:ShippingNotice、ShippingTaskEvent 模型与事件查询逻辑。
|
|
逻辑:由 inventory_rpa_workflow、fulfillment_workflow 等模块调用本文件,决定是否还能继续触发 RPA 重试。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from datetime import datetime
|
|
|
|
from app.models import ShippingNotice, ShippingTaskEvent
|
|
|
|
DEFAULT_RPA_UPLOAD_MAX_ATTEMPTS = 3
|
|
|
|
_INVENTORY_SUCCESS_EVENTS = {
|
|
"inventory_uploaded",
|
|
"inventory_parsed",
|
|
"inventory_rpa_drop_ingested",
|
|
"inventory_rpa_ready",
|
|
"inventory_template_mapping_used",
|
|
}
|
|
|
|
_INVENTORY_ATTEMPT_RESET_EVENTS = _INVENTORY_SUCCESS_EVENTS | {
|
|
"decision_retry_rpa",
|
|
}
|
|
|
|
_UPLOAD_SUCCESS_EVENTS = {
|
|
"upload_rpa_success",
|
|
"upload_manual_confirmed",
|
|
}
|
|
|
|
|
|
def inventory_rpa_attempts(task: ShippingNotice | None) -> int:
|
|
"""统计自上次库存成功事件以来的 RPA 库存导出触发/失败次数。"""
|
|
if not task or task.id is None:
|
|
return 0
|
|
|
|
last_reset = (
|
|
ShippingTaskEvent.query.filter_by(task_id=task.id)
|
|
.filter(ShippingTaskEvent.event_type.in_(_INVENTORY_ATTEMPT_RESET_EVENTS))
|
|
.order_by(ShippingTaskEvent.id.desc())
|
|
.first()
|
|
)
|
|
since: datetime | None = last_reset.created_at if last_reset else None
|
|
|
|
triggered = ShippingTaskEvent.query.filter_by(task_id=task.id).filter(
|
|
ShippingTaskEvent.event_type == "inventory_rpa_triggered"
|
|
)
|
|
failed = ShippingTaskEvent.query.filter_by(task_id=task.id).filter(
|
|
ShippingTaskEvent.event_type == "inventory_rpa_failed"
|
|
)
|
|
if since:
|
|
triggered = triggered.filter(ShippingTaskEvent.created_at >= since)
|
|
failed = failed.filter(ShippingTaskEvent.created_at >= since)
|
|
|
|
return max(triggered.count(), failed.count())
|
|
|
|
|
|
def upload_rpa_attempts(task: ShippingNotice | None) -> int:
|
|
"""统计自上次上传成功事件以来的 RPA 货代上传触发/失败次数。"""
|
|
if not task or task.id is None:
|
|
return 0
|
|
|
|
last_success = (
|
|
ShippingTaskEvent.query.filter_by(task_id=task.id)
|
|
.filter(ShippingTaskEvent.event_type.in_(_UPLOAD_SUCCESS_EVENTS))
|
|
.order_by(ShippingTaskEvent.id.desc())
|
|
.first()
|
|
)
|
|
since: datetime | None = last_success.created_at if last_success else None
|
|
|
|
triggered = ShippingTaskEvent.query.filter_by(task_id=task.id).filter(
|
|
ShippingTaskEvent.event_type == "upload_rpa_triggered"
|
|
)
|
|
failed = ShippingTaskEvent.query.filter_by(task_id=task.id).filter(
|
|
ShippingTaskEvent.event_type == "upload_rpa_failed"
|
|
)
|
|
if since:
|
|
triggered = triggered.filter(ShippingTaskEvent.created_at >= since)
|
|
failed = failed.filter(ShippingTaskEvent.created_at >= since)
|
|
|
|
return max(triggered.count(), failed.count())
|
|
|
|
|
|
def max_rpa_upload_attempts() -> int:
|
|
"""RPA 货代上传最大重试次数(环境变量 SHIPPING_NOTICE_RPA_UPLOAD_MAX_ATTEMPTS)。"""
|
|
value = os.getenv("SHIPPING_NOTICE_RPA_UPLOAD_MAX_ATTEMPTS")
|
|
if value is None or not str(value).strip():
|
|
return DEFAULT_RPA_UPLOAD_MAX_ATTEMPTS
|
|
try:
|
|
parsed = int(value)
|
|
except (TypeError, ValueError):
|
|
return DEFAULT_RPA_UPLOAD_MAX_ATTEMPTS
|
|
return max(parsed, 1)
|