← back to Dw Image Shrink

enqueue-videos.js

25 lines

// Populate vid_shrink_ledger from videos-inventory.jsonl (built by enum-videos.js).
// Scope = active+draft product videos (archived → delete track). Idempotent via
// ON CONFLICT(orig_media_id) DO NOTHING. Resumable.
//   node enqueue-videos.js [--status active,draft]
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 STATUS=arg('status','active,draft').split(',').map(s=>s.toUpperCase());
const rows=fs.readFileSync(DIR+'/videos-inventory.jsonl','utf8').split('\n').filter(Boolean).map(l=>JSON.parse(l))
  .filter(r=>STATUS.includes(String(r.prod_status||'').toUpperCase()));
console.log(`enqueue-videos: status[${STATUS}] → ${rows.length} product-videos`);
const c=new pg.Client({host:'/tmp',database:'dw_unified'}); await c.connect();
let ins=0;
for(const r of rows){
  const res=await c.query(`INSERT INTO vid_shrink_ledger
    (product_id,handle,prod_status,shard,orig_media_id,orig_url,orig_bytes,orig_format,orig_width,orig_height)
    VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) ON CONFLICT (orig_media_id) DO NOTHING`,
    [r.product_id,r.handle,r.prod_status,r.product_id%8,r.media_id,r.orig_url,r.orig_bytes,r.orig_format,r.orig_width,r.orig_height]);
  ins+=res.rowCount;
}
const {rows:[cnt]}=await c.query("SELECT count(*) n, count(*) FILTER(WHERE state='pending') p, pg_size_pretty(sum(orig_bytes)::numeric) gb FROM vid_shrink_ledger");
console.log(`inserted ${ins} | ledger total ${cnt.n} (${cnt.p} pending, ${cnt.gb} original)`);
await c.end();