渲染链路加固: - PageRenderPage 加 AmisErrorBoundary class component + SchemaErrorCard,覆盖 schema 拉取失败/JSON 异常/amis 渲染抛错三种路径,不白屏不崩 Tab - schema 加载成功后调用 recordRecentPage 写入最近打开 - src/api/schema.ts mock 模式下未配置 templateId 自动 fallback 后端接口 全量健康度扫描 (scripts/schema-scan.mjs): - 登录→psql 拉取 1489 个 template_id→并发≤8 扫描→产出报告 - 结果: 1489/1489 HTTP 200,JSON 可解析率 100%,顶层 type 全为 page,零失败 抽样渲染实测 (e2e/schema-samples.spec.ts): - 9 种类型 26 个样本,Node 侧预登录注入真实 JWT,断言 amis-host 挂载且无错误卡片 - 结果: 26/26 全部通过,截图存 e2e/artifacts/ 配置更新: - playwright.config.ts 后端反代改为 8093 (dev profile,含所有端点) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
253 lines
7.2 KiB
JavaScript
253 lines
7.2 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* F1-4 全量 schema 健康度扫描
|
|
* 用法: node scripts/schema-scan.mjs
|
|
*
|
|
* 流程:
|
|
* 1. 登录拿 JWT (admin/111111)
|
|
* 2. 从 psql 拉取全量 template_id 清单 (eip3.sys_page_schema)
|
|
* 3. 串行/小并发 (≤8) GET /api/page/schema/{id}
|
|
* 4. 统计 200/非200、JSON 可解析率、顶层 type 分布
|
|
* 5. 产出 scripts/schema-scan-report.json + 失败清单
|
|
*/
|
|
|
|
import { execSync } from 'node:child_process';
|
|
import { writeFileSync } from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { dirname, join } from 'node:path';
|
|
import http from 'node:http';
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const BACKEND = 'http://localhost:8093';
|
|
const CONCURRENCY = 8;
|
|
|
|
// ---- 工具函数 ----
|
|
|
|
function httpGet(url, token) {
|
|
return new Promise((resolve, reject) => {
|
|
const opts = new URL(url);
|
|
const req = http.request(
|
|
{
|
|
hostname: opts.hostname,
|
|
port: opts.port,
|
|
path: opts.pathname + opts.search,
|
|
method: 'GET',
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
Accept: 'application/json',
|
|
},
|
|
timeout: 15000,
|
|
},
|
|
(res) => {
|
|
let body = '';
|
|
res.on('data', (chunk) => (body += chunk));
|
|
res.on('end', () => resolve({ status: res.statusCode, body }));
|
|
},
|
|
);
|
|
req.on('error', reject);
|
|
req.on('timeout', () => {
|
|
req.destroy();
|
|
reject(new Error('timeout'));
|
|
});
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
function httpPost(url, data) {
|
|
return new Promise((resolve, reject) => {
|
|
const body = JSON.stringify(data);
|
|
const opts = new URL(url);
|
|
const req = http.request(
|
|
{
|
|
hostname: opts.hostname,
|
|
port: opts.port,
|
|
path: opts.pathname,
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Content-Length': Buffer.byteLength(body),
|
|
},
|
|
timeout: 15000,
|
|
},
|
|
(res) => {
|
|
let b = '';
|
|
res.on('data', (chunk) => (b += chunk));
|
|
res.on('end', () => resolve({ status: res.statusCode, body: b }));
|
|
},
|
|
);
|
|
req.on('error', reject);
|
|
req.on('timeout', () => {
|
|
req.destroy();
|
|
reject(new Error('timeout'));
|
|
});
|
|
req.write(body);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
async function runWithConcurrency(tasks, concurrency) {
|
|
const results = new Array(tasks.length);
|
|
let idx = 0;
|
|
|
|
async function worker() {
|
|
while (idx < tasks.length) {
|
|
const i = idx++;
|
|
results[i] = await tasks[i]();
|
|
}
|
|
}
|
|
|
|
const workers = Array.from({ length: Math.min(concurrency, tasks.length) }, () => worker());
|
|
await Promise.all(workers);
|
|
return results;
|
|
}
|
|
|
|
// ---- 主流程 ----
|
|
|
|
async function main() {
|
|
console.log('[schema-scan] 开始执行...');
|
|
|
|
// 1. 登录
|
|
console.log('[schema-scan] 登录 admin...');
|
|
const loginResp = await httpPost(`${BACKEND}/api/auth/login`, {
|
|
account: 'admin',
|
|
password: '111111',
|
|
});
|
|
const loginData = JSON.parse(loginResp.body);
|
|
if (!loginData.success || !loginData.token) {
|
|
throw new Error('登录失败: ' + loginData.msg);
|
|
}
|
|
const token = loginData.token;
|
|
console.log('[schema-scan] 登录成功');
|
|
|
|
// 2. 从 psql 拉取 template_id 清单
|
|
console.log('[schema-scan] 从数据库拉取 template_id 清单...');
|
|
let ids;
|
|
try {
|
|
const psqlOut = execSync(
|
|
'psql -h localhost -p 5433 -d eip4dev -Atc "select template_id from eip3.sys_page_schema"',
|
|
{ encoding: 'utf8' },
|
|
);
|
|
ids = psqlOut
|
|
.trim()
|
|
.split('\n')
|
|
.map((s) => s.trim())
|
|
.filter(Boolean);
|
|
} catch (e) {
|
|
throw new Error('psql 查询失败: ' + e.message);
|
|
}
|
|
console.log(`[schema-scan] 共 ${ids.length} 个 template_id`);
|
|
|
|
// 3. 并发扫描
|
|
const results = [];
|
|
let done = 0;
|
|
const printInterval = setInterval(() => {
|
|
process.stdout.write(`\r[schema-scan] 进度: ${done}/${ids.length}`);
|
|
}, 1000);
|
|
|
|
const tasks = ids.map((id) => async () => {
|
|
const url = `${BACKEND}/api/page/schema/${encodeURIComponent(id)}`;
|
|
let status = 0;
|
|
let jsonOk = false;
|
|
let topLevelType = null;
|
|
let errorKind = null;
|
|
let errorMsg = null;
|
|
|
|
try {
|
|
const resp = await httpGet(url, token);
|
|
status = resp.status;
|
|
if (status === 200) {
|
|
try {
|
|
const parsed = JSON.parse(resp.body);
|
|
jsonOk = true;
|
|
topLevelType = parsed?.type ?? '__missing__';
|
|
} catch {
|
|
jsonOk = false;
|
|
errorKind = 'json_parse_error';
|
|
errorMsg = 'JSON 解析失败';
|
|
}
|
|
} else if (status === 404) {
|
|
errorKind = '404';
|
|
errorMsg = 'Not Found';
|
|
} else if (status === 403 || status === 401) {
|
|
errorKind = '权限';
|
|
errorMsg = `HTTP ${status}`;
|
|
} else if (status >= 500) {
|
|
errorKind = '500';
|
|
errorMsg = `HTTP ${status}`;
|
|
} else {
|
|
errorKind = `http_${status}`;
|
|
errorMsg = `HTTP ${status}`;
|
|
}
|
|
} catch (e) {
|
|
status = 0;
|
|
errorKind = 'network';
|
|
errorMsg = e.message;
|
|
}
|
|
|
|
done++;
|
|
return { id, status, jsonOk, topLevelType, errorKind, errorMsg };
|
|
});
|
|
|
|
const scanResults = await runWithConcurrency(tasks, CONCURRENCY);
|
|
clearInterval(printInterval);
|
|
process.stdout.write(`\r[schema-scan] 进度: ${ids.length}/${ids.length}\n`);
|
|
|
|
// 4. 统计
|
|
const total = scanResults.length;
|
|
const success200 = scanResults.filter((r) => r.status === 200).length;
|
|
const jsonParsable = scanResults.filter((r) => r.jsonOk).length;
|
|
const failures = scanResults.filter((r) => r.status !== 200 || !r.jsonOk);
|
|
|
|
// 顶层 type 分布
|
|
const typeDistribution = {};
|
|
for (const r of scanResults) {
|
|
if (r.topLevelType !== null) {
|
|
typeDistribution[r.topLevelType] = (typeDistribution[r.topLevelType] ?? 0) + 1;
|
|
}
|
|
}
|
|
|
|
// 失败归因分桶
|
|
const errorBuckets = {};
|
|
for (const r of failures) {
|
|
const bucket = r.errorKind ?? 'unknown';
|
|
if (!errorBuckets[bucket]) errorBuckets[bucket] = [];
|
|
errorBuckets[bucket].push({ id: r.id, msg: r.errorMsg, status: r.status });
|
|
}
|
|
|
|
const report = {
|
|
scanTime: new Date().toISOString(),
|
|
total,
|
|
success200,
|
|
successRate: ((success200 / total) * 100).toFixed(2) + '%',
|
|
jsonParsable,
|
|
jsonParseRate: ((jsonParsable / total) * 100).toFixed(2) + '%',
|
|
failureCount: failures.length,
|
|
typeDistribution,
|
|
errorBuckets,
|
|
failureList: failures.map((r) => ({
|
|
templateId: r.id,
|
|
httpStatus: r.status,
|
|
errorKind: r.errorKind,
|
|
errorMsg: r.errorMsg,
|
|
})),
|
|
};
|
|
|
|
// 5. 写报告
|
|
const reportPath = join(__dirname, 'schema-scan-report.json');
|
|
writeFileSync(reportPath, JSON.stringify(report, null, 2), 'utf8');
|
|
|
|
console.log('\n[schema-scan] ====== 扫描结果 ======');
|
|
console.log(`总计: ${total}`);
|
|
console.log(`HTTP 200: ${success200} (${report.successRate})`);
|
|
console.log(`JSON 可解析: ${jsonParsable} (${report.jsonParseRate})`);
|
|
console.log(`失败数: ${failures.length}`);
|
|
console.log(`顶层 type 分布:`, typeDistribution);
|
|
console.log(`失败归因分桶:`, Object.fromEntries(Object.entries(errorBuckets).map(([k, v]) => [k, v.length])));
|
|
console.log(`[schema-scan] 报告已写入: ${reportPath}`);
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error('[schema-scan] 执行失败:', e.message);
|
|
process.exit(1);
|
|
});
|