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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 10x 10x 1x 1x 10x 10x 1x 1x 1x 1x 1x 1x 1x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 1x 1x 1x 1x 30x 30x 7x 7x 23x 30x 4x 4x 19x 19x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 10x 10x 10x 10x 10x 10x 10x 10x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x | export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { withAdmin, withErrorHandling } from '@/lib/api';
import type { Prisma } from '@prisma/client';
/**
* Build Prisma where clause from filter parameters
*/
function buildWhereClause(params: {
category?: string;
level?: string;
search?: string;
startDate?: string;
endDate?: string;
}): Prisma.ErrorLogWhereInput {
const where: Prisma.ErrorLogWhereInput = {};
if (params.category) {
where.category = params.category;
}
if (params.level) {
where.level = params.level;
}
if (params.search) {
// Note: MySQL default collation is case-insensitive, so we don't need mode parameter
where.OR = [
{ message: { contains: params.search } },
{ stack: { contains: params.search } },
{ url: { contains: params.search } },
];
}
if (params.startDate || params.endDate) {
where.createdAt = {};
if (params.startDate) {
where.createdAt.gte = new Date(params.startDate);
}
if (params.endDate) {
where.createdAt.lte = new Date(params.endDate);
}
}
return where;
}
/**
* Escape CSV field value
*/
function escapeCSVField(value: string | null | undefined): string {
if (value === null || value === undefined) {
return '';
}
// If contains comma, newline, or quote, wrap in quotes and escape quotes
if (value.includes(',') || value.includes('\n') || value.includes('"')) {
return `"${value.replace(/"/g, '""')}"`;
}
return value;
}
/**
* Convert errors to CSV format
*/
function convertToCSV(
errors: Array<{
id: number;
level: string;
category: string;
message: string;
stack: string | null;
url: string | null;
createdAt: Date;
fingerprint: string;
}>
): string {
const headers = ['ID', 'Timestamp', 'Level', 'Category', 'Message', 'URL', 'Fingerprint', 'Stack'];
const rows = errors.map(error => [
error.id.toString(),
new Date(error.createdAt).toISOString(),
error.level,
error.category,
escapeCSVField(error.message),
escapeCSVField(error.url),
error.fingerprint,
escapeCSVField(error.stack),
].join(','));
return [headers.join(','), ...rows].join('\n');
}
/**
* GET /api/admin/monitoring/errors/export
*
* Export error logs as CSV or JSON.
* Requires admin authentication.
*
* Query params:
* - format: Export format (csv, json) (default: csv)
* - category: Filter by category
* - level: Filter by level
* - search: Full-text search
* - startDate: Filter after date
* - endDate: Filter before date
*/
async function handleGet(request: NextRequest): Promise<NextResponse> {
const { searchParams } = new URL(request.url);
// Parse filter params
const format = searchParams.get('format') || 'csv';
const category = searchParams.get('category') || undefined;
const level = searchParams.get('level') || undefined;
const search = searchParams.get('search') || undefined;
const startDate = searchParams.get('startDate') || undefined;
const endDate = searchParams.get('endDate') || undefined;
// Build where clause
const where = buildWhereClause({ category, level, search, startDate, endDate });
// Fetch errors (max 10000 for export)
const errors = await prisma.errorLog.findMany({
where,
orderBy: { createdAt: 'desc' },
take: 10000,
select: {
id: true,
level: true,
category: true,
message: true,
stack: true,
url: true,
createdAt: true,
fingerprint: true,
},
});
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
if (format === 'json') {
return new NextResponse(JSON.stringify(errors, null, 2), {
headers: {
'Content-Type': 'application/json',
'Content-Disposition': `attachment; filename=errors-${timestamp}.json`,
},
});
}
// Default to CSV
const csv = convertToCSV(errors);
return new NextResponse(csv, {
headers: {
'Content-Type': 'text/csv; charset=utf-8',
'Content-Disposition': `attachment; filename=errors-${timestamp}.csv`,
},
});
}
export const GET = withErrorHandling(withAdmin(handleGet));
|