Files
claudetools/projects/dataforth-dos/tools/slotmap-from-hoffman.js
Mike Swanson 6260b5a488 dataforth(datasheet): converge 8B/5B/SCM onto the DSCA template machinery (gated; WIP)
Generalized the shared renderer (datasheet-exact.js) so the per-model template path is
no longer DSCA-only — 8B/SCM5B now route through the same templates/slotMap/fround path
as DSCA. SCM7B stays on its separate parse7BRawData/legacy path (excluded). Changes
(all behind the per-model `validated` gate; non-templated families byte-unchanged, and
DSCA verified non-regressed at 54/56 + staged-92 still render):
- Load 8b5bscm-templates.json (MINED_8B5BSCM); for family 8B/SCM5B, a mined template
  takes the template path (else falls through to the legacy DATA_LINES path).
- Final-Test header is now the verbatim captured `ftHeader` (8B/5B use two schemes —
  legacy "Measured Value" vs DSCA "Measured Value*"); enrich-8b5bscm.js captured it for
  all 136 from the Hoffman originals.
- Accuracy data formatter chosen by input type detected from accHeader: frequency(Hz)/
  AC(AAC/VAC) -> formatAccuracyLineDSCA3345 (now takes inputType, not model prefix);
  standard voltage/current/temp -> legacy formatAccuracyLine.
- parseRawData step-skip generalized to any templated model (skipStepIfStatus).
- Footer 240VAC/Hi-Pot family blocks skipped for templated models (rows carry them).
- Templated SCM5B prepends "SCM" to the printed model name.
- slotmap-from-hoffman.js parameterized (DSCA_TPL) — derived 80 slotMaps for 8b5bscm.

Validation vs live Hoffman originals (validate-mined.js): 37/136 pass and are marked
validated -> 13 real 8B/SCM5B render live via the template path; 24 SCM7B already match
via the unchanged legacy path. Everything else stays null (gated, safe) — nothing
published.

Remaining long tail (well-characterized, next session): ~66 render-null need slotMaps
the Hoffman-oracle derivation couldn't get (vintage / parse); ~33 multi-nuance fails:
Final-Test sign handling on some 8B/5B measured values, accuracy-precision fround in the
legacy std formatter, "Packing Check List" vs "Check List" variants, and other per-model
footer/format quirks. Each failing model stacks several, so single fixes don't flip them.

Tools: enrich-8b5bscm.js, validate-mined.js (generalized validator), slotmap-from-hoffman.js.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 16:21:24 -07:00

80 lines
5.5 KiB
JavaScript

