修复代码质量问题并更新任务进度
修复内容: - 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:
1 parent
d8553c06fa
commit
bb9d691f7a
5 files changed
+47
-44
No files matched your search
@@ -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}`);
|
||||||
|
|||||||
@@ -49,23 +49,24 @@ 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 }];
|
||||||
|
if (article.url) {
|
||||||
|
whereConditions.push({ url: article.url });
|
||||||
|
}
|
||||||
|
|
||||||
const existing = await prisma.article.findFirst({
|
const existing = await prisma.article.findFirst({
|
||||||
where: {
|
where: {
|
||||||
sourceId: source.id,
|
sourceId: source.id,
|
||||||
OR: [
|
OR: whereConditions,
|
||||||
{ url: article.url || undefined },
|
|
||||||
{ title: article.title },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
await prisma.article.create({
|
articlesToCreate.push({
|
||||||
data: {
|
|
||||||
sourceId: source.id,
|
sourceId: source.id,
|
||||||
title: article.title,
|
title: article.title,
|
||||||
content: article.content,
|
content: article.content,
|
||||||
@@ -73,13 +74,17 @@ export class CollectorService {
|
|||||||
publishedAt: article.publishedAt,
|
publishedAt: article.publishedAt,
|
||||||
collectedAt: new Date(),
|
collectedAt: new Date(),
|
||||||
rawData: article.rawData,
|
rawData: article.rawData,
|
||||||
},
|
|
||||||
});
|
});
|
||||||
articlesCount++;
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
logger.warn('保存资讯失败', { title: article.title, error });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 批量插入
|
||||||
|
if (articlesToCreate.length > 0) {
|
||||||
|
await prisma.article.createMany({
|
||||||
|
data: articlesToCreate,
|
||||||
|
skipDuplicates: true,
|
||||||
|
});
|
||||||
|
articlesCount = articlesToCreate.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新数据源的最后采集时间
|
// 更新数据源的最后采集时间
|
||||||
|
|||||||
@@ -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
@@ -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,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)
|
||||||
|
|||||||
Reference in new issue
Block a user