Spaces:
Sleeping
Sleeping
| export const dynamic = 'force-dynamic'; | |
| import type { ApiResponse } from '@/types/api'; | |
| import type { AnalyticsResponse } from '@/types/analytics'; | |
| import { getAnalyticsSummary } from '@/lib/analytics/calculate'; | |
| import { generateInsightsFromMetrics } from '@/lib/analytics/insights'; | |
| export async function GET(request: Request): Promise<Response> { | |
| try { | |
| const { searchParams } = new URL(request.url); | |
| const period = (searchParams.get('period') || 'weekly') as 'daily' | 'weekly' | 'monthly'; | |
| // Fetch analytics summary | |
| const summary = await getAnalyticsSummary(period); | |
| // Generate insights from metrics | |
| const allInsights = generateInsightsFromMetrics(summary.insights); | |
| const response: ApiResponse<AnalyticsResponse> = { | |
| success: true, | |
| data: { | |
| period, | |
| start_date: summary.daily_metrics[summary.daily_metrics.length - 1]?.date || new Date().toISOString().split('T')[0], | |
| end_date: summary.daily_metrics[0]?.date || new Date().toISOString().split('T')[0], | |
| total_carousels: summary.total_carousels, | |
| total_engagement: summary.total_engagement, | |
| avg_engagement_rate: summary.avg_engagement_rate, | |
| top_performing_carousel: summary.top_carousel, | |
| daily_metrics: summary.daily_metrics, | |
| insights: summary.insights, | |
| }, | |
| }; | |
| return Response.json(response); | |
| } catch (error) { | |
| console.error('[analytics] GET /api/analytics failed:', error); | |
| return Response.json( | |
| { | |
| success: false, | |
| error: 'Failed to fetch analytics', | |
| } as ApiResponse<never>, | |
| { status: 500 } | |
| ); | |
| } | |
| } | |