feat(shipping-notice): 添加识别定义和规则集管理功能,支持客户和站点的可配置识别规则
This commit is contained in:
1 parent
88408490d7
commit
34d489a2d1
167 files changed
+10365
-7978
No files matched your search
@@ -1,147 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
backend/scripts/backfill_shipping_task_lines.py
|
||||
|
||||
用途:将存量 ShippingTask.lines_json 幂等同步到 shipping_task_lines 规范表。
|
||||
依赖:app.create_app、models.ShippingTask、task_line_store.sync_task_order_lines_from_json。
|
||||
逻辑:运维 CLI;默认 dry-run;支持分页、断点续跑、单 Task 修复与失败汇总。
|
||||
|
||||
Usage:
|
||||
cd backend
|
||||
python scripts/backfill_shipping_task_lines.py --dry-run
|
||||
python scripts/backfill_shipping_task_lines.py --apply
|
||||
python scripts/backfill_shipping_task_lines.py --apply --task-no SN-xxx
|
||||
python scripts/backfill_shipping_task_lines.py --apply --after-id 1200 --limit 200
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
BACKEND_ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(BACKEND_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(BACKEND_ROOT))
|
||||
|
||||
from app import create_app, db # noqa: E402
|
||||
from app.models import ShippingTask # noqa: E402
|
||||
from app.services.shipping_notice.task_line_store import ( # noqa: E402
|
||||
sync_task_order_lines_from_json,
|
||||
)
|
||||
|
||||
|
||||
def _query_tasks(*, after_id: int, limit: int, task_no: str | None) -> list[ShippingTask]:
|
||||
"""按游标分页加载待回填任务。"""
|
||||
query = ShippingTask.query.order_by(ShippingTask.id.asc())
|
||||
if task_no:
|
||||
query = query.filter(ShippingTask.task_no == task_no)
|
||||
else:
|
||||
query = query.filter(ShippingTask.id > after_id)
|
||||
if limit > 0:
|
||||
query = query.limit(limit)
|
||||
return list(query.all())
|
||||
|
||||
|
||||
def backfill_shipping_task_lines(
|
||||
*,
|
||||
dry_run: bool = True,
|
||||
after_id: int = 0,
|
||||
limit: int = 200,
|
||||
task_no: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
执行一页(或单 Task)订单行回填。
|
||||
|
||||
核心逻辑:逐任务调用 sync_task_order_lines_from_json;单任务失败隔离,
|
||||
apply 模式下仅成功任务提交,失败任务 rollback 到 savepoint。
|
||||
|
||||
Returns:
|
||||
含 counts、failures、last_id 的汇总字典。
|
||||
"""
|
||||
tasks = _query_tasks(after_id=after_id, limit=limit, task_no=task_no)
|
||||
summary: dict[str, Any] = {
|
||||
'dry_run': dry_run,
|
||||
'scanned': len(tasks),
|
||||
'synced': 0,
|
||||
'skipped': 0,
|
||||
'failed': 0,
|
||||
'last_id': after_id,
|
||||
'failures': [],
|
||||
'results': [],
|
||||
}
|
||||
|
||||
for task in tasks:
|
||||
summary['last_id'] = int(task.id)
|
||||
nested = db.session.begin_nested()
|
||||
try:
|
||||
result = sync_task_order_lines_from_json(task, dry_run=dry_run)
|
||||
status = str(result.get('status') or 'failed')
|
||||
entry = {
|
||||
'task_id': task.id,
|
||||
'task_no': task.task_no,
|
||||
**result,
|
||||
}
|
||||
summary['results'].append(entry)
|
||||
if status == 'synced':
|
||||
summary['synced'] += 1
|
||||
if not dry_run:
|
||||
nested.commit()
|
||||
else:
|
||||
nested.rollback()
|
||||
elif status == 'skipped':
|
||||
summary['skipped'] += 1
|
||||
nested.rollback()
|
||||
else:
|
||||
summary['failed'] += 1
|
||||
summary['failures'].append(entry)
|
||||
nested.rollback()
|
||||
except Exception as exc: # noqa: BLE001 — 回填必须隔离单条失败
|
||||
nested.rollback()
|
||||
entry = {
|
||||
'task_id': task.id,
|
||||
'task_no': task.task_no,
|
||||
'status': 'failed',
|
||||
'reason': str(exc),
|
||||
}
|
||||
summary['failed'] += 1
|
||||
summary['failures'].append(entry)
|
||||
summary['results'].append(entry)
|
||||
|
||||
if not dry_run:
|
||||
db.session.commit()
|
||||
else:
|
||||
db.session.rollback()
|
||||
return summary
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""CLI 入口:解析参数并打印 JSON 汇总。"""
|
||||
parser = argparse.ArgumentParser(description='Backfill shipping_task_lines from lines_json')
|
||||
parser.add_argument('--dry-run', action='store_true', help='只校验不写入(默认)')
|
||||
parser.add_argument('--apply', action='store_true', help='实际写入规范表')
|
||||
parser.add_argument('--after-id', type=int, default=0, help='断点续跑游标(不含)')
|
||||
parser.add_argument('--limit', type=int, default=200, help='本页处理数量;0 表示不限')
|
||||
parser.add_argument('--task-no', type=str, default=None, help='仅处理指定 task_no')
|
||||
args = parser.parse_args()
|
||||
|
||||
dry_run = not bool(args.apply)
|
||||
if args.dry_run:
|
||||
dry_run = True
|
||||
|
||||
app = create_app()
|
||||
with app.app_context():
|
||||
summary = backfill_shipping_task_lines(
|
||||
dry_run=dry_run,
|
||||
after_id=int(args.after_id or 0),
|
||||
limit=int(args.limit),
|
||||
task_no=(str(args.task_no).strip() or None) if args.task_no else None,
|
||||
)
|
||||
print(json.dumps(summary, ensure_ascii=False, indent=2))
|
||||
return 1 if summary['failed'] else 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
@@ -61,7 +61,6 @@ def _source_payload(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
for key in ("program_group", "program_label", "report_group", "report_name")
|
||||
} if connector_type == "dbs" else {}),
|
||||
"query_parameters_json": parameters,
|
||||
"match_aliases_json": [str(alias).strip() for alias in raw.get("match_aliases") or [] if str(alias).strip()],
|
||||
}
|
||||
|
||||
|
||||
@@ -139,7 +138,6 @@ def import_sources(path: Path) -> tuple[int, int]:
|
||||
source.fixed_warehouse_id = data["fixed_warehouse_id"]
|
||||
source.report_path_json = data["report_path_json"]
|
||||
source.query_parameters_json = data["query_parameters_json"]
|
||||
source.match_aliases_json = data["match_aliases_json"]
|
||||
source.snapshot_ttl_seconds = 86400
|
||||
# Templates are not present in legacy YAML; activation requires published bindings.
|
||||
source.enabled = False
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
backend/scripts/verify_shipping_task_lines.py
|
||||
|
||||
用途:比对 ShippingTask.lines_json 与 shipping_task_lines 投影,输出影子差异报告。
|
||||
依赖:app.create_app、models.ShippingTask、task_line_store.compare_task_order_line_projections。
|
||||
逻辑:运维 CLI;切 READ_MODE=table 前必须全量 equal;支持分页与单 Task。
|
||||
|
||||
Usage:
|
||||
cd backend
|
||||
python scripts/verify_shipping_task_lines.py
|
||||
python scripts/verify_shipping_task_lines.py --task-no SN-xxx
|
||||
python scripts/verify_shipping_task_lines.py --after-id 1200 --limit 200
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
BACKEND_ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(BACKEND_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(BACKEND_ROOT))
|
||||
|
||||
from app import create_app # noqa: E402
|
||||
from app.models import ShippingTask # noqa: E402
|
||||
from app.services.shipping_notice.task_line_store import ( # noqa: E402
|
||||
compare_task_order_line_projections,
|
||||
)
|
||||
|
||||
|
||||
def _query_tasks(*, after_id: int, limit: int, task_no: str | None) -> list[ShippingTask]:
|
||||
"""按游标分页加载待比对任务。"""
|
||||
query = ShippingTask.query.order_by(ShippingTask.id.asc())
|
||||
if task_no:
|
||||
query = query.filter(ShippingTask.task_no == task_no)
|
||||
else:
|
||||
query = query.filter(ShippingTask.id > after_id)
|
||||
if limit > 0:
|
||||
query = query.limit(limit)
|
||||
return list(query.all())
|
||||
|
||||
|
||||
def verify_shipping_task_lines(
|
||||
*,
|
||||
after_id: int = 0,
|
||||
limit: int = 200,
|
||||
task_no: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
比对一页(或单 Task)JSON 快照与规范表投影。
|
||||
|
||||
Returns:
|
||||
含 scanned/equal/mismatch/mismatches/last_id 的汇总。
|
||||
"""
|
||||
tasks = _query_tasks(after_id=after_id, limit=limit, task_no=task_no)
|
||||
summary: dict[str, Any] = {
|
||||
'scanned': len(tasks),
|
||||
'equal': 0,
|
||||
'mismatch': 0,
|
||||
'last_id': after_id,
|
||||
'mismatches': [],
|
||||
}
|
||||
|
||||
for task in tasks:
|
||||
summary['last_id'] = int(task.id)
|
||||
comparison = compare_task_order_line_projections(task)
|
||||
if comparison.get('equal'):
|
||||
summary['equal'] += 1
|
||||
continue
|
||||
summary['mismatch'] += 1
|
||||
summary['mismatches'].append(comparison)
|
||||
return summary
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""CLI 入口:输出 JSON 汇总;存在 mismatch 时退出码为 1。"""
|
||||
parser = argparse.ArgumentParser(description='Verify shipping_task_lines vs lines_json')
|
||||
parser.add_argument('--after-id', type=int, default=0)
|
||||
parser.add_argument('--limit', type=int, default=200)
|
||||
parser.add_argument('--task-no', type=str, default=None)
|
||||
args = parser.parse_args()
|
||||
|
||||
app = create_app()
|
||||
with app.app_context():
|
||||
summary = verify_shipping_task_lines(
|
||||
after_id=args.after_id,
|
||||
limit=args.limit,
|
||||
task_no=args.task_no,
|
||||
)
|
||||
print(json.dumps(summary, ensure_ascii=False, indent=2))
|
||||
return 1 if summary['mismatch'] else 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user