新增一批一次性排查脚本(script/tmp-*.cjs),与仓库既有 script/tmp- 约定保持一致, 覆盖波次池边界与优先级核对、库位/出库报表核对、订单状态与更新范围核验等场景
633 lines
23 KiB
JavaScript
633 lines
23 KiB
JavaScript
/**
|
||
* 只读校验脚本(开发库 127.0.0.1:3307/wms,仅 SELECT):
|
||
* 验证 stock.report 修复后的 intervalEdge / preStart 快照 SQL 正确性。
|
||
*
|
||
* 场景1:货主口径(wms_stock_transaction, store_id=1000000)全量恒等式 + oracle 对照
|
||
* 场景3:库位口径(wms_storage_transaction, store_id=1000000)全量恒等式 + oracle 对照
|
||
* 场景4:start_time 边界(preStart 快照,含同秒多行 tie-break)+ 带 start_time 的窗口恒等式
|
||
*
|
||
* 无任何写操作、无清理逻辑。
|
||
* 用法:
|
||
* Remove-Item Env:\NODE_ENV -ErrorAction SilentlyContinue
|
||
* node script/tmp-test-stockreport-fix-dev.cjs
|
||
*/
|
||
|
||
'use strict';
|
||
|
||
const path = require('path');
|
||
const { createRequire } = require('module');
|
||
|
||
const projectRoot = path.resolve(__dirname, '..');
|
||
const projectRequire = createRequire(path.join(projectRoot, 'package.json'));
|
||
const mysql = projectRequire('mysql2/promise');
|
||
|
||
function loadDevDbConfig() {
|
||
const configPath = path.join(projectRoot, 'config', 'config.local.js');
|
||
const factory = require(configPath);
|
||
const config = typeof factory === 'function'
|
||
? factory({ name: 'tmp-test-stockreport-fix', baseDir: projectRoot })
|
||
: factory;
|
||
const client = config && config.mysql && config.mysql.clients && config.mysql.clients.wms;
|
||
if (client && client.host) {
|
||
return {
|
||
host: client.host,
|
||
port: Number(client.port),
|
||
user: client.user,
|
||
password: client.password,
|
||
database: client.database,
|
||
};
|
||
}
|
||
throw new Error('无法解析开发库配置 config.local.js(mysql.clients.wms)');
|
||
}
|
||
|
||
const STORE_ID = 1000000;
|
||
|
||
/** 汇总通过/失败计数 */
|
||
const counters = { pass: 0, fail: 0 };
|
||
function verdict(name, pass, detail) {
|
||
if (pass) {
|
||
counters.pass += 1;
|
||
console.log(`PASS | ${name}${detail ? ` | ${detail}` : ''}`);
|
||
} else {
|
||
counters.fail += 1;
|
||
console.log(`FAIL | ${name}${detail ? ` | ${detail}` : ''}`);
|
||
}
|
||
return pass;
|
||
}
|
||
|
||
function num(value) {
|
||
if (value === null || value === undefined) {
|
||
return null;
|
||
}
|
||
const n = Number(value);
|
||
return Number.isFinite(n) ? n : null;
|
||
}
|
||
|
||
function dimKey(row, isBySpace) {
|
||
const spaceId = isBySpace ? (num(row.space_id) || 0) : 0;
|
||
return isBySpace
|
||
? `${row.stock_id}_${spaceId}_${row.store_id}`
|
||
: `${row.stock_id}_${row.store_id}`;
|
||
}
|
||
|
||
/**
|
||
* 用全量流水在 Node 侧推演 oracle 快照:
|
||
* 首条(created_at, id 最小)的 before_quantity、末条(created_at, id 最大)的 after_quantity。
|
||
* ct 为 DATE_FORMAT 字符串('%Y-%m-%d %H:%i:%s.%f'),字典序即时间序。
|
||
* @param {Object[]} rows 全量流水行
|
||
* @param {boolean} isBySpace 是否按库位维度
|
||
* @return {Map<string, {first: {ct: string, id: number, before: number|null}, last: {ct: string, id: number, after: number|null}}>} oracle 映射
|
||
*/
|
||
function buildFullOracle(rows, isBySpace) {
|
||
const map = new Map();
|
||
for (const row of rows) {
|
||
const key = dimKey(row, isBySpace);
|
||
const id = num(row.id);
|
||
const ct = String(row.ct);
|
||
if (!map.has(key)) {
|
||
map.set(key, {
|
||
first: { ct, id, before: num(row.before_quantity) },
|
||
last: { ct, id, after: num(row.after_quantity) },
|
||
});
|
||
continue;
|
||
}
|
||
const entry = map.get(key);
|
||
if (ct < entry.first.ct || (ct === entry.first.ct && id < entry.first.id)) {
|
||
entry.first = { ct, id, before: num(row.before_quantity) };
|
||
}
|
||
if (ct > entry.last.ct || (ct === entry.last.ct && id > entry.last.id)) {
|
||
entry.last = { ct, id, after: num(row.after_quantity) };
|
||
}
|
||
}
|
||
return map;
|
||
}
|
||
|
||
/**
|
||
* 拉取指定维度全量流水明细(含格式化时间字符串)。
|
||
* @param {Object} conn mysql2 连接
|
||
* @param {string} tableName 表名
|
||
* @param {boolean} isBySpace 是否按库位维度
|
||
* @param {number} primaryVal 主维度值(store_id)
|
||
* @return {Promise<Object[]>} 流水明细列表
|
||
*/
|
||
async function fetchAllRows(conn, tableName, isBySpace, primaryVal) {
|
||
const spaceSelect = isBySpace ? 'COALESCE(space_id, 0) AS space_id, ' : '0 AS space_id, ';
|
||
const [ rows ] = await conn.query(
|
||
`SELECT id, stock_id, ${spaceSelect}store_id,
|
||
transaction_type, quantity, before_quantity, after_quantity,
|
||
DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s.%f') AS ct
|
||
FROM ${tableName}
|
||
WHERE store_id = ?`,
|
||
[ primaryVal ]
|
||
);
|
||
return rows;
|
||
}
|
||
|
||
/**
|
||
* 构造 rangeRows 汇总 SQL(与 report.js 口径一致)。
|
||
* @param {string} tableName 表名
|
||
* @param {boolean} isBySpace 是否按库位维度
|
||
* @param {boolean} withStart 是否带 created_at >= start 条件
|
||
* @return {string} SQL 文本
|
||
*/
|
||
function buildRangeSql(tableName, isBySpace, withStart) {
|
||
const spaceSelect = isBySpace ? 'COALESCE(t.space_id, 0) AS space_id, ' : '';
|
||
const spaceGroup = isBySpace ? ', COALESCE(t.space_id, 0)' : '';
|
||
const startClause = withStart ? ' AND t.created_at >= ?' : '';
|
||
return `
|
||
SELECT t.stock_id,
|
||
${spaceSelect}t.store_id,
|
||
SUM(CASE WHEN t.transaction_type = 'INBOUND' THEN t.quantity ELSE 0 END) AS inbound_total,
|
||
SUM(CASE WHEN t.transaction_type = 'OUTBOUND' THEN t.quantity ELSE 0 END) AS outbound_total
|
||
FROM ${tableName} t
|
||
WHERE t.store_id = ?${startClause}
|
||
GROUP BY t.stock_id${spaceGroup}, t.store_id
|
||
`;
|
||
}
|
||
|
||
/**
|
||
* 构造 intervalEdge 快照 SQL(fixed:含 stock/store 关联;buggy:不含)。
|
||
* @param {string} tableName 表名
|
||
* @param {boolean} isBySpace 是否按库位维度
|
||
* @param {boolean} withStart 是否带 created_at >= start 条件
|
||
* @param {boolean} fixed 是否修复版
|
||
* @return {string} SQL 文本
|
||
*/
|
||
function buildEdgeSql(tableName, isBySpace, withStart, fixed) {
|
||
const spaceSelect = isBySpace ? 'COALESCE(t.space_id, 0) AS space_id, ' : '0 AS space_id, ';
|
||
const spaceGroup = isBySpace ? ', COALESCE(t.space_id, 0)' : '';
|
||
const startClauseG = withStart ? ' AND t.created_at >= ?' : '';
|
||
const startClause2 = withStart ? ' AND t2.created_at >= ?' : '';
|
||
const startClause3 = withStart ? ' AND t3.created_at >= ?' : '';
|
||
const spaceJoin2 = isBySpace ? ' AND COALESCE(t2.space_id, 0) = g.space_id' : '';
|
||
const spaceJoin3 = isBySpace ? ' AND COALESCE(t3.space_id, 0) = g.space_id' : '';
|
||
const dimJoin2 = fixed ? ' AND t2.stock_id = g.stock_id AND t2.store_id = g.store_id' : '';
|
||
const dimJoin3 = fixed ? ' AND t3.stock_id = g.stock_id AND t3.store_id = g.store_id' : '';
|
||
return `
|
||
SELECT g.stock_id, g.space_id, g.store_id,
|
||
ft.before_quantity, lt.after_quantity
|
||
FROM (
|
||
SELECT t.stock_id,
|
||
${spaceSelect}t.store_id,
|
||
MIN(t.created_at) AS _min_ct,
|
||
MAX(t.created_at) AS _max_ct
|
||
FROM ${tableName} t
|
||
WHERE t.store_id = ?${startClauseG}
|
||
GROUP BY t.stock_id${spaceGroup}, t.store_id
|
||
) g
|
||
LEFT JOIN ${tableName} ft
|
||
ON ft.id = (
|
||
SELECT MIN(t2.id)
|
||
FROM ${tableName} t2
|
||
WHERE t2.store_id = ?${startClause2}${spaceJoin2}${dimJoin2}
|
||
AND t2.created_at = g._min_ct
|
||
)
|
||
LEFT JOIN ${tableName} lt
|
||
ON lt.id = (
|
||
SELECT MAX(t3.id)
|
||
FROM ${tableName} t3
|
||
WHERE t3.store_id = ?${startClause3}${spaceJoin3}${dimJoin3}
|
||
AND t3.created_at = g._max_ct
|
||
)
|
||
ORDER BY g.stock_id, g.space_id, g.store_id
|
||
`;
|
||
}
|
||
|
||
/**
|
||
* 构造 preStart 快照 SQL(fixed:含 stock/store 关联;buggy:不含)。
|
||
* @param {string} tableName 表名
|
||
* @param {boolean} isBySpace 是否按库位维度
|
||
* @param {boolean} fixed 是否修复版
|
||
* @return {string} SQL 文本
|
||
*/
|
||
function buildPreStartSql(tableName, isBySpace, fixed) {
|
||
const spaceSelect = isBySpace ? 'COALESCE(t.space_id, 0) AS space_id, ' : '0 AS space_id, ';
|
||
const spaceGroup = isBySpace ? ', COALESCE(t.space_id, 0)' : '';
|
||
const spaceJoin = isBySpace ? ' AND COALESCE(t3.space_id, 0) = g.space_id' : '';
|
||
const dimJoin = fixed ? ' AND t3.stock_id = g.stock_id AND t3.store_id = g.store_id' : '';
|
||
return `
|
||
SELECT g.stock_id, g.space_id, g.store_id, lt.after_quantity
|
||
FROM (
|
||
SELECT t.stock_id,
|
||
${spaceSelect}t.store_id,
|
||
MAX(t.created_at) AS _max_ct
|
||
FROM ${tableName} t
|
||
WHERE t.store_id = ? AND t.created_at < ?
|
||
GROUP BY t.stock_id${spaceGroup}, t.store_id
|
||
) g
|
||
LEFT JOIN ${tableName} lt
|
||
ON lt.id = (
|
||
SELECT MAX(t3.id)
|
||
FROM ${tableName} t3
|
||
WHERE t3.store_id = ? AND t3.created_at < ?${spaceJoin}${dimJoin}
|
||
AND t3.created_at = g._max_ct
|
||
)
|
||
ORDER BY g.stock_id, g.space_id, g.store_id
|
||
`;
|
||
}
|
||
|
||
function edgeParams(storeId, withStart, startLiteral) {
|
||
const base = [ storeId ];
|
||
if (withStart) {
|
||
base.push(startLiteral);
|
||
}
|
||
// g 外层 1 次 + t2 子查询 1 次 + t3 子查询 1 次
|
||
return [ ...base, ...base, ...base ];
|
||
}
|
||
|
||
function preStartParams(storeId, startLiteral) {
|
||
return [ storeId, startLiteral, storeId, startLiteral ];
|
||
}
|
||
|
||
/**
|
||
* 分析每条维度键的流水链路一致性(原始数据质量),识别:
|
||
* - null_before_after:before/after 为 NULL
|
||
* - delta_mismatch:INBOUND 应为 +qty、OUTBOUND 应为 -qty、其他应为 0
|
||
* - chain_break:前一条 after 与后一条 before 不衔接
|
||
* @param {Object[]} rows 全量流水行
|
||
* @param {boolean} isBySpace 是否按库位维度
|
||
* @return {Map<string, string[]>} 异常键到原因列表的映射
|
||
*/
|
||
function analyzeChain(rows, isBySpace) {
|
||
const byKey = new Map();
|
||
for (const row of rows) {
|
||
const key = dimKey(row, isBySpace);
|
||
if (!byKey.has(key)) {
|
||
byKey.set(key, []);
|
||
}
|
||
byKey.get(key).push(row);
|
||
}
|
||
|
||
const anomalies = new Map();
|
||
for (const [ key, list ] of byKey.entries()) {
|
||
list.sort((a, b) => {
|
||
if (a.ct !== b.ct) {
|
||
return a.ct < b.ct ? -1 : 1;
|
||
}
|
||
return num(a.id) - num(b.id);
|
||
});
|
||
|
||
const reasons = [];
|
||
let prevAfter = null;
|
||
for (let i = 0; i < list.length; i += 1) {
|
||
const row = list[i];
|
||
const before = num(row.before_quantity);
|
||
const after = num(row.after_quantity);
|
||
if (before === null || after === null) {
|
||
reasons.push(`null_before_after@id=${row.id}`);
|
||
prevAfter = after;
|
||
continue;
|
||
}
|
||
const qty = num(row.quantity);
|
||
if (qty !== null) {
|
||
let expectedDelta = 0;
|
||
if (row.transaction_type === 'INBOUND') {
|
||
expectedDelta = qty;
|
||
} else if (row.transaction_type === 'OUTBOUND') {
|
||
expectedDelta = -qty;
|
||
}
|
||
if (after - before !== expectedDelta) {
|
||
reasons.push(`delta_mismatch@id=${row.id}`);
|
||
}
|
||
}
|
||
if (i > 0 && prevAfter !== null && prevAfter !== before) {
|
||
reasons.push(`chain_break@id=${row.id}(prev_after=${prevAfter},before=${before})`);
|
||
}
|
||
prevAfter = after;
|
||
}
|
||
if (reasons.length > 0) {
|
||
anomalies.set(key, reasons);
|
||
}
|
||
}
|
||
return anomalies;
|
||
}
|
||
|
||
/**
|
||
* 全量(无 start_time)场景:fixed/buggy 快照 SQL vs oracle + 恒等式。
|
||
* @param {Object} conn mysql2 连接
|
||
* @param {string} tableName 表名
|
||
* @param {boolean} isBySpace 是否按库位维度
|
||
* @param {string} label 场景标签
|
||
* @return {Promise<Object>} 校验中间结果
|
||
*/
|
||
async function verifyFullRange(conn, tableName, isBySpace, label) {
|
||
console.log(`\n===== ${label}:全量 intervalEdge 快照校验(store_id=${STORE_ID}) =====`);
|
||
const rows = await fetchAllRows(conn, tableName, isBySpace, STORE_ID);
|
||
console.log(`全量流水行数: ${rows.length}`);
|
||
|
||
const oracle = buildFullOracle(rows, isBySpace);
|
||
|
||
const [ rangeRows ] = await conn.query(buildRangeSql(tableName, isBySpace, false), [ STORE_ID ]);
|
||
const [ fixedRows ] = await conn.query(buildEdgeSql(tableName, isBySpace, false, true), edgeParams(STORE_ID, false, null));
|
||
const [ buggyRows ] = await conn.query(buildEdgeSql(tableName, isBySpace, false, false), edgeParams(STORE_ID, false, null));
|
||
|
||
const fixedMap = new Map();
|
||
for (const row of fixedRows) {
|
||
fixedMap.set(dimKey(row, isBySpace), row);
|
||
}
|
||
const buggyMap = new Map();
|
||
for (const row of buggyRows) {
|
||
buggyMap.set(dimKey(row, isBySpace), row);
|
||
}
|
||
const rangeMap = new Map();
|
||
for (const row of rangeRows) {
|
||
rangeMap.set(dimKey(row, isBySpace), row);
|
||
}
|
||
|
||
const dimKeyCount = oracle.size;
|
||
console.log(`维度键(stock[,space],store)数: ${dimKeyCount}`);
|
||
|
||
const anomalies = analyzeChain(rows, isBySpace);
|
||
|
||
let fixedBeginMismatch = 0;
|
||
let fixedEndMismatch = 0;
|
||
let buggyMismatch = 0;
|
||
let identityMismatch = 0;
|
||
let identityChecked = 0;
|
||
let nullEdgeKeys = 0;
|
||
const buggySamples = [];
|
||
const identitySamples = [];
|
||
|
||
for (const [ key, expected ] of oracle.entries()) {
|
||
const fixedRow = fixedMap.get(key);
|
||
const buggyRow = buggyMap.get(key);
|
||
const rangeRow = rangeMap.get(key);
|
||
|
||
if (!fixedRow) {
|
||
fixedBeginMismatch += 1;
|
||
fixedEndMismatch += 1;
|
||
continue;
|
||
}
|
||
const fixedBegin = num(fixedRow.before_quantity);
|
||
const fixedEnd = num(fixedRow.after_quantity);
|
||
if (fixedBegin === null || fixedEnd === null) {
|
||
nullEdgeKeys += 1;
|
||
}
|
||
if (fixedBegin !== expected.first.before) {
|
||
fixedBeginMismatch += 1;
|
||
}
|
||
if (fixedEnd !== expected.last.after) {
|
||
fixedEndMismatch += 1;
|
||
}
|
||
if (buggyRow && (num(buggyRow.before_quantity) !== expected.first.before
|
||
|| num(buggyRow.after_quantity) !== expected.last.after)) {
|
||
buggyMismatch += 1;
|
||
if (buggySamples.length < 5) {
|
||
buggySamples.push(`${key}: oracle(${expected.first.before}/${expected.last.after}) buggy(${num(buggyRow.before_quantity)}/${num(buggyRow.after_quantity)})`);
|
||
}
|
||
}
|
||
|
||
// 恒等式只对链路一致的键断言(null/断链/增量不一致属原始数据质量问题,与本次修复无关)
|
||
if (anomalies.has(key)) {
|
||
continue;
|
||
}
|
||
const inbound = rangeRow ? num(rangeRow.inbound_total) : 0;
|
||
const outbound = rangeRow ? num(rangeRow.outbound_total) : 0;
|
||
identityChecked += 1;
|
||
if (fixedEnd !== fixedBegin + inbound - outbound) {
|
||
identityMismatch += 1;
|
||
if (identitySamples.length < 5) {
|
||
identitySamples.push(`${key}: end=${fixedEnd} begin=${fixedBegin} in=${inbound} out=${outbound} diff=${fixedEnd - (fixedBegin + inbound - outbound)}`);
|
||
}
|
||
}
|
||
}
|
||
|
||
verdict(`${label} | fixed 首条 before 与 oracle 完全一致`, fixedBeginMismatch === 0,
|
||
fixedBeginMismatch === 0 ? `0 不一致 / ${dimKeyCount} 键` : `${fixedBeginMismatch} 键不一致`);
|
||
verdict(`${label} | fixed 末条 after 与 oracle 完全一致`, fixedEndMismatch === 0,
|
||
fixedEndMismatch === 0 ? `0 不一致 / ${dimKeyCount} 键` : `${fixedEndMismatch} 键不一致`);
|
||
const consistentKeyCount = dimKeyCount - anomalies.size;
|
||
console.log(` 链路一致的维度键: ${consistentKeyCount}/${dimKeyCount};异常键(原始数据质量): ${anomalies.size}`);
|
||
if (nullEdgeKeys > 0) {
|
||
console.log(` 说明: ${nullEdgeKeys} 键的首/末行 before/after 为 NULL(原始数据既有,report.js toNumber 会按 0 处理),不属于修复问题`);
|
||
}
|
||
if (anomalies.size > 0) {
|
||
for (const [ key, reasons ] of anomalies.entries()) {
|
||
console.log(` 数据异常键 ${key}: ${reasons.slice(0, 4).join(' ; ')}`);
|
||
}
|
||
}
|
||
verdict(`${label} | 恒等式 end = begin + in - out(链路一致键)全部成立`, identityMismatch === 0,
|
||
`${identityMismatch} 违反 / 校验 ${identityChecked} 键`);
|
||
verdict(`${label} | buggy(无关联) 与 oracle 不一致样本数`, true,
|
||
`不一致 ${buggyMismatch} 键(用于说明缺陷影响面)`);
|
||
if (buggySamples.length > 0) {
|
||
console.log(` buggy 不一致样本: ${buggySamples.join(' ; ')}`);
|
||
}
|
||
if (identitySamples.length > 0) {
|
||
console.log(` 恒等式违反样本: ${identitySamples.join(' ; ')}`);
|
||
}
|
||
|
||
return { rows, oracle, fixedMap, rangeMap, anomalies };
|
||
}
|
||
|
||
/**
|
||
* 场景4:start_time 边界——preStart 快照(start 前最近一条 after),
|
||
* 特别选取「期初边界处同秒多条流水」的时间点。
|
||
* @param {Object} conn mysql2 连接
|
||
* @param {string} tableName 表名
|
||
* @param {boolean} isBySpace 是否按库位维度
|
||
* @param {string} label 场景标签
|
||
* @return {Promise<Object>} 校验中间结果
|
||
*/
|
||
async function verifyPreStartBoundary(conn, tableName, isBySpace, label) {
|
||
console.log(`\n===== ${label}:start_time 边界 preStart 快照校验 =====`);
|
||
const rows = await fetchAllRows(conn, tableName, isBySpace, STORE_ID);
|
||
|
||
// 找「两个相邻时间戳都含同键多行」的维度键,把较晚时间戳作为 start(前一批成为期初边界)。
|
||
const byKey = new Map();
|
||
for (const row of rows) {
|
||
const key = dimKey(row, isBySpace);
|
||
if (!byKey.has(key)) {
|
||
byKey.set(key, new Map());
|
||
}
|
||
const ctMap = byKey.get(key);
|
||
ctMap.set(String(row.ct), (ctMap.get(String(row.ct)) || 0) + 1);
|
||
}
|
||
|
||
let chosenKey = null;
|
||
let chosenStart = null;
|
||
for (const [ key, ctMap ] of byKey.entries()) {
|
||
const cts = [ ...ctMap.keys() ].sort();
|
||
for (let i = 1; i < cts.length; i += 1) {
|
||
if ((ctMap.get(cts[i - 1]) || 0) >= 2 && (ctMap.get(cts[i]) || 0) >= 2) {
|
||
chosenKey = key;
|
||
chosenStart = cts[i];
|
||
break;
|
||
}
|
||
}
|
||
if (chosenKey) {
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (!chosenStart) {
|
||
// 退而求其次:任一含同秒多行的时间戳,往后取相邻时间戳作为 start
|
||
let found = false;
|
||
for (const [ key, ctMap ] of byKey.entries()) {
|
||
if (found) {
|
||
break;
|
||
}
|
||
const cts = [ ...ctMap.keys() ].sort();
|
||
for (let i = 0; i < cts.length; i += 1) {
|
||
if ((ctMap.get(cts[i]) || 0) >= 2) {
|
||
chosenKey = key;
|
||
chosenStart = i + 1 < cts.length ? cts[i + 1] : cts[i];
|
||
found = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!chosenStart) {
|
||
verdict(`${label} | preStart 边界同秒样本`, false, '未找到同秒多行样本,跳过');
|
||
return { oraclePreStart: null, chosenStart: null, rows, chosenKey: null };
|
||
}
|
||
|
||
console.log(`选中维度键: ${chosenKey}, start_time 边界: ${chosenStart}`);
|
||
|
||
// oracle:created_at < start 的最近一条(created_at, id 排序)
|
||
const preStartOracle = new Map();
|
||
for (const row of rows) {
|
||
const key = dimKey(row, isBySpace);
|
||
if (String(row.ct) >= chosenStart) {
|
||
continue;
|
||
}
|
||
const id = num(row.id);
|
||
const ct = String(row.ct);
|
||
const after = num(row.after_quantity);
|
||
if (!preStartOracle.has(key)) {
|
||
preStartOracle.set(key, { ct, id, after });
|
||
continue;
|
||
}
|
||
const current = preStartOracle.get(key);
|
||
if (ct > current.ct || (ct === current.ct && id > current.id)) {
|
||
preStartOracle.set(key, { ct, id, after });
|
||
}
|
||
}
|
||
|
||
const [ fixedPreRows ] = await conn.query(
|
||
buildPreStartSql(tableName, isBySpace, true),
|
||
preStartParams(STORE_ID, chosenStart)
|
||
);
|
||
const [ buggyPreRows ] = await conn.query(
|
||
buildPreStartSql(tableName, isBySpace, false),
|
||
preStartParams(STORE_ID, chosenStart)
|
||
);
|
||
|
||
const fixedPreMap = new Map();
|
||
for (const row of fixedPreRows) {
|
||
fixedPreMap.set(dimKey(row, isBySpace), row);
|
||
}
|
||
const buggyPreMap = new Map();
|
||
for (const row of buggyPreRows) {
|
||
buggyPreMap.set(dimKey(row, isBySpace), row);
|
||
}
|
||
|
||
let fixedPreMismatch = 0;
|
||
let nullPreCount = 0;
|
||
let buggyPreMismatch = 0;
|
||
for (const [ key, expected ] of preStartOracle.entries()) {
|
||
const fixedRow = fixedPreMap.get(key);
|
||
if (!fixedRow) {
|
||
fixedPreMismatch += 1;
|
||
continue;
|
||
}
|
||
if (num(fixedRow.after_quantity) === null) {
|
||
nullPreCount += 1;
|
||
}
|
||
if (num(fixedRow.after_quantity) !== expected.after) {
|
||
fixedPreMismatch += 1;
|
||
}
|
||
const buggyRow = buggyPreMap.get(key);
|
||
if (buggyRow && num(buggyRow.after_quantity) !== expected.after) {
|
||
buggyPreMismatch += 1;
|
||
}
|
||
}
|
||
|
||
verdict(`${label} | preStart 最近一条 after 与 oracle 完全一致`, fixedPreMismatch === 0,
|
||
`不一致 ${fixedPreMismatch},共 ${preStartOracle.size} 键`);
|
||
if (nullPreCount > 0) {
|
||
console.log(` 说明: ${nullPreCount} 键的期初行 after_quantity 为 NULL(原始数据既有,report.js toNumber 会按 0 处理),不属于修复问题`);
|
||
}
|
||
verdict(`${label} | preStart buggy 不一致样本数`, true,
|
||
`不一致 ${buggyPreMismatch} 键(用于说明缺陷影响面)`);
|
||
|
||
// 窗口恒等式:以 chosenStart 为 start、以该键末条时间为 end,验证
|
||
// end_total = preStartAfter + Σin - Σout(含 start_time 的 intervalEdge 修复版)
|
||
const chosenParts = chosenKey.split('_');
|
||
const chosenStockId = chosenParts[0];
|
||
const [ rangeWithStart ] = await conn.query(
|
||
buildRangeSql(tableName, isBySpace, true),
|
||
[ STORE_ID, chosenStart ]
|
||
);
|
||
const [ edgeWithStart ] = await conn.query(
|
||
buildEdgeSql(tableName, isBySpace, true, true),
|
||
edgeParams(STORE_ID, true, chosenStart)
|
||
);
|
||
const rangeWithStartMap = new Map();
|
||
for (const row of rangeWithStart) {
|
||
rangeWithStartMap.set(dimKey(row, isBySpace), row);
|
||
}
|
||
const edgeWithStartMap = new Map();
|
||
for (const row of edgeWithStart) {
|
||
edgeWithStartMap.set(dimKey(row, isBySpace), row);
|
||
}
|
||
|
||
const anomalies = analyzeChain(rows, isBySpace);
|
||
let windowIdentityMismatch = 0;
|
||
let windowChecked = 0;
|
||
for (const [ key, expected ] of preStartOracle.entries()) {
|
||
if (anomalies.has(key)) {
|
||
continue;
|
||
}
|
||
const edgeRow = edgeWithStartMap.get(key);
|
||
const rangeRow = rangeWithStartMap.get(key);
|
||
if (!edgeRow || num(edgeRow.after_quantity) === null) {
|
||
continue;
|
||
}
|
||
const end = num(edgeRow.after_quantity);
|
||
const begin = expected.after;
|
||
const inbound = rangeRow ? num(rangeRow.inbound_total) : 0;
|
||
const outbound = rangeRow ? num(rangeRow.outbound_total) : 0;
|
||
windowChecked += 1;
|
||
if (end !== begin + inbound - outbound) {
|
||
windowIdentityMismatch += 1;
|
||
}
|
||
}
|
||
|
||
verdict(`${label} | 带 start_time 窗口恒等式 end = 期初 + in - out(链路一致键)`, windowIdentityMismatch === 0,
|
||
`${windowIdentityMismatch} 违反 / 校验 ${windowChecked} 键`);
|
||
|
||
return { oraclePreStart: preStartOracle, chosenStart, rows, chosenKey, chosenStockId };
|
||
}
|
||
|
||
(async () => {
|
||
const dbConfig = loadDevDbConfig();
|
||
console.log('目标: 开发库', `${dbConfig.host}:${dbConfig.port}/${dbConfig.database}`);
|
||
|
||
const connection = await mysql.createConnection(dbConfig);
|
||
try {
|
||
// 场景1:货主口径(by_space=false 等价,维度键 stock+store)
|
||
await verifyFullRange(connection, 'wms_stock_transaction', false, '场景1 货主口径(wms_stock_transaction)');
|
||
|
||
// 场景3:库位口径(by_space=true,维度键 stock+space+store)
|
||
await verifyFullRange(connection, 'wms_storage_transaction', true, '场景3 库位口径(wms_storage_transaction)');
|
||
|
||
// 场景4:start_time 边界
|
||
await verifyPreStartBoundary(connection, 'wms_stock_transaction', false, '场景4 货主口径 start_time 边界');
|
||
await verifyPreStartBoundary(connection, 'wms_storage_transaction', true, '场景4 库位口径 start_time 边界');
|
||
} finally {
|
||
await connection.end();
|
||
}
|
||
|
||
console.log('\n===== 汇总 =====');
|
||
console.log(`PASS: ${counters.pass}, FAIL: ${counters.fail}`);
|
||
if (counters.fail > 0) {
|
||
process.exit(1);
|
||
}
|
||
})().catch(err => {
|
||
console.error(err && err.stack ? err.stack : String(err));
|
||
process.exit(1);
|
||
});
|