统一全系统资源加载、轻量趋势与缓存恢复

This commit is contained in:
2026-11-02 09:54:01 +08:00
parent 8b8f9b7728
commit b711327c8e
58 changed files with 3150 additions and 661 deletions
+24 -15
View File
@@ -10,6 +10,8 @@ import type { AuthUser } from './types';
import { BackupOperationsPanel, OperationsMonitorPanel } from './GovernanceCompletion';
import { configurationDateTime } from './configurationName';
import { backupSummary, isActive, isFailed, monitorLabel, orderedTasks, taskFailure, taskPercent, type MonitorRow } from './taskMonitorModel';
import { useDelayedBusy } from './useDelayedBusy';
import { useVisiblePolling } from './useVisiblePolling';
export function TasksMonitorPage({user,onUnauthorized}:{user:AuthUser;onUnauthorized:(error:unknown)=>boolean}) {
const {message}=App.useApp();
@@ -18,29 +20,35 @@ export function TasksMonitorPage({user,onUnauthorized}:{user:AuthUser;onUnauthor
const [items,setItems]=useState<MonitorRow[]>([]);
const [backup,setBackup]=useState<MonitorRow>({});
const [taskError,setTaskError]=useState(''); const [backupError,setBackupError]=useState('');
const [loading,setLoading]=useState(true);
const loaded = useRef(false);
const [tasksLoaded,setTasksLoaded]=useState(false); const [backupLoaded,setBackupLoaded]=useState(false);
const [section,setSection] = useState('tasks');
const taskBusy = useDelayedBusy(); const backupBusy = useDelayedBusy();
const loading = sectionLoading();
const [filter,setFilter]=useState('all'); const [selected,setSelected]=useState<MonitorRow|null>(null);
const actionInFlight = useRef(false);
const canManage=user.roles.some(role=>['SYSTEM_ADMIN','SYSTEM_OPERATOR'].includes(role));
const load=useCallback(async()=>{
setLoading(true);
await Promise.all([
api.taskOverview().then(value=>{setItems(value.items);setTaskError('');}).catch(error=>{if(!onUnauthorized(error))setTaskError(error instanceof Error?error.message:'任务读取失败');}),
api.latestBackup().then(value=>{setBackup(value);setBackupError('');}).catch(error=>{if(!onUnauthorized(error))setBackupError(error instanceof Error?error.message:'备份状态读取失败');}),
]);setLoading(false);
},[onUnauthorized]);
useEffect(()=>{if(!loaded.current){loaded.current=true;void load();}},[load]);
function sectionLoading() { return section === 'tasks' ? taskBusy.busy : backupBusy.busy; }
const loadTasks=useCallback(()=>taskBusy.run(async()=>{
try { const value=await api.taskOverview();setItems(value.items);setTaskError('');setTasksLoaded(true); }
catch(error){ if(!onUnauthorized(error))setTaskError(error instanceof Error?error.message:'任务读取失败'); }
}),[onUnauthorized,taskBusy.run]);
const loadBackup=useCallback(()=>backupBusy.run(async()=>{
try { const value=await api.latestBackup();setBackup(value);setBackupError('');setBackupLoaded(true); }
catch(error){ if(!onUnauthorized(error))setBackupError(error instanceof Error?error.message:'备份状态读取失败'); }
}),[onUnauthorized,backupBusy.run]);
const load=useCallback(()=>section==='tasks'?loadTasks():loadBackup(),[section,loadTasks,loadBackup]);
useEffect(()=>{if(!active)return;if(section==='tasks'&&!tasksLoaded)void loadTasks();if(section==='latestBackup'&&!backupLoaded)void loadBackup();},[active,section,tasksLoaded,backupLoaded,loadTasks,loadBackup]);
useMaintenanceRefresh(load);
useEffect(()=>{if(!active || !['tasks','latestBackup'].includes(section))return;const timer=window.setInterval(()=>{if(document.visibilityState==='visible')void load();},15000);return()=>window.clearInterval(timer);},[load,active,section]);
useVisiblePolling(load,15000,active&&['tasks','latestBackup'].includes(section));
const runAction=async(row:MonitorRow,action:'retry'|'cancel')=>{
if(actionInFlight.current)return; actionInFlight.current=true;
setBusy(true);
try {
if(action==='retry') {const releases=await api.releases();await api.retryTask(String(row.id),{idempotencyKey:crypto.randomUUID(),expectedCurrentReleaseId:releases.currentReleaseId??null});}
else await api.cancelTask(String(row.id),{expectedStatus:String(row.status)});
message.success(action==='retry'?'已提交重试,请查看最新任务状态':'已取消排队任务');setSelected(null);await load();
} catch(error){if(!onUnauthorized(error))message.error(error instanceof Error?error.message:'任务操作失败');}
finally{setBusy(false);}
finally{actionInFlight.current=false;setBusy(false);}
};
const latest=(backup.latest || {}) as MonitorRow;
const columns:ColumnsType<MonitorRow>=[
@@ -54,13 +62,14 @@ export function TasksMonitorPage({user,onUnauthorized}:{user:AuthUser;onUnauthor
<ManagementSections onSelect={setSection} items={[
{ key:'tasks', label:'任务进度', children: <> <section className="panel section-panel">
<div className="completion-panel-heading"><strong>任务进度</strong><div className="completion-actions"><Select aria-label="筛选任务" value={filter} onChange={setFilter} options={[{value:'all',label:'全部任务'},{value:'active',label:'进行中'},{value:'failed',label:'失败 / 未完成'},{value:'completed',label:'已完成'}]}/><Button icon={<ReloadOutlined/>} loading={loading} onClick={()=>void load()}>刷新</Button></div></div>
{taskError&&<Alert showIcon type="error" message="任务暂不可用" description={taskError}/>}
{loading&&!items.length?<Spin/>:!taskError&&<Table size="small" bordered columns={columns} dataSource={orderedTasks(items,filter)} rowKey={row=>`${row.sourceKind}-${row.id}`} pagination={{pageSize:15,showSizeChanger:false}} scroll={{x:1000}} locale={{emptyText:<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无符合条件的任务"/>}}/>}
{taskError&&<Alert showIcon type="error" message="任务暂不可用" description={tasksLoaded?`${taskError};以下为上次成功读取的结果。`:taskError} action={!tasksLoaded?<Button size="small" onClick={()=>void loadTasks()}>重试</Button>:undefined}/>}
{loading&&!tasksLoaded?<Spin/>:tasksLoaded&&<Table size="small" bordered columns={columns} dataSource={orderedTasks(items,filter)} rowKey={row=>`${row.sourceKind}-${row.id}`} pagination={{pageSize:15,showSizeChanger:false}} scroll={{x:1000}} locale={{emptyText:<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无符合条件的任务"/>}}/>}
<p className="completion-note">每类最多显示 50 条记录,进行中及失败任务优先。智能分析仅显示本人任务。</p>
</section>
</> },
{ key:'latestBackup', label:'最近备份', children: <> <section className="panel section-panel"><div className="completion-panel-heading"><strong>最近备份状态</strong></div>
{backupError?<Alert showIcon type="error" message="备份状态暂不可用" description={backupError}/>:loading&&!latest.createdAt?<Spin/>:<div className="governance-monitor-grid"><div><small>最近备份</small><strong>{backupSummary(latest)}</strong></div><div><small>备份时间</small><strong>{configurationDateTime(latest.createdAt)||'—'}</strong></div><div><small>恢复验证时间</small><strong>{configurationDateTime(latest.restoreVerifiedAt)||'—'}</strong></div><div><small>自动备份</small><strong>{backup.planActive===true?'已启用':'未启用'}</strong></div></div>}
{backupError&&<Alert showIcon type="error" message="备份状态暂不可用" description={backupLoaded?`${backupError};以下为上次成功读取的结果。`:backupError} action={!backupLoaded?<Button size="small" onClick={()=>void loadBackup()}>重试</Button>:undefined}/>}
{loading&&!backupLoaded?<Spin/>:backupLoaded&&<div className="governance-monitor-grid"><div><small>最近备份</small><strong>{backupSummary(latest)}</strong></div><div><small>备份时间</small><strong>{configurationDateTime(latest.createdAt)||'—'}</strong></div><div><small>恢复验证时间</small><strong>{configurationDateTime(latest.restoreVerifiedAt)||'—'}</strong></div><div><small>自动备份</small><strong>{backup.planActive===true?'已启用':'未启用'}</strong></div></div>}
</section>
</> },
...(canManage ? [{ key:'monitor', label:'运行监控与告警', children:<OperationsMonitorPanel user={user} onUnauthorized={onUnauthorized}/> },{ key:'backups', label:'备份管理', children:<BackupOperationsPanel user={user} onUnauthorized={onUnauthorized}/> }] : []),