def text_summary()

in pageload-summary/summarize.py [0:0]


def text_summary(summary, width=20, plat_width=50):
    """Outputs the two newest points of the summary as a table.

    Returns the results as a list that could be saved to a CSV file.

    Ex:

    Platform          | App       | Variant            | Type   | 04/12/2021 | 04/13/2021
    -------------------------------------------------------------------------------------
    linux64-shippable | firefox   | e10s               | cold   | 1900       | 1850
                      |           |                    | warm   | 800        | 750
                      |           -------------------------------------------------------
                      |           | webrender          | cold   |            |
                      |           |                    | warm   |            |
                      |           -------------------------------------------------------
                      |           | fission            | cold   |
                      |           |                    | warm   |
                      |           ------------------------------------------
                      |           | fission-webrender  | cold   |
                      |           |                    | warm   |
                      ------------------------------------------------------
                      | chrome    |                    |        |
                      ------------------------------------------------------
                      | chromium  |                    |        |
    ------------------------------------------------------------------------
    """

    csv_lines = []
    lines = []

    # Get the two newest data points, for tests without data at those points
    # we'll take the two newest data points they have regardless of date.
    all_times = []
    for platform, apps in summary.items():
        for app, variants in apps.items():
            for variant, pl_types in variants.items():
                for pl_type, data in pl_types.items():
                    # if len(data.get("moving_average", [])) > 0:
                    all_times.append(data["moving_average"][-1][0])
    sorted_times = sorted(list(set(all_times)))

    newest_point = sorted_times[-1]
    previous_point = newest_point
    if len(sorted_times) > 1:
        previous_point = sorted_times[-2]

    format_line = (
        "{:<{plat_width}}| {:<{width}}| {:<{width}}| {:<10}| {:<{width}}| {:<{width}}"
    )
    header_line = format_line.format(
        "Platform",
        "Application",
        "Variant",
        "Type",
        previous_point,
        newest_point,
        width=width,
        plat_width=plat_width,
    )
    table_len = len(header_line)
    lines.append(header_line)
    lines.append("-" * table_len)

    csv_lines.append(
        ["Platform", "Application", "Variant", "Type", previous_point, newest_point]
    )

    platform_output = False
    app_output = False
    variant_output = False

    for platform, apps in sorted(summary.items()):

        if platform_output:
            lines.append("-" * table_len)
        if len(platform) >= plat_width:
            platform = platform[: plat_width - 1]

        platform_output = False
        app_output = False
        variant_output = False
        for app, variants in sorted(apps.items(), reverse=1):

            if app_output:
                spacer = width * 2
                lines.append(" " * spacer + "-" * (table_len - spacer))

            app_output = False
            variant_output = False
            for variant, pl_types in sorted(variants.items(), reverse=1):
                if app in ("chrome", "chromium"):
                    variant = ""

                if variant_output:
                    spacer = width * 3
                    lines.append(" " * spacer + "-" * (table_len - spacer))

                variant_output = False
                for pl_type, data in pl_types.items():
                    platform_str = platform
                    app_str = app
                    variant_str = variant

                    if platform_output:
                        platform_str = ""
                    if variant_output:
                        variant_str = ""
                    if app_output:
                        app_str = ""

                    cur = np.round(data["moving_average"][-1][1], 2)
                    prev = cur
                    if len(data["moving_average"]) > 1:
                        prev = np.round(data["moving_average"][-2][1], 2)

                    if prev > 0.0:
                        delta = f" ({np.round(cur/prev,4)})"
                    else:
                        delta = " (NaN)"
                    lines.append(
                        format_line.format(
                            platform_str,
                            app_str,
                            variant_str,
                            pl_type,
                            prev,
                            str(cur) + delta,
                            width=width,
                            plat_width=plat_width,
                        )
                    )
                    csv_lines.append([platform, app, variant, pl_type, prev, cur])

                    if not variant_output:
                        variant_output = True
                    if not app_output:
                        app_output = True
                    if not platform_output:
                        platform_output = True

    for line in lines:
        print(line)

    return csv_lines