Fixed some issues with gpu info refreshing
This commit is contained in:
@@ -3,26 +3,34 @@ import useGPUInfo from '@/hooks/useGPUInfo';
|
|||||||
import GPUWidget from '@/components/GPUWidget';
|
import GPUWidget from '@/components/GPUWidget';
|
||||||
import { getJobConfig, getTotalSteps } from '@/utils/jobs';
|
import { getJobConfig, getTotalSteps } from '@/utils/jobs';
|
||||||
import { Cpu, HardDrive, Info } from 'lucide-react';
|
import { Cpu, HardDrive, Info } from 'lucide-react';
|
||||||
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
interface JobOverviewProps {
|
interface JobOverviewProps {
|
||||||
job: Job;
|
job: Job;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function JobOverview({ job }: JobOverviewProps) {
|
export default function JobOverview({ job }: JobOverviewProps) {
|
||||||
const gpuIds = job.gpu_ids.split(',').map(id => parseInt(id));
|
const gpuIds = useMemo(() => job.gpu_ids.split(',').map(id => parseInt(id)), [job.gpu_ids]);
|
||||||
const { gpuList, isGPUInfoLoaded } = useGPUInfo(gpuIds);
|
|
||||||
|
const { gpuList, isGPUInfoLoaded } = useGPUInfo(gpuIds, 5000);
|
||||||
const totalSteps = getTotalSteps(job);
|
const totalSteps = getTotalSteps(job);
|
||||||
const progress = (job.step / totalSteps) * 100;
|
const progress = (job.step / totalSteps) * 100;
|
||||||
const isStopping = job.stop && job.status === 'running';
|
const isStopping = job.stop && job.status === 'running';
|
||||||
|
|
||||||
const getStatusColor = (status: string) => {
|
const getStatusColor = (status: string) => {
|
||||||
switch (status.toLowerCase()) {
|
switch (status.toLowerCase()) {
|
||||||
case 'running': return 'bg-emerald-500/10 text-emerald-500';
|
case 'running':
|
||||||
case 'stopping': return 'bg-amber-500/10 text-amber-500';
|
return 'bg-emerald-500/10 text-emerald-500';
|
||||||
case 'stopped': return 'bg-gray-500/10 text-gray-400';
|
case 'stopping':
|
||||||
case 'completed': return 'bg-blue-500/10 text-blue-500';
|
return 'bg-amber-500/10 text-amber-500';
|
||||||
case 'error': return 'bg-rose-500/10 text-rose-500';
|
case 'stopped':
|
||||||
default: return 'bg-gray-500/10 text-gray-400';
|
return 'bg-gray-500/10 text-gray-400';
|
||||||
|
case 'completed':
|
||||||
|
return 'bg-blue-500/10 text-blue-500';
|
||||||
|
case 'error':
|
||||||
|
return 'bg-rose-500/10 text-rose-500';
|
||||||
|
default:
|
||||||
|
return 'bg-gray-500/10 text-gray-400';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -37,9 +45,7 @@ export default function JobOverview({ job }: JobOverviewProps) {
|
|||||||
<div className="col-span-2 bg-gray-900 rounded-xl shadow-lg overflow-hidden border border-gray-800">
|
<div className="col-span-2 bg-gray-900 rounded-xl shadow-lg overflow-hidden border border-gray-800">
|
||||||
<div className="bg-gray-800 px-4 py-3 flex items-center justify-between">
|
<div className="bg-gray-800 px-4 py-3 flex items-center justify-between">
|
||||||
<h2 className="font-semibold text-gray-100">Job Details</h2>
|
<h2 className="font-semibold text-gray-100">Job Details</h2>
|
||||||
<span className={`px-3 py-1 rounded-full text-sm ${getStatusColor(job.status)}`}>
|
<span className={`px-3 py-1 rounded-full text-sm ${getStatusColor(job.status)}`}>{job.status}</span>
|
||||||
{job.status}
|
|
||||||
</span>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="p-4 space-y-6">
|
<div className="p-4 space-y-6">
|
||||||
@@ -52,10 +58,7 @@ export default function JobOverview({ job }: JobOverviewProps) {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="w-full bg-gray-800 rounded-full h-2">
|
<div className="w-full bg-gray-800 rounded-full h-2">
|
||||||
<div
|
<div className="h-2 rounded-full bg-blue-500 transition-all" style={{ width: `${progress}%` }} />
|
||||||
className="h-2 rounded-full bg-blue-500 transition-all"
|
|
||||||
style={{ width: `${progress}%` }}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -89,11 +92,7 @@ export default function JobOverview({ job }: JobOverviewProps) {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* GPU Widget Panel */}
|
{/* GPU Widget Panel */}
|
||||||
<div className="col-span-1">
|
<div className="col-span-1">{isGPUInfoLoaded && gpuList.length > 0 && <GPUWidget gpu={gpuList[0]} />}</div>
|
||||||
{isGPUInfoLoaded && gpuList.length > 0 && (
|
|
||||||
<GPUWidget gpu={gpuList[0]} />
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,35 +3,52 @@
|
|||||||
import { GPUApiResponse, GpuInfo } from '@/types';
|
import { GPUApiResponse, GpuInfo } from '@/types';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
export default function useGPUInfo(gpuIds: null | number[] = null) {
|
export default function useGPUInfo(gpuIds: null | number[] = null, reloadInterval: null | number = null) {
|
||||||
const [gpuList, setGpuList] = useState<GpuInfo[]>([]);
|
const [gpuList, setGpuList] = useState<GpuInfo[]>([]);
|
||||||
const [isGPUInfoLoaded, setIsLoaded] = useState(false);
|
const [isGPUInfoLoaded, setIsLoaded] = useState(false);
|
||||||
useEffect(() => {
|
const [status, setStatus] = useState<'idle' | 'loading' | 'success' | 'error'>('idle');
|
||||||
const fetchGpuInfo = async () => {
|
|
||||||
try {
|
|
||||||
const response = await fetch('/api/gpu');
|
|
||||||
|
|
||||||
if (!response.ok) {
|
const fetchGpuInfo = async () => {
|
||||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
setStatus('loading');
|
||||||
}
|
try {
|
||||||
|
const response = await fetch('/api/gpu');
|
||||||
|
|
||||||
const data: GPUApiResponse = await response.json();
|
if (!response.ok) {
|
||||||
let gpus = data.gpus.sort((a, b) => a.index - b.index);
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
if (gpuIds) {
|
|
||||||
gpus = gpus.filter(gpu => gpuIds.includes(gpu.index));
|
|
||||||
}
|
|
||||||
|
|
||||||
setGpuList(gpus);
|
|
||||||
} catch (err) {
|
|
||||||
console.log(`Failed to fetch GPU data: ${err instanceof Error ? err.message : String(err)}`);
|
|
||||||
} finally {
|
|
||||||
setIsLoaded(true);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
const data: GPUApiResponse = await response.json();
|
||||||
|
let gpus = data.gpus.sort((a, b) => a.index - b.index);
|
||||||
|
if (gpuIds) {
|
||||||
|
gpus = gpus.filter(gpu => gpuIds.includes(gpu.index));
|
||||||
|
}
|
||||||
|
|
||||||
|
setGpuList(gpus);
|
||||||
|
setStatus('success');
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Failed to fetch GPU data: ${err instanceof Error ? err.message : String(err)}`);
|
||||||
|
setStatus('error');
|
||||||
|
} finally {
|
||||||
|
setIsLoaded(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
// Fetch immediately on component mount
|
// Fetch immediately on component mount
|
||||||
fetchGpuInfo();
|
fetchGpuInfo();
|
||||||
}, []);
|
|
||||||
|
|
||||||
return { gpuList, setGpuList, isGPUInfoLoaded };
|
// Set up interval if specified
|
||||||
}
|
if (reloadInterval) {
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
fetchGpuInfo();
|
||||||
|
}, reloadInterval);
|
||||||
|
|
||||||
|
// Cleanup interval on unmount
|
||||||
|
return () => {
|
||||||
|
clearInterval(interval);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, [gpuIds, reloadInterval]); // Added dependencies
|
||||||
|
|
||||||
|
return { gpuList, setGpuList, isGPUInfoLoaded, status, refreshGpuInfo: fetchGpuInfo };
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user