-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
68 lines (61 loc) · 1.94 KB
/
route.ts
File metadata and controls
68 lines (61 loc) · 1.94 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
import { NextRequest, NextResponse } from 'next/server';
import { searchAnalytics } from '@/lib/analytics';
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const timeRange = searchParams.get('timeRange');
const query = searchParams.get('query');
const action = searchParams.get('action');
// Handle different actions
if (action === 'recent') {
const limit = parseInt(searchParams.get('limit') || '50');
const recentSearches = searchAnalytics.getRecentSearches(limit);
return NextResponse.json({
recentSearches,
count: recentSearches.length,
timestamp: new Date().toISOString(),
});
}
if (action === 'history' && query) {
const limit = parseInt(searchParams.get('limit') || '10');
const searchHistory = searchAnalytics.getSearchHistory(query, limit);
return NextResponse.json({
query,
searchHistory,
count: searchHistory.length,
timestamp: new Date().toISOString(),
});
}
// Default: get analytics stats
let timeRangeMs: number | undefined;
switch (timeRange) {
case '1h':
timeRangeMs = 60 * 60 * 1000;
break;
case '24h':
timeRangeMs = 24 * 60 * 60 * 1000;
break;
case '7d':
timeRangeMs = 7 * 24 * 60 * 60 * 1000;
break;
case '30d':
timeRangeMs = 30 * 24 * 60 * 60 * 1000;
break;
default:
timeRangeMs = undefined; // All time
}
const stats = searchAnalytics.getStats(timeRangeMs);
return NextResponse.json({
analytics: stats,
timeRange: timeRange || 'all',
timeRangeMs,
timestamp: new Date().toISOString(),
});
} catch (error) {
console.error('Search analytics error:', error);
return NextResponse.json(
{ error: 'Failed to retrieve search analytics' },
{ status: 500 }
);
}
}