"""Summarize results.json from test.py."""
import json
from collections import Counter
from email.utils import parsedate_to_datetime

R = json.load(open("results.json"))
for kind in ["homepage", "inner"]:
    rows = [r[kind] for r in R if r.get(kind) and "error" not in r[kind] and r[kind]["status"] == 200
            and "html" in (r[kind].get("content_type") or "")]
    n = len(rows)
    et = [r for r in rows if r["etag_1"]]
    lm = [r for r in rows if r["last_modified_1"]]
    none = [r for r in rows if not r["etag_1"] and not r["last_modified_1"]]
    got304 = [r for r in rows if 304 in (r.get("if_none_match_status"), r.get("if_modified_since_status"))]
    # A Last-Modified within a minute of the response's Date is the time of the request, not of a change.
    lm_now = [r for r in lm if r["date"] and abs((parsedate_to_datetime(r["date"]) - parsedate_to_datetime(r["last_modified_1"])).total_seconds()) < 60]
    print(f"== {kind}: {n} pages with a 200")
    print(f"  ETag {len(et)} ({len(et)/n:.0%}), Last-Modified {len(lm)} ({len(lm)/n:.0%}), neither {len(none)} ({len(none)/n:.0%})")
    print(f"  ETag changed between 2 requests 1 s apart: {sum(r['etag_1'] != r['etag_2'] for r in et)}")
    print(f"  Last-Modified equal to the request time: {len(lm_now)}")
    print(f"  returned 304 to a conditional request: {len(got304)} ({len(got304)/n:.0%})")
    print(f"  visible text changed between 2 requests: {sum(not r['same_text'] for r in rows)}")
    print(f"  304 although the visible text changed between 2 requests: {sum(not r['same_text'] for r in got304)}")
    print(f"  If-None-Match: {dict(Counter(r.get('if_none_match_status') for r in et))}")
    print(f"  If-Modified-Since: {dict(Counter(r.get('if_modified_since_status') for r in lm))}")
    full = sorted(r["bytes"] for r in got304)
    # Bytes of the conditional response that came back as a 304.
    cond = sorted(r["if_none_match_bytes"] if r.get("if_none_match_status") == 304 else r["if_modified_since_bytes"] for r in got304)
    print(f"  pages with a 304: median full response {full[len(full)//2]:,} bytes, median 304 body {cond[len(cond)//2]:,} bytes, total full {sum(full):,} vs 304 {sum(cond):,}")
