Files
claudetools/projects/dataforth-dos/datasheet-pipeline/implementation/investigate_skipped.py
Mike Swanson 45083f4735 Add SCMVAS/SCMHVAS datasheet pipeline extension (Dataforth)
Extends the Test Datasheet Pipeline on AD2:C:\Shares\testdatadb to
generate web-published datasheets for the SCMVAS-Mxxx (obsolete) and
SCMHVAS-Mxxxx (replacement) High Voltage Input Module product lines.
Both are tested either with the existing TESTHV3 software (production
VASLOG .DAT logs) or in Engineering with plain .txt output.

Key changes on AD2 (all deployed 2026-04-12 with dated backups):

- parsers/spec-reader.js: getSpecs() returns a `{_family:'SCMVAS',
  _noSpecs:true}` sentinel for SCMVAS/SCMHVAS/VAS-M/HVAS-M model prefixes
  so the export pipeline does not silently skip them for missing specs.
- templates/datasheet-exact.js: new Accuracy-only template branch
  (generateSCMVASDatasheet + helpers) that mirrors the existing shipped
  format byte-for-byte. Extraction regex covers both QuickBASIC STR$()
  output formats: scientific-with-trailing-status-digit (98.4% of
  records) and plain-decimal (1.6% of records above QB's threshold).
- parsers/vaslog-engtxt.js (new): parses the Engineering-Tested .txt
  files in TS-3R\LOGS\VASLOG\VASLOG - Engineering Tested\. Filename SN
  regex strips optional trailing 14-digit timestamp; in-file "SN:"
  header is the authoritative source when the filename is malformed.
- database/import.js: LOG_TYPES grows a VASLOG_ENG entry with
  subfolder + recursive flags. Pre-existing 7 log types keep their
  implicit recursive=true behaviour (config.recursive !== false).
  importFiles() routes VASLOG_ENG paths before the generic loop so a
  VASLOG - Engineering Tested/*.txt path does not mis-dispatch to the
  multiline parser.
- database/export-datasheets.js: VASLOG_ENG records are written
  verbatim via fs.copyFileSync(source_file, For_Web/<SN>.TXT) for true
  byte-level pass-through, with a graceful raw_data fallback when the
  source file is no longer on disk.

Deploy outcome:
- 27,503 SCMVAS/SCMHVAS datasheets rendered (27,065 from scientific +
  438 from plain-decimal PASS lines, post-patch rerun)
- 434 Engineering-Tested .txt files pass-through-copied to For_Web
- 0 errors across both batches

Repo layout added here:
- scmvas-hvas-research/: discovery artifacts (source .BAS, hvin.dat,
  sample .DAT + .txt, binary-format notes, IMPLEMENTATION_PLAN.md)
- implementation/: staged final code + deploy helpers + local test
  harness + per-step verification scripts
- backups/pre-deploy-20260412/: independent local snapshot of the 4
  AD2 files replaced, pulled byte-for-byte before deploy

All helper scripts fetch the AD2 password at runtime from the SOPS
vault (clients/dataforth/ad2.sops.yaml). None of the committed files
contain the plaintext credential. Known vault-entry hygiene issue
(stale shell-escape backslash before the `!`) is documented in the
fetcher comments and stripped at read-time; flagged separately for
cleanup.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 07:36:45 -07:00

134 lines
6.2 KiB
Python

"""Deep-dive on the 438 skipped records.
Looking for patterns: date range, test station, source file, model-family drift,
prior ship status, accuracy magnitude.
"""
import base64, json, subprocess, yaml, paramiko
NODE_SCRIPT = r'''
const db = require('./database/db');
(async () => {
console.log('======================================================================');
console.log('SKIPPED RECORDS INVESTIGATION');
console.log('======================================================================');
const WHERE_SKIPPED = "overall_result='PASS' AND forweb_exported_at IS NULL " +
"AND (model_number LIKE 'SCMVAS%' OR model_number LIKE 'SCMHVAS%') " +
"AND log_type='VASLOG'";
// -----------------------------------------------------------------------
console.log('\n--- [1] Date range of SKIPPED vs RENDERED ---');
const dateRanges = await db.query(
"SELECT CASE WHEN forweb_exported_at IS NULL THEN 'SKIPPED' ELSE 'RENDERED' END AS status, " +
"MIN(test_date) mindate, MAX(test_date) maxdate, COUNT(*) cnt " +
"FROM test_records WHERE overall_result='PASS' AND log_type='VASLOG' " +
"AND (model_number LIKE 'SCMVAS%' OR model_number LIKE 'SCMHVAS%') " +
"GROUP BY CASE WHEN forweb_exported_at IS NULL THEN 'SKIPPED' ELSE 'RENDERED' END"
);
for (const r of dateRanges) console.log(' ' + r.status.padEnd(10) + ' ' + r.mindate + ' .. ' + r.maxdate + ' (' + r.cnt + ' records)');
// -----------------------------------------------------------------------
console.log('\n--- [2] Test station of SKIPPED ---');
const stations = await db.query(
"SELECT COALESCE(test_station,'(null)') ts, COUNT(*) cnt FROM test_records " +
"WHERE " + WHERE_SKIPPED + " GROUP BY test_station ORDER BY cnt DESC"
);
for (const r of stations) console.log(' ' + r.ts.padEnd(10) + ' ' + r.cnt);
// -----------------------------------------------------------------------
console.log('\n--- [3] Source file of SKIPPED (grouped) ---');
const sources = await db.query(
"SELECT source_file, COUNT(*) cnt FROM test_records " +
"WHERE " + WHERE_SKIPPED + " GROUP BY source_file ORDER BY cnt DESC LIMIT 20"
);
for (const r of sources) console.log(' ' + r.cnt.toString().padEnd(6) + ' ' + r.source_file);
// -----------------------------------------------------------------------
console.log('\n--- [4] Year distribution: SKIPPED ---');
const skippedYears = await db.query(
"SELECT strftime('%Y', test_date) yr, COUNT(*) cnt FROM test_records " +
"WHERE " + WHERE_SKIPPED + " GROUP BY yr ORDER BY yr"
);
for (const r of skippedYears) console.log(' ' + r.yr + ' ' + r.cnt);
console.log('\n--- [5] Year distribution: RENDERED ---');
const renderedYears = await db.query(
"SELECT strftime('%Y', test_date) yr, COUNT(*) cnt FROM test_records " +
"WHERE overall_result='PASS' AND log_type='VASLOG' AND forweb_exported_at IS NOT NULL " +
"AND (model_number LIKE 'SCMVAS%' OR model_number LIKE 'SCMHVAS%') " +
"GROUP BY yr ORDER BY yr"
);
for (const r of renderedYears) console.log(' ' + r.yr + ' ' + r.cnt);
// -----------------------------------------------------------------------
console.log('\n--- [6] Sample raw_data: SKIPPED vs same-model RENDERED ---');
const pair = await db.query(
"SELECT 'SKIPPED' AS tag, serial_number, model_number, test_date, test_station, source_file, raw_data " +
"FROM test_records WHERE " + WHERE_SKIPPED + " AND model_number='SCMVAS-M700' LIMIT 2"
);
const pair2 = await db.query(
"SELECT 'RENDERED' AS tag, serial_number, model_number, test_date, test_station, source_file, raw_data " +
"FROM test_records WHERE overall_result='PASS' AND log_type='VASLOG' AND forweb_exported_at IS NOT NULL " +
"AND model_number='SCMVAS-M700' LIMIT 2"
);
for (const r of [...pair, ...pair2]) {
console.log(' [' + r.tag + '] sn=' + r.serial_number + ' date=' + r.test_date +
' station=' + (r.test_station || '-') + ' src=' + r.source_file);
console.log(' raw_data: ' + JSON.stringify((r.raw_data||'').replace(/\n/g,'\\n')));
}
// -----------------------------------------------------------------------
console.log('\n--- [7] Accuracy-value magnitude distribution ---');
const accMag = await db.query(
"SELECT raw_data FROM test_records WHERE " + WHERE_SKIPPED + " LIMIT 50"
);
const vals = [];
for (const r of accMag) {
const m = (r.raw_data || '').match(/"(PASS|FAIL)\s*(-?\.?\d+\.?\d*)"/);
if (m) vals.push(parseFloat(m[2]));
}
if (vals.length) {
const abs = vals.map(Math.abs).sort((a,b)=>a-b);
console.log(' sample count: ' + vals.length);
console.log(' min |val|: ' + abs[0]);
console.log(' median |val|: ' + abs[Math.floor(abs.length/2)]);
console.log(' max |val|: ' + abs[abs.length-1]);
}
await db.close();
})();
'''
def pwd():
r = subprocess.run(['sops','-d','D:/vault/clients/dataforth/ad2.sops.yaml'],
capture_output=True, text=True, timeout=30, check=True)
return yaml.safe_load(r.stdout)['credentials']['password'].replace('\\','')
def ps(c, cmd, to=180):
enc = base64.b64encode(cmd.encode('utf-16-le')).decode()
stdin, stdout, stderr = c.exec_command(f'powershell -NoProfile -EncodedCommand {enc}', timeout=to)
return stdout.read().decode('utf-8','replace'), stderr.read().decode('utf-8','replace'), stdout.channel.recv_exit_status()
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect('192.168.0.6', username='sysadmin', password=pwd(), timeout=30, banner_timeout=45, look_for_keys=False, allow_agent=False)
try:
sftp = c.open_sftp()
remote = 'C:/Shares/testdatadb/_invest.js'
with sftp.open(remote,'w') as fh: fh.write(NODE_SCRIPT)
sftp.close()
out, err, rc = ps(c, r'cd C:\Shares\testdatadb; & node ./_invest.js')
print(out)
if err.strip() and 'CLIXML' not in err:
print('--- STDERR ---')
print(err[:2000])
sftp = c.open_sftp()
try: sftp.remove(remote)
except Exception: pass
sftp.close()
finally:
c.close()