236 lines
9.8 KiB
JavaScript
236 lines
9.8 KiB
JavaScript
/**
|
||
* 测试脚本:验证 REDCap 计算字段导出和全量质控
|
||
*
|
||
* 测试内容:
|
||
* 1. 验证 exportCalculatedFields 参数是否生效
|
||
* 2. 检查年龄等计算字段是否正确获取
|
||
* 3. 执行全量质控并查看结果
|
||
*/
|
||
import { PrismaClient } from '@prisma/client';
|
||
import { RedcapAdapter } from './src/modules/iit-manager/adapters/RedcapAdapter.js';
|
||
import { SkillRunner } from './src/modules/iit-manager/engines/SkillRunner.js';
|
||
const prisma = new PrismaClient();
|
||
async function main() {
|
||
console.log('='.repeat(60));
|
||
console.log('📋 REDCap 计算字段导出测试');
|
||
console.log('='.repeat(60));
|
||
// 1. 获取项目配置
|
||
const project = await prisma.iitProject.findFirst({
|
||
where: { name: { contains: 'test0207' } },
|
||
select: {
|
||
id: true,
|
||
name: true,
|
||
redcapUrl: true,
|
||
redcapApiToken: true,
|
||
fieldMappings: true
|
||
}
|
||
});
|
||
if (!project) {
|
||
console.log('❌ 未找到 test0207 项目');
|
||
await prisma.$disconnect();
|
||
return;
|
||
}
|
||
console.log(`\n✅ 找到项目: ${project.name} (ID: ${project.id})`);
|
||
// 2. 创建 REDCap 适配器(使用默认的 exportCalculatedFields=true)
|
||
const adapter = new RedcapAdapter(project.redcapUrl, project.redcapApiToken);
|
||
// ===============================
|
||
// 测试 1:导出单条记录
|
||
// ===============================
|
||
console.log('\n' + '='.repeat(60));
|
||
console.log('📊 测试 1:导出 Record ID=1 的数据');
|
||
console.log('='.repeat(60));
|
||
try {
|
||
const record = await adapter.getRecordById('1');
|
||
if (!record) {
|
||
console.log('❌ 未找到 Record ID=1');
|
||
}
|
||
else {
|
||
// 显示所有字段
|
||
console.log('\n📋 所有字段及其值:');
|
||
const sortedKeys = Object.keys(record).sort();
|
||
for (const key of sortedKeys) {
|
||
const value = record[key];
|
||
const displayValue = value === '' ? '(空)' : value;
|
||
console.log(` ${key}: ${displayValue}`);
|
||
}
|
||
// 特别检查关键字段
|
||
console.log('\n🔍 关键字段检查:');
|
||
const keyFields = ['record_id', 'age', '年龄', 'date_of_birth', '出生日期', 'gender', '性别'];
|
||
for (const field of keyFields) {
|
||
if (record[field] !== undefined) {
|
||
console.log(` ✅ ${field} = ${record[field] || '(空)'}`);
|
||
}
|
||
}
|
||
// 检查是否有年龄相关字段
|
||
console.log('\n🔍 年龄相关字段(模糊匹配):');
|
||
const ageRelated = Object.entries(record).filter(([key]) => key.toLowerCase().includes('age') ||
|
||
key.toLowerCase().includes('年龄') ||
|
||
key.toLowerCase().includes('birth') ||
|
||
key.toLowerCase().includes('出生'));
|
||
if (ageRelated.length > 0) {
|
||
for (const [key, value] of ageRelated) {
|
||
console.log(` ${key} = ${value || '(空)'}`);
|
||
}
|
||
}
|
||
else {
|
||
console.log(' ⚠️ 未找到年龄相关字段');
|
||
}
|
||
}
|
||
}
|
||
catch (error) {
|
||
console.error('❌ REDCap 查询失败:', error.message);
|
||
}
|
||
// ===============================
|
||
// 测试 2:导出所有记录并统计
|
||
// ===============================
|
||
console.log('\n' + '='.repeat(60));
|
||
console.log('📊 测试 2:导出所有记录统计');
|
||
console.log('='.repeat(60));
|
||
try {
|
||
const allRecords = await adapter.getAllRecordsMerged();
|
||
console.log(`\n📋 共获取 ${allRecords.length} 条记录`);
|
||
// 检查每条记录的年龄字段
|
||
const ageField = 'age'; // 假设字段名为 age
|
||
let hasAge = 0;
|
||
let noAge = 0;
|
||
for (const record of allRecords) {
|
||
// 查找任何包含 age 的字段
|
||
const ageValue = record['age'] ?? record['Age'] ?? record['年龄'];
|
||
if (ageValue !== undefined && ageValue !== '') {
|
||
hasAge++;
|
||
}
|
||
else {
|
||
noAge++;
|
||
}
|
||
}
|
||
console.log(` 有年龄值的记录: ${hasAge}`);
|
||
console.log(` 无年龄值的记录: ${noAge}`);
|
||
// 显示前 3 条记录的摘要
|
||
console.log('\n📋 前 3 条记录摘要:');
|
||
for (let i = 0; i < Math.min(3, allRecords.length); i++) {
|
||
const r = allRecords[i];
|
||
console.log(` [Record ${r.record_id}] age=${r.age ?? '(无)'}, date_of_birth=${r.date_of_birth ?? '(无)'}`);
|
||
}
|
||
}
|
||
catch (error) {
|
||
console.error('❌ 导出所有记录失败:', error.message);
|
||
}
|
||
// ===============================
|
||
// 测试 3:检查质控规则
|
||
// ===============================
|
||
console.log('\n' + '='.repeat(60));
|
||
console.log('📊 测试 3:检查项目质控规则');
|
||
console.log('='.repeat(60));
|
||
const skills = await prisma.iitSkill.findMany({
|
||
where: { projectId: project.id },
|
||
select: { id: true, name: true, config: true, isActive: true }
|
||
});
|
||
console.log(`\n📋 项目共有 ${skills.length} 个 Skill:`);
|
||
for (const skill of skills) {
|
||
console.log(` ${skill.isActive ? '✅' : '❌'} ${skill.name} (${skill.id})`);
|
||
// 显示规则摘要
|
||
const config = skill.config;
|
||
if (config?.rules && Array.isArray(config.rules)) {
|
||
console.log(` 规则数: ${config.rules.length}`);
|
||
for (const rule of config.rules.slice(0, 3)) {
|
||
console.log(` - ${rule.name}: field=${rule.field}`);
|
||
}
|
||
if (config.rules.length > 3) {
|
||
console.log(` ... 还有 ${config.rules.length - 3} 条规则`);
|
||
}
|
||
}
|
||
}
|
||
// ===============================
|
||
// 测试 4:执行全量质控
|
||
// ===============================
|
||
console.log('\n' + '='.repeat(60));
|
||
console.log('📊 测试 4:执行全量质控');
|
||
console.log('='.repeat(60));
|
||
try {
|
||
const activeSkills = skills.filter(s => s.isActive);
|
||
if (activeSkills.length === 0) {
|
||
console.log('⚠️ 没有激活的 Skill,跳过质控测试');
|
||
}
|
||
else {
|
||
console.log(`\n🚀 开始执行 ${activeSkills.length} 个激活的 Skill...`);
|
||
for (const skill of activeSkills) {
|
||
console.log(`\n 执行 Skill: ${skill.name}`);
|
||
const runner = new SkillRunner(prisma);
|
||
const result = await runner.execute({
|
||
projectId: project.id,
|
||
skillId: skill.id,
|
||
dryRun: false,
|
||
});
|
||
console.log(` ✅ 完成!`);
|
||
console.log(` 检查记录数: ${result.recordsChecked}`);
|
||
console.log(` 发现问题数: ${result.allIssues?.length ?? 0}`);
|
||
// 显示问题摘要
|
||
if (result.allIssues && result.allIssues.length > 0) {
|
||
console.log(` 问题摘要:`);
|
||
// 按规则分组统计
|
||
const ruleStats = {};
|
||
for (const issue of result.allIssues) {
|
||
const ruleName = issue.ruleName || 'unknown';
|
||
ruleStats[ruleName] = (ruleStats[ruleName] || 0) + 1;
|
||
}
|
||
for (const [ruleName, count] of Object.entries(ruleStats)) {
|
||
console.log(` ${ruleName}: ${count} 次`);
|
||
}
|
||
// 显示前 3 个问题详情
|
||
console.log(` 前 3 个问题详情:`);
|
||
for (const issue of result.allIssues.slice(0, 3)) {
|
||
console.log(` [${issue.recordId}] ${issue.ruleName}`);
|
||
console.log(` 实际值: ${issue.actualValue ?? '(空)'}`);
|
||
console.log(` 期望: ${issue.expectedValue ?? '(未知)'}`);
|
||
console.log(` 消息: ${issue.llmMessage ?? issue.message}`);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (error) {
|
||
console.error('❌ 质控执行失败:', error.message);
|
||
console.error(error.stack);
|
||
}
|
||
// ===============================
|
||
// 测试 5:查看最新质控日志
|
||
// ===============================
|
||
console.log('\n' + '='.repeat(60));
|
||
console.log('📊 测试 5:最新质控日志');
|
||
console.log('='.repeat(60));
|
||
const recentLogs = await prisma.iitQcLog.findMany({
|
||
where: { project_id: project.id },
|
||
orderBy: { created_at: 'desc' },
|
||
take: 5,
|
||
select: {
|
||
id: true,
|
||
record_id: true,
|
||
status: true,
|
||
issues: true,
|
||
created_at: true,
|
||
}
|
||
});
|
||
console.log(`\n📋 最近 ${recentLogs.length} 条质控日志:`);
|
||
for (const log of recentLogs) {
|
||
const issues = log.issues;
|
||
const issueCount = Array.isArray(issues)
|
||
? issues.length
|
||
: (issues?.items?.length ?? 0);
|
||
console.log(` [${log.record_id}] ${log.status} - ${issueCount} 个问题 (${log.created_at.toISOString()})`);
|
||
// 显示问题详情
|
||
const issueList = Array.isArray(issues) ? issues : (issues?.items ?? []);
|
||
for (const issue of issueList.slice(0, 2)) {
|
||
console.log(` ${issue.ruleName}: actualValue=${issue.actualValue ?? '(空)'}, expectedValue=${issue.expectedValue ?? '(未知)'}`);
|
||
}
|
||
}
|
||
console.log('\n' + '='.repeat(60));
|
||
console.log('✅ 测试完成');
|
||
console.log('='.repeat(60));
|
||
await prisma.$disconnect();
|
||
}
|
||
main().catch(async (error) => {
|
||
console.error('❌ 测试脚本出错:', error);
|
||
await prisma.$disconnect();
|
||
process.exit(1);
|
||
});
|