🚀 Complete Production Setup
✨ Features: - Analytics Dashboard with real-time metrics - Redis caching for performance optimization - Import/Export functionality for projects - Complete admin system with security - Production-ready Docker setup 🔧 Technical: - Removed Ghost CMS dependencies - Added Redis container with caching - Implemented API response caching - Enhanced admin interface with analytics - Optimized for dk0.dev domain 🛡️ Security: - Admin authentication with Basic Auth - Protected analytics endpoints - Secure environment configuration 📊 Analytics: - Performance metrics dashboard - Project statistics visualization - Real-time data with caching - Umami integration for GDPR compliance 🎯 Production Ready: - Multi-container Docker setup - Health checks for all services - Automatic restart policies - Resource limits configured - Ready for Nginx Proxy Manager
This commit is contained in:
87
app/api/analytics/performance/route.ts
Normal file
87
app/api/analytics/performance/route.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
// Check admin authentication
|
||||
const authHeader = request.headers.get('authorization');
|
||||
const basicAuth = process.env.ADMIN_BASIC_AUTH;
|
||||
|
||||
if (!basicAuth) {
|
||||
return new NextResponse('Admin access not configured', { status: 500 });
|
||||
}
|
||||
|
||||
if (!authHeader || !authHeader.startsWith('Basic ')) {
|
||||
return new NextResponse('Authentication required', { status: 401 });
|
||||
}
|
||||
|
||||
const credentials = authHeader.split(' ')[1];
|
||||
const [username, password] = Buffer.from(credentials, 'base64').toString().split(':');
|
||||
const [expectedUsername, expectedPassword] = basicAuth.split(':');
|
||||
|
||||
if (username !== expectedUsername || password !== expectedPassword) {
|
||||
return new NextResponse('Invalid credentials', { status: 401 });
|
||||
}
|
||||
|
||||
// Get performance data from database
|
||||
const pageViews = await prisma.pageView.findMany({
|
||||
orderBy: { timestamp: 'desc' },
|
||||
take: 1000 // Last 1000 page views
|
||||
});
|
||||
|
||||
const userInteractions = await prisma.userInteraction.findMany({
|
||||
orderBy: { timestamp: 'desc' },
|
||||
take: 1000 // Last 1000 interactions
|
||||
});
|
||||
|
||||
// Calculate performance metrics
|
||||
const performance = {
|
||||
pageViews: {
|
||||
total: pageViews.length,
|
||||
last24h: pageViews.filter(pv => {
|
||||
const dayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
|
||||
return new Date(pv.timestamp) > dayAgo;
|
||||
}).length,
|
||||
last7d: pageViews.filter(pv => {
|
||||
const weekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
|
||||
return new Date(pv.timestamp) > weekAgo;
|
||||
}).length,
|
||||
last30d: pageViews.filter(pv => {
|
||||
const monthAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
|
||||
return new Date(pv.timestamp) > monthAgo;
|
||||
}).length
|
||||
},
|
||||
interactions: {
|
||||
total: userInteractions.length,
|
||||
last24h: userInteractions.filter(ui => {
|
||||
const dayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
|
||||
return new Date(ui.timestamp) > dayAgo;
|
||||
}).length,
|
||||
last7d: userInteractions.filter(ui => {
|
||||
const weekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
|
||||
return new Date(ui.timestamp) > weekAgo;
|
||||
}).length,
|
||||
last30d: userInteractions.filter(ui => {
|
||||
const monthAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
|
||||
return new Date(ui.timestamp) > monthAgo;
|
||||
}).length
|
||||
},
|
||||
topPages: pageViews.reduce((acc, pv) => {
|
||||
acc[pv.page] = (acc[pv.page] || 0) + 1;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
topInteractions: userInteractions.reduce((acc, ui) => {
|
||||
acc[ui.type] = (acc[ui.type] || 0) + 1;
|
||||
return acc;
|
||||
}, {} as Record<string, number>)
|
||||
};
|
||||
|
||||
return NextResponse.json(performance);
|
||||
} catch (error) {
|
||||
console.error('Performance analytics error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch performance data' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user