#!/bin/bash
# Window W1 (19 Sep 2026): waits for the three first runs to end, writes their outcome into the
# window entry, and tells rian. Adapted from sweep-2026-09-19b/finish.sh (not edited there).
# CLAUDE.md: a long unattended job that finishes is worth a message; routine narration is not.
set -u
D="$(cd "$(dirname "$0")" && pwd)"
WINDOW="$D/../window-2026-09-19d-new-collectors.md"
SLUGS="'avolta-las','avolta-lgw','avolta-hel'"
CODES="'LAS','LGW','HEL'"

live() { docker exec dutyfreeprofessor-app sh -c 'for p in /proc/[0-9]*; do c=$(tr "\0" "\n" < $p/cmdline 2>/dev/null | tail -1); case "$c" in avolta-las|avolta-lgw|avolta-hel) echo x;; esac; done' 2>/dev/null | wc -l; }
sql() { docker exec dutyfreeprofessor-db psql -U dfp -d dfp -tA -F' ' -c "$1" 2>/dev/null; }

sleep 300                      # the queue admits one every 90 s; let all three get going
until [ "$(live)" -eq 0 ] && grep -q 'queue done' "$D/admit-queue.log" 2>/dev/null; do sleep 180; done

# The run rows, this window's three sources only: slug, run id, status, prices, seen, skipped.
SUMMARY=$(sql "SELECT s.slug, r.id, r.status, r.prices_written, r.products_seen, r.skipped_no_price,
                      coalesce(r.error, '-')
               FROM collection_runs r JOIN sources s ON s.id=r.source_id
               WHERE s.slug IN ($SLUGS) AND r.started_at > now() - interval '12 hours'
               ORDER BY s.slug, r.id;")
# What each run refused and why (a 404 category, an unreadable variant page, a size veto).
REJECTS=$(sql "SELECT s.slug, o.stage, o.reason, count(*)
               FROM rejected_observations o JOIN collection_runs r ON r.id=o.run_id
               JOIN sources s ON s.id=r.source_id
               WHERE s.slug IN ($SLUGS) AND r.started_at > now() - interval '12 hours'
               GROUP BY 1,2,3 ORDER BY 1,4 DESC;")
# The barcode share per new shop: listings the shop published a GTIN on, and listings whose
# product variant holds one (its own or matched through another shop's).
GTIN=$(sql "SELECT sh.code, count(*) AS listings, count(l.listed_gtin) AS listed_gtin,
                   count(pv.gtin) AS variant_gtin,
                   round(100.0*count(pv.gtin)/nullif(count(*),0)) AS pct
            FROM listings l JOIN shops sh ON sh.id=l.shop_id
            JOIN product_variants pv ON pv.id=l.variant_id
            WHERE sh.iata IN ($CODES) GROUP BY sh.code ORDER BY sh.code;")
SHOPS=$(sql "SELECT code, iata, CASE WHEN visible THEN 'visible' ELSE 'HIDDEN' END
             FROM shops WHERE iata IN ($CODES) ORDER BY code;")
OK=$(echo "$SUMMARY" | grep -c ' ok ')
BAD=$(echo "$SUMMARY" | grep -vc ' ok ')
PRICES=$(echo "$SUMMARY" | awk '{s+=$4} END{print s+0}')
PEAK=$(awk -F'\t' 'NR>1 && $2>m {m=$2} END{print m+0}' "$D/memory-trace.tsv" 2>/dev/null)

{
  echo
  echo "## Closed $(date '+%F %H:%M'): the three first runs ended"
  echo
  echo "$OK run(s) ok, $BAD not, $PRICES prices written. Peak container memory ${PEAK} MiB of the"
  echo "4096 MiB cap (the trace is \`memory-trace.tsv\`; anything shed is in \`guard.log\`)."
  echo
  echo "Run rows (slug, run id, status, prices, seen, skipped, error):"
  echo '```'
  echo "$SUMMARY"
  echo '```'
  echo "Rejected observations by reason (slug, stage, reason, count):"
  echo '```'
  echo "${REJECTS:-none}"
  echo '```'
  echo "Barcode share per new shop (code, listings, listed_gtin, variant_gtin, pct):"
  echo '```'
  echo "${GTIN:-no listings}"
  echo '```'
  echo "Shops (every new one must read HIDDEN):"
  echo '```'
  echo "${SHOPS:-no shop rows: the runs created none}"
  echo '```'
  echo
  echo "Next, by hand: \`app.cli backfill places --check\` then \`app.cli backfill places\` (one airport"
  echo "place per new IATA code), then the airport page check on a restored copy (the section above)."
  echo "Staging may be deployed again (D2)."
} >> "$WINDOW"

srv-gw notify --from "AW1 W1" "W1 closed: $OK of 3 first runs ok, $BAD not, $PRICES prices. Peak ${PEAK}MiB of 4096. Next: backfill places, then D2 may deploy." >/dev/null 2>&1
echo "$(date '+%F %T') window finished: $OK ok, $BAD not, $PRICES prices, peak ${PEAK}MiB" >> "$D/guard.log"
