-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroute.ts
More file actions
103 lines (86 loc) · 2.99 KB
/
route.ts
File metadata and controls
103 lines (86 loc) · 2.99 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { NextRequest } from 'next/server'
import { cacheAnalytics } from '@/lib/cache-analytics-server'
import { UnifiedCache } from '@/lib/unified-cache-system'
// Force Node.js runtime for API routes
export const runtime = 'nodejs';
const routes = [
'/api/hackathons',
'/api/users/profile',
'/api/leaderboard/stats',
'/api/internships/apply',
'/_next/static/css/app.css',
'/_next/static/chunks/main.js',
'/hackathons',
'/leaderboard',
'/internship',
'/admin'
]
const strategies = ['STATIC_ASSETS', 'API_REALTIME', 'API_STANDARD', 'PAGES_DYNAMIC'] as const
/**
* Generate test cache events for development/demo purposes
*/
export async function POST(request: NextRequest) {
try {
// Only allow in development or with proper auth
if (process.env.NODE_ENV === 'production') {
return new Response('Not available in production', { status: 403 })
}
const { events = 100 } = await request.json().catch(() => ({ events: 100 }))
console.log(`🔄 Generating ${events} cache test events...`)
// Generate random cache events
for (let i = 0; i < events; i++) {
const route = routes[Math.floor(Math.random() * routes.length)]
const strategy = strategies[Math.floor(Math.random() * strategies.length)]
const isHit = Math.random() > 0.15 // 85% hit rate
const responseTime = Math.floor(Math.random() * 200) + 10 // 10-210ms
cacheAnalytics.recordEvent({
type: isHit ? 'hit' : 'miss',
strategy,
route,
responseTime,
buildId: process.env.BUILD_ID || 'dev-build',
userAgent: 'test-generator',
region: 'us-east-1'
})
}
// Generate some invalidation events
for (let i = 0; i < 5; i++) {
const route = routes[Math.floor(Math.random() * routes.length)]
cacheAnalytics.recordEvent({
type: 'invalidation',
strategy: 'API_REALTIME',
route,
buildId: process.env.BUILD_ID || 'dev-build'
})
}
// Generate a few error events
for (let i = 0; i < 3; i++) {
const route = routes[Math.floor(Math.random() * routes.length)]
cacheAnalytics.recordEvent({
type: 'error',
strategy: 'API_REALTIME',
route,
buildId: process.env.BUILD_ID || 'dev-build'
})
}
const totalGenerated = events + 5 + 3
console.log(`✅ Generated ${totalGenerated} cache events successfully!`)
// Return current analytics data
const analytics = cacheAnalytics.getDetailedAnalytics()
return UnifiedCache.createResponse({
success: true,
message: `Generated ${totalGenerated} cache events`,
analytics
}, 'REALTIME')
} catch (error) {
console.error('Error generating cache test data:', error)
return UnifiedCache.createResponse(
{
success: false,
error: 'Failed to generate cache test data',
message: error instanceof Error ? error.message : 'Unknown error'
},
'USER_PRIVATE'
)
}
}