Buckets:
| import { NextResponse } from 'next/server'; | |
| import prisma from '@/server/prisma'; | |
| import { isMac } from '@/helpers/basic'; | |
| import { cached } from '@/server/apiCache'; | |
| export async function GET(request: Request) { | |
| const { searchParams } = new URL(request.url); | |
| const id = searchParams.get('id'); | |
| const job_ref = searchParams.get('job_ref'); | |
| const job_type = searchParams.get('job_type'); | |
| const only_active = searchParams.get('only_active'); | |
| try { | |
| if (id) { | |
| const job = await prisma.job.findUnique({ | |
| where: { id }, | |
| }); | |
| return NextResponse.json(job); | |
| } | |
| if (job_ref) { | |
| const job = await prisma.job.findFirst({ | |
| where: { job_ref }, | |
| orderBy: { updated_at: 'desc' }, | |
| }); | |
| return NextResponse.json(job); | |
| } | |
| const where: any = {}; | |
| if (job_type) { | |
| where.job_type = job_type; | |
| } | |
| if (only_active === 'true') { | |
| where.status = { in: ['running', 'queued', 'stopping'] }; | |
| const jobs = await cached( | |
| 'jobs-active', | |
| () => | |
| prisma.job.findMany({ | |
| where, | |
| orderBy: { created_at: 'desc' }, | |
| }), | |
| 5000, | |
| { job_type }, | |
| ); | |
| return NextResponse.json({ jobs: jobs }); | |
| } | |
| const jobs = await prisma.job.findMany({ | |
| where, | |
| orderBy: { created_at: 'desc' }, | |
| }); | |
| return NextResponse.json({ jobs: jobs }); | |
| } catch (error) { | |
| console.error(error); | |
| return NextResponse.json({ error: 'Failed to fetch training data' }, { status: 500 }); | |
| } | |
| } | |
| export async function POST(request: Request) { | |
| try { | |
| const body = await request.json(); | |
| const { id, name, job_config } = body; | |
| let gpu_ids: string = body.gpu_ids; | |
| if (isMac()) { | |
| gpu_ids = 'mps'; | |
| } | |
| const extra: any = {}; | |
| if ('job_ref' in body) { | |
| extra['job_ref'] = body.job_ref; | |
| } | |
| if ('job_type' in body) { | |
| extra['job_type'] = body.job_type; | |
| } | |
| if (id) { | |
| // Update existing training | |
| const training = await prisma.job.update({ | |
| where: { id }, | |
| data: { | |
| name, | |
| gpu_ids, | |
| job_config: JSON.stringify(job_config), | |
| ...extra, | |
| }, | |
| }); | |
| return NextResponse.json(training); | |
| } else { | |
| // find the highest queue position and add 1000 | |
| const highestQueuePosition = await prisma.job.aggregate({ | |
| _max: { | |
| queue_position: true, | |
| }, | |
| }); | |
| const newQueuePosition = (highestQueuePosition._max.queue_position || 0) + 1000; | |
| // Create new training | |
| const training = await prisma.job.create({ | |
| data: { | |
| name, | |
| gpu_ids, | |
| job_config: JSON.stringify(job_config), | |
| queue_position: newQueuePosition, | |
| ...extra, | |
| }, | |
| }); | |
| return NextResponse.json(training); | |
| } | |
| } catch (error: any) { | |
| if (error.code === 'P2002') { | |
| // Handle unique constraint violation, 409=Conflict | |
| return NextResponse.json({ error: 'Job name already exists' }, { status: 409 }); | |
| } | |
| console.error(error); | |
| // Handle other errors | |
| return NextResponse.json({ error: 'Failed to save training data' }, { status: 500 }); | |
| } | |
| } | |
Xet Storage Details
- Size:
- 3.22 kB
- Xet hash:
- a87e9cbc5db451f93e872e2bacc51efd27c5125ca197d5f07a399faada07fe53
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.