Files

78 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

import { DatabaseService } from './src/services/database.js';
import { ScraperService } from './src/services/scraper.js';
import { SilverBaselineMonitor } from './src/services/silver-baseline-monitor.js';
import { AlertService } from './src/utils/alert.js';
import 'dotenv/config';
/**
* 测试白银基线监控功能
*/
async function testSilverBaseline(): Promise<void> {
console.log('========================================');
console.log('白银基线监控测试');
console.log('========================================\n');
const db = new DatabaseService();
const scraper = new ScraperService();
const alertService = new AlertService();
try {
// 初始化
console.log('1. 初始化服务...');
await db.init();
await alertService.init();
await scraper.init();
console.log('✅ 初始化完成\n');
// 创建监控服务
const threshold = Number(process.env.SILVER_BASELINE_THRESHOLD) || 0.2;
const monitor = new SilverBaselineMonitor(
db,
threshold,
alertService,
process.env.WEBHOOK_URL
);
// 获取白银价格
console.log('2. 获取白银价格...');
const silverPrice = await scraper.fetchSilverPrice();
if (!silverPrice) {
console.error('❌ 获取白银价格失败');
return;
}
console.log('✅ 获取成功:');
console.log(` 商品名称: ${silverPrice.name}`);
console.log(` 销售价格: ${silverPrice.sellPrice}`);
console.log(` 回购价格: ${silverPrice.buyPrice}`);
console.log(` 最高价: ${silverPrice.high}`);
console.log(` 最低价: ${silverPrice.low}\n`);
// 处理价格(测试基线初始化或告警触发)
console.log('3. 处理价格监控...');
await monitor.processPrice(silverPrice);
console.log('✅ 处理完成\n');
// 显示统计信息
console.log('4. 统计信息:');
const stats = monitor.getStats();
console.log(` 当前基线: ${stats.currentBaseline !== null ? stats.currentBaseline.toFixed(3) : '未初始化'}`);
console.log(` 总价格记录: ${stats.totalPrices}`);
console.log(` 总告警次数: ${stats.totalAlerts}`);
console.log(` 上涨告警: ${stats.riseCount}`);
console.log(` 下跌告警: ${stats.fallCount}\n`);
console.log('========================================');
console.log('测试完成!');
console.log('========================================');
} catch (error) {
console.error('测试失败:', error);
} finally {
await scraper.close();
}
}
// 运行测试
testSilverBaseline();