Files
podcastdistributiona/lib/admin/ops.ts
T

68 lines
2.4 KiB
TypeScript

import type { Prisma } from "@prisma/client";
import { prisma } from "@/lib/db";
export const JOBS_PAGE_SIZE = 25;
export const WEBHOOKS_PAGE_SIZE = 30;
export async function listJobs(params: { status?: string; page: number; pageSize?: number }) {
const pageSize = params.pageSize ?? JOBS_PAGE_SIZE;
const where: Prisma.GenerationJobWhereInput = {};
if (params.status) where.status = params.status;
const [rows, total] = await Promise.all([
prisma.generationJob.findMany({
where,
orderBy: { createdAt: "desc" },
skip: (params.page - 1) * pageSize,
take: pageSize,
include: { episode: { select: { id: true, title: true } } },
}),
prisma.generationJob.count({ where }),
]);
return { rows, total };
}
export async function getJobStatusCounts(): Promise<Record<string, number>> {
const groups = await prisma.generationJob.groupBy({ by: ["status"], _count: true });
const counts: Record<string, number> = { queued: 0, running: 0, completed: 0, failed: 0 };
for (const g of groups) counts[g.status] = g._count;
return counts;
}
export async function getEpisodeStatusCounts() {
const groups = await prisma.episode.groupBy({ by: ["status"], _count: true });
return groups.map((g) => ({ status: g.status as string, count: g._count }));
}
export async function getModerationQueue() {
return prisma.contentFlag.findMany({
where: { status: "open" },
orderBy: { createdAt: "desc" },
include: { episode: { select: { id: true, title: true } } },
});
}
export async function listWebhookEvents(params: {
provider?: string;
status?: string;
page: number;
pageSize?: number;
}) {
const pageSize = params.pageSize ?? WEBHOOKS_PAGE_SIZE;
const where: Prisma.WebhookEventWhereInput = {};
if (params.provider) where.provider = params.provider;
if (params.status) where.status = params.status;
const dayAgo = new Date(Date.now() - 86_400_000);
const [rows, total, recentFailures, recentTotal] = await Promise.all([
prisma.webhookEvent.findMany({
where,
orderBy: { createdAt: "desc" },
skip: (params.page - 1) * pageSize,
take: pageSize,
}),
prisma.webhookEvent.count({ where }),
prisma.webhookEvent.count({ where: { status: "failed", createdAt: { gte: dayAgo } } }),
prisma.webhookEvent.count({ where: { createdAt: { gte: dayAgo } } }),
]);
return { rows, total, recentFailures, recentTotal };
}