Files
syops/backend/app/services/shipping_notice/rpa_attempts.py
T

104 lines
3.5 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
FIXED_RPA_INVENTORY_MAX_ATTEMPTS = 1
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_inventory_attempts() -> int:
"""RPA 库存导出固定只允许自动尝试一次;再次尝试必须由用户显式点击 retry_rpa。"""
return FIXED_RPA_INVENTORY_MAX_ATTEMPTS
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)