import re


def parse_stats(output):
    """ Parses logcat output and returns the stats """
    lines = [line for line in output if "[Stats]" in line]
    stats = {
        'totals': {'time': 0, 'tasks': 0, 'avg': 0}
    }
    for line in lines:
        m = re.search(r'\((\d+) ms\).+\((\d+)\).+\((\d+) us.+\)', line)
        if not m:
            continue
        dt, tasks, avg = map(int, m.groups())
        if 'totals' in line:
            stats['totals'] = {'time': dt, 'tasks': tasks, 'avg': avg}
    return stats