You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/public/cloud/admin-operation-presentatio...

102 lines
4.2 KiB
JavaScript

const terminalStates = new Set(['succeeded', 'failed']);
export function operationText(value, fallback = '未记录') {
return value === null || value === undefined || String(value).trim() === ''
? fallback : String(value);
}
export function operationDate(value) {
if (!value) return '时间未记录';
const numeric = Number(value);
const parsed = Number.isFinite(numeric)
? new Date(numeric * (numeric < 1e12 ? 1000 : 1)) : new Date(String(value));
if (!Number.isFinite(parsed.getTime())) return '时间未记录';
return parsed.toLocaleString('zh-CN', { hour12: false });
}
export function presentOperation(task) {
if (!task || typeof task !== 'object' || Array.isArray(task)
|| !String(task.id || '').trim()) {
return {
terminal: true,
status: 'failed',
label: '协议错误',
tone: 'text-danger',
protocolError: true,
};
}
if (task.completed === false && task.status === 'active') {
return {
terminal: false,
status: 'running',
label: '执行中',
tone: 'text-primary',
protocolError: false,
};
}
if (task.completed === true && typeof task.status === 'string'
&& terminalStates.has(task.status)) {
if (task.recovery?.pending === true) {
const code = String(task.recovery?.code || task.finalization_error_code || '');
const label = code === 'recovery_dispatch_ledger_conflict'
|| task.dispatch_state === 'invalid' ? '派发账本异常 · 待修复'
: task.dispatch_state === 'not_dispatched' ? 'PHP 本地账本收尾中'
: task.status === 'failed' ? '已失败 · 持续恢复中' : '安全恢复中';
return {
terminal: true,
status: task.status,
label,
tone: 'text-warning',
protocolError: false,
};
}
return {
terminal: true,
status: task.status,
label: { succeeded: '已成功', failed: '已失败' }[task.status],
tone: { succeeded: 'text-success', failed: 'text-danger' }[task.status],
protocolError: false,
};
}
return {
terminal: true,
status: 'failed',
label: '协议错误',
tone: 'text-danger',
protocolError: true,
};
}
export function operationFailureText(job, view = presentOperation(job)) {
if (view.protocolError) {
return '任务数据不符合公开协议;资源按可能已生效处理并保持锁定。';
}
if (job.recovery?.pending === true) {
const code = String(job.recovery?.code || job.finalization_error_code || '');
const message = operationText(job.recovery.message, '系统仍会继续恢复。');
if (code === 'recovery_dispatch_ledger_conflict' || job.dispatch_state === 'invalid') {
return `派发账本异常,任务原终态保持不变,资源继续安全锁定;修复账本后系统会重新核验。${message}`;
}
if (job.dispatch_state === 'not_dispatched') {
return `宿主机操作已确认未派发,但 PHP 本地账本收尾尚未完成;资源保持安全锁定,系统会持续重试本地收尾。${message}`;
}
return `任务尚在持续恢复,资源保持安全锁定;系统不会重复执行原操作。${message}`;
}
if (view.status !== 'failed') return '';
if (job.kind === 'host_storage_configure') {
return '旧存储任务已终止且不会重放;部分固定存储池可能已应用。可到宿主机存储目录页修正后提交新的独立任务。';
}
if (job.effect_may_have_applied === true && job.effect_resolution === 'unknown') {
return '操作效果可能已发生;资源已安全锁定,需管理员人工处理。';
}
if (job.effect_may_have_applied === true && job.effect_resolution === 'applied'
&& job.effect_scope === 'partial') {
return '任务仍为失败;系统已确认部分宿主机已应用、部分未应用,未确认状态已收敛,任务级安全栅栏已解除。';
}
if (job.effect_may_have_applied === true
&& ['applied', 'not_applied'].includes(job.effect_resolution)) {
return `任务仍为失败;系统已确认操作${job.effect_resolution === 'applied' ? '已应用' : '未应用'},任务级安全栅栏已解除。`;
}
return operationText(job.error, '任务执行失败。');
}