2025-12-08 16:31:30 +03:00

69 lines
1.8 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { redis } from '@/lib/redis'
export async function GET(req: NextRequest, { params }: { params: { fid: string } }) {
try {
const { fid } = params
const votes = await redis.lrange(`votes:${fid}`, 0, -1)
let totalVotes = 0
let totalWins = 0
let totalLosses = 0
let totalEarnings = 0
let totalSpent = 0
let currentStreak = 0
let bestStreak = 0
let tempStreak = 0
const recentVotes = []
for (const voteStr of votes) {
const vote = JSON.parse(voteStr)
totalVotes++
totalSpent += vote.amount || 0
if (vote.won) {
totalWins++
totalEarnings += vote.winnings || 0
tempStreak++
if (tempStreak > bestStreak) bestStreak = tempStreak
} else {
totalLosses++
tempStreak = 0
}
if (recentVotes.length < 10) {
recentVotes.push({
pollId: vote.pollId,
question: vote.pollQuestion || 'Poll',
won: vote.won || false,
amount: vote.won ? (vote.winnings || 0) : (vote.amount || 0),
timestamp: vote.timestamp
})
}
}
currentStreak = tempStreak
const winRate = totalVotes > 0 ? Math.round((totalWins / totalVotes) * 100) : 0
const netProfit = totalEarnings - totalSpent
const avgVoteAmount = totalVotes > 0 ? totalSpent / totalVotes : 0
return NextResponse.json({
totalVotes,
totalWins,
totalLosses,
winRate,
totalEarnings,
totalSpent,
netProfit,
currentStreak,
bestStreak,
avgVoteAmount,
recentVotes
})
} catch (error) {
console.error('Stats API error:', error)
return NextResponse.json({ error: 'Failed to fetch stats' }, { status: 500 })
}
}