import json
from typing import Callable, Optional
from typing_extensions import Dict
import time

from models.data_thread import Data_Thread, Threads

def time_func(name: str, function_name: str, x : Callable, repeat : Optional[int] = None) -> Dict[str, float]:
    if (not repeat):
        a = time.perf_counter()
        r = x()
        timed = time.perf_counter()-a
        print(f"{name} took {timed:.3f}s")
        if r: print(f"|> {r}")
        return {function_name: timed}
        
    else:
        timings = []
        for _ in range(repeat):
            a = time.perf_counter()
            r = x()
            timings.append(time.perf_counter()-a)
        print(f"{name} took in average {sum(timings)/len(timings):.3f}s over {repeat} runs")
        if r: print(f"|> {r}")
        return {function_name: sum(timings)/len(timings)}
    

def preprocess_json():
    for x in range(0, 1):
        with open('data.json', 'r') as f:
            data : dict = json.load(f)
            for d in data:
                x = Data_Thread()
                for k in d.keys():
                    x.__setattr__(k, d[k])
                Threads.append(x)