Files

39 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""数据质量稽核脚本(架构文档要求的 dq_check.py)。
运行方式:
docker compose exec erp python -m scripts.dq_check # 容器内
python -m scripts.dq_check # backend 目录下直连数据库
python -m scripts.dq_check --json # 机器可读输出(供 Dify/Agent 调用)
退出码:0 = 无 ERROR 级问题;1 = 存在 ERROR 级问题。
"""
import json
import sys
from app.database import SessionLocal
from app.services.dq import run_checks
def main():
as_json = "--json" in sys.argv
with SessionLocal() as db:
report = run_checks(db)
if as_json:
print(json.dumps(report, ensure_ascii=False, indent=2))
else:
print(f"数据质量稽核报告 {report['checked_at']}")
print("=" * 72)
for r in report["results"]:
mark = "PASS" if r["passed"] else ("WARN" if r["level"] == "WARN" else "FAIL")
print(f"[{mark}] {r['id']} {r['name']} 问题数={r['issue_count']}")
for s in r["samples"]:
print(f" - {s}")
print("=" * 72)
print(f"共 {report['total_rules']} 条规则:通过 {report['passed']},"
f"失败 {report['failed']},警告 {report['warned']}")
sys.exit(0 if report["healthy"] else 1)
if __name__ == "__main__":
main()