import { PrismaClient } from '@prisma/client';
import { PrismaPg } from '@prisma/adapter-pg';
import { Pool } from 'pg';

const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const prisma = new PrismaClient({ adapter: new PrismaPg(pool) });

try {
  const counts = await prisma.gscJob.groupBy({
    by: ['type', 'status'],
    _count: true,
    orderBy: [{ type: 'asc' }, { status: 'asc' }],
  });
  console.log('=== queue depth by type x status ===');
  for (const c of counts) console.log(`${c.type.padEnd(20)} ${c.status.padEnd(10)} ${c._count}`);

  const recent = await prisma.gscJob.findMany({
    where: { completedAt: { gte: new Date(Date.now() - 5 * 60 * 1000) } },
    select: { type: true, status: true },
  });
  console.log(`\n=== completed in last 5 min: ${recent.length} jobs ===`);
  const byType = {};
  for (const j of recent) byType[j.type] = (byType[j.type] || 0) + 1;
  for (const [t, n] of Object.entries(byType)) console.log(`  ${t.padEnd(20)} ${n}`);
} finally {
  await prisma.$disconnect();
  await pool.end();
}
