
import csv
import matplotlib.pyplot as plt
from collections import defaultdict


data = []

with open("results.csv", "r") as f:
    reader = csv.reader(f)
    next(reader)  # header
    for row in reader:
        structure, order, insert, find, delete = row
        data.append((structure, order, float(insert), float(find), float(delete)))


def group(metric_index):
    result = defaultdict(list)
    for s, o, ins, f, d in data:
        key = (s, o)
        result[key].append([ins, f, d][metric_index])
    return result


def save_plot(metric_name, index, filename):

    grouped = group(index)

    plt.figure()

    labels = []

    for (structure, order), values in grouped.items():
        label = f"{structure}-{order}"
        labels.append(label)
        plt.plot(values, label=label)

    plt.title(metric_name)
    plt.xlabel("Run")
    plt.ylabel("Time (sec)")
    plt.legend()

    plt.savefig(filename, dpi=300, bbox_inches="tight")
    plt.close()


save_plot("INSERT TIME", 0, "insert.png")
save_plot("FIND TIME", 1, "find.png")
save_plot("DELETE TIME", 2, "delete.png")

print("Графики сохранены: insert.png, find.png, delete.png")