33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getAllPolls } from '@/lib/polls'
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const polls = await getAllPolls()
|
|
const now = Date.now()
|
|
const sevenDaysAgo = now - 7 * 24 * 60 * 60 * 1000
|
|
|
|
const recentPolls = polls.filter(p => p.createdAt > sevenDaysAgo)
|
|
|
|
const totalVotes = recentPolls.reduce((sum, p) => sum + p.totalVotes, 0)
|
|
const totalVolume = recentPolls.reduce((sum, p) => sum + p.totalAmount, 0)
|
|
|
|
const avgVotesPerPoll = recentPolls.length > 0 ? totalVotes / recentPolls.length : 0
|
|
const avgVolumePerPoll = recentPolls.length > 0 ? totalVolume / recentPolls.length : 0
|
|
|
|
const growthRate = polls.length > 0 ? (recentPolls.length / polls.length) * 100 : 0
|
|
|
|
return NextResponse.json({
|
|
recentPolls: recentPolls.length,
|
|
totalVotes,
|
|
totalVolume,
|
|
avgVotesPerPoll,
|
|
avgVolumePerPoll,
|
|
growthRate
|
|
})
|
|
} catch (error) {
|
|
console.error('Trends analytics error:', error)
|
|
return NextResponse.json({ error: 'Failed to fetch trends' }, { status: 500 })
|
|
}
|
|
}
|