audit-noodle-innovation.mjs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import fs from 'node:fs';
  2. import path from 'node:path';
  3. import { request, invoke } from '../server/scripts/platform-client.mjs';
  4. const out = path.resolve('work/noodle-innovation-20261108-r1');
  5. const classes = ['KfCategory', 'KfCategoryRelation', 'KfProduct', 'KfSku', 'KfMaterial',
  6. 'KfProcess', 'KfProcessStep', 'KfStandard', 'KfSupplier', 'KfSupplyQuote', 'KfRetailPriceSnapshot'];
  7. const save = (name, data) => fs.writeFileSync(path.join(out, name), JSON.stringify(data, null, 2) + '\n');
  8. fs.mkdirSync(out, { recursive: true });
  9. try {
  10. const schema = await request('GET', '/schemas');
  11. const tables = schema.results.filter(t => classes.includes(t.className) || t.className === 'KfSource');
  12. save('live-schema.json', tables);
  13. const contract = await invoke('ksfGetIngestContract', { requestId: 'noodle-innovation-audit-20261108-r1' });
  14. if (contract.http !== 200 || !contract.body.result?.ok) throw Error('CONTRACT_READ_FAILED');
  15. save('ingest-contract.json', contract.body.result.data);
  16. const results = [];
  17. for (const name of classes) {
  18. let last, total, rows = [];
  19. for (let page = 0; page < 100; page++) {
  20. const where = { dataMode: { $ne: 'sample' }, ...(last ? { objectId: { $gt: last } } : {}) };
  21. const query = new URLSearchParams({ where: JSON.stringify(where), limit: '100', order: 'objectId', count: '1' });
  22. const batch = await request('GET', '/classes/' + name + '?' + query);
  23. if (total === undefined) total = batch.count;
  24. rows.push(...batch.results);
  25. if (batch.results.length < 100) break;
  26. last = batch.results.at(-1).objectId;
  27. }
  28. if (rows.length !== total || new Set(rows.map(r => r.objectId)).size !== total) throw Error('PAGINATION_MISMATCH');
  29. save(name + '.json', rows);
  30. results.push({ className: name, nonSampleRecords: total,
  31. modes: Object.fromEntries([...new Set(rows.map(r => r.dataMode || 'unspecified'))].map(m => [m, rows.filter(r => (r.dataMode || 'unspecified') === m).length])) });
  32. }
  33. const sourceIds = new Set(classes.flatMap(name => JSON.parse(fs.readFileSync(path.join(out, name + '.json'), 'utf8')).map(r => r.sourceRef?.objectId).filter(Boolean)));
  34. const sources = [];
  35. for (const id of sourceIds) sources.push(await request('GET', '/classes/KfSource/' + id));
  36. save('linked-sources.json', sources);
  37. const summary = { checkedAt: new Date().toISOString(), classes: results, linkedSources: sources.length, readOnly: true, databaseWrites: 0, paidCalls: 0 };
  38. save('parse-audit.json', summary);
  39. console.log(JSON.stringify(summary));
  40. } catch {
  41. console.error(JSON.stringify({ ok: false, code: 'NOODLE_AUDIT_FAILED', diagnostic: 'No credentials or raw exceptions printed.' }));
  42. process.exitCode = 1;
  43. }