from collections import Counter
from ripe.atlas.sagan import TracerouteResult
from pathpattern import PathPattern
SUBLEN = 16  # assume that 16 is the pid period
TRACE_INTV = 1800  # the built-in traceroute interval is 30min


def sagan_ip_path(trace):
    """given an sagan traceroute measurement obj,
    return a list of IP hop, each hop contain only one IP address.

    Args:
        trace (ripe.atlas.sagan.traceroute.TracerouteResult): one single instance of traceroute measurement;
            in sagan object, for each hop, have at most three IP address.

    Returns:
        single_path (list): e.x. ['137.194.164.254', '137.194.4.240', '212.73.200.45', ...]
            that is one IP per hop
    """
    single_path = []
    full_path = trace.ip_path
    for hops in full_path:
        single_path.append(Counter(hops).most_common(1)[0][0])
    return single_path


def trace_formatter(res_json):
    """format mutiple traceroute measurements in json

    Args:
        res_json (list): list of traceroute measurments in dict(json) format

    Returns:
        res_list (list): list of ripe.atlas.sagan.traceroute.TracerouteResult
    """
    res_list = []
    for rec in res_json:
        rec_sagan = TracerouteResult(rec)
        res_list.append(rec_sagan)
    return res_list


def polish(path_list):
    """given a list of such tuple (pid, path, timestamp),
    pad the list where pid is not continuous

    Args: