import re


def extract_aogm_info(tra_log_file):
    """ Extracts from the TRA log file the number of different tracking errors.

    Args:
        tra_log_file: path to the tra log file

    Returns: dict containing tracking errors overall count and over time

    """
    pattern = re.compile('\[*T=\d+')
    with open(tra_log_file, "r") as f:
        lines = f.readlines()

        error_counts = {'EdgeSplit': 0,
                        'EdgeAdd': 0,
                        'EdgeDelete': 0,
                        'EdgeSemantic': 0,
                        'FP': 0,
                        'FN': 0}

        errors_over_t = {'EdgeSplit': {},  # time id: value
                         'EdgeAdd': {},
                         'EdgeDelete': {},
                         'EdgeSemantic': {},
                         'FP': {},
                         'FN': {}}
        counter_key = None
        for line in lines:
            match = re.match(pattern, line)
            if match is not None and counter_key is not None:
                time_id = int(match.group().split('=')[-1])
                if time_id not in errors_over_t[counter_key].keys():
                    errors_over_t[counter_key][time_id] = 0
                errors_over_t[counter_key][time_id] += 1

            if 'Splitting' in line:
                counter_key = 'EdgeSplit'
            elif 'False Negative Vertices' in line:
                counter_key = 'FN'
            elif 'False Positive Vertices' in line:
                counter_key = 'FP'
            elif 'Redundant Edges' in line:
                counter_key = 'EdgeDelete'
            elif 'Edges To Be Added' in line:
                counter_key = 'EdgeAdd'
            elif 'Edges with Wrong Semantics' in line:
                counter_key = 'EdgeSemantic'
            elif line.startswith('==='):
                break
            else:
                if counter_key is not None:
                    error_counts[counter_key] += 1
        aogm_measure = {'counts': error_counts, 't': errors_over_t}

    return aogm_measure