Files
image-workbench-desktop/src/runtime-diagnostics.js
T

57 lines
2.8 KiB
JavaScript

;(function (global) {
'use strict'
const safeKeys = ['status', 'degraded', 'lastCheckedAt', 'error', 'checkedAt', 'ok']
function own(value) { return value && typeof value === 'object' ? value : {} }
function healthSnapshot(health) {
const input = own(health)
const result = {}
Object.keys(input).forEach((name) => {
const value = input[name]
if (!value || typeof value !== 'object') {
if (safeKeys.indexOf(name) >= 0) result[name] = value
return
}
const item = {}
safeKeys.forEach((key) => { if (value[key] !== undefined) item[key] = value[key] })
result[name] = item
})
return result
}
function taskSnapshot(tasks) {
const list = Array.isArray(tasks) ? tasks : []
return list.slice(0, 100).map((task) => {
const value = own(task)
return {
id: value.id ? String(value.id) : '',
nodeId: value.nodeId ? String(value.nodeId) : '',
state: value.state ? String(value.state) : '',
attempts: Number(value.attempts) || 0,
progress: value.progress && typeof value.progress === 'object' ? { done: Number(value.progress.done) || 0, total: Number(value.progress.total) || 0 } : undefined,
error: value.error && typeof value.error === 'object' ? { category: value.error.category, code: value.error.code, retryable: !!value.error.retryable } : undefined
}
})
}
function create(options) {
const opt = options || {}
return {
version: String(opt.version || 'unknown'),
commit: String(opt.commit || 'unknown'),
generatedAt: Date.now(),
browser: opt.browser ? String(opt.browser).slice(0, 120) : undefined,
workspace: { id: opt.workspaceId ? String(opt.workspaceId) : '', nodeCount: Number(opt.nodeCount) || 0 },
tasks: taskSnapshot(opt.tasks),
storage: healthSnapshot(opt.storage),
lifecycle: opt.lifecycle && typeof opt.lifecycle.snapshot === 'function' ? own(opt.lifecycle.snapshot()) : {},
scheduler: opt.scheduler && typeof opt.scheduler.stats === 'function' ? own(opt.scheduler.stats()) : {},
metrics: opt.metrics && typeof opt.metrics === 'object' ? own(opt.metrics) : {},
// v3.87.12:允许携带额外诊断段。原来这里是**固定白名单** → 新加的 crash(崩溃记录/内存采样/面包屑)
// 被静默丢弃,用户看到的导出"内容不完整",也无法据此定位崩溃。
crash: opt.crash && typeof opt.crash === 'object' ? own(opt.crash) : undefined,
...(opt.extra && typeof opt.extra === 'object' ? opt.extra : {})
}
}
function toJson(options) { return JSON.stringify(create(options), null, 2) }
global.IWBRuntimeDiagnostics = { create, toJson, healthSnapshot, taskSnapshot }
})(typeof window !== 'undefined' ? window : globalThis)