run_uat_kpi_factory_scope.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python3
  2. """Normalize generated UAT KPI business facts to dashboard factory scope 1."""
  3. from __future__ import annotations
  4. import argparse
  5. import json
  6. from apply_sql_file import connect
  7. TENANTS = (838257186181189, 838257212780613, 838257237606469)
  8. TABLES = (
  9. ("crm_seorder", "bill_no"),
  10. ("crm_seorderentry", "bill_no"),
  11. )
  12. def apply() -> dict[str, int]:
  13. conn = connect()
  14. counts: dict[str, int] = {}
  15. try:
  16. conn.begin()
  17. with conn.cursor() as cursor:
  18. placeholders = ",".join(["%s"] * len(TENANTS))
  19. for table, biz_column in TABLES:
  20. cursor.execute(
  21. f"""
  22. UPDATE `{table}`
  23. SET factory_id=1
  24. WHERE tenant_id IN ({placeholders})
  25. AND (`{biz_column}` LIKE 'UAT%%' OR `{biz_column}` LIKE 'DEMO-SO-%%')
  26. AND COALESCE(NULLIF(factory_id,0),1)<>1
  27. """,
  28. TENANTS,
  29. )
  30. counts[table] = cursor.rowcount
  31. conn.commit()
  32. except Exception:
  33. conn.rollback()
  34. raise
  35. finally:
  36. conn.close()
  37. return counts
  38. def main() -> int:
  39. parser = argparse.ArgumentParser()
  40. parser.add_argument("--apply", action="store_true")
  41. args = parser.parse_args()
  42. result = {"mode": "apply", "updated": apply()} if args.apply else {"mode": "dry-run", "tenants": TENANTS}
  43. print(json.dumps(result, ensure_ascii=False, indent=2))
  44. return 0
  45. if __name__ == "__main__":
  46. raise SystemExit(main())