-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
140 lines (125 loc) · 4.54 KB
/
route.ts
File metadata and controls
140 lines (125 loc) · 4.54 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
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
import { NextResponse } from 'next/server';
import { createSupabaseServerClient } from '@/lib/supabase';
import { searchAnalytics } from '@/lib/analytics';
import { searchCache } from '@/lib/cache';
import { searchRateLimiter } from '@/lib/rate-limit';
export async function GET() {
try {
const supabase = await createSupabaseServerClient();
// Get total repositories count
const { count: totalRepositories } = await supabase
.from('repositories')
.select('*', { count: 'exact', head: true });
// Get analyzed repositories count using repository_analysis table
const { count: totalAnalyzed } = await supabase
.from('repository_analysis')
.select('*', { count: 'exact', head: true });
// Get aggregate statistics from repository_analysis table
const { data: statsData } = await supabase
.from('repository_analysis')
.select(`
total_lines,
files_processed,
repository_id
`);
let totalLines = 0;
let totalFiles = 0;
const languageCounts: Record<string, number> = {};
if (statsData) {
statsData.forEach(stat => {
totalLines += stat.total_lines || 0;
totalFiles += stat.files_processed || 0;
});
}
// Get language distribution from repositories
const { data: repositories } = await supabase
.from('repositories')
.select('author');
if (repositories) {
repositories.forEach(repo => {
const language = repo.author || 'Unknown'; // Using author as proxy since language field doesn't exist
languageCounts[language] = (languageCounts[language] || 0) + 1;
});
}
const averageComplexity = 0; // Not available in current schema
// Calculate language percentages
const totalReposWithLanguage = Object.values(languageCounts).reduce((sum, count) => sum + count, 0);
const topLanguages = Object.entries(languageCounts)
.map(([language, count]) => ({
language,
count,
percentage: totalReposWithLanguage > 0 ? (count / totalReposWithLanguage) * 100 : 0,
}))
.sort((a, b) => b.count - a.count)
.slice(0, 10);
// Get recent analyses
const { data: recentAnalyses } = await supabase
.from('repositories')
.select(`
name,
author,
updated_at
`)
.order('updated_at', { ascending: false })
.limit(10);
const formattedRecentAnalyses = recentAnalyses?.map(repo => ({
repository: {
full_name: repo.name,
stars_count: 0, // Not available in current schema
language: repo.author || 'Unknown',
},
analyzed_at: repo.updated_at,
})) || [];
// Get search analytics (last 24 hours)
const searchStats = searchAnalytics.getStats(24 * 60 * 60 * 1000);
const cacheStats = searchCache.getStats();
const rateLimitStats = searchRateLimiter.getStats();
const recentSearches = searchAnalytics.getRecentSearches(5);
const dashboardStats = {
totalRepositories: totalRepositories || 0,
totalAnalyzed: totalAnalyzed || 0,
totalLines,
totalFiles,
averageComplexity,
topLanguages,
recentAnalyses: formattedRecentAnalyses,
searchAnalytics: {
totalSearches24h: searchStats.totalSearches,
uniqueQueries24h: searchStats.uniqueQueries,
averageResponseTime: searchStats.averageResponseTime,
cacheHitRate: searchStats.cacheHitRate,
errorRate: searchStats.errorRate,
rateLimitRate: searchStats.rateLimitRate,
topQueries: searchStats.topQueries.slice(0, 5), // Top 5
topFilters: searchStats.topFilters.slice(0, 5), // Top 5
recentSearches: recentSearches.map(search => ({
query: search.query,
results_count: search.results_count,
response_time_ms: search.response_time_ms,
cached: search.cached,
timestamp: new Date(search.timestamp).toISOString(),
error: search.error,
})),
},
systemStats: {
cache: {
size: cacheStats.size,
hitRate: cacheStats.hitRate,
memoryUsage: cacheStats.totalMemoryUsage,
},
rateLimiting: {
activeClients: rateLimitStats.activeClients,
totalBlocked: rateLimitStats.totalBlocked,
blockRate: rateLimitStats.blockRate,
},
},
};
return NextResponse.json(dashboardStats);
} catch (error) {
console.error('Dashboard stats error:', error);
return NextResponse.json(
{ error: 'Failed to fetch dashboard statistics' },
{ status: 500 }
);
}
}