/** * 金融联合风控 · 真实场景装置(fixture) * * 用一套贯穿全流程的真实业务角色与真实数据,替换掉「随机串 + 占位字段」式的测试数据: * * 零售银行信贷部(数据提供方) 持有账户还款行为 + 12 个月违约标签,标签方 * 持牌征信机构(数据服务方) 持有外部征信评分 + 多头借贷指标,特征方 * 消费金融风控中心(数据使用方)联合建模发起方与结果接收方 * * 数据是 fixtures/gen_riskctrl_data.py 生成的、带业务因果的真实感样本 * (逾期次数↑/额度使用率↑/多头查询↑ → 违约概率↑;征信分↑/收入↑ → 违约概率↓, * 整体违约率约 10.6%,与真实零售信贷量级一致)。 * * 计算是真的:数据集 location 指向真实 CSV,SSWFlow 把它注入 StepContext, * NaxComputeEngine 起 Python 子进程调 nax_adapter.py,用 pandas/numpy 真跑 * PSI 求交、纵向逻辑回归、打分与 KS/AUC 评估——断言的是真实算出来的指标, * 不是脚本里写死的期望值。 */ import path from 'node:path' import { fileURLToPath } from 'node:url' const HERE = path.dirname(fileURLToPath(import.meta.url)) export const FIXTURE_DIR = path.resolve(HERE, '../fixtures') /** 三家参与机构(partyCode 幂等键) */ export const PARTIES = [ { partyCode: 'BANK_RETAIL', name: '零售银行信贷部', orgName: '华夏零售银行股份有限公司', role: 'GUEST', status: 'ONLINE', desc: '数据提供方:持有账户还款行为与逾期违约标签' }, { partyCode: 'CREDIT_BUREAU', name: '持牌征信机构', orgName: '中金征信服务有限公司', role: 'GUEST', status: 'ONLINE', desc: '数据服务方:持有外部征信评分与多头借贷指标' }, { partyCode: 'CONSUMER_FIN', name: '消费金融风控中心', orgName: '普惠消费金融有限公司', role: 'GUEST', status: 'ONLINE', desc: '数据使用方:联合建模发起方与评分卡结果接收方' } ] export const PROJECT_NAME = '金融联合风控·联合信用评分' /** 两份真实数据集(datasetCode 幂等键) */ export const DATASETS = [ { key: 'bank', datasetCode: 'DS_BANK_CREDIT', name: '零售银行·账户还款行为表', owner: 'BANK_RETAIL', file: 'bank_credit_features.csv', rowCount: 4000, columns: ['uid', 'age', 'monthly_income', 'credit_limit', 'used_ratio', 'overdue_cnt_12m', 'acct_age_month', 'default_flag'], desc: '4000 名零售信贷客户的账户特征与 12 个月违约标签(default_flag),标签方数据' }, { key: 'bureau', datasetCode: 'DS_BUREAU_SCORE', name: '征信机构·外部信用评分表', owner: 'CREDIT_BUREAU', file: 'bureau_credit_scores.csv', rowCount: 3600, columns: ['uid', 'credit_score', 'query_cnt_3m', 'loan_org_cnt', 'has_guarantee'], desc: '3600 名自然人的征信评分、近 3 月查询次数、多头借贷机构数,特征方数据' } ] export const LABEL_COLUMN = 'default_flag' export const JOIN_KEY = 'uid' // --------------------------------------------------------------------------- 场景装配 /** * 幂等准备场景:三家机构 → 项目 → 项目成员 → 两份真实数据集。 * 已存在的直接复用(按 partyCode / name / datasetCode 命中),跑多少遍都不会造重复垃圾数据。 */ export async function ensureScene(t) { const ids = { party: {}, dataset: {} } // 1) 三家机构 const exist = await t.apiList('/api/ssw/sswflow/Party', { pageSize: 200 }) for (const p of PARTIES) { const hit = exist.find((x) => x.partyCode === p.partyCode) if (hit) { ids.party[p.partyCode] = hit.id continue } const r = await t.api('POST', '/api/ssw/sswflow/Party', p) if (r.code !== 200) throw new Error(`创建参与方 ${p.partyCode} 失败:${r.msg}`) ids.party[p.partyCode] = typeof r.data === 'string' ? r.data : r.data?.id } // 2) 联合建模项目 const projects = await t.apiList('/api/ssw/sswflow/Project', { pageSize: 200 }) const prj = projects.find((x) => x.name === PROJECT_NAME) if (prj) { ids.projectId = prj.id } else { const r = await t.api('POST', '/api/ssw/sswflow/Project', { name: PROJECT_NAME, desc: '银行 × 征信机构纵向联邦建模,产出反欺诈/信用评分模型,原始数据不出域', computeMode: 'MPC', ownerPartyId: ids.party.CONSUMER_FIN, status: 'RUNNING' }) if (r.code !== 200) throw new Error(`创建项目失败:${r.msg}`) ids.projectId = typeof r.data === 'string' ? r.data : r.data?.id } // 3) 项目成员(授权投票的选民名单来自这里,缺成员会导致授权无法聚合通过) const members = (await t.api('GET', `/api/ssw/sswflow/Project/${ids.projectId}/Party`))?.data || [] for (const p of PARTIES) { const pid = ids.party[p.partyCode] const hit = members.find((m) => m.partyId === pid) if (hit) { if (hit.joinStatus !== 'JOINED' && hit.id) { await t.api('PUT', `/api/ssw/sswflow/Project/${ids.projectId}/Party/${hit.id}/Accept`) } continue } const r = await t.api('POST', `/api/ssw/sswflow/Project/${ids.projectId}/Party`, { partyId: pid, role: p.partyCode === 'CONSUMER_FIN' ? 'HOST' : 'GUEST' }) const linkId = typeof r.data === 'string' ? r.data : r.data?.id if (r.code === 200 && linkId) { await t.api('PUT', `/api/ssw/sswflow/Project/${ids.projectId}/Party/${linkId}/Accept`) } } // 4) 两份真实数据集(location 指向本地真实 CSV,引擎按这个路径 pd.read_csv) const existDs = await t.apiList('/api/ssw/sswflow/Dataset', { pageSize: 200 }) for (const d of DATASETS) { const hit = existDs.find((x) => x.datasetCode === d.datasetCode) if (hit) { ids.dataset[d.key] = hit.id continue } const r = await t.api('POST', '/api/ssw/sswflow/Dataset', { datasetCode: d.datasetCode, name: d.name, partyId: ids.party[d.owner], storageType: 'LOCAL_CSV', location: path.join(FIXTURE_DIR, d.file), desc: d.desc, rowCount: d.rowCount, columns: JSON.stringify(d.columns) }) if (r.code !== 200) throw new Error(`登记数据集 ${d.datasetCode} 失败:${r.msg}`) ids.dataset[d.key] = typeof r.data === 'string' ? r.data : r.data?.id } return ids } /** * 数据集对项目授权(“可用不可见”的准入闸门): * 发起申请 → 各参与方投票 → 全票通过才 APPROVED,之后才会出现在 /Dataset/Authorized。 * 已经 APPROVED 的直接复用。返回 { authId, status, alreadyApproved }。 */ export async function ensureDatasetAuth(t, datasetId, projectId, partyIds) { const auths = (await t.api('GET', `/api/ssw/sswflow/Dataset/${datasetId}/Auth`))?.data || [] let auth = auths.find((a) => a.projectId === projectId && a.authStatus === 'APPROVED') if (auth) return { authId: auth.id, status: 'APPROVED', alreadyApproved: true } auth = auths.find((a) => a.projectId === projectId && a.authStatus === 'PENDING') let authId = auth?.id if (!authId) { const r = await t.api('POST', `/api/ssw/sswflow/Dataset/${datasetId}/Auth`, { projectId }) if (r.code !== 200) throw new Error(`发起数据集授权失败:${r.msg}`) authId = typeof r.data === 'string' ? r.data : r.data?.id } // 逐个参与方投同意票;持有方在申请时已自动同意 const votes = (await t.api('GET', `/api/ssw/sswflow/Dataset/Auth/${authId}/Votes`))?.data || [] for (const v of votes) { if (v.voteStatus === 'APPROVED') continue await t.api('POST', `/api/ssw/sswflow/Dataset/Auth/${authId}/Vote`, { partyId: v.partyId, voteStatus: 'APPROVED', comment: '合规审查通过:用途限于联合信用评分建模,原始数据不出域' }) } const after = (await t.api('GET', `/api/ssw/sswflow/Dataset/${datasetId}/Auth`))?.data || [] const now = after.find((a) => a.id === authId) return { authId, status: now?.authStatus, alreadyApproved: false } } // --------------------------------------------------------------------------- 画布 /** 联合建模画布:银行 + 征信 → PSI → 拆分 → 纵向 LR → 打分 → KS/AUC → 出域 */ export function jointCanvas(ids) { const steps = [ { stepId: 's1', operatorKey: 'source.dataset', label: '银行侧·账户还款行为', params: { datasetId: ids.dataset.bank }, position: { x: 0, y: 0 } }, { stepId: 's2', operatorKey: 'source.dataset', label: '征信侧·外部信用评分', params: { datasetId: ids.dataset.bureau }, position: { x: 0, y: 180 } }, { stepId: 's3', operatorKey: 'prep.intersect', label: '隐私求交(ECDH-PSI)', params: { leftKeys: [JOIN_KEY], rightKeys: [JOIN_KEY], protocol: 'ECDH', receiver: 'CONSUMER_FIN' }, position: { x: 240, y: 90 } }, { stepId: 's4', operatorKey: 'prep.split', label: '训练/测试拆分 7:3', params: { trainRate: 0.7, shuffle: true }, position: { x: 480, y: 90 } }, { stepId: 's5', operatorKey: 'model.lr.train', label: '纵向联邦逻辑回归', params: { labelColumn: [LABEL_COLUMN], epochs: 60, learningRate: 0.3, regularization: 'l2', lambda: 0.01 }, position: { x: 720, y: 0 } }, { stepId: 's6', operatorKey: 'model.predict', label: '测试集打分', params: { scoreColumn: 'score' }, position: { x: 960, y: 90 } }, { stepId: 's7', operatorKey: 'model.evaluate', label: '风控效果评估(KS/AUC)', params: { labelColumn: [LABEL_COLUMN], scoreColumn: 'score', bucketNum: 10 }, position: { x: 1200, y: 90 } }, { stepId: 's8', operatorKey: 'sink.export', label: '评分卡结果出域', params: { targetParty: 'CONSUMER_FIN', fileName: 'joint_credit_scorecard.csv', format: 'csv', needApproval: false }, position: { x: 1440, y: 90 } } ] const edges = [ { sourceStepId: 's1', targetStepId: 's3', sourcePort: 'out', targetPort: 'left' }, { sourceStepId: 's2', targetStepId: 's3', sourcePort: 'out', targetPort: 'right' }, { sourceStepId: 's3', targetStepId: 's4', sourcePort: 'out', targetPort: 'in' }, { sourceStepId: 's4', targetStepId: 's5', sourcePort: 'train', targetPort: 'in' }, { sourceStepId: 's5', targetStepId: 's6', sourcePort: 'model', targetPort: 'model' }, { sourceStepId: 's4', targetStepId: 's6', sourcePort: 'test', targetPort: 'in' }, { sourceStepId: 's6', targetStepId: 's7', sourcePort: 'out', targetPort: 'in' }, { sourceStepId: 's7', targetStepId: 's8', sourcePort: 'report', targetPort: 'in' } ] return { steps, edges } } /** 银行单方基线画布:只用银行自己的 6 个特征建模,用来量化「融合了征信数据到底多赚多少」 */ export function bankSoloCanvas(ids) { const steps = [ { stepId: 'b1', operatorKey: 'source.dataset', label: '银行侧·账户还款行为', params: { datasetId: ids.dataset.bank }, position: { x: 0, y: 0 } }, { stepId: 'b2', operatorKey: 'prep.split', label: '训练/测试拆分 7:3', params: { trainRate: 0.7, shuffle: true }, position: { x: 240, y: 0 } }, { stepId: 'b3', operatorKey: 'model.lr.train', label: '单方逻辑回归(基线)', params: { labelColumn: [LABEL_COLUMN], epochs: 60, learningRate: 0.3, regularization: 'l2', lambda: 0.01 }, position: { x: 480, y: -80 } }, { stepId: 'b4', operatorKey: 'model.predict', label: '测试集打分', params: { scoreColumn: 'score' }, position: { x: 720, y: 0 } }, { stepId: 'b5', operatorKey: 'model.evaluate', label: '基线效果评估(KS/AUC)', params: { labelColumn: [LABEL_COLUMN], scoreColumn: 'score', bucketNum: 10 }, position: { x: 960, y: 0 } }, { stepId: 'b6', operatorKey: 'sink.export', label: '基线结果留存', params: { targetParty: 'BANK_RETAIL', fileName: 'bank_solo_baseline.csv', format: 'csv', needApproval: false }, position: { x: 1200, y: 0 } } ] const edges = [ { sourceStepId: 'b1', targetStepId: 'b2', sourcePort: 'out', targetPort: 'in' }, { sourceStepId: 'b2', targetStepId: 'b3', sourcePort: 'train', targetPort: 'in' }, { sourceStepId: 'b3', targetStepId: 'b4', sourcePort: 'model', targetPort: 'model' }, { sourceStepId: 'b2', targetStepId: 'b4', sourcePort: 'test', targetPort: 'in' }, { sourceStepId: 'b4', targetStepId: 'b5', sourcePort: 'out', targetPort: 'in' }, { sourceStepId: 'b5', targetStepId: 'b6', sourcePort: 'report', targetPort: 'in' } ] return { steps, edges } } /** 幂等建流程并写入画布,返回 flowId * 注意:后端 getAlive/getOne 对 f_steps/f_edges 的读取存在不稳定(同名旧流程会读到空画布, * 导致 FlowRun/Start 报「输入端口未连线」)。为彻底避开陈旧/空画布,这里用「runId 唯一后缀」 * 每次新建一个全新流程并写入画布,保证 getById 读到的就是本次写入的内容。 */ export async function ensureFlow(t, projectId, name, canvas, desc = '', runId = '') { const suffix = runId ? `-${runId}` : '' const uniqueName = `${name}${suffix}` const r = await t.api('POST', '/api/ssw/sswflow/Flow', { projectId, name: uniqueName, desc }) if (r.code !== 200) throw new Error(`创建流程失败:${r.msg}`) const flowId = typeof r.data === 'string' ? r.data : r.data?.id const cv = await t.api('PUT', `/api/ssw/sswflow/Flow/${flowId}/Canvas`, { steps: JSON.stringify(canvas.steps), edges: JSON.stringify(canvas.edges) }) if (cv.code !== 200) throw new Error(`保存画布失败:${cv.msg}`) // 浏览器 FlowEditor 加载时 fit-view-on-init 触发 nodes 数组的 deep watch,800ms 防抖后 // 会再发一次 Canvas PUT。如果不稍作等待就进入下一步,后续 GET / Validate / Start 可能正好 // 落在浏览器自动保存触发 → saveCanvas 还没执行 的窗口里,导致读到旧画布触发「端口未连线」。 // 1.5s 已足够稳定让两次 Canvas PUT 都落库,浏览器那一次空画布 PUT 也会被覆盖回完整版。 await new Promise((r) => setTimeout(r, 1500)) return flowId } // --------------------------------------------------------------------------- 运行 /** 发起运行并轮询到终态,返回 { runId, status, events, artifacts, costMs } */ export async function runFlow(t, flowId, { maxWaitMs = 180000, intervalMs = 2000 } = {}) { const r = await t.api('POST', '/api/ssw/sswflow/FlowRun/Start', { flowId, mode: 'FULL' }) if (r.code !== 200) throw new Error(`发起运行失败:${r.msg}`) const runId = r.data?.id || (typeof r.data === 'string' ? r.data : null) const deadline = Date.now() + maxWaitMs let status = 'PENDING' let costMs = 0 while (Date.now() < deadline) { await new Promise((res) => setTimeout(res, intervalMs)) const s = await t.api('GET', `/api/ssw/sswflow/FlowRun/${runId}`) status = s?.data?.status costMs = s?.data?.costMs || 0 if (status === 'SUCCESS' || status === 'FAILED' || status === 'STOPPED') break } const ev = await t.api('GET', `/api/ssw/sswflow/FlowRun/${runId}/Events?afterSeq=0`) const arts = await t.api('GET', `/api/ssw/sswflow/FlowRun/${runId}/Artifacts`) const artifacts = arts?.data?.list || (Array.isArray(arts?.data) ? arts.data : []) return { runId, status, costMs, events: ev?.data?.events || [], artifacts } } /** 从产物里取某节点某端口 */ export function findArtifact(artifacts, stepId, portKey) { return artifacts.find((a) => a.stepId === stepId && a.portKey === portKey) } /** 产物 metrics 兼容字符串/对象两种落库形态 */ export function metricsOf(artifact) { if (!artifact) return {} const m = artifact.metrics ?? artifact.f_metrics if (!m) return {} if (typeof m === 'string') { try { return JSON.parse(m) } catch { return {} } } return m } /** 事件里挑第一条命中关键字的消息,用于把真实引擎日志带进断言文案 */ export function eventText(events, keyword) { const hit = (events || []).find((e) => (e.message || '').includes(keyword)) return hit?.message || '' }