<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Finding flakiest test suites
The following produces a table of test suites ordered by flakiest suite first.
Step1:  Finding flakiest individual tests
Now we fetch the logs for each individual test and find the flakiest individual tests.
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
df = pd.DataFrame.from_dict(results)
# Transpose
df = df.T
# NaN -> 0
df = df.fillna(0)
df['pass_rate'] = df['success'] / (df['failed'] + df['success'])
# sort by failed
df = df.sort_values(by=["pass_rate"])
df
Explanation: Finding flakiest test suites
The following produces a table of test suites ordered by flakiest suite first.
End of explanation
import requests
cache = {}
build_info_cache = {}
build_map = {}
def parse_build(build):
    tests = []
    failed = set()
    passed = set()
    skipped = set()
    try:
        output_url = build["steps"][5]["actions"][0]["output_url"]
    except Exception as e:
        print(f"Got error: {e}, continuing...")
        return set(), set(), set()
    
    if output_url in cache:
        lines = cache[output_url]
    else:
        lines = requests.get(output_url).json()
        cache[output_url] = lines
    for line in lines[0]["message"].split("\n"):
        #print(line)
        if "RUN" in line:
            parts = line.split("RUN")
            if len(parts) != 2:
                continue
            preamble, test = parts
            test = test.strip()
            # ignore docker RUN lines which contain e.g. "#10"
            if "#" in preamble or "Step" in preamble:
                continue
            if len(test) > 100:
                # some base64 gunk
                continue
            if "AcceptEnv" in test or "cd pachyderm" in test \
                    or "git clone" in test or "NING" in test or "_BAD_TESTS" in test:
                continue
            tests.append(test)
        if "FAIL" in line:
            test = line.split("FAIL")[1].replace(":", "").strip().split("(")[0].strip()
            if "github.com" in test or test == "":
                # filter out some more noise
                continue
            if len(test) > 100:
                # some base64 gunk
                continue
            failed.add(test)
        if "PASS" in line:
            test = line.split("PASS")[1].replace(":", "").strip().split("(")[0].strip()
            if test == "":
                # This happens when all the tests pass, we get a "PASS" on its own.
                continue
            if len(test) > 100:
                # some base64 gunk
                continue
            if "\\n" in test:
                continue
            passed.add(test)
        if "SKIP" in line:
            test = line.split("SKIP")[1].replace(":", "").strip().split("(")[0].strip()
            if test == "":
                # This happens when all the tests pass, we get a "PASS" on its own.
                continue
            if len(test) > 100:
                # some base64 gunk
                continue
            if "\\n" in test:
                continue
            skipped.add(test)
    all_tests = set(tests)
    for test in all_tests:
        if test not in build_map:
            build_map[test] = build["workflows"]["job_name"], \
            f"<a target='_blank' href='{build['build_url']}'>{build['build_num']}</a>"
    hung = all_tests - failed - passed - skipped
    assert all_tests == (failed | passed | hung | skipped), \
        f"all={all_tests}, failed={failed}, passed={passed}, hung={hung}, skipped={skipped}"
    return passed, failed, hung
build_results = defaultdict(lambda: defaultdict(int))
for build in builds:
    print(".", end="")
    if build["build_num"] in build_info_cache:
        build_info = build_info_cache[build["build_num"]]
    else:
        build_info = circleci.get_build_info("pachyderm", "pachyderm", build["build_num"])
        build_info_cache[build["build_num"]] = build_info
    passed, failed, hung = parse_build(build_info)
    for b in passed:
        build_results[b]["pass"] += 1
        build_results[b]["bucket"] = build_map[b][0]
    for b in failed:
        build_results[b]["fail"] += 1
        build_results[b]["bucket"] = build_map[b][0]
        build_results[b]["example"] = build_map[b][1]
    for b in hung:
        build_results[b]["hung"] += 1
        build_results[b]["bucket"] = build_map[b][0]
        build_results[b]["example"] = build_map[b][1]
import pandas as pd
df = pd.DataFrame.from_dict(build_results)
# Transpose
df = df.T
# NaN -> 0
df = df.fillna(0)
df['hang_rate'] = df['hung'] / (df['pass'] + df['fail'] + df['hung'])
df['fail_rate'] = df['fail'] / (df['pass'] + df['fail'] + df['hung'])
df['hang_or_fail'] = df['hang_rate'] + df['fail_rate']
hangy = df[df["hang_rate"] > 0]
faily = df[df["fail_rate"] > 0]
# sort by failed
hangy = hangy.sort_values(by=["hang_rate"], ascending=False)
faily = hangy.sort_values(by=["fail_rate"], ascending=False)
bad = df[df["hang_or_fail"] > 0]
bad = bad.sort_values(by=["hang_or_fail"], ascending=False)
pandas.set_option('display.max_rows', None)
from IPython.display import HTML
HTML(bad.to_html(escape=False))
Explanation: Finding flakiest individual tests
Now we fetch the logs for each individual test and find the flakiest individual tests.
End of explanation
<END_TASK>