#!/usr/bin/env python3
"""Build the benchmark pages of the website from the result files.

    make_page.py [--site SITE_PUBLIC_DIR]

Reads bench/results/{public,real,archive-2026-11-13} and bench/results/setup.json, fills the generated regions
(<!--gen:NAME--> ... <!--/gen:NAME-->) of technology/benchmark/index.html and technology/index.html, and
copies the downloadable files, with their sizes and sha256, into technology/benchmark/files/.
Nothing in the tables is typed by hand.
"""
import argparse, gzip, hashlib, html, io, json, os, re, shutil, tarfile

import report as R

HERE = os.path.dirname(os.path.abspath(__file__))
RES = os.path.join(HERE, "results")
LOGO = {"leyr": "/brand/leyr-mark.svg", "navidrome": "/technology/logos/navidrome.svg", "gonic": "/technology/logos/gonic.svg",
        "jellyfin": "/technology/logos/jellyfin.svg", "plex": "/technology/logos/plex.svg"}
ORDER_SITE = ["leyr", "gonic", "navidrome", "jellyfin", "plex"]
e = html.escape


def th(s):
    # Leyr's own mark takes the page tint and turns while music plays (site.js).
    cls = "srv-logo brand-tint" if s == "leyr" else "srv-logo"
    return (f'<th scope="col" data-srv="{s}"><span class="srv"><img class="{cls}" src="{LOGO[s]}" alt="{R.NAMES[s]} logo" '
            f'width="20" height="20">{R.NAMES[s]}</span></th>')


def table(sec, srv, caption=None):
    out = ['<div class="cmp-wrap"><table class="cmp bench">']
    out.append(f"<caption>{e(caption or sec['note'])}</caption>")
    out.append("<thead><tr><th scope=\"col\">What</th>" + "".join(th(s) for s in srv) + "</tr></thead><tbody>")
    for r in sec["rows"]:
        best = None
        if len(r["val"]) > 1:
            best = min(r["val"], key=r["val"].get)
        cells = []
        for s in srv:
            cls = ' class="win"' if best == s else ""
            cells.append(f'<td data-label="{R.NAMES[s]}" data-srv="{s}"{cls}>{e(r["cells"].get(s, "-"))}</td>')
        out.append(f'<tr><th scope="row">{e(r["label"])}</th>' + "".join(cells) + "</tr>")
    out.append("</tbody></table></div>")
    return "\n".join(out)


def measured(d):
    """Which day each server's result files were written (their modification time)."""
    import datetime, glob
    out = {}
    for s in R.SERVERS:
        days = sorted({datetime.date.fromtimestamp(os.path.getmtime(p)).isoformat() for p in glob.glob(f"{d}/{s}-*.json")})
        if days:
            out[s] = days[0] if len(days) == 1 else f"{days[0]} to {days[-1]}"
    return out


def lib_html(d, tag):
    main, scans = R.load(d)
    secs = R.sections(main, scans)
    srv = [s for s in ORDER_SITE if s in main]
    when = measured(d)
    parts = ['<p>Measured: ' + e("; ".join(f"{R.NAMES[k]} {v}" for k, v in when.items())) + '. Leyr can be rerun alone, so its date can differ from the others.</p>']
    parts += [f'<h3 id="{tag}-{i}">{e(sc["title"])}</h3>\n' + table(sc, srv) for i, sc in enumerate(secs)]
    nb = R.not_best(secs)
    lines = []
    for title, label, kind, other, lv, ov in nb:
        word = "behind" if kind == "behind" else "within 25% of"
        lines.append(f"<li><b>{e(label)}</b> ({e(title.lower())}): Leyr {R.f(lv, 1)}, {R.NAMES[other]} {R.f(ov, 1)}. Leyr is {word} the best.</li>")
    if not lines:
        lines.append("<li>None.</li>")
    parts.append(f'<h3 id="{tag}-notbest">Where Leyr is not the best</h3>\n<p>Rows where another server had the lower median, or where '
                 f'Leyr was within 25% of the lowest (a tie, given the noise). Values are the median of the runs, in the row\'s unit.</p>\n<ul class="plain">'
                 + "\n".join(lines) + "</ul>")
    return "\n".join(parts), main, scans, secs


