← back to Dw Unbuyable Recovery Pilot
tk11040-momentum-reconcile/measure-match.mjs
30 lines
// TK-11040 unblock: measure how many of the 199 PR-Momentum non-joiners produce a
// HIGH-CONFIDENCE feed match (exact pattern_name + exact color). READ-ONLY.
import { execSync } from 'node:child_process';
import { searchHits } from './meilisearch-client.mjs';
const HOST='https://ms-e886719d86e7-4256.sfo.meilisearch.io';
const KEY=process.env.MEILISEARCH_API_KEY;
if(!KEY) throw new Error('MEILISEARCH_API_KEY is required');
const norm=s=>(s||'').toUpperCase().replace(/[^A-Z0-9]/g,'');
const rows=JSON.parse(execSync(`psql -h /tmp -d dw_unified -tA -c "
select json_agg(json_build_object('pid',regexp_replace(shopify_id,'.*/',''),'mfr',mfr_sku,'title',title))
from shopify_products sp
where sp.vendor='Phillipe Romano' and sp.status='ACTIVE' and not coalesce(sp.has_product_variant,false) and sp.supplier_name ~* 'mom'
and not exists (select 1 from momentum_colorways m where (upper(m.alt_sku)=upper(sp.mfr_sku) or upper(m.momentum_sku)=upper(sp.mfr_sku) or upper(m.dw_sku)=upper(sp.dw_sku)) and m.hw_price>0)"`,{encoding:'utf8'}).trim());
async function feed(q){return searchHits({host:HOST,key:KEY,query:q});}
let confident=0, ambiguous=0, none=0; const samples=[];
for(const r of rows){
// title: "<Pattern>-<Colorway> <Type/DurableVinyl...> | Phillipe Romano"
const base=r.title.split('|')[0].replace(/\b(Durable Vinyl|Vinyl|Type 2|Natural|Wallcovering|Memo Sample)\b/gi,'').trim();
const seg=base.split('-').map(s=>s.trim()).filter(Boolean);
const pat=seg[0]||'', col=seg.slice(1).join(' ')||'';
const hits=await feed(`${pat} ${col}`);
// high-confidence: exactly one hit whose (pattern_name matches pat) AND (preferred_color_name matches col), with a price
const exact=hits.filter(h=>(norm(h.pattern_name)===norm(pat) && norm(h.preferred_color_name)===norm(col) && h.price>0));
if(exact.length===1){confident++; if(samples.length<8)samples.push(`OK ${r.mfr} "${pat}"/"${col}" -> ${exact[0].pattern_name}/${exact[0].preferred_color_name} #${exact[0].number} $${exact[0].price}`);}
else if(hits.some(h=>h.price>0)){ambiguous++; if(samples.filter(s=>s.startsWith('AMB')).length<4)samples.push(`AMB ${r.mfr} "${pat}"/"${col}" -> ${hits.length} hits, ${exact.length} exact`);}
else {none++;}
}
console.log(`total=${rows.length} CONFIDENT(exact 1)=${confident} AMBIGUOUS=${ambiguous} NO-MATCH=${none}`);
console.log(samples.join('\n'));