import { FastifyInstance } from 'fastify'; import prisma from '../../config/database'; import { successResponse, paginatedResponse } from '../../utils/response'; export async function collectionLogRoutes(app: FastifyInstance): Promise { // 获取采集日志列表 app.get('/', async (request, reply) => { const { page = 1, limit = 20, sourceId, status } = request.query as any; const where: any = {}; if (sourceId) where.sourceId = parseInt(sourceId); if (status) where.status = status; const [items, total] = await Promise.all([ prisma.collectionLog.findMany({ where, include: { source: true }, orderBy: { createdAt: 'desc' }, skip: (Number(page) - 1) * Number(limit), take: Number(limit), }), prisma.collectionLog.count({ where }), ]); paginatedResponse(reply, items, total, Number(page), Number(limit)); }); // 获取数据源统计 app.get('/stats', async (request, reply) => { const sources = await prisma.source.findMany({ include: { collectionLogs: { orderBy: { createdAt: 'desc' }, take: 1, }, _count: { select: { articles: true, collectionLogs: true }, }, }, }); const stats = sources.map((source) => ({ id: source.id, name: source.name, type: source.type, enabled: source.enabled, lastCollectedAt: source.lastCollectedAt, lastLog: source.collectionLogs[0] || null, totalArticles: source._count.articles, totalCollections: source._count.collectionLogs, })); successResponse(reply, stats); }); }