split.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import yaml
  2. from pathlib import Path
  3. def ensure_structure(metrics_dict, full_dict, path):
  4. """确保每一级都包含name和priority字段"""
  5. if not isinstance(metrics_dict, dict):
  6. return metrics_dict
  7. # 从完整配置中获取当前路径的结构
  8. current = full_dict
  9. for key in path.split('.'):
  10. if key in current:
  11. current = current[key]
  12. else:
  13. break
  14. # 如果原结构中有name和priority,就保留它们
  15. result = {}
  16. if isinstance(current, dict):
  17. if 'name' in current:
  18. result['name'] = current['name']
  19. if 'priority' in current:
  20. result['priority'] = current['priority']
  21. # 添加自定义内容
  22. for key, value in metrics_dict.items():
  23. if key not in ['name', 'priority']:
  24. result[key] = ensure_structure(value, full_dict, f"{path}.{key}" if path else key)
  25. return result
  26. def find_custom_metrics(all_metrics, builtin_metrics, current_path=""):
  27. """递归比较两个配置,找出自定义指标"""
  28. custom_metrics = {}
  29. if isinstance(all_metrics, dict) and isinstance(builtin_metrics, dict):
  30. for key in all_metrics:
  31. if key not in builtin_metrics:
  32. # 完全新增的键,保留完整结构
  33. custom_metrics[key] = all_metrics[key]
  34. else:
  35. # 递归比较子结构
  36. child_custom = find_custom_metrics(
  37. all_metrics[key],
  38. builtin_metrics[key],
  39. f"{current_path}.{key}" if current_path else key
  40. )
  41. if child_custom:
  42. custom_metrics[key] = child_custom
  43. elif all_metrics != builtin_metrics:
  44. # 值不同的情况
  45. return all_metrics
  46. # 对结果进行结构调整,确保每层都有name和priority
  47. if custom_metrics:
  48. return ensure_structure(custom_metrics, all_metrics, current_path)
  49. return None
  50. def split_metrics_config(all_metrics_path, builtin_metrics_path, custom_metrics_path):
  51. # 加载完整的指标配置
  52. with open(all_metrics_path, 'r', encoding='utf-8') as f:
  53. all_metrics = yaml.safe_load(f) or {}
  54. # 加载内置指标配置作为基准
  55. with open(builtin_metrics_path, 'r', encoding='utf-8') as f:
  56. builtin_metrics = yaml.safe_load(f) or {}
  57. # 找出自定义指标
  58. custom_metrics = find_custom_metrics(all_metrics, builtin_metrics)
  59. # 保存自定义指标
  60. if custom_metrics:
  61. with open(custom_metrics_path, 'w', encoding='utf-8') as f:
  62. yaml.dump(custom_metrics, f, allow_unicode=True, sort_keys=False, indent=2)
  63. print(f"成功拆分指标配置:")
  64. print(f"- 内置指标已保存到: {builtin_metrics_path}")
  65. print(f"- 自定义指标已保存到: {custom_metrics_path}")
  66. print("\n自定义指标内容:")
  67. print(yaml.dump(custom_metrics, allow_unicode=True, sort_keys=False, indent=2))
  68. else:
  69. print("未发现自定义指标")
  70. if __name__ == "__main__":
  71. # 配置文件路径
  72. all_metrics_path = '/home/kevin/kevin/zhaoyuan/zhaoyuan_v2.0/zhaoyuan_new/config/all_metrics_config.yaml'
  73. builtin_metrics_path = '/home/kevin/kevin/zhaoyuan/zhaoyuan_v2.0/zhaoyuan_new/config/metrics_config.yaml'
  74. custom_metrics_path = '/home/kevin/kevin/zhaoyuan/zhaoyuan_v2.0/zhaoyuan_new/config/custom_metrics_config.yaml'
  75. # 确保文件存在
  76. if not Path(all_metrics_path).exists():
  77. raise FileNotFoundError(f"{all_metrics_path} 文件不存在")
  78. if not Path(builtin_metrics_path).exists():
  79. raise FileNotFoundError(f"{builtin_metrics_path} 文件不存在")
  80. # 执行拆分
  81. split_metrics_config(all_metrics_path, builtin_metrics_path, custom_metrics_path)