修复代码质量问题并更新任务进度
修复内容: - 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
+64
-61
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 = {
|
||||
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}`);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
// 更新数据源的最后采集时间
|
||||
|
||||
@@ -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 {
|
||||
/**
|
||||
* 处理资讯列表
|
||||
|
||||
+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 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;
|
||||
}
|
||||
|
||||
// 推送器类型
|
||||
|
||||
Reference in new issue
Block a user