#!/usr/bin/env python3
"""Turn bench results into tables.

    report.py RESULTS_DIR            writes RESULTS_DIR/summary.csv and RESULTS_DIR/report-tables.md
    from report import load, sections   (make_page.py uses these to build the website tables)

RESULTS_DIR holds <server>-main-<n>.json (a full run) and <server>-scan-<n>.json (a scan-only run).
Every cell lists every run, so nothing is averaged away: "a ; b" is run 1 ; run 2. Latency cells are
median/p95 in ms. The value used to find where Leyr is not the best is the median of the runs' medians
(lower is better in every row).
"""
import csv, glob, json, os, statistics, sys

SERVERS = ["leyr", "navidrome", "gonic", "jellyfin", "plex"]
NAMES = {"leyr": "Leyr", "navidrome": "Navidrome", "gonic": "gonic", "jellyfin": "Jellyfin", "plex": "Plex"}
RUNS = [1, 2]
OGG = "4f676753"


def load(d):
    main, scans = {}, {}
    for s in SERVERS:
        for n in RUNS:
            p = f"{d}/{s}-main-{n}.json"
            if os.path.exists(p):
                main.setdefault(s, {})[n] = json.load(open(p))
        files = sorted(glob.glob(f"{d}/{s}-main-*.json") + glob.glob(f"{d}/{s}-scan-*.json"))
        if files:
            scans[s] = [json.load(open(p))["scan_run"] for p in files]
    return main, scans


def f(x, d=1):
    return "n/a" if x is None else f"{x:,.{d}f}"


def med(xs):
    xs = [x for x in xs if x is not None]
    return statistics.median(xs) if xs else None


def sections(main, scans):
    """A list of sections: dict(title, note, rows=[dict(label, cells={server: text}, val={server: number}, csv={})])."""
    srv = [s for s in SERVERS if s in main]
    out = []

    def runs(s, fn):
        return [fn(main[s][n]) for n in RUNS if n in main[s]]

    def sec(title, note=""):
        d = {"title": title, "note": note, "rows": []}
        out.append(d)
        return d

    def row(d, label, cells, val=None, csv=None):
        d["rows"].append({"label": label, "cells": cells, "val": val or {}, "csv": csv or {}})

    d = sec("Scan of the library", "Seconds from trigger to finished and quiet. Every scan run is listed (the full runs count as scan runs).")
    cells, val, cv = {}, {}, {}
    for s in srv:
        t = [r.get("scan_s_log", r["scan_s_from_trigger"]) for r in scans[s]]
        cells[s] = ", ".join(f(x) for x in t) + f" (median {f(statistics.median(t))})"
        val[s] = statistics.median(t); cv[s] = t
    row(d, "Scan time (s)", cells, val, cv)
    cells, val, cv = {}, {}, {}
    for s in srv:
        t = [r["rss_peak_mb"] for r in scans[s]]
        cells[s] = ", ".join(f(x, 0) for x in t); val[s] = statistics.median(t); cv[s] = t
    row(d, "Peak memory during scan (MB)", cells, val, cv)
    cells, val, cv = {}, {}, {}
    for s in srv:
        t = [r["anon_peak_mb"] for r in scans[s] if r.get("anon_peak_mb") is not None]
        cells[s] = ", ".join(f(x, 0) for x in t) if t else "n/a"
        if t:
            val[s] = statistics.median(t); cv[s] = t
    row(d, "Private memory, peak during scan (MB)", cells, val, cv)
    row(d, "Tracks indexed", {s: str(scans[s][0]["tracks"]) for s in srv})

    d = sec("Memory and threads", "Resident memory (RSS) summed over every process in the container. Private memory is the Anonymous total of each process's smaps_rollup, summed the same way: heap, stacks and every other page the process owns that is not backed by a file, so it leaves out the executable, libraries and mapped database files, which the kernel can take back whenever it needs memory. n/a: the run was made before this was recorded. Run 1 ; run 2.")
    for label, fn, dd in (("Idle memory, 5 min after the scan (MB)", lambda r: r["idle"]["rss_mb"], 0),
                          ("Idle private memory (MB)", lambda r: r["idle"].get("anon_mb"), 0),
                          ("Idle threads", lambda r: r["idle"]["threads"], 0),
                          ("Memory under load, median (MB)", lambda r: r["rss_under_load"]["rss_median_mb"], 0),
                          ("Memory under load, peak (MB)", lambda r: r["rss_under_load"]["rss_peak_mb"], 0),
                          ("Private memory under load, peak (MB)", lambda r: r["rss_under_load"].get("anon_peak_mb"), 0)):
        have = {s: [x for x in runs(s, fn) if x is not None] for s in srv}
        row(d, label, {s: (" ; ".join(f(x, dd) for x in have[s]) or "n/a") for s in srv},
            {s: med(have[s]) for s in srv if have[s]}, {s: have[s] for s in srv if have[s]})

    d = sec("Start-up", "Container start to first good answer. First boot is an empty volume; restarts use the populated database (5 restarts per run).")
    for label, fn, dd in (("First boot, empty volume (s)", lambda r: r["scan_run"]["first_boot_ready_s"], 1),
                          ("Restart, median of 5 (ms)", lambda r: r["cold_start_populated_ms"]["median"], 0),
                          ("Restart, slowest of 5 (ms)", lambda r: r["cold_start_populated_ms"]["max"], 0)):
        row(d, label, {s: " ; ".join(f(x, dd) for x in runs(s, fn)) for s in srv},
            {s: med(runs(s, fn)) for s in srv}, {s: runs(s, fn) for s in srv})

    eps = list(main[srv[0]][1]["latency"]) if srv and 1 in main[srv[0]] else []
    for mode, ttl in (("sequential", "one client at a time"), ("concurrent16", "16 clients at once")):
        d = sec(f"API latency, {ttl}", "Median/p95 in ms, run 1 ; run 2. 200 requests (one client) or 800 (16 clients) per run. Jellyfin and Plex use their own APIs, so their rows compare similar work, not identical calls.")
        for e in eps:
            cells, val, cv = {}, {}, {}
            for s in srv:
                parts, meds = [], []
                for n in RUNS:
                    if n not in main[s]:
                        continue
                    x = main[s][n]["latency"][e][mode]
                    if list(x["codes"]) != ["200"]:
                        parts.append("n/a")
                        continue
                    parts.append(f"{f(x['median'], 1)}/{f(x['p95'], 1)}")
                    meds.append(x["median"])
                cells[s] = " ; ".join(parts)
                if meds:
                    val[s] = statistics.median(meds)
                cv[s] = meds
            row(d, e, cells, val, cv)

    d = sec("Streams: time to first audio byte", "Median/p95 in ms per series, run 1 ; run 2. 20 different tracks per series, 10 requests on the long file.")
    labels = []
    for s in srv:
        for r in main[s].values():
            for l in r["stream_ttfb_ms"]:
                if l not in labels:
                    labels.append(l)
    for l in labels:
        cells, val, cv = {}, {}, {}
        for s in srv:
            parts, meds = [], []
            for n in RUNS:
                x = main[s].get(n, {}).get("stream_ttfb_ms", {}).get(l)
                if x is None:
                    continue
                if (s == "jellyfin" and "seek" in l) or not x.get("n") or \
                        ("opus" in l and (x.get("sample") or {}).get("first4") != OGG):
                    parts.append("n/a")
                    continue
                extra = f" (first {f(x['first_ms'], 0)})" if "repeated" in l and x.get("first_ms") is not None else ""
                parts.append(f"{f(x['median'], 1)}/{f(x['p95'], 1)}{extra}")
                meds.append(x["median"])
            cells[s] = " ; ".join(parts) or "-"
            if meds:
                val[s] = statistics.median(meds)
            cv[s] = meds
        row(d, l, cells, val, cv)
    return out


