修复代码质量问题并更新任务进度

修复内容:
- API 采集器添加 30 秒超时控制 (AbortController)
- 采集服务优化批量插入,修复 URL 为空时的去重逻辑
- 整理类型定义,将 ProcessedContent 移至 types/index.ts
- 添加 ReportStatus 和 ReportGenerationOptions 类型

更新任务进度:
- 任务 01-05 标记为已完成
- 任务 06-09 待开发

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Opus 4.5 committed 2026-02-04 21:23:53 +08:00
1 parent d8553c06fa
commit bb9d691f7a
5 files changed
+64 -61

No files matched your search

+11 -1
View File
@@ -27,12 +27,17 @@ export class ApiCollector extends BaseCollector {
}); });
} }
// 设置超时控制
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 30000);
const options: RequestInit = { const options: RequestInit = {
method: this.apiConfig.method || 'GET', method: this.apiConfig.method || 'GET',
headers: { headers: {
'User-Agent': 'News-Aggregator/1.0', 'User-Agent': 'News-Aggregator/1.0',
...this.apiConfig.headers, ...this.apiConfig.headers,
}, },
signal: controller.signal,
}; };
if (this.apiConfig.body && this.apiConfig.method === 'POST') { 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) { if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`); throw new Error(`HTTP ${response.status}: ${response.statusText}`);
+34 -29
View File
@@ -49,37 +49,42 @@ export class CollectorService {
throw new Error(result.error || 'Collection failed'); throw new Error(result.error || 'Collection failed');
} }
// 保存资讯(去重) // 保存资讯(批量去重)
const articlesToCreate = [];
for (const article of result.articles) { for (const article of result.articles) {
try { // 检查是否已存在(通过 URL 或标题去重)
// 检查是否已存在(通过 URL 或标题去重) const whereConditions: any[] = [{ title: article.title }];
const existing = await prisma.article.findFirst({ if (article.url) {
where: { whereConditions.push({ url: article.url });
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 });
} }
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;
} }
// 更新数据源的最后采集时间 // 更新数据源的最后采集时间
+1 -7
View File
@@ -1,15 +1,9 @@
import { Article } from '@prisma/client'; import { Article } from '@prisma/client';
import { LlmClient } from '../utils/llm-client'; import { LlmClient } from '../utils/llm-client';
import { LlmClientConfig } from '../types/llm'; import { LlmClientConfig } from '../types/llm';
import { ProcessMode } from '../types'; import { ProcessMode, ProcessedContent } from '../types';
import logger from '../utils/logger'; import logger from '../utils/logger';
export interface ProcessedContent {
content: string;
mode: ProcessMode;
articlesCount: number;
}
export class ProcessorService { export class ProcessorService {
/** /**
* 处理资讯列表 * 处理资讯列表
+13 -19
View File
@@ -21,29 +21,23 @@ export interface PaginatedResponse<T> extends ApiResponse<T[]> {
}; };
} }
// 采集器类型
export interface CollectorConfig {
url: string;
method?: 'GET' | 'POST';
headers?: Record<string, string>;
body?: any;
}
export interface RawArticle {
title: string;
content?: string;
url?: string;
publishedAt?: Date;
[key: string]: any;
}
// 处理器类型 // 处理器类型
export type ProcessMode = 'aggregate' | 'summary' | 'deep_analysis'; export type ProcessMode = 'aggregate' | 'summary' | 'deep_analysis';
export interface ProcessingResult { export interface ProcessedContent {
title: string;
content: string; 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;
} }
// 推送器类型 // 推送器类型
+5 -5
View File
@@ -5,13 +5,13 @@
## 后端任务 ## 后端任务
### 基础设施 ### 基础设施
- [x] [任务 01:项目初始化](./01-project-setup.md) - [x] [任务 01:项目初始化](./01-project-setup.md) - 已完成
- [x] [任务 02:数据库搭建](./02-database-setup.md) - Schema 和种子数据已创建 - [x] [任务 02:数据库搭建](./02-database-setup.md) - 已完成
- [ ] [任务 03:后端核心框架](./03-backend-core.md) - 基础模块已创建,待完善中间件和路由 - [x] [任务 03:后端核心框架](./03-backend-core.md) - 已完成
### 核心功能 ### 核心功能
- [ ] [任务 04:采集器实现](./04-collectors.md) - [x] [任务 04:采集器实现](./04-collectors.md) - 已完成
- [ ] [任务 05:内容处理](./05-processor.md) - [x] [任务 05:内容处理](./05-processor.md) - 已完成
- [ ] [任务 06:报告生成](./06-report-generator.md) - [ ] [任务 06:报告生成](./06-report-generator.md)
- [ ] [任务 07:分发器实现](./07-distributors.md) - [ ] [任务 07:分发器实现](./07-distributors.md)
- [ ] [任务 08:定时任务调度](./08-scheduler.md) - [ ] [任务 08:定时任务调度](./08-scheduler.md)