86 lines
2.3 KiB
TypeScript
86 lines
2.3 KiB
TypeScript
// =====================================================
|
|
// 連線監控 API
|
|
// =====================================================
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { connectionMonitor } from '@/lib/connection-monitor';
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
// 獲取連線統計
|
|
const stats = await connectionMonitor.getConnectionStats();
|
|
|
|
// 獲取監控狀態
|
|
const isMonitoring = connectionMonitor.isCurrentlyMonitoring();
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: {
|
|
stats,
|
|
isMonitoring,
|
|
timestamp: new Date().toISOString()
|
|
}
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('獲取連線監控數據失敗:', error);
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '獲取連線監控數據失敗',
|
|
error: error instanceof Error ? error.message : '未知錯誤'
|
|
}, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const { action } = body;
|
|
|
|
switch (action) {
|
|
case 'start_monitoring':
|
|
const interval = body.interval || 30000;
|
|
connectionMonitor.startMonitoring(interval);
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: `連線監控已啟動,檢查間隔: ${interval}ms`
|
|
});
|
|
|
|
case 'stop_monitoring':
|
|
connectionMonitor.stopMonitoring();
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '連線監控已停止'
|
|
});
|
|
|
|
case 'force_cleanup':
|
|
await connectionMonitor.forceCleanup();
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '強制清理連線完成'
|
|
});
|
|
|
|
case 'check_health':
|
|
const healthStats = await connectionMonitor.checkConnectionHealth();
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: healthStats
|
|
});
|
|
|
|
default:
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '無效的操作'
|
|
}, { status: 400 });
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('執行連線監控操作失敗:', error);
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '執行連線監控操作失敗',
|
|
error: error instanceof Error ? error.message : '未知錯誤'
|
|
}, { status: 500 });
|
|
}
|
|
}
|