← back to Wallco Ai
scripts/cw-rooms-driver.sh
69 lines
#!/usr/bin/env bash
# Colorway sub-category room-mockup generator (Mac2 local only — no prod rsync per DTD verdict B).
# Reads /tmp/cw-cats.txt (one category per line), generates PIL living_room composites,
# inserts wallco_rooms rows, reports per-cohort.
set -uo pipefail
cd /Users/macstudio3/Projects/wallco-ai
OUT=public/uploads/rooms
mkdir -p "$OUT"
PG="dw_unified"
CATS=/tmp/cw-cats.txt
LOG=/tmp/cw-rooms-run.log
: > "$LOG"
log(){ echo "$(date '+%H:%M:%S') $*" | tee -a "$LOG"; }
# ---- 1. Build master list of missing ids across all cohorts ----
: > /tmp/cw-all-ids.txt
i=0
while IFS= read -r CAT; do
[ -z "$CAT" ] && continue
i=$((i+1))
CAT_ESC=$(printf '%s' "$CAT" | sed "s/'/''/g")
psql "$PG" -At -q -c "SELECT id FROM all_designs WHERE category='$CAT_ESC' AND is_published=TRUE AND local_path IS NOT NULL AND NOT EXISTS (SELECT 1 FROM wallco_rooms r WHERE r.design_id=all_designs.id AND r.image_path IS NOT NULL) ORDER BY id;" > "/tmp/cwc-${i}-ids.txt"
cat "/tmp/cwc-${i}-ids.txt" >> /tmp/cw-all-ids.txt
done < "$CATS"
sort -u /tmp/cw-all-ids.txt -o /tmp/cw-all-ids.txt
TOTAL=$(wc -l < /tmp/cw-all-ids.txt | tr -d ' ')
log "PIL pass starting: $TOTAL designs across $i cohorts"
# ---- 2. Parallel PIL composite (8 workers) ----
# Each invocation processes one id; script skips if output already exists.
cat /tmp/cw-all-ids.txt | xargs -P 8 -n 25 python3 scripts/pil-room-composite.py --out "$OUT" >> "$LOG" 2>&1
PNGCOUNT=$(ls "$OUT"/design_*_living_room.png 2>/dev/null | wc -l | tr -d ' ')
log "PIL pass done. living_room PNGs now on disk: $PNGCOUNT"
# ---- 3. Per-cohort SQL insert + report ----
GRAND=0
i=0
while IFS= read -r CAT; do
[ -z "$CAT" ] && continue
i=$((i+1))
IDS="/tmp/cwc-${i}-ids.txt"
[ -s "$IDS" ] || { log "[$i] $CAT — 0 ids, skip"; continue; }
# which of this cohort's ids actually produced a PNG
: > "/tmp/cwc-${i}-final.txt"
while IFS= read -r id; do
[ -z "$id" ] && continue
[ -f "$OUT/design_${id}_living_room.png" ] && echo "$id" >> "/tmp/cwc-${i}-final.txt"
done < "$IDS"
# build + run insert SQL
python3 - "$i" <<'PY' > "/tmp/cwc-${i}-rooms.sql"
import sys
i=sys.argv[1]
ids=[l.strip() for l in open(f'/tmp/cwc-{i}-final.txt') if l.strip()]
print('BEGIN;')
for id in ids:
fn=f'design_{id}_living_room.png'
print(f"INSERT INTO wallco_rooms (design_id, room_type, filename, image_path) SELECT {id}, 'living_room', '{fn}', '/uploads/rooms/{fn}' WHERE NOT EXISTS (SELECT 1 FROM wallco_rooms WHERE design_id={id} AND filename='{fn}');")
print('COMMIT;')
PY
BEFORE=$(psql "$PG" -At -q -c "SELECT COUNT(*) FROM wallco_rooms WHERE room_type='living_room';")
psql "$PG" -q < "/tmp/cwc-${i}-rooms.sql" >> "$LOG" 2>&1
AFTER=$(psql "$PG" -At -q -c "SELECT COUNT(*) FROM wallco_rooms WHERE room_type='living_room';")
INS=$((AFTER-BEFORE))
GRAND=$((GRAND+INS))
FINAL=$(wc -l < "/tmp/cwc-${i}-final.txt" | tr -d ' ')
log "[$i] $CAT — png:$FINAL inserted:$INS"
done < "$CATS"
log "DONE. Total new wallco_rooms inserts: $GRAND"