"""ONE window at 61bfd85c: inventories, both-directions diff, then producers 2-5.

Sequential. One attempt each, no retry. A producer that reds does NOT stop the
sequence - a cancelled later producer is a labelled hole, and this window is the
allocation I have.
"""
import json, os, sys, time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import lane_producers as P
import lane_run as L

REPORT = os.path.join(L.EVID, "window-61bfd85c.json")

INV = [
    ("spt-bins-inv", P.RC, "rc::tests::"),
    ("webserve-e2e-inv", P.WEBSERVE_E2E, ""),
    ("spt-e2e-inv", P.ATTACH_PUSH + P.CROSS_NODE, ""),
]
RUNS = ["spt-bins-run", "webserve-e2e-run", "attach-push-run", "cross-node-run"]


def emit(state):
    json.dump(state, open(REPORT, "w"), indent=2)


def main():
    state = {"tip": "61bfd85c", "start_utc": time.strftime(
        "%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "inventories": {}, "runs": {}}
    emit(state)

    for label, selected, prefix in INV:
        obs = P.PRODUCERS[label]()
        row = {"exit": obs["producer_exit"], "stop_reason": obs["stop_reason"],
               "elapsed": round(obs["elapsed_seconds"], 1)}
        if obs["producer_exit"] == 0:
            pop = P.population(os.path.join(L.EVID, label + ".stdout"), prefix)
            row["by_suite"] = {k: len(v) for k, v in pop.items()}
            row["diff"] = P.diff(pop, selected)
        state["inventories"][label] = row
        emit(state)

    for label in RUNS:
        obs = P.PRODUCERS[label]()
        state["runs"][label] = {
            "exit": obs["producer_exit"], "stop_reason": obs["stop_reason"],
            "elapsed": round(obs["elapsed_seconds"], 1),
            "survivors": obs["survivors"],
            "pool_growth_bytes": obs["pool_growth_bytes"],
        }
        emit(state)

    state["end_utc"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
    state["post_census"] = L.builder_census()["by_owner"]
    emit(state)
    print(json.dumps(state, indent=2))


main()
