import yaml
from pathlib import Path

def ensure_structure(metrics_dict, full_dict, path):
    """确保每一级都包含name和priority字段"""
    if not isinstance(metrics_dict, dict):
        return metrics_dict
    
    # 从完整配置中获取当前路径的结构
    current = full_dict
    for key in path.split('.'):
        if key in current:
            current = current[key]
        else:
            break
    
    # 如果原结构中有name和priority,就保留它们
    result = {}
    if isinstance(current, dict):
        if 'name' in current:
            result['name'] = current['name']
        if 'priority' in current:
            result['priority'] = current['priority']
    
    # 添加自定义内容
    for key, value in metrics_dict.items():
        if key not in ['name', 'priority']:
            result[key] = ensure_structure(value, full_dict, f"{path}.{key}" if path else key)
    
    return result

def find_custom_metrics(all_metrics, builtin_metrics, current_path=""):
    """递归比较两个配置,找出自定义指标"""
    custom_metrics = {}
    
    if isinstance(all_metrics, dict) and isinstance(builtin_metrics, dict):
        for key in all_metrics:
            if key not in builtin_metrics:
                # 完全新增的键,保留完整结构
                custom_metrics[key] = all_metrics[key]
            else:
                # 递归比较子结构
                child_custom = find_custom_metrics(
                    all_metrics[key], 
                    builtin_metrics[key],
                    f"{current_path}.{key}" if current_path else key
                )
                if child_custom:
                    custom_metrics[key] = child_custom
    elif all_metrics != builtin_metrics:
        # 值不同的情况
        return all_metrics
    
    # 对结果进行结构调整,确保每层都有name和priority
    if custom_metrics:
        return ensure_structure(custom_metrics, all_metrics, current_path)
    return None

def split_metrics_config(all_metrics_path, builtin_metrics_path, custom_metrics_path):
    # 加载完整的指标配置
    with open(all_metrics_path, 'r', encoding='utf-8') as f:
        all_metrics = yaml.safe_load(f) or {}
    
    # 加载内置指标配置作为基准
    with open(builtin_metrics_path, 'r', encoding='utf-8') as f:
        builtin_metrics = yaml.safe_load(f) or {}
    
    # 找出自定义指标
    custom_metrics = find_custom_metrics(all_metrics, builtin_metrics)
    
    # 保存自定义指标
    if custom_metrics:
        with open(custom_metrics_path, 'w', encoding='utf-8') as f:
            yaml.dump(custom_metrics, f, allow_unicode=True, sort_keys=False, indent=2)
        
        print(f"成功拆分指标配置:")
        print(f"- 内置指标已保存到: {builtin_metrics_path}")
        print(f"- 自定义指标已保存到: {custom_metrics_path}")
        print("\n自定义指标内容:")
        print(yaml.dump(custom_metrics, allow_unicode=True, sort_keys=False, indent=2))
    else:
        print("未发现自定义指标")

if __name__ == "__main__":
    # 配置文件路径
    all_metrics_path = '/home/kevin/kevin/zhaoyuan/zhaoyuan_v2.0/zhaoyuan_new/config/all_metrics_config.yaml'
    builtin_metrics_path = '/home/kevin/kevin/zhaoyuan/zhaoyuan_v2.0/zhaoyuan_new/config/metrics_config.yaml'
    custom_metrics_path = '/home/kevin/kevin/zhaoyuan/zhaoyuan_v2.0/zhaoyuan_new/config/custom_metrics_config.yaml'
    
    # 确保文件存在
    if not Path(all_metrics_path).exists():
        raise FileNotFoundError(f"{all_metrics_path} 文件不存在")
    if not Path(builtin_metrics_path).exists():
        raise FileNotFoundError(f"{builtin_metrics_path} 文件不存在")
    
    # 执行拆分
    split_metrics_config(all_metrics_path, builtin_metrics_path, custom_metrics_path)