← back to Dw Image Shrink
enqueue.js
40 lines
// Populate img_shrink_ledger from inventory.jsonl (ground truth).
// Default scope = oversized (>2048px longest edge), statuses active+draft
// (archived → purge track, not compression), skips GIFs (never JPEG a spin-gif).
// Idempotent via ON CONFLICT(orig_image_id) DO NOTHING. Resumable.
// node enqueue.js [--band oversized|1501] [--status active|active,draft] [--limit N] [--nshards 8]
import fs from 'fs';
import pg from 'pg';
const DIR='/Users/macstudio3/Projects/dw-image-shrink';
const arg=(k,d)=>{const a=process.argv.find(x=>x.startsWith('--'+k+'='));return a?a.split('=').slice(1).join('='):d;};
const BAND=arg('band','oversized');
const STATUS=arg('status','active,draft').split(',');
const LIMIT=+arg('limit','0')||0;
const N=+arg('nshards','8');
const isGif=s=>/\.gif(\?|$)/i.test(s||'');
const longest=r=>Math.max(r.width||0,r.height||0);
const minEdge = BAND==='1501'?1501:2049;
const rows=fs.readFileSync(DIR+'/inventory.jsonl','utf8').split('\n').filter(Boolean).map(l=>JSON.parse(l))
.filter(r=>STATUS.includes(r.status) && !isGif(r.src) && longest(r)>=minEdge);
const picked = LIMIT? rows.slice(0,LIMIT) : rows;
console.log(`enqueue: band>=${minEdge}px, status[${STATUS}] → ${rows.length} candidates${LIMIT?`, taking ${picked.length}`:''}`);
const c=new pg.Client({host:'/tmp',database:'dw_unified'});
await c.connect();
let ins=0;
for(let i=0;i<picked.length;i+=500){
const chunk=picked.slice(i,i+500);
const vals=[]; const ph=[];
chunk.forEach((r,j)=>{const b=j*8; ph.push(`($${b+1},$${b+2},$${b+3},$${b+4},$${b+5},$${b+6},$${b+7},$${b+8})`);
vals.push(r.product_id, r.product_id%N, r.image_id, r.src, r.position, r.status==null?null:null, r.width, r.height);});
// note: col order below
const q=`INSERT INTO img_shrink_ledger
(product_id,shard,orig_image_id,orig_src,orig_position,orig_alt,orig_width,orig_height)
VALUES ${ph.join(',')} ON CONFLICT (orig_image_id) DO NOTHING`;
const res=await c.query(q,vals); ins+=res.rowCount;
}
const {rows:[cnt]}=await c.query("SELECT count(*) n, count(*) FILTER(WHERE state='pending') p FROM img_shrink_ledger");
console.log(`inserted ${ins} new rows | ledger total ${cnt.n} (${cnt.p} pending)`);
await c.end();