feat: 实现LLM趋势告警历史上下文判断功能

新增功能:
- 在LLM趋势分析中集成历史告警上下文,提供最近3次已触发告警的记录
- LLM自动判断本次分析是否与历史告警相似,避免重复通知
- 新增shouldNotify和notifyReason字段,支持智能通知决策

技术改进:
- 扩展TrendAnalysisResponse类型,增加通知决策字段
- 数据库服务新增getRecentTriggeredAnalyses()方法
- LLM客户端新增analyzeTrendWithHistory()和相关方法
- 趋势分析服务集成历史上下文获取和通知决策逻辑

测试验证:
- 创建test-db-history-method.ts验证数据库方法
- 创建test-llm-history-context.ts测试历史上下文功能
- 通过TypeScript类型检查和构建验证

这个功能可以有效减少重复告警,提升告警质量,避免通知疲劳。

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Sonnet 4.5 committed 2026-01-15 19:37:28 +08:00
commit ccf523543a
52 files changed
+7763

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
import type { PriceDataPoint } from './src/types/index.js';
/**
* 计算最近 N 个数据点的价格波动幅度
*/
function calculatePriceRange(dataPoints: PriceDataPoint[], count: number = 10): number {
const recentPoints = dataPoints.slice(-count);
if (recentPoints.length < 2) return 0;
const prices = recentPoints.map(p => p.price);
const maxPrice = Math.max(...prices);
const minPrice = Math.min(...prices);
return maxPrice - minPrice;
}
/**
* 测试波动幅度阈值逻辑
*/
function testVolatilityThreshold() {
console.log('========================================');
console.log('测试波动幅度阈值逻辑');
console.log('========================================\n');
// 测试场景 1:小幅波动(0.15)
console.log('场景 1: 小幅波动');
const scenario1: PriceDataPoint[] = Array.from({ length: 10 }, (_, i) => ({
price: 22.0 + (i % 3) * 0.05 + Math.random() * 0.02,
timestamp: new Date(Date.now() - (10 - i) * 60000).toISOString()
}));
const range1 = calculatePriceRange(scenario1, 10);
console.log(` 波动幅度: ${range1.toFixed(3)}`);
console.log(` 是否触发告警: ${range1 > 0.2 ? '是 ✅' : '否 ❌'}`);
console.log('');
// 测试场景 2:中等波动(0.25)
console.log('场景 2: 中等波动');
const scenario2: PriceDataPoint[] = Array.from({ length: 10 }, (_, i) => ({
price: 22.0 + (i % 5) * 0.06 + Math.random() * 0.02,
timestamp: new Date(Date.now() - (10 - i) * 60000).toISOString()
}));
const range2 = calculatePriceRange(scenario2, 10);
console.log(` 波动幅度: ${range2.toFixed(3)}`);
console.log(` 是否触发告警: ${range2 > 0.2 ? '是 ✅' : '否 ❌'}`);
console.log('');
// 测试场景 3:大幅波动(0.5)
console.log('场景 3: 大幅波动');
const scenario3: PriceDataPoint[] = Array.from({ length: 10 }, (_, i) => ({
price: 22.0 + (i % 7) * 0.08 + Math.random() * 0.02,
timestamp: new Date(Date.now() - (10 - i) * 60000).toISOString()
}));
const range3 = calculatePriceRange(scenario3, 10);
console.log(` 波动幅度: ${range3.toFixed(3)}`);
console.log(` 是否触发告警: ${range3 > 0.2 ? '是 ✅' : '否 ❌'}`);
console.log('');
// 测试场景 4:模拟真实剧烈波动
console.log('场景 4: 模拟真实剧烈波动(22.0 → 22.6 → 22.2)');
const scenario4: PriceDataPoint[] = [
{ price: 22.0, timestamp: new Date(Date.now() - 9 * 60000).toISOString() },
{ price: 22.1, timestamp: new Date(Date.now() - 8 * 60000).toISOString() },
{ price: 22.2, timestamp: new Date(Date.now() - 7 * 60000).toISOString() },
{ price: 22.35, timestamp: new Date(Date.now() - 6 * 60000).toISOString() },
{ price: 22.5, timestamp: new Date(Date.now() - 5 * 60000).toISOString() },
{ price: 22.6, timestamp: new Date(Date.now() - 4 * 60000).toISOString() },
{ price: 22.5, timestamp: new Date(Date.now() - 3 * 60000).toISOString() },
{ price: 22.35, timestamp: new Date(Date.now() - 2 * 60000).toISOString() },
{ price: 22.25, timestamp: new Date(Date.now() - 1 * 60000).toISOString() },
{ price: 22.2, timestamp: new Date().toISOString() }
];
const range4 = calculatePriceRange(scenario4, 10);
console.log(` 最高价: ${Math.max(...scenario4.map(p => p.price)).toFixed(3)}`);
console.log(` 最低价: ${Math.min(...scenario4.map(p => p.price)).toFixed(3)}`);
console.log(` 波动幅度: ${range4.toFixed(3)}`);
console.log(` 是否触发告警: ${range4 > 0.2 ? '是 ✅' : '否 ❌'}`);
console.log('');
console.log('========================================');
console.log('✅ 测试完成!');
console.log('========================================');
}
testVolatilityThreshold();