import re
def read(file_name):

# 定义正则表达式模式
    pattern = re.compile(r"time_count\.time_forward=(\d+\.\d+).*?time_count\.time_backward=(\d+\.\d+).*?time_count\.time_memory_updater=(\d+\.\d+).*?time_count\.time_embedding=(\d+\.\d+).*?time_count\.time_local_update=(\d+\.\d+).*?time_count\.time_memory_sync=(\d+\.\d+).*?time_count\.time_sample_and_build=(\d+\.\d+).*?time_count\.time_memory_fetch=(\d+\.\d+)")
    pattern = re.compile(r"time_count\.time_forward=(\d+\.\d+).*?time_count\.time_backward=(\d+\.\d+).*?time_count\.time_memory_updater=(\d+\.\d+).*?time_count\.time_embedding=(\d+\.\d+).*?time_count\.time_local_update=(\d+).*?time_count\.time_memory_sync=(\d+\.\d+).*?time_count\.time_sample_and_build=(\d+\.\d+).*?time_count\.time_memory_fetch=(\d+\.\d+)")

    # 读取日志文件
    with open(file_name, 'r') as file:
        log_content = file.read()
    #print(log_content)

    # 查找所有匹配的行
    matches = pattern.findall(log_content)
    # 初始化累加器
    forward_times = []
    backward_times = []
    memory_updater_times = []
    embedding_times = []
    local_update_times = []
    memory_sync_times = []
    sample_and_build_times = []
    memory_fetch_times = []

    # 提取数据并累加
    for match in matches:

        forward_times.append(float(match[0]))
        backward_times.append(float(match[1]))
        memory_updater_times.append(float(match[2]))
        embedding_times.append(float(match[3]))
        local_update_times.append(float(match[4]))
        memory_sync_times.append(float(match[5]))
        sample_and_build_times.append(float(match[6]))
        memory_fetch_times.append(float(match[7]))

    # 计算平均值
    def calculate_average(times):
        return sum(times) / len(times) if times else 0

    average_forward = calculate_average(forward_times)
    average_backward = calculate_average(backward_times)
    average_memory_updater = calculate_average(memory_updater_times)
    average_embedding = calculate_average(embedding_times)
    average_local_update = calculate_average(local_update_times)
    average_memory_sync = calculate_average(memory_sync_times)
    average_sample_and_build = calculate_average    (sample_and_build_times)
    average_memory_fetch = calculate_average(memory_fetch_times)
    averages = {
        'average_forward': average_forward,
        'average_backward': average_backward,
        'average_memory_updater': average_memory_updater,
        'average_embedding': average_embedding,
        'average_local_update': average_local_update,
        'average_memory_sync': average_memory_sync,
        'average_sample_and_build': average_sample_and_build,
        'average_memory_fetch':average_memory_fetch,
    }
    return averages

import csv


def write_to_csv(data,theta_list, output_file):
    model = "TGN" if data == "LASTFM" else "TGN_large"
    neighbor = "10" if data == "LASTFM" else "20"
    # 定义CSV文件的列名
    fieldnames = [
        'theta',
        'average_forward',
        'average_backward',
        'average_memory_updater',
        'average_embedding',
        'average_local_update',
        'average_memory_sync',
        'average_sample_and_build',
        'average_memory_fetch'
    ]

    # 打开CSV文件进行写操作
    with open(output_file, 'w', newline='') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        
        # 写入表头
        writer.writeheader()
        
        # 遍历每个theta值
        for theta in theta_list:
            print("theta:",theta)
            file_name = f"MemShare_v1/all_12357/{data}/{model}/8-ours-0.1-historical-0.3-boundery_recent_decay-{theta}-{neighbor}.out"
            averages = read(file_name)
            averages['theta'] = theta  # 添加theta列
            writer.writerow(averages)

# 示例theta参数列表
theta_list = ["0.2","0.4","0.6","0.8"]

# 输出CSV文件
data = "WikiTalk"
output_file = f"{data}_averages.csv"

# 调用函数写入CSV
write_to_csv(data,theta_list, output_file)