def rng(vals, fmt, unit):
    """fmt makes the number text; the unit is added once: 14-16 ms."""
    vals = [v for v in vals if v is not None]
    if not vals:
        return "n/a"
    lo, hi = fmt(min(vals)), fmt(max(vals))
    return f"{lo} {unit}" if lo == hi else f"{lo}\u2013{hi} {unit}"


def compared_html(secs, srv):
    def find(title_part, label):
        for sc in secs:
            if title_part in sc["title"]:
                for r in sc["rows"]:
                    if r["label"] == label:
                        return r
    rows = [("Scan the whole library", find("Scan", "Scan time (s)"), lambda v: f"{v:,.1f}", "median", "s"),
            ("Browse the newest albums, 16 clients at once", find("16 clients", "getAlbumList2_newest50"), lambda v: f"{v:,.0f}", "range", "ms"),
            ("Search, 16 clients at once", find("16 clients", "search3"), lambda v: f"{v:,.0f}", "range", "ms"),
            ("Start a converted stream at 25:00 in a 63-minute FLAC", find("Streams", "long_flac_mp3_320_seek_1500s_plus"), lambda v: f"{v:,.0f}", "range", "ms"),
            ("Memory at rest", find("Memory", "Idle memory, 5 min after the scan (MB)"), lambda v: f"{v:,.0f}", "range", "MB"),
            ("Private memory at rest", find("Memory", "Idle private memory (MB)"), lambda v: f"{v:,.0f}", "range", "MB"),
            ("Memory while scanning", find("Scan", "Peak memory during scan (MB)"), lambda v: f"{v:,.0f}", "range", "MB"),
            ("Private memory while scanning", find("Scan", "Private memory, peak during scan (MB)"), lambda v: f"{v:,.0f}", "range", "MB"),
            ("Memory under 16-client load, peak", find("Memory", "Memory under load, peak (MB)"), lambda v: f"{v:,.0f}", "range", "MB"),
            ("Private memory under load, peak", find("Memory", "Private memory under load, peak (MB)"), lambda v: f"{v:,.0f}", "range", "MB")]
    out = ['<table class="cmp">', '<caption>Lower is better in every row. Public generated library. Private memory is resident memory not backed by a file (the Anonymous total of smaps_rollup, summed over the container), so it leaves out the executable, libraries and mapped database files.</caption>',
           "<thead><tr><th scope=\"col\">What</th>" + "".join(th(s) for s in srv) + "</tr></thead>", "<tbody>"]
    for label, r, fmt, mode, unit in rows:
        best = min(r["val"], key=r["val"].get) if r and len(r["val"]) > 1 else None
        cells = []
        for s in srv:
            vals = r["csv"].get(s, []) if r else []
            if mode == "median":
                txt = f'{fmt(r["val"][s])} {unit}' if s in r["val"] else "n/a"
            else:
                txt = rng(vals, fmt, unit)
            if label.startswith("Start a converted") and s == "jellyfin":
                txt = "n/a"
            cls = ' class="win"' if best == s else ""
            cells.append(f'<td data-label="{R.NAMES[s]}" data-srv="{s}"{cls}>{txt}</td>')
        out.append(f'<tr><th scope="row">{e(label)}</th>' + "".join(cells) + "</tr>")
    out.append("</tbody></table>")
    return "\n".join(out)


def sha(path):
    h = hashlib.sha256()
    with open(path, "rb") as f:
        for b in iter(lambda: f.read(1 << 20), b""):
            h.update(b)
    return h.hexdigest()


