# -*- coding: utf-8 -*- """抽样现有 KpiMaster 的 Formula / CalcRule / DopFields,评估公式结构化改造的真实样本形态。""" from __future__ import annotations import sys import pymysql sys.stdout.reconfigure(encoding='utf-8') CONN = dict( host='123.60.180.165', port=3306, user='aidopremote', password='1234567890aiDOP#', database='aidopdev', charset='utf8mb4', autocommit=True, ) def main() -> None: conn = pymysql.connect(**CONN) with conn.cursor(pymysql.cursors.DictCursor) as cur: cur.execute( "SELECT COUNT(*) AS c, " " SUM(CASE WHEN Formula IS NOT NULL AND Formula<>'' THEN 1 ELSE 0 END) AS fc, " " SUM(CASE WHEN CalcRule IS NOT NULL AND CalcRule<>'' THEN 1 ELSE 0 END) AS cc, " " SUM(CASE WHEN DopFields IS NOT NULL AND DopFields<>'' THEN 1 ELSE 0 END) AS dc " "FROM ado_smart_ops_kpi_master WHERE IsEnabled=1" ) s = cur.fetchone() print(f"总启用指标: {s['c']} 有 Formula: {s['fc']} 有 CalcRule: {s['cc']} 有 DopFields: {s['dc']}") print("\n=== Formula 长度分布 ===") cur.execute( "SELECT LENGTH(Formula) AS L, COUNT(*) AS c FROM ado_smart_ops_kpi_master " "WHERE IsEnabled=1 AND Formula IS NOT NULL AND Formula<>'' GROUP BY L ORDER BY L" ) bins = {"0-40": 0, "41-80": 0, "81-150": 0, "151-400": 0, "400+": 0} for r in cur.fetchall(): L = r['L'] n = r['c'] if L <= 40: bins["0-40"] += n elif L <= 80: bins["41-80"] += n elif L <= 150: bins["81-150"] += n elif L <= 400: bins["151-400"] += n else: bins["400+"] += n for k, v in bins.items(): print(f" {k:>8} 字符 : {v}") print("\n=== 随机抽样 20 条(不同模块/层级) ===") cur.execute( "SELECT ModuleCode, MetricLevel, MetricCode, MetricName, Formula, CalcRule, DopFields " "FROM ado_smart_ops_kpi_master WHERE IsEnabled=1 AND TenantId=1300000000001 " " AND Formula IS NOT NULL AND Formula<>'' " "ORDER BY ModuleCode, MetricLevel, SortNo LIMIT 20" ) for r in cur.fetchall(): print(f"\n[{r['ModuleCode']} L{r['MetricLevel']}] {r['MetricCode']} {r['MetricName']}") print(f" Formula : {r['Formula']}") if r['CalcRule']: cr = r['CalcRule'].replace('\n', ' ') print(f" CalcRule : {cr[:120]}{'…' if len(cr)>120 else ''}") if r['DopFields']: print(f" DopFields: {r['DopFields']}") conn.close() if __name__ == "__main__": main()