from os import *

import psutil

from utils.log.trans_log import logger


def print_memory_usage(detail=""):
    # 获取当前进程ID
    pid = getpid()
    # 获取进程信息
    py = psutil.Process(pid)
    # 获取内存信息
    memory_info = py.memory_info()
    # RSS (Resident Set Size) 是进程实际占用的物理内存大小
    memory_usage_rss = memory_info.rss
    # VMS (Virtual Memory Size) 是进程使用的虚拟内存大小
    memory_usage_vms = memory_info.vms

    # 将字节转换为更易读的单位
    memory_usage_rss_mb = memory_usage_rss / (1024 ** 2)
    memory_usage_vms_mb = memory_usage_vms / (1024 ** 2)

    logger.info(f"{detail},Memory usage (RSS): {memory_usage_rss_mb:.2f} MB")
    logger.info(f"{detail},Memory usage (VMS): {memory_usage_vms_mb:.2f} MB")


def get_cpu_count():
    return psutil.cpu_count()


def get_available_cpu_count_with_percent(percent: float = 1):
    cpu_count = get_cpu_count()
    return int(cpu_count * percent)


def get_file_size(file_path):
    return path.getsize(file_path)


def get_dir_size(dir_path):
    return sum(get_file_size(path.join(dir_path, file)) for file in listdir(dir_path) if
               path.isfile(path.join(dir_path, file)))


def get_available_memory_with_percent(percent: float = 1):
    memory_info = psutil.virtual_memory()
    return int(memory_info.available * percent)


def get_max_file_size(file_paths: list[str]):
    max_size = 0
    for file_path in file_paths:
        file_size = get_file_size(file_path)
        if file_size > max_size:
            max_size = file_size
    return max_size


def use_files_get_max_cpu_count(file_paths: list[str], memory_percent: float = 1 / 12, cpu_percent: float = 2 / 5):
    max_file_size = get_max_file_size(file_paths)
    free_memory = get_available_memory_with_percent(memory_percent)
    count = int(free_memory / max_file_size)
    max_cpu_count = get_available_cpu_count_with_percent(cpu_percent)
    result = count if count <= max_cpu_count else max_cpu_count
    if result == 0:
        result = 1

    if result > len(file_paths):
        result = len(file_paths)

    logger.info(f"总文件数:{len(file_paths)},获取最大文件大小:{str(round(max_file_size / 2 ** 20, 2))}M"
                f"可用内存:{str(get_available_memory_with_percent(1) / 2 ** 20)}M"
                f"总CPU数:{get_cpu_count()}CPU使用比例:{round(cpu_percent, 2)}"
                f"CPU可用数量:{max_cpu_count},最终确定使用进程数:{result}")
    return result


def max_file_size_get_max_cpu_count(max_file_size, memory_percent: float = 1 / 6, cpu_percent: float = 2 / 5):
    free_memory = get_available_memory_with_percent(memory_percent)
    count = int(free_memory / max_file_size)
    max_cpu_count = get_available_cpu_count_with_percent(cpu_percent)
    result = count if count <= max_cpu_count else max_cpu_count
    if result == 0:
        result = 1
    logger.info(f"获取最大文件大小:{str(round(max_file_size / 2 ** 20, 2))}M"
                f"可用内存:{str(get_available_memory_with_percent(1) / 2 ** 20)}M"
                f"总CPU数:{get_cpu_count()}CPU使用比例:{round(cpu_percent, 2)}"
                f"CPU可用数量:{max_cpu_count},最终确定使用进程数:{result}")
    return result
