feat(freight-rpa): 增加出站报表配置与回调支持,重构相关服务
- 将 RPA 管理 API 扩展至出站报表配置,新增出站相关的回调接口。 - 重构 freight_rpa_report_config_service,支持入站与出站报表配置的管理。 - 更新 ORM 模型,增加出站报表相关的持久化模型。 - 统一命名规范,确保在手、入站与出站的概念一致性。
This commit is contained in:
1 parent
39fe3c05f2
commit
911afa0f73
152 files changed
+9861
-2984
No files matched your search
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
backend/app/api/schedule/rpa_workers.py
|
||||
|
||||
用途:管理端查询货代库存/收料 RPA Worker 在线状态与就绪摘要。
|
||||
用途:管理端查询货代库存/入站/出站 RPA Worker 在线状态与就绪摘要。
|
||||
依赖:schedule.blueprint、freight_rpa.common.worker_heartbeat.serialize_rpa_worker_status。
|
||||
逻辑:ScheduleWorkbenchPanel 在库存或收料 RPA 排程工作台展示对应 worker 心跳状态。
|
||||
逻辑:ScheduleWorkbenchPanel 在库存、入站或出站 RPA 排程工作台展示对应 worker 心跳状态。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -23,7 +23,7 @@ def get_rpa_workers_status():
|
||||
返回 RPA Worker 心跳摘要(在线/离线、最后心跳时间、就绪原因)。
|
||||
|
||||
Query:
|
||||
scope: onhand(默认)或 inbound。
|
||||
scope: onhand(默认)、inbound 或 outbound。
|
||||
"""
|
||||
scope = str(request.args.get('scope') or 'onhand').strip().lower() or 'onhand'
|
||||
return ok(serialize_rpa_worker_status(scope=scope))
|
||||
@@ -2,8 +2,10 @@
|
||||
backend/app/api/schedule/runs.py
|
||||
|
||||
用途:排程运行触发与运行记录查询 API。
|
||||
依赖:schedule.blueprint、services/schedule/query_service、queue_service。
|
||||
依赖:schedule.blueprint、services/schedule/query_service、queue_service、
|
||||
freight_rpa inbound/outbound archive 与 trigger_io。
|
||||
逻辑:手动执行仅创建 queued run/job;独立 schedule worker 承担实际运行。
|
||||
入站/出站 raw-zip 下载与失败单元 list/retry 对照同构。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -18,14 +20,15 @@ from app.api.schedule.blueprint import schedule_admin_bp
|
||||
from app.core.auth.jwt import get_current_user
|
||||
from app.core.decorators import require_admin
|
||||
from app.core.errors import ConflictError, NotFoundError
|
||||
from app.models import ScheduleRun, ScheduleTask, FreightRpaInboundReportFile, FreightRpaInboundUnitRun
|
||||
from app.models import ScheduleRun, ScheduleTask, FreightRpaInboundUnitRun, FreightRpaOutboundUnitRun
|
||||
from app.services.schedule.query_service import get_run, list_runs, serialize_run
|
||||
from app.services.schedule.queue_service import enqueue_manual_schedule_run
|
||||
from app.services.schedule.freight_rpa_onhand_archive import resolve_inventory_raw_zip_download
|
||||
from app.services.schedule.freight_rpa_inbound_archive import resolve_receipt_raw_zip_download
|
||||
from app.services.freight_rpa.inbound.report_archive import deliver_inbound_file
|
||||
from app.services.schedule.freight_rpa_inbound_archive import resolve_inbound_raw_zip_download
|
||||
from app.services.schedule.freight_rpa_outbound_archive import resolve_outbound_raw_zip_download
|
||||
from app.services.shipping_notice.audit_service import append_shipping_audit_event
|
||||
from app.services.freight_rpa.inbound.trigger_io import SCHEMA, write_inbound_export_batch_trigger
|
||||
from app.services.freight_rpa.outbound.trigger_io import SCHEMA as OUTBOUND_SCHEMA, write_outbound_export_batch_trigger
|
||||
|
||||
|
||||
@schedule_admin_bp.route('/schedules/<int:task_id>/run', methods=['POST'])
|
||||
@@ -124,18 +127,18 @@ def get_schedule_run_inventory_raw_zip(run_id: int):
|
||||
)
|
||||
|
||||
|
||||
@schedule_admin_bp.route('/schedule-runs/<int:run_id>/receipt-raw-zip', methods=['GET'])
|
||||
@schedule_admin_bp.route('/schedule-runs/<int:run_id>/inbound-raw-zip', methods=['GET'])
|
||||
@require_admin
|
||||
def get_schedule_run_receipt_raw_zip(run_id: int):
|
||||
def get_schedule_run_inbound_raw_zip(run_id: int):
|
||||
"""
|
||||
下载收料 RPA 排程 run 的原始收料 Excel 打包(ZIP + manifest)。
|
||||
下载入站 RPA 排程 run 的原始入站 Excel 打包(ZIP + manifest)。
|
||||
|
||||
核心逻辑:对齐 inventory-raw-zip;展示名为 收料RPA-{日期}-{时刻}-收料.zip。
|
||||
核心逻辑:对齐 inventory-raw-zip;展示名为 入站RPA-{日期}-{时刻}-入站.zip。
|
||||
"""
|
||||
run = get_run(run_id)
|
||||
if not run:
|
||||
raise NotFoundError('Not found')
|
||||
zip_path, file_name = resolve_receipt_raw_zip_download(run)
|
||||
zip_path, file_name = resolve_inbound_raw_zip_download(run)
|
||||
return send_file(
|
||||
zip_path,
|
||||
as_attachment=True,
|
||||
@@ -144,31 +147,10 @@ def get_schedule_run_receipt_raw_zip(run_id: int):
|
||||
)
|
||||
|
||||
|
||||
@schedule_admin_bp.route('/inbound-report-files/<int:file_id>/resend', methods=['POST'])
|
||||
@require_admin
|
||||
def resend_receipt_report_file(file_id: int):
|
||||
"""管理员重发已归档的收料文件;禁止借重发重新触发 RPA 导出。"""
|
||||
report_file = db.session.get(FreightRpaInboundReportFile, file_id)
|
||||
if report_file is None or report_file.archive_deleted_at is not None:
|
||||
raise NotFoundError('Receipt report file not found')
|
||||
user = get_current_user()
|
||||
outcome = deliver_inbound_file(report_file.id, force=True)
|
||||
append_shipping_audit_event(
|
||||
event_type='shipping_inbound_report_notification_resent', aggregate_type='shipping_inbound_file',
|
||||
aggregate_id=str(report_file.id), actor_type='admin', actor_id=getattr(user, 'id', None),
|
||||
owner_user_id=getattr(user, 'id', None), unit_id=report_file.unit_run_id, result='success',
|
||||
after={'status': 'resent', 'owner_user_id': getattr(user, 'id', None)},
|
||||
artifact_refs=[{'kind': 'receipt_report', 'id': str(report_file.id), 'sha256': report_file.sha256}],
|
||||
metadata={'success_count': int(outcome.get('success_count', 0)), 'failed_count': int(outcome.get('failed_count', 0))},
|
||||
)
|
||||
db.session.commit()
|
||||
return jsonify({'success': True, 'data': outcome})
|
||||
|
||||
|
||||
@schedule_admin_bp.route('/inbound-rpa-units', methods=['GET'])
|
||||
@require_admin
|
||||
def list_receipt_rpa_units():
|
||||
"""管理员查看收料单元运行事实,供失败单元填写原因后人工重试。"""
|
||||
def list_inbound_rpa_units():
|
||||
"""管理员查看入站单元运行事实,供失败单元填写原因后人工重试。"""
|
||||
status = str(request.args.get('status') or '').strip().lower()
|
||||
if status and status not in {'queued', 'awaiting_rpa', 'processing', 'success', 'empty', 'failed', 'cancelled'}:
|
||||
from app.core.errors import ValidationError
|
||||
@@ -190,7 +172,7 @@ def list_receipt_rpa_units():
|
||||
aggregate_id=str(getattr(user, 'id', 'admin')), actor_type='admin', actor_id=getattr(user, 'id', None),
|
||||
owner_user_id=getattr(user, 'id', None), result='success',
|
||||
after={'visible_count': len(units), 'status': status or None, 'limit': limit},
|
||||
metadata={'surface': 'workspace_receipt_report_files'},
|
||||
metadata={'surface': 'admin_inbound_rpa_units'},
|
||||
)
|
||||
db.session.commit()
|
||||
return jsonify({'success': True, 'data': {'items': [{
|
||||
@@ -209,23 +191,23 @@ def list_receipt_rpa_units():
|
||||
|
||||
@schedule_admin_bp.route('/inbound-rpa-units/<int:unit_id>/retry', methods=['POST'])
|
||||
@require_admin
|
||||
def retry_receipt_rpa_unit(unit_id: int):
|
||||
"""管理员为失败收料单元创建独立重试事实;旧单元永不覆盖。"""
|
||||
def retry_inbound_rpa_unit(unit_id: int):
|
||||
"""管理员为失败入站单元创建独立重试事实;旧单元永不覆盖。"""
|
||||
failed = db.session.get(FreightRpaInboundUnitRun, unit_id)
|
||||
if failed is None:
|
||||
raise NotFoundError('Receipt RPA unit not found')
|
||||
raise NotFoundError('Inbound RPA unit not found')
|
||||
if failed.status not in {'failed', 'cancelled'}:
|
||||
raise ConflictError('Only failed receipt RPA units can be retried')
|
||||
raise ConflictError('Only failed inbound RPA units can be retried')
|
||||
data = request.get_json(silent=True) or {}
|
||||
reason = str(data.get('reason') or '').strip()
|
||||
if not reason:
|
||||
from app.core.errors import ValidationError
|
||||
raise ValidationError('reason is required', code='SHIPPING.RECEIPT_RETRY_REASON_REQUIRED')
|
||||
raise ValidationError('reason is required', code='SHIPPING.INBOUND_RETRY_REASON_REQUIRED')
|
||||
user = get_current_user()
|
||||
retry = FreightRpaInboundUnitRun(
|
||||
schedule_run_id=failed.schedule_run_id, source_id=failed.source_id,
|
||||
retry_of_id=failed.id, source_code=failed.source_code, connector_type=failed.connector_type,
|
||||
warehouse_id=failed.warehouse_id, callback_id=f'receipt:{uuid.uuid4().hex}', status='awaiting_rpa',
|
||||
warehouse_id=failed.warehouse_id, callback_id=f'inbound:{uuid.uuid4().hex}', status='awaiting_rpa',
|
||||
config_snapshot_json=dict(failed.config_snapshot_json or {}),
|
||||
)
|
||||
db.session.add(retry)
|
||||
@@ -234,7 +216,7 @@ def retry_receipt_rpa_unit(unit_id: int):
|
||||
profile = source.rpa_profile if source is not None else None
|
||||
if source is None or profile is None or not profile.enabled:
|
||||
db.session.rollback()
|
||||
raise ConflictError('Receipt RPA profile is unavailable')
|
||||
raise ConflictError('Inbound RPA profile is unavailable')
|
||||
snapshot = dict(retry.config_snapshot_json or {})
|
||||
trigger_unit = {
|
||||
'unit_run_id': retry.id, 'callback_id': retry.callback_id, 'source_code': retry.source_code,
|
||||
@@ -253,11 +235,11 @@ def retry_receipt_rpa_unit(unit_id: int):
|
||||
)
|
||||
if not trigger_path:
|
||||
db.session.rollback()
|
||||
raise ConflictError('Receipt RPA trigger directory is unavailable')
|
||||
raise ConflictError('Inbound RPA trigger directory is unavailable')
|
||||
run.status = 'running'
|
||||
run.finished_at = None
|
||||
summary = dict(run.response_summary_json or {})
|
||||
summary['awaiting_receipt_rpa'] = True
|
||||
summary['awaiting_inbound_rpa'] = True
|
||||
run.response_summary_json = summary
|
||||
append_shipping_audit_event(
|
||||
event_type='shipping_inbound_rpa_unit_retried', aggregate_type='shipping_inbound_unit',
|
||||
@@ -269,3 +251,136 @@ def retry_receipt_rpa_unit(unit_id: int):
|
||||
)
|
||||
db.session.commit()
|
||||
return jsonify({'success': True, 'data': {'unit_id': retry.id, 'retry_of_id': failed.id, 'callback_id': retry.callback_id, 'trigger_path': trigger_path}})
|
||||
|
||||
|
||||
@schedule_admin_bp.route('/schedule-runs/<int:run_id>/outbound-raw-zip', methods=['GET'])
|
||||
@require_admin
|
||||
def get_schedule_run_outbound_raw_zip(run_id: int):
|
||||
"""
|
||||
下载出站 RPA 排程 run 的原始出站 Excel 打包(ZIP + manifest)。
|
||||
|
||||
核心逻辑:对齐 inbound-raw-zip;展示名为 出站RPA-{日期}-{时刻}-出站.zip。
|
||||
"""
|
||||
run = get_run(run_id)
|
||||
if not run:
|
||||
raise NotFoundError('Not found')
|
||||
zip_path, file_name = resolve_outbound_raw_zip_download(run)
|
||||
return send_file(
|
||||
zip_path,
|
||||
as_attachment=True,
|
||||
download_name=file_name,
|
||||
mimetype='application/zip',
|
||||
)
|
||||
|
||||
|
||||
@schedule_admin_bp.route('/outbound-rpa-units', methods=['GET'])
|
||||
@require_admin
|
||||
def list_outbound_rpa_units():
|
||||
"""管理员查出站单元运行事实,供失败单元填写原因后人工重试。"""
|
||||
status = str(request.args.get('status') or '').strip().lower()
|
||||
if status and status not in {'queued', 'awaiting_rpa', 'processing', 'success', 'empty', 'failed', 'cancelled'}:
|
||||
from app.core.errors import ValidationError
|
||||
raise ValidationError('status is invalid')
|
||||
try:
|
||||
limit = max(1, min(int(request.args.get('limit') or 100), 200))
|
||||
except (TypeError, ValueError):
|
||||
from app.core.errors import ValidationError
|
||||
raise ValidationError('limit is invalid')
|
||||
query = FreightRpaOutboundUnitRun.query.order_by(
|
||||
FreightRpaOutboundUnitRun.created_at.desc(), FreightRpaOutboundUnitRun.id.desc(),
|
||||
)
|
||||
if status:
|
||||
query = query.filter(FreightRpaOutboundUnitRun.status == status)
|
||||
units = query.limit(limit).all()
|
||||
user = get_current_user()
|
||||
append_shipping_audit_event(
|
||||
event_type='shipping_outbound_rpa_units_listed', aggregate_type='shipping_outbound_unit_collection',
|
||||
aggregate_id=str(getattr(user, 'id', 'admin')), actor_type='admin', actor_id=getattr(user, 'id', None),
|
||||
owner_user_id=getattr(user, 'id', None), result='success',
|
||||
after={'visible_count': len(units), 'status': status or None, 'limit': limit},
|
||||
metadata={'surface': 'admin_outbound_rpa_units'},
|
||||
)
|
||||
db.session.commit()
|
||||
return jsonify({'success': True, 'data': {'items': [{
|
||||
'id': unit.id,
|
||||
'source_code': unit.source_code,
|
||||
'warehouse_id': unit.warehouse_id,
|
||||
'status': unit.status,
|
||||
'error_code': unit.error_code,
|
||||
'error_message': unit.error_message,
|
||||
'retry_of_id': unit.retry_of_id,
|
||||
'schedule_run_id': unit.schedule_run_id,
|
||||
'created_at': unit.created_at.isoformat() if unit.created_at else None,
|
||||
'finished_at': unit.finished_at.isoformat() if unit.finished_at else None,
|
||||
} for unit in units]}})
|
||||
|
||||
|
||||
@schedule_admin_bp.route('/outbound-rpa-units/<int:unit_id>/retry', methods=['POST'])
|
||||
@require_admin
|
||||
def retry_outbound_rpa_unit(unit_id: int):
|
||||
"""管理员为失败出站单元创建独立重试事实;旧单元永不覆盖。调拨按需单元无排程 run 时仍可重试。"""
|
||||
failed = db.session.get(FreightRpaOutboundUnitRun, unit_id)
|
||||
if failed is None:
|
||||
raise NotFoundError('Outbound RPA unit not found')
|
||||
if failed.status not in {'failed', 'cancelled'}:
|
||||
raise ConflictError('Only failed outbound RPA units can be retried')
|
||||
data = request.get_json(silent=True) or {}
|
||||
reason = str(data.get('reason') or '').strip()
|
||||
if not reason:
|
||||
from app.core.errors import ValidationError
|
||||
raise ValidationError('reason is required', code='SHIPPING.OUTBOUND_RETRY_REASON_REQUIRED')
|
||||
user = get_current_user()
|
||||
retry = FreightRpaOutboundUnitRun(
|
||||
schedule_run_id=failed.schedule_run_id, source_id=failed.source_id,
|
||||
retry_of_id=failed.id, source_code=failed.source_code, connector_type=failed.connector_type,
|
||||
warehouse_id=failed.warehouse_id, callback_id=f'outbound:{uuid.uuid4().hex}', status='awaiting_rpa',
|
||||
config_snapshot_json=dict(failed.config_snapshot_json or {}),
|
||||
)
|
||||
db.session.add(retry)
|
||||
db.session.flush()
|
||||
source = retry.source
|
||||
profile = source.rpa_profile if source is not None else None
|
||||
if source is None or profile is None or not profile.enabled:
|
||||
db.session.rollback()
|
||||
raise ConflictError('Outbound RPA profile is unavailable')
|
||||
snapshot = dict(retry.config_snapshot_json or {})
|
||||
trigger_unit = {
|
||||
'unit_run_id': retry.id, 'callback_id': retry.callback_id, 'source_code': retry.source_code,
|
||||
'connector_type': retry.connector_type, 'warehouse_id': retry.warehouse_id,
|
||||
'rpa_profile_id': profile.id, 'rpa_profile_code': profile.profile_code, 'login_url': profile.login_url,
|
||||
'login_config': dict(profile.login_config_json or {}),
|
||||
'credential_envelope': {'username_ciphertext': profile.username_ciphertext, 'password_ciphertext': profile.password_ciphertext},
|
||||
'report_path_json': dict(snapshot.get('report_path_json') or {}),
|
||||
'query_parameters_json': list(snapshot.get('query_parameters_json') or []), 'config_snapshot': snapshot,
|
||||
}
|
||||
retry.trigger_snapshot_json = trigger_unit
|
||||
run = retry.schedule_run
|
||||
run_no = run.run_no if run is not None else f'outbound-retry-{retry.id}'
|
||||
trigger_meta = {
|
||||
'schema_version': OUTBOUND_SCHEMA, 'retry_of_unit_id': failed.id,
|
||||
}
|
||||
if run is not None:
|
||||
trigger_meta['schedule_run_id'] = run.id
|
||||
trigger_meta['schedule_task_id'] = run.schedule_task_id
|
||||
trigger_path = write_outbound_export_batch_trigger(
|
||||
f'{run_no}-retry-{retry.id}', [trigger_unit], trigger_meta,
|
||||
)
|
||||
if not trigger_path:
|
||||
db.session.rollback()
|
||||
raise ConflictError('Outbound RPA trigger directory is unavailable')
|
||||
if run is not None:
|
||||
run.status = 'running'
|
||||
run.finished_at = None
|
||||
summary = dict(run.response_summary_json or {})
|
||||
summary['awaiting_outbound_rpa'] = True
|
||||
run.response_summary_json = summary
|
||||
append_shipping_audit_event(
|
||||
event_type='shipping_outbound_rpa_unit_retried', aggregate_type='shipping_outbound_unit',
|
||||
aggregate_id=str(retry.id), actor_type='admin', actor_id=getattr(user, 'id', None),
|
||||
owner_user_id=getattr(user, 'id', None), unit_id=retry.id, result='success', reason=reason,
|
||||
before={'status': failed.status, 'error_code': failed.error_code},
|
||||
after={'status': retry.status, 'source_run_no': retry.callback_id, 'owner_user_id': getattr(user, 'id', None)},
|
||||
metadata={'retry_of_id': failed.id},
|
||||
)
|
||||
db.session.commit()
|
||||
return jsonify({'success': True, 'data': {'unit_id': retry.id, 'retry_of_id': failed.id, 'callback_id': retry.callback_id, 'trigger_path': trigger_path}})
|
||||
Reference in new issue
Block a user