import { useMemo } from 'react'; import useJobsList from '@/hooks/useJobsList'; import Link from 'next/link'; import UniversalTable, { TableColumn } from '@/components/UniversalTable'; import { GpuInfo, JobConfig } from '@/types'; import JobActionBar from './JobActionBar'; import { Job, Queue } from '@prisma/client'; import useQueueList from '@/hooks/useQueueList'; import classNames from 'classnames'; import { startQueue, stopQueue } from '@/utils/queue'; import { CgSpinner } from 'react-icons/cg'; import useGPUInfo from '@/hooks/useGPUInfo'; interface JobsTableProps { autoStartQueue?: boolean; onlyActive?: boolean; job_type?: string | null; } export default function JobsTable({ onlyActive = false, job_type = null }: JobsTableProps) { const { jobs, status, refreshJobs } = useJobsList({ onlyActive, reloadInterval: 5000, job_type }); const { queues, status: queueStatus, refreshQueues } = useQueueList(); const { gpuList, isGPUInfoLoaded } = useGPUInfo(); const refresh = () => { refreshJobs(); refreshQueues(); }; const columns: TableColumn[] = [ { title: 'Name', key: 'name', render: row => { let title = row.name; // if (row.job_type === 'train') title = `Train: ${title}`; if (row.job_type === 'caption') { let splits = row.job_ref.split(/[/\\]/); const datasetPath = `${splits[splits.length - 1]}`; title = ( <> CAPTION: {datasetPath} ); } return ( {['running', 'stopping'].includes(row.status) ? ( ) : null} {title} ); }, }, { title: 'Steps', key: 'steps', render: row => { const jobConfig: JobConfig = JSON.parse(row.job_config); if (row.job_type !== 'train') { return <>; } const totalSteps = jobConfig.config.process[0].train?.steps; return (
{row.step} / {totalSteps}
); }, }, { title: 'GPU', key: 'gpu_ids', }, { title: 'Status', key: 'status', render: row => { let statusClass = 'text-gray-400'; if (row.status === 'completed') statusClass = 'text-green-400'; if (row.status === 'failed') statusClass = 'text-red-400'; if (row.status === 'running') statusClass = 'text-blue-400'; return {row.status}; }, }, { title: 'Info', key: 'info', className: 'truncate max-w-xs', }, { title: 'Actions', key: 'actions', className: 'text-right', render: row => { return ; }, }, ]; const jobsDict = useMemo(() => { if (!isGPUInfoLoaded) return {}; if (jobs.length === 0) return {}; let jd: { [key: string]: { name: string; jobs: Job[] } } = {}; gpuList.forEach(gpu => { jd[`${gpu.index}`] = { name: `${gpu.name}`, jobs: [] }; }); jd['Idle'] = { name: 'Idle', jobs: [] }; jobs.forEach(job => { const gpu = gpuList.find(gpu => job.gpu_ids?.split(',').includes(gpu.index.toString())) as GpuInfo; const key = `${gpu?.index || '0'}`; if (['queued', 'running', 'stopping'].includes(job.status) && key in jd) { jd[key].jobs.push(job); } else { jd['Idle'].jobs.push(job); } }); // sort the queued/running jobs by queue position Object.keys(jd).forEach(key => { if (key === 'Idle') { jd[key].jobs.sort((a, b) => { // sort by updated_at, newest first return new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime(); }); } else { jd[key].jobs.sort((a, b) => { if (a.queue_position === null) return 1; if (b.queue_position === null) return -1; return a.queue_position - b.queue_position; }); } }); return jd; }, [jobs, queues, isGPUInfoLoaded]); let isLoading = status === 'loading' || queueStatus === 'loading' || !isGPUInfoLoaded; // if job dict is populated, we are always loaded if (Object.keys(jobsDict).length > 0) isLoading = false; return (
{Object.keys(jobsDict) .sort() .filter(key => key !== 'Idle') .map(gpuKey => { const queue = queues.find(q => `${q.gpu_ids}` === gpuKey) as Queue; return (

{jobsDict[gpuKey].name}

# {queue?.gpu_ids}
{queue?.is_running ? ( <> Queue Running ) : ( <> Queue Stopped )}
); })} {!onlyActive && Object.keys(jobsDict).includes('Idle') && (

Idle

)}
); }