# /// script
# requires-python = ">=3.10"
# ///
# Copyright (c) 2026 Aayush Sharma · SPDX-License-Identifier: MIT
# https://thedataengineering.com/lab-code/LICENSE.txt
"""The catch-up condition.

Arrivals at a = 500 per second, a 60-second outage, then processing at full
throughput M: time to empty the queue for M = 550 to 2,000, against
backlog / (M - a). Recovery time explodes as a / M approaches 1.
"""
import argparse, json

p = argparse.ArgumentParser(description=__doc__.splitlines()[0])
p.add_argument("--arrival", type=float, default=500, help="a: arrivals per second.")
p.add_argument("--outage", type=float, default=60, help="Seconds with no processing.")
p.add_argument("--capacities", type=lambda s: [float(v) for v in s.split(",")], default=[525, 550, 625, 750, 1000, 2000], help="M values.")
p.add_argument("--json", help="Also write the results to this JSON file.")
args = p.parse_args()
a, T, DT = args.arrival, args.outage, 0.01

rows = []
for M in args.capacities:
    q, t = 0.0, 0.0
    while True:
        q += a * DT
        if t >= T: q = max(0.0, q - M * DT)
        t += DT
        if t > T and q <= 0: break
    rows.append({"M": M, "utilization": a / M, "recovery_s": t - T, "formula_s": a * T / (M - a)})
    print(f"M = {M:6.0f} (a/M = {a / M:4.0%}): queue empty after {t - T:7.1f} s   formula {a * T / (M - a):7.1f} s")

if args.json:
    with open(args.json, "w") as f:
        json.dump({"tables": {"capacities": rows}}, f)
