← back to Dw Yolo Loop

scripts/thibaut-disco-drift/thibaut-disco-drift.mjs

24 lines

const TOK=process.env.TOK,GQL=process.env.GQL;
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){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 vendor:Thibaut"){ pageInfo{hasNextPage endCursor} edges{node{handle variants(first:5){edges{node{title price sku}}}}} } }`;
let cursor=null,pages=0,total=0; const abn=[];
while(pages<30){ const j=await gql(Q,{cursor}); if(!j?.data)break; const c=j.data.products;
  for(const e of c.edges){ const n=e.node; total++; const vs=(n.variants?.edges||[]).map(x=>x.node); if(vs.length!==1)continue; const v=vs[0]; const p=parseFloat(v.price);
    if(isSample(v.title)&&!isNaN(p)&&p!==0&&p!==4.25) abn.push({h:n.handle,price:p,sku:v.sku||''}); }
  if(!c.pageInfo.hasNextPage)break; cursor=c.pageInfo.endCursor; pages++; await sleep(180); }
console.log(`Thibaut active scanned=${total} | abnormal-sample=${abn.length}`);
// classify by SKU prefix + price band
const is71=s=>/^DWTT-71/i.test(s);
const dwtt71=abn.filter(a=>is71(a.sku));
const rollPriced=abn.filter(a=>a.price>=20);   // roll-scale (mislabel)
const subSample=abn.filter(a=>a.price<20);      // near-sample price anomaly (e.g. $3.50)
console.log(`  DWTT-71xx (discontinued set, should be ARCHIVED): ${dwtt71.length}`);
console.log(`  roll-priced (>=20 dollars, mislabel): ${rollPriced.length} | sub-20-dollar (price anomaly): ${subSample.length}`);
const pfx={}; abn.forEach(a=>{const k=(a.sku.match(/^([A-Z]+-\d{2})/i)||[,'?'])[1]; pfx[k]=(pfx[k]||0)+1;});
console.log('  SKU-prefix histogram:', JSON.stringify(pfx));
const sp=abn.map(a=>a.price).sort((x,y)=>x-y); console.log(`  price range: $${sp[0]} .. $${sp[sp.length-1]} (median $${sp[Math.floor(sp.length/2)]})`);
console.log('  DWTT-71xx examples:'); dwtt71.slice(0,5).forEach(a=>console.log(`    ${a.h.slice(0,40)} sku=${a.sku} $${a.price}`));
console.log('  sub-20 examples:'); subSample.slice(0,5).forEach(a=>console.log(`    ${a.h.slice(0,40)} sku=${a.sku} $${a.price}`));