"""Compare prefill aggregation vs baseline (both fresh restart)."""
import json, os, sys

def stats(path):
    rows = [json.loads(l) for l in open(path)]
    ok = [r for r in rows if not r.get("error")]
    ttfts = sorted([r["ttft_s"] for r in ok if r.get("ttft_s")])
    tpots = sorted([r["tpot_s"] for r in ok if r.get("tpot_s") and r["tpot_s"]>0])
    lats = sorted([r["latency_s"] for r in ok])
    p = lambda v,q: v[min(int(q*len(v)),len(v)-1)] if v else 0
    return {"ok": len(ok), "n": len(rows),
            "t50": p(ttfts,.5), "t90": p(ttfts,.9),
            "p50": p(tpots,.5), "p90": p(tpots,.9),
            "e50": p(lats,.5), "e90": p(lats,.9)}

configs = [
    ("outputs/baseline_dash1/metrics.jsonl", "Baseline (8 combined, dash1)"),
    ("outputs/prefill_agg/metrics.jsonl", "Aggregation (1agg+7comb, dash0)"),
]

print("PREFILL AGGREGATION vs BASELINE")
print("Both: fresh restart, 200 req, same trace, time_scale=20")
print("=" * 72)
fmt = "%-35s %6s %8s %8s %8s %8s %8s"
print(fmt % ("Config", "OK/N", "TTFT50", "TTFT90", "TPOT50", "TPOT90", "E2E50"))
print("-" * 72)

results = {}
for path, label in configs:
    if not os.path.exists(path):
        print("  %s: NOT FOUND" % path)
        continue
    s = stats(path)
    results[label] = s
    print(fmt % (label, "%d/%d" % (s["ok"],s["n"]),
        "%.3f" % s["t50"], "%.3f" % s["t90"],
        "%.3f" % s["p50"], "%.3f" % s["p90"], "%.3f" % s["e50"]))

if len(results) == 2:
    b = list(results.values())[0]
    a = list(results.values())[1]
    print()
    print("DELTA (Aggregation vs Baseline):")
    for label, bv, av in [
        ("TTFT p50", b["t50"], a["t50"]),
        ("TTFT p90", b["t90"], a["t90"]),
        ("TPOT p50", b["p50"], a["p50"]),
        ("TPOT p90", b["p90"], a["p90"]),
        ("E2E p50", b["e50"], a["e50"]),
    ]:
        d = (av/bv-1)*100 if bv > 0 else 0
        print("  %s: %.3f -> %.3f (%+.1f%%)" % (label, bv, av, d))

# Breakdown by class (from proxy)
try:
    import urllib.request
    data = json.loads(urllib.request.urlopen("http://localhost:9090/breakdown", timeout=5).read())
    from collections import Counter
    classes = Counter(d.get("route_class", "?") for d in data)
    print()
    print("Request classification (aggregation):")
    for cls in ["WARM", "MEDIUM", "HEAVY_AGG", "HEAVY_COLO"]:
        n = classes.get(cls, 0)
        subset = [d for d in data if d.get("route_class") == cls and "t_first_token" in d]
        if subset:
            ttfts = sorted([d["t_first_token"] - d["t_proxy_recv"] for d in subset])
            p50 = ttfts[len(ttfts)//2]
            print("  %s: n=%d  TTFT p50=%.3fs" % (cls, n, p50))
        elif n > 0:
            print("  %s: n=%d" % (cls, n))
except Exception as e:
    print("  (breakdown: %s)" % e)
