Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 4x 5x 4x 4x 4x 4x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 4x 4x 4x 5x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 5x 5x 5x 3x 3x 3x 3x 3x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 1x 1x | /**
* Performance Data Export API
*
* GET /api/admin/monitoring/performance/export
*
* Exports performance data in CSV or JSON format.
*/
export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { getExportData } from '@/lib/monitoring/performanceQueries';
import { convertToCSV } from '@/lib/monitoring/exporters/csv';
import { logger } from '@/lib/logging';
import {
withAdmin,
withErrorHandling,
errorResponse,
type ApiErrorResponse,
} from '@/lib/api';
// Rate limiting: Track export requests per IP
const exportRateLimits = new Map<string, { count: number; resetTime: number }>();
function checkRateLimit(ip: string): boolean {
const now = Date.now();
const windowMs = 60 * 1000; // 1 minute window
const maxExports = 10; // Max 10 exports per minute
const limit = exportRateLimits.get(ip);
if (!limit || now > limit.resetTime) {
exportRateLimits.set(ip, { count: 1, resetTime: now + windowMs });
return true;
}
if (limit.count >= maxExports) {
return false;
}
limit.count++;
return true;
}
/**
* GET handler for performance data export
*/
async function handleGet(
request: NextRequest
): Promise<NextResponse | NextResponse<ApiErrorResponse>> {
// Rate limiting
const ip = request.headers.get('x-forwarded-for') || 'unknown';
if (!checkRateLimit(ip)) {
return errorResponse('RATE_LIMITED', 'Too many export requests. Please wait a minute.', {
status: 429,
});
}
const { searchParams } = new URL(request.url);
// Parse query parameters
const format = searchParams.get('format') || 'csv';
const timeRange = searchParams.get('timeRange') || '24h';
const path = searchParams.get('path');
const includeSummary = searchParams.get('includeSummary') !== 'false';
// Validate format
const validFormats = ['csv', 'json'];
if (!validFormats.includes(format)) {
return errorResponse(
'INVALID_PARAM',
'Invalid format. Must be one of: csv, json',
{ status: 400 }
);
}
// Validate time range
const validRanges = ['1h', '6h', '24h', '7d', '30d'];
if (!validRanges.includes(timeRange)) {
return errorResponse(
'INVALID_PARAM',
'Invalid timeRange. Must be one of: 1h, 6h, 24h, 7d, 30d',
{ status: 400 }
);
}
try {
const data = await getExportData({
timeRange,
path: path || undefined,
includeSummary,
});
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `performance-export-${timestamp}`;
switch (format) {
case 'csv': {
const csv = convertToCSV(data);
return new NextResponse(csv, {
status: 200,
headers: {
'Content-Type': 'text/csv; charset=utf-8',
'Content-Disposition': `attachment; filename="${filename}.csv"`,
'Cache-Control': 'no-cache, no-store, must-revalidate',
},
});
}
case 'json':
default: {
const json = JSON.stringify(data, null, 2);
return new NextResponse(json, {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Disposition': `attachment; filename="${filename}.json"`,
'Cache-Control': 'no-cache, no-store, must-revalidate',
},
});
}
}
} catch (error) {
logger.error('Error exporting performance data', error instanceof Error ? error : new Error(String(error)), { category: 'API' });
return errorResponse('INTERNAL_ERROR', 'Failed to export performance data', {
status: 500,
});
}
}
export const GET = withErrorHandling(withAdmin(handleGet));
|