从 ssw-mvp 抽取隐私计算编排域,框架换 JNPF Lite 契约 (NestJS + TypeORM + PGlite 嵌入式 Postgres + React 18/Vite), 执行底座对接 ../ssw-mpc Rust CLI。 三个兄弟模块: - sswflow(编排,12 表):DAG 画布(React Flow @xyflow/react)、 19 算子目录、可插拔执行引擎(mpc / simulation)、运行监控 - sswdevice(设备注册,1 表):真源 = ssw-mpc 控制面 GET /nodes, TeeNode 字段逐一对应,含 TTL 90s 在线清单的下线语义 - sswchain(链上存证,1 表):真源 = ssw-mpc outbox 二进制 canonical_bytes + FISCO 国密链自有 RPC,Receipt 四验证位分开报 配套 scripts/ 六个门禁脚本(期望数字写死)+ docs/e2e 端到端演示 + CLAUDE.md 项目级上下文。
179 lines
9.2 KiB
JavaScript
179 lines
9.2 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* 灌入演示数据 —— 让新环境打开就能看到完整的 sswflow 场景,而不是空表。
|
||
* 用法:node scripts/seed-demo.js [后端地址]
|
||
* 默认 http://127.0.0.1:30000
|
||
*/
|
||
const B = process.argv[2] || 'http://127.0.0.1:30000';
|
||
// crypto-js 装在 src/backend/node_modules 下,从项目根的 scripts/ 跑时解析不到,
|
||
// 显式指向后端依赖目录。
|
||
const Crypto = require('../src/backend/node_modules/crypto-js');
|
||
|
||
function enc(pwd) {
|
||
const k = Crypto.enc.Utf8.parse(Crypto.MD5('EY8WePvjM5GGwQzn').toString());
|
||
const s = Crypto.enc.Utf8.parse(Crypto.MD5(pwd).toString());
|
||
return Crypto.AES.encrypt(s, k, { mode: Crypto.mode.ECB, padding: Crypto.pad.Pkcs7 })
|
||
.ciphertext.toString(Crypto.enc.Hex);
|
||
}
|
||
|
||
async function api(path, method = 'GET', body, token) {
|
||
const res = await fetch(`${B}${path}`, {
|
||
method,
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
...(token ? { Authorization: `bearer ${token}` } : {}),
|
||
},
|
||
body: body ? JSON.stringify(body) : undefined,
|
||
});
|
||
return res.json();
|
||
}
|
||
|
||
(async () => {
|
||
const login = await api('/api/oauth/Login', 'POST');
|
||
// Login 是表单提交,改用 fetch + URLSearchParams
|
||
const form = new URLSearchParams({
|
||
account: 'admin', password: enc('123456'), grant_type: 'password',
|
||
});
|
||
const lr = await fetch(`${B}/api/oauth/Login`, {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||
body: form.toString(),
|
||
});
|
||
const lj = await lr.json();
|
||
if (lj.code !== 200) { console.error('登录失败:', lj.msg); process.exit(1); }
|
||
const T = lj.data.token;
|
||
console.log('✓ 登录 admin');
|
||
|
||
const post = (p, b) => api(p, 'POST', b, T);
|
||
const put = (p, b) => api(p, 'PUT', b, T);
|
||
|
||
// ---- 参与方 ----
|
||
const parties = [
|
||
{ partyCode: 'P001', name: '上海数科', orgName: '上海数科信息科技有限公司', endpoint: 'https://shdsk.internal:8443', role: 'initiator', status: 'ACTIVE', selfMark: 1, computeMode: 'MPC' },
|
||
{ partyCode: 'P002', name: '深圳智算', orgName: '深圳智算科技有限公司', endpoint: 'https://szzk.internal:8443', role: 'participant', status: 'ACTIVE', selfMark: 0, computeMode: 'MPC' },
|
||
{ partyCode: 'P003', name: '北京数安', orgName: '北京数安研究院', endpoint: 'https://bjsan.internal:8443', role: 'participant', status: 'ACTIVE', selfMark: 0, computeMode: 'TEE' },
|
||
];
|
||
const partyIds = [];
|
||
for (const p of parties) {
|
||
const r = await post('/api/ssw/sswflow/Party/Save', p);
|
||
if (r.code === 200) { partyIds.push(r.data.id); console.log(' ✓ 参与方', p.name); }
|
||
}
|
||
|
||
// ---- 协作项目 ----
|
||
const projects = [
|
||
{ projectCode: 'PRJ001', name: '联合风控建模', desc: '银行 + 电商联合反欺诈模型', ownerPartyId: partyIds[0], computeMode: 'MPC', status: 'RUNNING' },
|
||
{ projectCode: 'PRJ002', name: '跨机构特征互补', desc: '联合特征工程与画像', ownerPartyId: partyIds[1], computeMode: 'MPC', status: 'PREPARING' },
|
||
];
|
||
const projectIds = [];
|
||
for (const p of projects) {
|
||
const r = await post('/api/ssw/sswflow/Project/Save', p);
|
||
if (r.code === 200) { projectIds.push(r.data.id); console.log(' ✓ 项目', p.name); }
|
||
}
|
||
|
||
// ---- 项目成员 ----
|
||
for (let i = 0; i < partyIds.length; i++) {
|
||
await post('/api/ssw/sswflow/ProjectParty/Save', {
|
||
projectId: projectIds[0], partyId: partyIds[i],
|
||
role: i === 0 ? 'owner' : 'participant', joinStatus: i === 0 ? 'JOINED' : 'INVITED',
|
||
});
|
||
}
|
||
console.log(' ✓ 项目成员关系');
|
||
|
||
// ---- 数据源 ----
|
||
const dsIds = [];
|
||
const dsList = [
|
||
{ name: '电商交易库', partyId: partyIds[1], type: 'MySQL', status: 'ONLINE', desc: '订单与支付流水' },
|
||
{ name: '银行信贷库', partyId: partyIds[0], type: 'PostgreSQL', status: 'ONLINE', desc: '授信与还款记录' },
|
||
];
|
||
for (const d of dsList) {
|
||
const r = await post('/api/ssw/sswflow/Datasource/Save', d);
|
||
if (r.code === 200) { dsIds.push(r.data.id); console.log(' ✓ 数据源', d.name); }
|
||
}
|
||
|
||
// ---- 数据集 + 授权 ----
|
||
const datasetIds = [];
|
||
const dsDef = [
|
||
{ datasetCode: 'DS001', name: '交易明细表', partyId: partyIds[1], datasourceId: dsIds[0], storageType: 'MySQL', location: 'ods.trade_detail', rowCount: 1280000, externalRef: 'trade_detail_2026' },
|
||
{ datasetCode: 'DS002', name: '授信申请表', partyId: partyIds[0], datasourceId: dsIds[1], storageType: 'PostgreSQL', location: 'public.credit_apply', rowCount: 860000, externalRef: 'credit_apply' },
|
||
];
|
||
for (const d of dsDef) {
|
||
const r = await post('/api/ssw/sswflow/Dataset/Save', d);
|
||
if (r.code === 200) { datasetIds.push(r.data.id); console.log(' ✓ 数据集', d.name); }
|
||
}
|
||
await post('/api/ssw/sswflow/DatasetAuth/Save', {
|
||
datasetId: datasetIds[1], projectId: projectIds[0], authStatus: 'APPROVED',
|
||
authUserId: 'admin', expireTime: new Date(Date.now() + 90 * 864e5).toISOString(),
|
||
});
|
||
await post('/api/ssw/sswflow/DatasetAuth/Save', {
|
||
datasetId: datasetIds[0], projectId: projectIds[0], authStatus: 'PENDING',
|
||
});
|
||
console.log(' ✓ 数据集授权');
|
||
|
||
// ---- 流程(两条完整 DAG)----
|
||
const flowDefs = [
|
||
{
|
||
name: '联合反欺诈模型训练', desc: '电商交易 + 银行授信 → 隐私求交 → 逻辑回归 → 导出',
|
||
projectId: projectIds[0],
|
||
steps: [
|
||
{ stepId: 'src-trade', operatorKey: 'source.dataset', label: '交易明细', x: 40, y: 60, params: { datasetId: datasetIds[0] } },
|
||
{ stepId: 'src-credit', operatorKey: 'source.dataset', label: '授信申请', x: 40, y: 220, params: { datasetId: datasetIds[1] } },
|
||
{ stepId: 'psi', operatorKey: 'prep.intersect', label: '隐私求交', x: 300, y: 140, params: { leftKeys: ['user_id'], rightKeys: ['cust_no'], protocol: 'ECDH', receiver: 'shdsk', keepOrder: true } },
|
||
{ stepId: 'split', operatorKey: 'prep.split', label: '训练集拆分', x: 540, y: 140, params: { testRatio: 0.2 } },
|
||
{ stepId: 'lr', operatorKey: 'model.lr.train', label: '逻辑回归训练', x: 780, y: 60, params: { labelColumn: 'is_fraud', n: 64, d: 16, epochs: 30, learningRate: 0.05 } },
|
||
{ stepId: 'eval', operatorKey: 'model.evaluate', label: '模型评估', x: 780, y: 220, params: { labelColumn: 'is_fraud' } },
|
||
{ stepId: 'export', operatorKey: 'sink.export', label: '模型导出', x: 1020, y: 140, params: { targetParty: 'shdsk', fileName: 'fraud_lr_model.bin' } },
|
||
],
|
||
edges: [
|
||
{ edgeId: 'e1', sourceStepId: 'src-trade', sourcePort: 'out', targetStepId: 'psi', targetPort: 'left' },
|
||
{ edgeId: 'e2', sourceStepId: 'src-credit', sourcePort: 'out', targetStepId: 'psi', targetPort: 'right' },
|
||
{ edgeId: 'e3', sourceStepId: 'psi', sourcePort: 'out', targetStepId: 'split', targetPort: 'in' },
|
||
{ edgeId: 'e4', sourceStepId: 'split', sourcePort: 'train', targetStepId: 'lr', targetPort: 'in' },
|
||
{ edgeId: 'e5', sourceStepId: 'split', sourcePort: 'test', targetStepId: 'eval', targetPort: 'in' },
|
||
{ edgeId: 'e6', sourceStepId: 'lr', sourcePort: 'model', targetStepId: 'export', targetPort: 'in' },
|
||
],
|
||
},
|
||
{
|
||
name: '密态特征相关性分析', desc: 'SCQL 密态 SQL 执行统计查询',
|
||
projectId: projectIds[1],
|
||
steps: [
|
||
{ stepId: 'src2', operatorKey: 'source.dataset', label: '联合特征集', x: 60, y: 80, params: { datasetId: datasetIds[0] } },
|
||
{ stepId: 'profile', operatorKey: 'stat.profile', label: '全表统计', x: 320, y: 80, params: {} },
|
||
{ stepId: 'corr', operatorKey: 'stat.correlation', label: '相关性分析', x: 560, y: 80, params: { method: 'pearson' } },
|
||
{ stepId: 'iv', operatorKey: 'stat.iv', label: 'IV 评估', x: 800, y: 80, params: { labelColumn: 'y' } },
|
||
],
|
||
edges: [
|
||
{ edgeId: 'f1', sourceStepId: 'src2', sourcePort: 'out', targetStepId: 'profile', targetPort: 'in' },
|
||
{ edgeId: 'f2', sourceStepId: 'profile', sourcePort: 'report', targetStepId: 'corr', targetPort: 'in' },
|
||
{ edgeId: 'f3', sourceStepId: 'corr', sourcePort: 'report', targetStepId: 'iv', targetPort: 'in' },
|
||
],
|
||
},
|
||
];
|
||
|
||
const flowIds = [];
|
||
for (const f of flowDefs) {
|
||
const r = await post('/api/ssw/sswflow/Flow/Save', {
|
||
name: f.name, desc: f.desc, projectId: f.projectId,
|
||
steps: JSON.stringify(f.steps), edges: JSON.stringify(f.edges),
|
||
});
|
||
if (r.code === 200) { flowIds.push(r.data.id); console.log(' ✓ 流程', f.name, `(${f.steps.length} 节点)`); }
|
||
}
|
||
|
||
// ---- 周期调度 ----
|
||
await post('/api/ssw/sswflow/FlowSchedule/Save', {
|
||
flowId: flowIds[0], projectId: projectIds[0], name: '每日凌晨重训',
|
||
cron: '0 2 * * *', enabledMark: 0, misfirePolicy: 'FIRE_NOW', runScope: 'FULL',
|
||
});
|
||
console.log(' ✓ 周期调度');
|
||
|
||
// ---- 跑一次,留下运行实例和事件 ----
|
||
const run = await post('/api/ssw/sswflow/FlowRun/Start', {
|
||
flowId: flowIds[0], trigger: 'MANUAL', mode: 'FULL',
|
||
});
|
||
if (run.code === 200) {
|
||
console.log(' ✓ 已触发一次运行:', run.data.id);
|
||
await new Promise((r) => setTimeout(r, 6000));
|
||
}
|
||
|
||
console.log('\n完成。打开 http://127.0.0.1:5173 登录 admin / 123456 查看。');
|
||
})().catch((e) => { console.error('失败:', e.message); process.exit(1); });
|