← back to Dw Yolo Loop
scripts/inverse-mislabel-sweep/inverse-mislabel-sweep.mjs
46 lines
// inverse-mislabel-sweep — READ-ONLY, $0. Cycle 84. Measures the c83-named residual the
// "Sample"-titled sweeps structurally missed: single-variant products priced like a SAMPLE
// (<= THRESH) but titled as the FULL product (NO "sample" word) and NOT tagged Showroom —
// a shopper adds what reads as a wallcovering and receives a swatch (inverse of the
// roll-priced-"Sample" mislabel). EXCLUDES the deliberate Showroom-Lines (tag ~ /showroom/),
// which legitimately list single-variant at $4.25. STRUCTURAL signature (c83): single-variant
// + price<=THRESH + title lacks /sample/i + variant title is "Default Title" (never split into
// Sample+roll) + not showroom-tagged. NEGATIVE CONTROL: a $4.25 "Sample"-titled product must
// NOT match; a full-price wallcovering must NOT match. READ-ONLY Admin GraphQL. EXCLUDES PJ-vendor.
import fs from 'fs';
const ENV=fs.readFileSync('/Users/macstudio3/Projects/secrets-manager/.env','utf8');
const TOK=((ENV.match(/^SHOPIFY_ADMIN_TOKEN=(.+)$/m)||[])[1]||'').replace(/['"\r]/g,'').trim();
const GQL='https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-10/graphql.json';
const PJ=/phillip[- ]?jeffries/i;
const THRESH=Number(process.env.THRESH||10); // sample-scale ceiling
const sleep=ms=>new Promise(r=>setTimeout(r,ms));
async function gql(q,v){for(let a=0;a<6;a++){const r=await fetch(GQL,{method:'POST',headers:{'X-Shopify-Access-Token':TOK,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})});if(r.status===429||r.status>=500){await sleep(2000*(a+1));continue;}const j=await r.json();if(j.errors&&JSON.stringify(j.errors).includes('Throttled')){await sleep(2500*(a+1));continue;}return j;}return null;}
const isSample=t=>/sample/i.test(t||'');
const Q=`query($cursor:String){ products(first:100, after:$cursor, query:"status:active"){ pageInfo{hasNextPage endCursor} edges{node{title vendor tags productType variants(first:3){edges{node{title price}}}}} } }`;
let cursor=null,pages=0,scanned=0,complete=true;
let invMislabel=0, showroomExcl=0, sampleTitled=0;
const byVendor={}, byVariantTitle={}, ex=[];
while(true){ const j=await gql(Q,{cursor}); if(!j?.data){complete=false;break;} const c=j.data.products;
for(const e of c.edges){ const n=e.node; if(PJ.test(n.vendor||''))continue; scanned++; const vs=(n.variants?.edges||[]).map(x=>x.node); if(vs.length!==1)continue; const v=vs[0]; const p=parseFloat(v.price);
if(isNaN(p)||p>THRESH) continue; // sample-scale single-variant only
if(isSample(n.title)||isSample(v.title)){ sampleTitled++; continue; } // titled Sample = the OTHER (already-decoded) class
if((n.tags||[]).some(t=>/showroom/i.test(t))){ showroomExcl++; continue; } // deliberate showroom-line, EXCLUDE
invMislabel++;
byVendor[n.vendor]=(byVendor[n.vendor]||0)+1;
byVariantTitle[v.title]=(byVariantTitle[v.title]||0)+1;
if(ex.length<20) ex.push({t:n.title.slice(0,46),vt:v.title,p,type:n.productType,vendor:n.vendor});
}
pages++; if(!c.pageInfo.hasNextPage)break; cursor=c.pageInfo.endCursor;
const cost=j.extensions?.cost?.throttleStatus; if(cost&&cost.currentlyAvailable<800) await sleep(1500); else await sleep(200);
if(pages%100===0) console.log(` ${pages}p scanned=${scanned} inv-mislabel=${invMislabel}`);
}
console.log(`\n=== inverse-mislabel-sweep (READ-ONLY, $0) ===`);
console.log(`active scanned: ${scanned} (complete=${complete}, ${pages}p)`);
console.log(`single-variant <=$${THRESH}, title NOT 'sample', NOT showroom-tagged = INVERSE-MISLABEL: ${invMislabel}`);
console.log(` (excluded: ${showroomExcl} deliberate Showroom-Lines; ${sampleTitled} 'Sample'-titled = the already-decoded class)`);
console.log(`\nby vendor (top 15):`); Object.entries(byVendor).sort((a,b)=>b[1]-a[1]).slice(0,15).forEach(([v,n])=>console.log(` ${v}: ${n}`));
console.log(`\nby variant-title:`); Object.entries(byVariantTitle).sort((a,b)=>b[1]-a[1]).slice(0,8).forEach(([t,n])=>console.log(` "${t}": ${n}`));
console.log(`\nexamples:`); ex.forEach(x=>console.log(` "${x.t}" v="${x.vt}" \$${x.p} type=${x.type} [${x.vendor}]`));
fs.writeFileSync('/tmp/inverse-mislabel-sweep.json',JSON.stringify({ts:new Date().toISOString(),scanned,complete,thresh:THRESH,invMislabel,showroomExcl,sampleTitled,byVendor,byVariantTitle,ex},null,2));
console.log(`\nwrote /tmp/inverse-mislabel-sweep.json`);