feat: add test monitoring and alerting system
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
console.log('🔍 测试监控和告警系统...');
|
||||
|
||||
const alertFile = 'test-results/alerts.json';
|
||||
|
||||
const mockMetrics = [
|
||||
{
|
||||
timestamp: Date.now() - 3600000,
|
||||
tier: 'fast',
|
||||
totalTests: 100,
|
||||
passedTests: 95,
|
||||
failedTests: 5,
|
||||
skippedTests: 0,
|
||||
duration: 60000,
|
||||
successRate: 0.95,
|
||||
},
|
||||
{
|
||||
timestamp: Date.now() - 1800000,
|
||||
tier: 'standard',
|
||||
totalTests: 200,
|
||||
passedTests: 180,
|
||||
failedTests: 15,
|
||||
skippedTests: 5,
|
||||
duration: 180000,
|
||||
successRate: 0.9,
|
||||
},
|
||||
{
|
||||
timestamp: Date.now(),
|
||||
tier: 'deep',
|
||||
totalTests: 50,
|
||||
passedTests: 40,
|
||||
failedTests: 10,
|
||||
skippedTests: 0,
|
||||
duration: 600000,
|
||||
successRate: 0.8,
|
||||
},
|
||||
];
|
||||
|
||||
console.log('📊 模拟测试指标...');
|
||||
|
||||
const alertManager = {
|
||||
alerts: [],
|
||||
alertFile,
|
||||
|
||||
createAlert(severity, message, tier, metrics) {
|
||||
const alert = {
|
||||
id: `alert-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
|
||||
timestamp: Date.now(),
|
||||
severity,
|
||||
message,
|
||||
tier,
|
||||
metrics,
|
||||
resolved: false,
|
||||
};
|
||||
this.alerts.push(alert);
|
||||
console.log(`🚨 [${severity.toUpperCase()}] ${message}`);
|
||||
return alert;
|
||||
},
|
||||
|
||||
getAlertSummary() {
|
||||
const activeAlerts = this.alerts.filter(a => !a.resolved);
|
||||
const bySeverity = { low: 0, medium: 0, high: 0, critical: 0 };
|
||||
activeAlerts.forEach(a => bySeverity[a.severity]++);
|
||||
return { total: this.alerts.length, active: activeAlerts.length, bySeverity };
|
||||
},
|
||||
};
|
||||
|
||||
console.log('🔍 检查告警规则...');
|
||||
|
||||
for (const metric of mockMetrics) {
|
||||
if (metric.successRate < 0.8) {
|
||||
alertManager.createAlert('critical', '测试通过率低于80%', metric.tier, metric);
|
||||
} else if (metric.successRate < 0.9) {
|
||||
alertManager.createAlert('high', '测试通过率低于90%', metric.tier, metric);
|
||||
}
|
||||
|
||||
if (metric.failedTests > 10) {
|
||||
alertManager.createAlert('high', '失败测试数量超过10个', metric.tier, metric);
|
||||
}
|
||||
|
||||
if (metric.tier === 'deep' && metric.failedTests > 0) {
|
||||
alertManager.createAlert('critical', '深度层测试存在失败', metric.tier, metric);
|
||||
}
|
||||
}
|
||||
|
||||
const summary = alertManager.getAlertSummary();
|
||||
console.log('\n📊 告警统计:');
|
||||
console.log(` 总告警数: ${summary.total}`);
|
||||
console.log(` 活跃告警: ${summary.active}`);
|
||||
console.log(` Critical: ${summary.bySeverity.critical}`);
|
||||
console.log(` High: ${summary.bySeverity.high}`);
|
||||
console.log(` Medium: ${summary.bySeverity.medium}`);
|
||||
console.log(` Low: ${summary.bySeverity.low}`);
|
||||
|
||||
if (summary.active > 0) {
|
||||
console.log('\n✅ 监控系统工作正常,已生成告警');
|
||||
process.exit(0);
|
||||
} else {
|
||||
console.log('\n⚠️ 未生成告警');
|
||||
process.exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user