share deformation job progress by account

This commit is contained in:
2026-05-03 01:23:10 +08:00
parent eb03bea7d4
commit 0aa9cffb97
2 changed files with 173 additions and 9 deletions

View File

@@ -48,8 +48,10 @@ type Page = 'overview' | 'library' | 'workspace' | 'users';
type BackendJob = {
id: string;
kind: 'deformation' | 'video';
owner?: string;
status: 'running' | 'completed' | 'failed';
message: string;
progress?: number;
result?: any;
error?: string;
};
@@ -88,6 +90,14 @@ const API_BASE = typeof window === 'undefined'
? 'http://127.0.0.1:8787'
: `${window.location.protocol}//${window.location.hostname}:8787`;
function progressFromJob(job: BackendJob, fallback = 0) {
if (job.status === 'completed') return 100;
if (typeof job.progress === 'number') {
return Math.max(0, Math.min(100, Math.round(job.progress)));
}
return Math.max(0, Math.min(95, fallback));
}
function readStoredDeformationJob(): StoredDeformationJob | null {
if (typeof window === 'undefined') return null;
try {
@@ -228,7 +238,7 @@ export default function App() {
const [cervicalRotation, setCervicalRotation] = useState(14.5);
const [transitionWidth, setTransitionWidth] = useState(90);
const [isSimulating, setIsSimulating] = useState(restoredDeformationJob?.job.status === 'running');
const [progress, setProgress] = useState(restoredDeformationJob?.job.status === 'completed' ? 100 : restoredDeformationJob?.progress || 0);
const [progress, setProgress] = useState(restoredDeformationJob ? progressFromJob(restoredDeformationJob.job, restoredDeformationJob.progress) : 0);
const [toastMessage, setToastMessage] = useState("");
const [backendOnline, setBackendOnline] = useState(false);
const [backendMessage, setBackendMessage] = useState('正在连接本地 Python 后端...');
@@ -295,6 +305,12 @@ export default function App() {
}
};
const applyDeformationJob = (job: BackendJob) => {
setDeformationJob(job);
setProgress(current => progressFromJob(job, current));
setIsSimulating(job.status === 'running');
};
const loadLibrary = async () => {
const data = await apiRequest('/api/library');
const items = data.items || [];
@@ -319,6 +335,31 @@ export default function App() {
refreshBackendDefaults();
}, []);
useEffect(() => {
if (!isLoggedIn || !currentUser?.username) return;
let isActive = true;
const restoreUserDeformationJob = async () => {
try {
const data = await apiRequest(
`/api/user/job?username=${encodeURIComponent(currentUser.username)}&kind=deformation`
) as { job?: BackendJob | null };
if (!isActive || !data.job) return;
applyDeformationJob(data.job);
if (data.job.status === 'running') {
showToast('已恢复当前账号的四状态任务进度');
}
} catch {
// 账号任务恢复失败不影响正常使用工作站。
}
};
restoreUserDeformationJob();
return () => {
isActive = false;
};
}, [isLoggedIn, currentUser?.username]);
useEffect(() => {
if (typeof window === 'undefined') return;
if (!deformationJob) {
@@ -344,7 +385,7 @@ export default function App() {
const job = await apiRequest(`/api/job?id=${deformationJob.id}`) as BackendJob;
if (!isActive) return;
setDeformationJob(job);
setProgress(value => job.status === 'completed' ? 100 : Math.min(value + 8, 95));
setProgress(value => progressFromJob(job, Math.min(value + 8, 95)));
if (job.status === 'completed') {
setIsSimulating(false);
showToast('四状态 DICOM 与过程图已生成');
@@ -568,12 +609,13 @@ export default function App() {
const job = await apiRequest('/api/deformation', {
method: 'POST',
body: JSON.stringify({
username: currentUser?.username || 'anonymous',
inputDir: selectedInputDir,
angleDegrees: cervicalRotation,
transitionWidth
})
}) as BackendJob;
setDeformationJob(job);
applyDeformationJob(job);
setBackendOnline(true);
setBackendMessage('run_deformation 任务已提交');
showToast('形变任务已提交');