diff --git a/backend/src/collectors/api.collector.ts b/backend/src/collectors/api.collector.ts index d248834..b835ef1 100644 --- a/backend/src/collectors/api.collector.ts +++ b/backend/src/collectors/api.collector.ts @@ -27,12 +27,17 @@ export class ApiCollector extends BaseCollector { }); } + // 设置超时控制 + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), 30000); + const options: RequestInit = { method: this.apiConfig.method || 'GET', headers: { 'User-Agent': 'News-Aggregator/1.0', ...this.apiConfig.headers, }, + signal: controller.signal, }; if (this.apiConfig.body && this.apiConfig.method === 'POST') { @@ -44,7 +49,12 @@ export class ApiCollector extends BaseCollector { } // 发送请求 - const response = await fetch(url.toString(), options); + let response: Response; + try { + response = await fetch(url.toString(), options); + } finally { + clearTimeout(timeout); + } if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); diff --git a/backend/src/services/collector.service.ts b/backend/src/services/collector.service.ts index d950847..5e213b8 100644 --- a/backend/src/services/collector.service.ts +++ b/backend/src/services/collector.service.ts @@ -49,37 +49,42 @@ export class CollectorService { throw new Error(result.error || 'Collection failed'); } - // 保存资讯(去重) + // 保存资讯(批量去重) + const articlesToCreate = []; for (const article of result.articles) { - try { - // 检查是否已存在(通过 URL 或标题去重) - const existing = await prisma.article.findFirst({ - where: { - sourceId: source.id, - OR: [ - { url: article.url || undefined }, - { title: article.title }, - ], - }, - }); - - if (!existing) { - await prisma.article.create({ - data: { - sourceId: source.id, - title: article.title, - content: article.content, - url: article.url, - publishedAt: article.publishedAt, - collectedAt: new Date(), - rawData: article.rawData, - }, - }); - articlesCount++; - } - } catch (error) { - logger.warn('保存资讯失败', { title: article.title, error }); + // 检查是否已存在(通过 URL 或标题去重) + const whereConditions: any[] = [{ title: article.title }]; + if (article.url) { + whereConditions.push({ url: article.url }); } + + const existing = await prisma.article.findFirst({ + where: { + sourceId: source.id, + OR: whereConditions, + }, + }); + + if (!existing) { + articlesToCreate.push({ + sourceId: source.id, + title: article.title, + content: article.content, + url: article.url, + publishedAt: article.publishedAt, + collectedAt: new Date(), + rawData: article.rawData, + }); + } + } + + // 批量插入 + if (articlesToCreate.length > 0) { + await prisma.article.createMany({ + data: articlesToCreate, + skipDuplicates: true, + }); + articlesCount = articlesToCreate.length; } // 更新数据源的最后采集时间 diff --git a/backend/src/services/processor.service.ts b/backend/src/services/processor.service.ts index 0e3c463..946c452 100644 --- a/backend/src/services/processor.service.ts +++ b/backend/src/services/processor.service.ts @@ -1,15 +1,9 @@ import { Article } from '@prisma/client'; import { LlmClient } from '../utils/llm-client'; import { LlmClientConfig } from '../types/llm'; -import { ProcessMode } from '../types'; +import { ProcessMode, ProcessedContent } from '../types'; import logger from '../utils/logger'; -export interface ProcessedContent { - content: string; - mode: ProcessMode; - articlesCount: number; -} - export class ProcessorService { /** * 处理资讯列表 diff --git a/backend/src/types/index.ts b/backend/src/types/index.ts index dc57b2e..a813bf5 100644 --- a/backend/src/types/index.ts +++ b/backend/src/types/index.ts @@ -21,29 +21,23 @@ export interface PaginatedResponse extends ApiResponse { }; } -// 采集器类型 -export interface CollectorConfig { - url: string; - method?: 'GET' | 'POST'; - headers?: Record; - body?: any; -} - -export interface RawArticle { - title: string; - content?: string; - url?: string; - publishedAt?: Date; - [key: string]: any; -} - // 处理器类型 export type ProcessMode = 'aggregate' | 'summary' | 'deep_analysis'; -export interface ProcessingResult { - title: string; +export interface ProcessedContent { content: string; - articleCount: number; + mode: ProcessMode; + articlesCount: number; +} + +// 报告类型 +export type ReportStatus = 'pending' | 'generating' | 'completed' | 'failed'; + +export interface ReportGenerationOptions { + topicId: number; + periodStart: Date; + periodEnd: Date; + manualTrigger?: boolean; } // 推送器类型 diff --git a/docs/tasks/README.md b/docs/tasks/README.md index 85fd461..95ca26c 100644 --- a/docs/tasks/README.md +++ b/docs/tasks/README.md @@ -5,13 +5,13 @@ ## 后端任务 ### 基础设施 -- [x] [任务 01:项目初始化](./01-project-setup.md) -- [x] [任务 02:数据库搭建](./02-database-setup.md) - Schema 和种子数据已创建 -- [ ] [任务 03:后端核心框架](./03-backend-core.md) - 基础模块已创建,待完善中间件和路由 +- [x] [任务 01:项目初始化](./01-project-setup.md) - 已完成 +- [x] [任务 02:数据库搭建](./02-database-setup.md) - 已完成 +- [x] [任务 03:后端核心框架](./03-backend-core.md) - 已完成 ### 核心功能 -- [ ] [任务 04:采集器实现](./04-collectors.md) -- [ ] [任务 05:内容处理](./05-processor.md) +- [x] [任务 04:采集器实现](./04-collectors.md) - 已完成 +- [x] [任务 05:内容处理](./05-processor.md) - 已完成 - [ ] [任务 06:报告生成](./06-report-generator.md) - [ ] [任务 07:分发器实现](./07-distributors.md) - [ ] [任务 08:定时任务调度](./08-scheduler.md)