import json
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np


def plottable(log: list[tuple[float, int]]):
    if log:
        return np.array(log).transpose()
    else:
        return np.array([[], []])


def plot(fn: str):
    plt.rcParams['figure.figsize'] = [64, 18]
    plt.style.use("dark_background")
    plt.subplots_adjust(left=0.05, right=0.99, top=0.95, bottom=0.05)
    plt.title(fn)
    plt.xlabel('time (s)')
    plt.ylabel('concurrency (1)')

    with open(fn) as file:
        jsonified: dict = json.load(file)

    def logplot(plot_function, metric: str, **kwargs):
        if (log := jsonified.pop(metric, None)) is not None:
            plot_function(*plottable(log), label=f'{metric} ({len(log)})', **kwargs)

    logplot(plt.plot, 'DelayedResolver:sleep:concurrency')
    logplot(plt.plot, 'ActiveBinaryTree:add:concurrency')
    logplot(plt.plot, 'ActiveBinaryTree:contains:concurrency')
    logplot(plt.plot, 'Stack:list:concurrency')
    logplot(plt.scatter, 'ActiveBinaryTree:add:entry', c='tomato', zorder=100, s=.5)
    logplot(plt.scatter, 'ActiveBinaryTree:add:exit', c='gold', zorder=99, s=.5)
    plt.legend()
    plt.show()


if __name__ == '__main__':
    plot('trace/latest.json')
    for fp in list(Path('trace').glob('*.json')):
        if fp != Path('trace/latest.json'):
            plot(str(fp))