import { createServerComponentClient } from '@supabase/auth-helpers-nextjs'; import { cookies } from 'next/headers'; import { redirect } from 'next/navigation'; import { checkIsAdmin, getGlobalApiStats } from '@/services/track-api-usage'; import { BarChart3, TrendingUp, Users, Calendar, AlertCircle } from 'lucide-react'; import Link from 'next/link'; export default async function AdminPage() { const supabase = createServerComponentClient({ cookies }); const { data: { user } } = await supabase.auth.getUser(); console.log('[Admin Page] User:', user?.id, user?.email); if (!user) { console.log('[Admin Page] No user found, redirecting to home'); redirect('/'); } const isAdmin = await checkIsAdmin(user.id); console.log('[Admin Page] Is admin check result:', isAdmin); if (!isAdmin) { console.log('[Admin Page] User is not admin, redirecting to home'); redirect('/'); } console.log('[Admin Page] Access granted, loading dashboard'); // Fetch global API stats const stats = await getGlobalApiStats(); // Fetch recent API usage const { data: recentUsage } = await supabase .from('api_usage') .select(` *, profiles:user_id ( username ) `) .order('created_at', { ascending: false }) .limit(50); // Fetch per-user statistics const { data: userStats } = await supabase .from('api_usage') .select('user_id, api_type') .order('created_at', { ascending: false }); // Group by user const userStatsMap = new Map(); userStats?.forEach(item => { const current = userStatsMap.get(item.user_id) || { googleSearch: 0, geminiAi: 0, total: 0 }; if (item.api_type === 'google_search') current.googleSearch++; if (item.api_type === 'gemini_ai') current.geminiAi++; current.total++; userStatsMap.set(item.user_id, current); }); // Get user details for top users const topUserIds = Array.from(userStatsMap.entries()) .sort((a, b) => b[1].total - a[1].total) .slice(0, 10) .map(([userId]) => userId); const { data: topUsers } = await supabase .from('profiles') .select('id, username') .in('id', topUserIds); const topUsersWithStats = topUsers?.map(user => ({ ...user, stats: userStatsMap.get(user.id)! })) || []; return (
{/* Header */}

Admin Dashboard

API Usage Monitoring & Statistics

Back to App
{/* Global Stats Cards */}
Total Calls
{stats?.totalCalls || 0}
All time
Today
{stats?.todayCalls || 0}
{stats?.todayCalls && stats.todayCalls >= 80 ? ( Limit reached ) : ( `${80 - (stats?.todayCalls || 0)} remaining` )}
Google Search
{stats?.googleSearchCalls || 0}
Whiskybase searches
Gemini AI
{stats?.geminiAiCalls || 0}
Bottle analyses
{/* Top Users */}

Top Users by API Usage

{topUsersWithStats.map((user, index) => (
{index + 1}
{user.username}
{user.stats.googleSearch} searches ยท {user.stats.geminiAi} analyses
{user.stats.total}
))}
{/* Recent Activity */}

Recent API Calls

{recentUsage?.map((item) => ( ))}
Time User API Type Status Error
{new Date(item.created_at).toLocaleString('de-DE')} {(item.profiles as any)?.username || 'Unknown'} {item.api_type === 'google_search' ? 'Search' : 'AI'} {item.success ? 'Success' : 'Failed'} {item.error_message || '-'}
); }