def not_best(secs, tie=1.25):
    """Rows where Leyr is not the lowest value (behind) or is within 25% of the lowest (ties)."""
    res = []
    for sc in secs:
        for r in sc["rows"]:
            v = r["val"]
            if "leyr" not in v or len(v) < 2:
                continue
            best = min(v, key=v.get)
            if best != "leyr":
                kind = "behind" if v["leyr"] > v[best] * tie else "ties"
                res.append((sc["title"], r["label"], kind, best, v["leyr"], v[best]))
            else:
                other = min((k for k in v if k != "leyr"), key=v.get)
                if v[other] < v["leyr"] * tie:
                    res.append((sc["title"], r["label"], "ties", other, v["leyr"], v[other]))
    return res


def markdown(secs):
    out = []
    for sc in secs:
        srv = [s for s in SERVERS if any(s in r["cells"] for r in sc["rows"])]
        out.append(f"### {sc['title']}\n")
        if sc["note"]:
            out.append(sc["note"] + "\n")
        out.append("| | " + " | ".join(NAMES[s] for s in srv) + " |\n|---|" + "---|" * len(srv))
        for r in sc["rows"]:
            out.append(f"| {r['label']} | " + " | ".join(r["cells"].get(s, "-") for s in srv) + " |")
        out.append("")
    return "\n".join(out)


def write_csv(path, secs):
    with open(path, "w", newline="") as fh:
        w = csv.writer(fh)
        w.writerow(["section", "metric", "server", "values_by_run"])
        for sc in secs:
            for r in sc["rows"]:
                for s, v in r["csv"].items():
                    w.writerow([sc["title"], r["label"], s, " ".join(f"{x:.3f}" for x in v)])


if __name__ == "__main__":
    d = sys.argv[1]
    main, scans = load(d)
    secs = sections(main, scans)
    write_csv(f"{d}/summary.csv", secs)
    open(f"{d}/report-tables.md", "w").write(markdown(secs))
    for t in not_best(secs):
        print("NOT BEST:", t)