// Derive slotMaps for DSCA33/45 models that still render null (no slotMap), using the
// Hoffman _srcSerial original as the oracle: match its Final-Test measured values, in
// order, to the DB record's value-bearing STATUS entries (numeric greedy). Writes the
// slotMap into dsca33-45-templates.json. --apply to persist.
process.env.DSCA_VALIDATE_MODE = '1';
const fs = require('fs'), https = require('https');
const db = require('./database/db');
const dse = require('./templates/datasheet-exact');
const TPL = process.env.DSCA_TPL || './dsca33-45-templates.json';
const c = JSON.parse(fs.readFileSync('C:\\ProgramData\\dataforth-uploader\\credentials.json', 'utf8'));
const APPLY = process.argv.includes('--apply');
const only = process.argv.slice(2).filter(a => !a.startsWith('--'));
function req(m, uri, h, b) { return new Promise((res, rej) => { const u = new URL(uri); const r = https.request({ hostname: u.hostname, port: 443, path: u.pathname, method: m, headers: h, timeout: 30000 }, x => { let d = ''; x.on('data', c => d += c); x.on('end', () => { try { res(JSON.parse(d)); } catch { res({ _raw: d }); } }); }); r.on('error', rej); if (b) r.write(b); r.end(); }); }
function colSpans(sep) { const cols = []; let m; const re = /=+/g; while ((m = re.exec(sep))) cols.push([m.index, m.index + m[0].length]); return cols; }
// Final-Test measured numeric values from a Hoffman original, in row order (spec rows only).
function hoffmanMeasured(content) {
const L = content.replace(/\r/g, '').split('\n');
const fi = L.findIndex(l => /FINAL TEST RESULTS/.test(l)); if (fi < 0) return null;
let hi = -1; for (let i = fi + 1; i < L.length; i++) if (/Parameter\s+Measured/.test(L[i])) { hi = i; break; } if (hi < 0) return null;
const cols = colSpans(L[hi + 1]); if (cols.length < 4) return null;
const [pc, mc, sc] = cols;
const out = [];
for (let i = hi + 2; i < L.length; i++) {
const l = L[i]; if (/^\s*_{5,}|Check List|It is hereby/.test(l)) break; if (!l.trim()) continue;
if (/^\s*Standard output load/i.test(l)) continue;
const meas = (l.slice(mc[0], sc[0]) || '').trim().split(/\s+/)[0];
const spec = (l.slice(sc[0], cols[3][0]) || '').trim();
if (!spec) continue; // spec-less row (Withstand/Hi-Pot/section head)
const v = parseFloat(meas);
if (isNaN(v)) return null;
out.push(v);
}
return out;
}
(async () => {
const tpl = JSON.parse(fs.readFileSync(TPL, 'utf8'));
const form = Object.entries({ grant_type: 'client_credentials', client_id: c.CF_CLIENT_ID, client_secret: c.CF_CLIENT_SECRET, scope: c.CF_SCOPE }).map(([k, v]) => encodeURIComponent(k) + '=' + encodeURIComponent(v)).join('&');
const t = (await req('POST', c.CF_TOKEN_URL, { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(form) }, form)).access_token;
const models = (only.length ? only : Object.keys(tpl)).filter(m => tpl[m] && !Array.isArray(tpl[m].slotMap));
let done = 0; const failed = [];
for (const m of models) {
const sn = tpl[m]._srcSerial; if (!sn) { failed.push(m + '(no srcSerial)'); continue; }
const rec = await db.queryOne('SELECT raw_data FROM test_records WHERE serial_number=$1 AND model_number=$2', [sn, m]);
if (!rec) { failed.push(m + '(no DB rec)'); continue; }
const g = await req('GET', c.CF_API_BASE + '/api/v1/TestReportDataFiles/' + encodeURIComponent(sn), { Authorization: 'Bearer ' + t });
if (!g.Content) { failed.push(m + '(no Hoffman)'); continue; }
const measured = hoffmanMeasured(g.Content);
const p = dse.parseRawData(rec.raw_data, 'DSCA');
if (!measured || !p) { failed.push(m + '(parse)'); continue; }
const specRows = tpl[m].rows.filter(r => (r.spec || '').trim()).length;
if (measured.length !== specRows) { failed.push(m + '(rows ' + measured.length + '!=' + specRows + ')'); continue; }
// numeric greedy match: each Final-Test measured value -> next status entry with equal value
const map = []; let j = 0; let ok = true;
for (const mv of measured) {
let found = -1;
for (let k = j; k < p.statusEntries.length; k++) {
const s = p.statusEntries[k];
if (!s || s.length <= 4) continue;
// compare at DISPLAY precision: fround + toFixed(decimal-code), like the
// renderer — so near-zero (raw 4.4e-5 displayed "0.0000") matches the
// Hoffman displayed value instead of failing a raw-value tolerance.
const code = parseInt(s[s.length - 1], 10);
const val = parseFloat(s.substring(4, s.length - 1).trim());
if (isNaN(val)) continue;
const disp = parseFloat(Math.fround(val).toFixed(isNaN(code) ? 1 : code));
if (Math.abs(disp - mv) < 1e-9) { found = k; break; }
}
if (found < 0) { ok = false; break; }
map.push(found); j = found + 1;
}
if (ok && map.length === specRows) { tpl[m].slotMap = map; done++; console.log(' ' + m.padEnd(13) + ' slotMap=[' + map.join(',') + '] (oracle ' + sn + ')'); }
else failed.push(m + '(no clean match)');
}
console.log('\nderived: ' + done);
if (failed.length) console.log('failed: ' + failed.join(', '));
if (APPLY) { fs.writeFileSync(TPL, JSON.stringify(tpl)); console.log('[APPLY] wrote ' + TPL); }
else console.log('(dry run — pass --apply to write)');
await db.close();
})().catch(e => { console.error('ERR', e.message, e.stack); process.exit(1); });