-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroute.ts
More file actions
81 lines (72 loc) · 2.83 KB
/
route.ts
File metadata and controls
81 lines (72 loc) · 2.83 KB
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
import { NextRequest } from 'next/server'
import { UnifiedCache } from '@/lib/unified-cache-system'
// Force Node.js runtime for API routes
export const runtime = 'nodejs';
/**
* API endpoint for cache analytics - Admin only
*
* Note: This provides basic cache analytics without the complex
* analytics system that was causing crashes.
*/
export async function GET(request: NextRequest) {
try {
// TODO: Add admin authentication check here
// const user = await getUser(request)
// if (!user?.isAdmin) {
// return new Response('Unauthorized', { status: 401 })
// }
const url = new URL(request.url)
// const period = url.searchParams.get('period')
const format = url.searchParams.get('format') as 'json' | 'csv' | null
// Default to 24 hours - period is used for future analytics filtering
// const _periodMs = period ? parseInt(period) * 1000 : 24 * 60 * 60 * 1000
// Provide basic analytics data for now
const basicAnalytics = {
overview: {
totalRequests: 1250,
cacheHits: 1000,
cacheMisses: 250,
hitRate: 80.0,
averageResponseTime: 120,
invalidations: 5,
errors: 2,
lastUpdated: new Date().toISOString()
},
byStrategy: {
'STATIC_ASSETS': { hits: 800, misses: 50, total: 850 },
'API_REALTIME': { hits: 150, misses: 100, total: 250 },
'PAGES_DYNAMIC': { hits: 50, misses: 100, total: 150 }
},
topRoutes: [
{ route: '/_next/static/chunks/', hits: 500, misses: 10, total: 510 },
{ route: '/api/leaderboard/stats', hits: 100, misses: 50, total: 150 },
{ route: '/protected/dashboard', hits: 80, misses: 20, total: 100 }
],
recentEvents: [
{ type: 'hit', strategy: 'STATIC_ASSETS', route: '/_next/static/', timestamp: Date.now() - 1000 },
{ type: 'miss', strategy: 'API_REALTIME', route: '/api/user/activity', timestamp: Date.now() - 2000 },
{ type: 'hit', strategy: 'PAGES_DYNAMIC', route: '/leaderboard', timestamp: Date.now() - 3000 }
]
}
if (format === 'csv') {
// Simple CSV export
const csvData = `Route,Hits,Misses,Total,Hit Rate\n` +
basicAnalytics.topRoutes.map(route =>
`${route.route},${route.hits},${route.misses},${route.total},${((route.hits / route.total) * 100).toFixed(1)}%`
).join('\n')
return new Response(csvData, {
headers: {
'Content-Type': 'text/csv',
'Content-Disposition': 'attachment; filename="cache-analytics.csv"'
}
})
}
return UnifiedCache.createResponse(basicAnalytics, 'API_STANDARD')
} catch (error) {
console.error('Cache analytics API error:', error)
return UnifiedCache.createResponse(
{ error: 'Failed to fetch cache analytics' },
'USER_PRIVATE'
)
}
}