_probe_formula_samples.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. """抽样现有 KpiMaster 的 Formula / CalcRule / DopFields,评估公式结构化改造的真实样本形态。"""
  3. from __future__ import annotations
  4. import sys
  5. import pymysql
  6. sys.stdout.reconfigure(encoding='utf-8')
  7. CONN = dict(
  8. host='123.60.180.165', port=3306,
  9. user='aidopremote', password='1234567890aiDOP#',
  10. database='aidopdev', charset='utf8mb4', autocommit=True,
  11. )
  12. def main() -> None:
  13. conn = pymysql.connect(**CONN)
  14. with conn.cursor(pymysql.cursors.DictCursor) as cur:
  15. cur.execute(
  16. "SELECT COUNT(*) AS c, "
  17. " SUM(CASE WHEN Formula IS NOT NULL AND Formula<>'' THEN 1 ELSE 0 END) AS fc, "
  18. " SUM(CASE WHEN CalcRule IS NOT NULL AND CalcRule<>'' THEN 1 ELSE 0 END) AS cc, "
  19. " SUM(CASE WHEN DopFields IS NOT NULL AND DopFields<>'' THEN 1 ELSE 0 END) AS dc "
  20. "FROM ado_smart_ops_kpi_master WHERE IsEnabled=1"
  21. )
  22. s = cur.fetchone()
  23. print(f"总启用指标: {s['c']} 有 Formula: {s['fc']} 有 CalcRule: {s['cc']} 有 DopFields: {s['dc']}")
  24. print("\n=== Formula 长度分布 ===")
  25. cur.execute(
  26. "SELECT LENGTH(Formula) AS L, COUNT(*) AS c FROM ado_smart_ops_kpi_master "
  27. "WHERE IsEnabled=1 AND Formula IS NOT NULL AND Formula<>'' GROUP BY L ORDER BY L"
  28. )
  29. bins = {"0-40": 0, "41-80": 0, "81-150": 0, "151-400": 0, "400+": 0}
  30. for r in cur.fetchall():
  31. L = r['L']
  32. n = r['c']
  33. if L <= 40: bins["0-40"] += n
  34. elif L <= 80: bins["41-80"] += n
  35. elif L <= 150: bins["81-150"] += n
  36. elif L <= 400: bins["151-400"] += n
  37. else: bins["400+"] += n
  38. for k, v in bins.items():
  39. print(f" {k:>8} 字符 : {v}")
  40. print("\n=== 随机抽样 20 条(不同模块/层级) ===")
  41. cur.execute(
  42. "SELECT ModuleCode, MetricLevel, MetricCode, MetricName, Formula, CalcRule, DopFields "
  43. "FROM ado_smart_ops_kpi_master WHERE IsEnabled=1 AND TenantId=1300000000001 "
  44. " AND Formula IS NOT NULL AND Formula<>'' "
  45. "ORDER BY ModuleCode, MetricLevel, SortNo LIMIT 20"
  46. )
  47. for r in cur.fetchall():
  48. print(f"\n[{r['ModuleCode']} L{r['MetricLevel']}] {r['MetricCode']} {r['MetricName']}")
  49. print(f" Formula : {r['Formula']}")
  50. if r['CalcRule']:
  51. cr = r['CalcRule'].replace('\n', ' ')
  52. print(f" CalcRule : {cr[:120]}{'…' if len(cr)>120 else ''}")
  53. if r['DopFields']:
  54. print(f" DopFields: {r['DopFields']}")
  55. conn.close()
  56. if __name__ == "__main__":
  57. main()