feat: add FastAPI model/load endpoint and all Next.js proxy routes (tasks 2-4)
This commit is contained in:
parent
b8e649e333
commit
2a02669222
29 changed files with 1110 additions and 780 deletions
37
src/app/api/model/load/route.ts
Normal file
37
src/app/api/model/load/route.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const INFERENCE_API_URL = process.env.INFERENCE_API_URL || 'http://localhost:8001';
|
||||
const INFERENCE_API_TIMEOUT = parseInt(process.env.INFERENCE_API_TIMEOUT || '30000', 10);
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), INFERENCE_API_TIMEOUT);
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
|
||||
const response = await fetch(`${INFERENCE_API_URL}/model/load`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ error: data.detail || 'Failed to load model' }, { status: response.status });
|
||||
}
|
||||
return NextResponse.json(data);
|
||||
} catch (error: any) {
|
||||
clearTimeout(timeoutId);
|
||||
if (error.name === 'AbortError') {
|
||||
return NextResponse.json({ error: 'Model load timed out' }, { status: 504 });
|
||||
}
|
||||
if (error.cause?.code === 'ECONNREFUSED' || error.message?.includes('fetch failed')) {
|
||||
return NextResponse.json({ error: 'Inference service unavailable' }, { status: 503 });
|
||||
}
|
||||
console.error('model/load proxy error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
34
src/app/api/patterns/available/route.ts
Normal file
34
src/app/api/patterns/available/route.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const INFERENCE_API_URL = process.env.INFERENCE_API_URL || 'http://localhost:8001';
|
||||
const INFERENCE_API_TIMEOUT = parseInt(process.env.INFERENCE_API_TIMEOUT || '10000', 10);
|
||||
|
||||
export async function GET(_request: NextRequest) {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), INFERENCE_API_TIMEOUT);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${INFERENCE_API_URL}/patterns/available`, {
|
||||
method: 'GET',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ error: data.detail || 'Request failed' }, { status: response.status });
|
||||
}
|
||||
return NextResponse.json(data);
|
||||
} catch (error: any) {
|
||||
clearTimeout(timeoutId);
|
||||
if (error.name === 'AbortError') {
|
||||
return NextResponse.json({ error: 'Request timed out' }, { status: 504 });
|
||||
}
|
||||
if (error.cause?.code === 'ECONNREFUSED' || error.message?.includes('fetch failed')) {
|
||||
return NextResponse.json({ error: 'Inference service unavailable' }, { status: 503 });
|
||||
}
|
||||
console.error('patterns/available proxy error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
38
src/app/api/patterns/detect/route.ts
Normal file
38
src/app/api/patterns/detect/route.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const INFERENCE_API_URL = process.env.INFERENCE_API_URL || 'http://localhost:8001';
|
||||
// Pattern detection may take longer on large datasets
|
||||
const INFERENCE_API_TIMEOUT = parseInt(process.env.INFERENCE_API_TIMEOUT || '60000', 10);
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), INFERENCE_API_TIMEOUT);
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
|
||||
const response = await fetch(`${INFERENCE_API_URL}/patterns/detect`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ error: data.detail || 'Pattern detection failed' }, { status: response.status });
|
||||
}
|
||||
return NextResponse.json(data);
|
||||
} catch (error: any) {
|
||||
clearTimeout(timeoutId);
|
||||
if (error.name === 'AbortError') {
|
||||
return NextResponse.json({ error: 'Pattern detection timed out' }, { status: 504 });
|
||||
}
|
||||
if (error.cause?.code === 'ECONNREFUSED' || error.message?.includes('fetch failed')) {
|
||||
return NextResponse.json({ error: 'Inference service unavailable' }, { status: 503 });
|
||||
}
|
||||
console.error('patterns/detect proxy error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { spanAnnotations } from '@/lib/db/schema';
|
||||
import { eq, desc } from 'drizzle-orm';
|
||||
import { eq, desc, and } from 'drizzle-orm';
|
||||
|
||||
// GET - List all span annotations for a chart
|
||||
export async function GET(request: NextRequest) {
|
||||
|
|
@ -87,3 +87,40 @@ export async function POST(request: NextRequest) {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Bulk delete span annotations by chartId + optional source/label filters
|
||||
export async function DELETE(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = request.nextUrl;
|
||||
const chartId = searchParams.get('chartId');
|
||||
const source = searchParams.get('source');
|
||||
const label = searchParams.get('label');
|
||||
|
||||
if (!chartId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'chartId parameter is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const chartIdInt = parseInt(chartId);
|
||||
|
||||
// Build filter conditions
|
||||
const conditions = [eq(spanAnnotations.chart_id, chartIdInt)];
|
||||
if (source) conditions.push(eq(spanAnnotations.source, source));
|
||||
if (label) conditions.push(eq(spanAnnotations.label, label));
|
||||
|
||||
const deleted = await db
|
||||
.delete(spanAnnotations)
|
||||
.where(and(...conditions))
|
||||
.returning({ id: spanAnnotations.id });
|
||||
|
||||
return NextResponse.json({ deleted: deleted.length });
|
||||
} catch (error) {
|
||||
console.error('Error bulk deleting span annotations:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete span annotations' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
34
src/app/api/training/dataset-info/route.ts
Normal file
34
src/app/api/training/dataset-info/route.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const INFERENCE_API_URL = process.env.INFERENCE_API_URL || 'http://localhost:8001';
|
||||
const INFERENCE_API_TIMEOUT = parseInt(process.env.INFERENCE_API_TIMEOUT || '10000', 10);
|
||||
|
||||
export async function GET(_request: NextRequest) {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), INFERENCE_API_TIMEOUT);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${INFERENCE_API_URL}/training/dataset-info`, {
|
||||
method: 'GET',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ error: data.detail || 'Failed to fetch dataset info' }, { status: response.status });
|
||||
}
|
||||
return NextResponse.json(data);
|
||||
} catch (error: any) {
|
||||
clearTimeout(timeoutId);
|
||||
if (error.name === 'AbortError') {
|
||||
return NextResponse.json({ error: 'Request timed out' }, { status: 504 });
|
||||
}
|
||||
if (error.cause?.code === 'ECONNREFUSED' || error.message?.includes('fetch failed')) {
|
||||
return NextResponse.json({ error: 'Inference service unavailable' }, { status: 503 });
|
||||
}
|
||||
console.error('training/dataset-info proxy error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
34
src/app/api/training/runs/route.ts
Normal file
34
src/app/api/training/runs/route.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const INFERENCE_API_URL = process.env.INFERENCE_API_URL || 'http://localhost:8001';
|
||||
const INFERENCE_API_TIMEOUT = parseInt(process.env.INFERENCE_API_TIMEOUT || '10000', 10);
|
||||
|
||||
export async function GET(_request: NextRequest) {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), INFERENCE_API_TIMEOUT);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${INFERENCE_API_URL}/training/runs`, {
|
||||
method: 'GET',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ error: data.detail || 'Failed to fetch training runs' }, { status: response.status });
|
||||
}
|
||||
return NextResponse.json(data);
|
||||
} catch (error: any) {
|
||||
clearTimeout(timeoutId);
|
||||
if (error.name === 'AbortError') {
|
||||
return NextResponse.json({ error: 'Request timed out' }, { status: 504 });
|
||||
}
|
||||
if (error.cause?.code === 'ECONNREFUSED' || error.message?.includes('fetch failed')) {
|
||||
return NextResponse.json({ error: 'Inference service unavailable' }, { status: 503 });
|
||||
}
|
||||
console.error('training/runs proxy error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
37
src/app/api/training/start/route.ts
Normal file
37
src/app/api/training/start/route.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const INFERENCE_API_URL = process.env.INFERENCE_API_URL || 'http://localhost:8001';
|
||||
const INFERENCE_API_TIMEOUT = parseInt(process.env.INFERENCE_API_TIMEOUT || '10000', 10);
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), INFERENCE_API_TIMEOUT);
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
|
||||
const response = await fetch(`${INFERENCE_API_URL}/training/start`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ error: data.detail || 'Failed to start training' }, { status: response.status });
|
||||
}
|
||||
return NextResponse.json(data);
|
||||
} catch (error: any) {
|
||||
clearTimeout(timeoutId);
|
||||
if (error.name === 'AbortError') {
|
||||
return NextResponse.json({ error: 'Request timed out' }, { status: 504 });
|
||||
}
|
||||
if (error.cause?.code === 'ECONNREFUSED' || error.message?.includes('fetch failed')) {
|
||||
return NextResponse.json({ error: 'Inference service unavailable' }, { status: 503 });
|
||||
}
|
||||
console.error('training/start proxy error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue