#!/usr/bin/env python3
import pathlib
import sys

if len(sys.argv) < 4:
    print("ERROR:missing_args:usage: metrics-batch-counts.py <registry> <batch> <domains_csv>")
    sys.exit(2)

registry = sys.argv[1]
batch = sys.argv[2]
latest_design_domains_csv = sys.argv[3]

reg = pathlib.Path(registry)
if not reg.exists():
    print(f"ERROR:registry_not_found:{registry}")
    sys.exit(2)

total = done = 0
scope = set([d for d in latest_design_domains_csv.split(",") if d])
for line in reg.read_text().splitlines()[2:]:
    if not line.startswith("|") or "--------" in line:
        continue
    cols = [c.strip() for c in line.strip("|").split("|")]
    if len(cols) < 6:
        continue
    if cols[4] != batch:
        continue
    if scope and cols[0] not in scope:
        continue
    total += 1
    if cols[3] == "Q":
        done += 1
print(f"OK:counts:{total},{done}")
