def compress_target_paths()

in otava/graphite.py [0:0]


def compress_target_paths(paths: List[str]) -> List[str]:
    """Uses the alternative syntax to reduce the total length of the query"""
    result = []
    prefix_map = {}
    for p in paths:
        components = p.rsplit(".", 1)
        if len(components) == 1:
            result.append(p)
            continue

        prefix = components[0]
        suffix = components[1]
        if prefix not in prefix_map:
            prefix_map[prefix] = [suffix]
        else:
            prefix_map[prefix].append(suffix)

    for prefix, suffixes in prefix_map.items():
        if len(suffixes) > 1:
            result.append(prefix + ".{" + ",".join(suffixes) + "}")
        else:
            result.append(prefix + "." + suffixes[0])

    return result