from typing import OrderedDict


def parse_tuple_latency(tl_file):
    """
    Read the log of latencies and extract the relevant values for each time window,
    returning an ordered dictionary of instant latencies per component.
    """
    latencies = OrderedDict()
    header = {}
    ts = 0

    for line in open(tl_file):
        fields = line.split(',')

        if 'timestamp' in fields:
            for idx, field in enumerate(fields):
                header[field.strip()] = idx
            continue

        if len(fields) != len(header):
            print('Fields lenght mismatch')
            continue

        try:
            latencies[ts] = float(fields[header['latency']].strip())
        except KeyError:
            print("Error getting key for %s file" % tl_file)
            raise

        ts = ts + 1

    return latencies