157 lines
5.1 KiB
TypeScript
157 lines
5.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getDb } from '@/lib/db';
|
|
import { polls, votes, distributions } from '@/lib/db/schema';
|
|
import { eq, desc, gte, count } from 'drizzle-orm';
|
|
|
|
interface CronHealthStatus {
|
|
timestamp: number;
|
|
status: 'healthy' | 'warning' | 'critical';
|
|
checks: {
|
|
database: boolean;
|
|
pendingVerifications: number;
|
|
pendingDistributions: number;
|
|
recentActivity: boolean;
|
|
protocolWallet: boolean;
|
|
};
|
|
metrics: {
|
|
totalPolls: number;
|
|
activePolls: number;
|
|
verifiedPolls: number;
|
|
distributedPolls: number;
|
|
totalVotes: number;
|
|
totalDistributed: number;
|
|
};
|
|
recommendations: string[];
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const startTime = Date.now();
|
|
const health: CronHealthStatus = {
|
|
timestamp: startTime,
|
|
status: 'healthy',
|
|
checks: {
|
|
database: false,
|
|
pendingVerifications: 0,
|
|
pendingDistributions: 0,
|
|
recentActivity: false,
|
|
protocolWallet: false
|
|
},
|
|
metrics: {
|
|
totalPolls: 0,
|
|
activePolls: 0,
|
|
verifiedPolls: 0,
|
|
distributedPolls: 0,
|
|
totalVotes: 0,
|
|
totalDistributed: 0
|
|
},
|
|
recommendations: []
|
|
};
|
|
|
|
try {
|
|
const db = getDb(process.env.DATABASE_URL!);
|
|
health.checks.database = true;
|
|
|
|
// Get poll counts by status
|
|
const [totalPollsResult] = await db.select({ count: count() }).from(polls);
|
|
const [activePollsResult] = await db.select({ count: count() }).from(polls).where(eq(polls.status, 'active'));
|
|
const [verifiedPollsResult] = await db.select({ count: count() }).from(polls).where(eq(polls.status, 'verified'));
|
|
const [distributedPollsResult] = await db.select({ count: count() }).from(polls).where(eq(polls.status, 'distributed'));
|
|
|
|
health.metrics.totalPolls = totalPollsResult.count;
|
|
health.metrics.activePolls = activePollsResult.count;
|
|
health.metrics.verifiedPolls = verifiedPollsResult.count;
|
|
health.metrics.distributedPolls = distributedPollsResult.count;
|
|
|
|
// Check for polls that need verification
|
|
const now = Date.now();
|
|
const pendingVerifications = await db.select()
|
|
.from(polls)
|
|
.where(eq(polls.status, 'active'));
|
|
|
|
health.checks.pendingVerifications = pendingVerifications.filter(p => p.endTime < now).length;
|
|
|
|
// Check for polls that need distribution
|
|
const pendingDistributions = await db.select()
|
|
.from(polls)
|
|
.where(eq(polls.status, 'verified'));
|
|
|
|
health.checks.pendingDistributions = pendingDistributions.filter(p => p.correctOptionId != null).length;
|
|
|
|
// Check recent activity (votes in last 24 hours)
|
|
const yesterday = now - (24 * 60 * 60 * 1000);
|
|
const [recentVotesResult] = await db.select({ count: count() })
|
|
.from(votes)
|
|
.where(gte(votes.timestamp, yesterday));
|
|
|
|
health.checks.recentActivity = recentVotesResult.count > 0;
|
|
|
|
// Check protocol wallet configuration
|
|
health.checks.protocolWallet = !!process.env.PROTOCOL_WALLET_PRIVATE_KEY;
|
|
|
|
// Calculate total votes and distributed amount
|
|
const [totalVotesResult] = await db.select({ count: count() }).from(votes);
|
|
health.metrics.totalVotes = totalVotesResult.count;
|
|
|
|
const [totalDistributedResult] = await db.select({
|
|
total: count()
|
|
}).from(distributions);
|
|
health.metrics.totalDistributed = totalDistributedResult.total;
|
|
|
|
// Generate recommendations
|
|
if (health.checks.pendingVerifications > 10) {
|
|
health.recommendations.push(`High number of polls pending verification: ${health.checks.pendingVerifications}`);
|
|
health.status = 'warning';
|
|
}
|
|
|
|
if (health.checks.pendingDistributions > 5) {
|
|
health.recommendations.push(`High number of polls pending distribution: ${health.checks.pendingDistributions}`);
|
|
health.status = 'warning';
|
|
}
|
|
|
|
if (!health.checks.protocolWallet) {
|
|
health.recommendations.push('Protocol wallet not configured - distributions will fail');
|
|
health.status = 'critical';
|
|
}
|
|
|
|
if (!health.checks.database) {
|
|
health.recommendations.push('Database connection failed');
|
|
health.status = 'critical';
|
|
}
|
|
|
|
if (health.metrics.totalPolls === 0) {
|
|
health.recommendations.push('No polls found - system may need initial setup');
|
|
}
|
|
|
|
if (health.metrics.distributedPolls === 0 && health.metrics.verifiedPolls > 0) {
|
|
health.recommendations.push('Verified polls exist but none have been distributed - check distribution cron');
|
|
health.status = 'warning';
|
|
}
|
|
|
|
const duration = Date.now() - startTime;
|
|
|
|
console.log(`Cron health check completed in ${duration}ms:`, {
|
|
status: health.status,
|
|
pendingVerifications: health.checks.pendingVerifications,
|
|
pendingDistributions: health.checks.pendingDistributions
|
|
});
|
|
|
|
return NextResponse.json({
|
|
...health,
|
|
duration,
|
|
endpoint: 'cron-health'
|
|
});
|
|
|
|
} catch (error) {
|
|
health.status = 'critical';
|
|
health.recommendations.push(`Health check failed: ${error}`);
|
|
|
|
console.error('Cron health check failed:', error);
|
|
|
|
return NextResponse.json({
|
|
...health,
|
|
error: error instanceof Error ? error.message : String(error),
|
|
duration: Date.now() - startTime
|
|
}, { status: 500 });
|
|
}
|
|
}
|