def tgz(dst, src_dir, arcname):
    """Reproducible tar.gz: sorted, fixed mtime and owner."""
    buf = io.BytesIO()
    with tarfile.open(fileobj=buf, mode="w") as t:
        for name in sorted(os.listdir(src_dir)):
            p = os.path.join(src_dir, name)
            if not os.path.isfile(p):
                continue
            ti = t.gettarinfo(p, arcname=f"{arcname}/{name}")
            ti.mtime, ti.uid, ti.gid, ti.uname, ti.gname = 0, 0, 0, "", ""
            with open(p, "rb") as fh:
                t.addfile(ti, fh)
    with open(dst, "wb") as out, gzip.GzipFile(fileobj=out, mode="wb", mtime=0) as g:
        g.write(buf.getvalue())


def human(n):
    return f"{n / 1e6:.1f} MB" if n >= 1e6 else f"{n / 1e3:.0f} kB"


def replace(text, name, body):
    pat = re.compile(rf"(<!--gen:{name}-->).*?(<!--/gen:{name}-->)", re.S)
    assert pat.search(text), name
    return pat.sub(lambda m: m.group(1) + "\n" + body + "\n" + m.group(2), text)


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--site", default=os.path.join(HERE, "..", "site", "public"))
    ap.add_argument("--library-files", default=os.path.join(RES, "public-library"), help="folder with checksums.sha256 and plan.json")
    a = ap.parse_args()
    site = os.path.abspath(a.site)
    bdir = os.path.join(site, "technology", "benchmark")
    files = os.path.join(bdir, "files")
    shutil.rmtree(files, ignore_errors=True)
    os.makedirs(files)

    pub_html, pmain, pscans, psecs = lib_html(os.path.join(RES, "public"), "pub")
    real_html, rmain, rscans, rsecs = lib_html(os.path.join(RES, "real"), "real")

    # downloads
    items = []
    def add(src, name, what):
        shutil.copyfile(src, os.path.join(files, name))
        items.append((name, what, os.path.getsize(os.path.join(files, name)), sha(os.path.join(files, name))))
    for n, what in (("bench.py", "the harness: one server per run"), ("rss_breakdown.py", "splits a server's resident memory by owner (used by bench.py)"), ("report.py", "turns result files into tables"),
                    ("make_library.py", "generates the public test library"), ("validate_seek.py", "checks that each server really seeks"),
                    ("build_subset.sh", "copies a subset of a music collection (for your own library)"),
                    ("make_page.py", "builds these web pages from the results"), ("run_all.sh", "the run order used")):
        if os.path.exists(os.path.join(HERE, n)):
            add(os.path.join(HERE, n), n, what)
    for lib in ("public", "real"):
        d = os.path.join(RES, lib)
        tgz(os.path.join(files, f"results-{lib}.tar.gz"), d, f"results-{lib}")
        items.append((f"results-{lib}.tar.gz", f"raw result JSON for every run on the {lib} library, with summary.csv, seek-validation.jsonl and report-tables.md",
                      os.path.getsize(os.path.join(files, f"results-{lib}.tar.gz")), sha(os.path.join(files, f"results-{lib}.tar.gz"))))
        for n, what in (("summary.csv", f"{lib} library: every value by run"), ("seek-validation.jsonl", f"{lib} library: the seek check")):
            if os.path.exists(os.path.join(d, n)):
                add(os.path.join(d, n), f"{lib}-{n}", what)
    arch = os.path.join(RES, "archive-2026-11-13")
    tgz(os.path.join(files, "results-archive-2026-11-13.tar.gz"), arch, "results-archive-2026-11-13")
    items.append(("results-archive-2026-11-13.tar.gz", "the first published run (2026-11-13): four servers, the private subset, before the changes listed below",
                  os.path.getsize(os.path.join(files, "results-archive-2026-11-13.tar.gz")), sha(os.path.join(files, "results-archive-2026-11-13.tar.gz"))))
    add(os.path.join(arch, "report-tables.md"), "archive-2026-11-13-tables.md", "the 2026-11-13 results as a table")
    for n, what in (("checksums.sha256", "sha256 of every file of the generated library"), ("plan.json", "size and format counts of the generated library")):
        add(os.path.join(a.library_files, n), "public-library-" + n, what)
    rows = "\n".join(f'<tr><td><a href="/technology/benchmark/files/{n}">{n}</a></td><td>{e(w)}</td><td>{human(s)}</td><td><code>{h}</code></td></tr>'
                     for n, w, s, h in items)
    dl = ('<div class="cmp-wrap"><table class="cmp bench dl"><caption>Files served from this site.</caption><thead><tr><th scope="col">File</th>'
          '<th scope="col">What it is</th><th scope="col">Size</th><th scope="col">sha256</th></tr></thead><tbody>' + rows + "</tbody></table></div>")

    setup = json.load(open(os.path.join(RES, "setup.json")))
    plan = json.load(open(os.path.join(a.library_files, "plan.json")))
    kv = lambda items: "<dl class=\"stack\">" + "".join(f"<div><dt>{e(k)}</dt><dd>{v}</dd></div>" for k, v in items) + "</dl>"
    srvrows = "".join(f"<tr><th scope=\"row\">{e(R.NAMES[s])}</th><td>{e(setup['servers'][s]['version'])}</td><td><code>{e(setup['servers'][s]['image'])}</code></td></tr>"
                      for s in R.SERVERS if s in setup["servers"])
    setup_html = (kv([("Run by", "The Leyr project. This is not an independent test."),
                      ("Dates", e(", ".join(setup["dates"]))), ("Hardware", e(setup["hardware"])),
                      ("Operating system", e(setup["os"])), ("Container engine", e(setup["podman"])),
                      ("Limits", "Every container: 4 CPU cores, 4 GB of memory, no swap (<code>--cpus=4 --memory=4g --memory-swap=4g</code>), one container at a time."),
                      ("Network", "Every container runs on an internal podman network with no route out (<code>podman network create --internal</code>). Published ports still reach the host. Before each run the harness tries to reach three outside addresses from inside the container and stops if any answers; each result file records that check."),
                      ("Leyr", e(setup["leyr"]) + ""),
                      ("Rerun", e(setup["rerun_note"])),
                      ("CPU pinning", "Rootless podman has no cpuset controller, so every container's processes were pinned to four fixed cores with <code>taskset</code> after start, on top of the 4-CPU limit, away from the cores another job on the machine was pinned to."),
                      ("Other load", "The machine was shared. The 1-minute load average before each run is in each library's <code>progress.log</code> inside the results download; it ranged from about 5 to 63. Other software (compiles and tests by other jobs on the same machine) ran during the runs."), ("Clients", "The harness runs on the same host, in Python, over HTTP on 127.0.0.1.")])
                  + '<div class="cmp-wrap"><table class="cmp bench"><caption>Server images, pinned by digest.</caption><thead><tr><th scope="col">Server</th><th scope="col">Version</th><th scope="col">Image</th></tr></thead><tbody>'
                  + srvrows + "</tbody></table></div>")

    ptxt = (f'{plan["albums"]} albums, {plan["tracks"]:,} tracks, {plan["total_bytes"] / 1e9:.1f} GB: '
            + ", ".join(f'{v["files"]:,} {k.upper()}' for k, v in plan["by_format"].items() if k in ("mp3", "flac", "m4a")))
    pg = open(os.path.join(bdir, "index.html")).read()
    pg = replace(pg, "setup", setup_html)
    pg = replace(pg, "public-lib", f"<p>{e(ptxt)}. Seed {plan['seed']}; generated with {e(plan['ffmpeg'])} in {plan['seconds'] // 60} minutes.</p>")
    pg = replace(pg, "results-public", pub_html)
    pg = replace(pg, "results-real", real_html)
    pg = replace(pg, "downloads", dl)
    open(os.path.join(bdir, "index.html"), "w").write(pg)

    tp = os.path.join(site, "technology", "index.html")
    t = open(tp).read()
    t = replace(t, "compared", compared_html(psecs, [s for s in ORDER_SITE if s in pmain]))
    open(tp, "w").write(t)
    print("pages written;", len(items), "downloads")


if __name__ == "__main__":
    main()
