Multi-level intelligent alerting system provides contextual notifications with actionable recommendations, preventing alert fatigue through smart aggregation and prioritization.
▶
// Show code example
click to expand
// Intelligent Alert Manager Implementation
class AlertManager {
constructor() {
this.alerts = [];
this.alertRules = new Map();
this.aggregationRules = new Map();
this.suppressionList = new Set();
this.initializeAlertRules();
}
initializeAlertRules() {
// API Rate Limit Alerts
this.alertRules.set('api_rate_limit', {
thresholds: {
warning: 0.80, // 80% quota usage
error: 0.95, // 95% quota usage
critical: 1.0 // Quota exceeded
},
cooldown: 300000, // 5 minutes between similar alerts
aggregation: 'api_source'
});
// Response Time Alerts
this.alertRules.set('response_time', {
thresholds: {
warning: 3000, // 3 seconds
error: 5000, // 5 seconds
critical: 10000 // 10 seconds
},
cooldown: 60000, // 1 minute between alerts
aggregation: 'time_based'
});
// Cache Performance Alerts
this.alertRules.set('cache_performance', {
thresholds: {
warning: 0.65, // Below 65% hit rate
error: 0.50, // Below 50% hit rate
critical: 0.30 // Below 30% hit rate
},
cooldown: 600000, // 10 minutes between alerts
aggregation: 'cache_tier'
});
}
async processAlert(alertType, data) {
const rule = this.alertRules.get(alertType);
if (!rule) return;
// Calculate alert severity
const severity = this.calculateSeverity(alertType, data.value, rule.thresholds);
if (severity === 'none') return;
// Check if alert should be suppressed
const alertKey = `${alertType}_${data.source}_${severity}`;
if (this.shouldSuppress(alertKey, rule.cooldown)) return;
// Create alert object
const alert = {
id: this.generateAlertId(),
type: alertType,
severity,
timestamp: Date.now(),
source: data.source,
value: data.value,
threshold: rule.thresholds[severity],
message: this.generateAlertMessage(alertType, severity, data),
recommendations: this.generateRecommendations(alertType, severity, data)
};
// Add to alerts and process
this.alerts.push(alert);
await this.processNewAlert(alert);
// Add to suppression list
this.suppressionList.add(alertKey);
setTimeout(() => this.suppressionList.delete(alertKey), rule.cooldown);
return alert;
}
generateAlertMessage(alertType, severity, data) {
const messages = {
api_rate_limit: {
warning: `${data.source} API approaching rate limit (${(data.value * 100).toFixed(1)}%)`,
error: `${data.source} API rate limit critical (${(data.value * 100).toFixed(1)}%)`,
critical: `${data.source} API rate limit exceeded - requests will fail`
},
response_time: {
warning: `${data.source} response time elevated (${data.value}ms)`,
error: `${data.source} response time high (${data.value}ms) - performance degraded`,
critical: `${data.source} response time critical (${data.value}ms) - service severely impacted`
},
cache_performance: {
warning: `Cache hit rate below target (${(data.value * 100).toFixed(1)}%) - performance impact`,
error: `Cache hit rate low (${(data.value * 100).toFixed(1)}%) - significant performance degradation`,
critical: `Cache hit rate critical (${(data.value * 100).toFixed(1)}%) - system performance severely impacted`
}
};
return messages[alertType]?.[severity] || `${alertType} alert: ${severity} level`;
}
generateRecommendations(alertType, severity, data) {
const recommendations = {
api_rate_limit: [
'Extend cache TTL to reduce API calls',
'Implement request prioritization',
'Consider upgrading to premium API tier',
'Enable request deduplication'
],
response_time: [
'Check API service status',
'Reduce concurrent request batch size',
'Enable circuit breaker if not active',
'Switch to cached data temporarily'
],
cache_performance: [
'Analyze cache invalidation patterns',
'Optimize cache TTL settings',
'Increase cache storage allocation',
'Review data access patterns'
]
};
return recommendations[alertType] || ['Check system logs for more details'];
}
async processNewAlert(alert) {
// Log to console with appropriate level
const logMethod = alert.severity === 'critical' ? 'error' :
alert.severity === 'error' ? 'error' :
alert.severity === 'warning' ? 'warn' : 'info';
console[logMethod](`🚨 [${alert.severity.toUpperCase()}] ${alert.message}`, {
alert,
recommendations: alert.recommendations
});
// Show browser notification if permissions granted
if ('Notification' in window && Notification.permission === 'granted') {
new Notification(`ESG Platform Alert: ${alert.severity}`, {
body: alert.message,
icon: '/favicon.ico',
tag: alert.id
});
}
// Update dashboard if visible
if (window.systemMonitor && window.systemMonitor.isVisible) {
window.systemMonitor.updateAlerts();
}
// Trigger alert handlers
this.triggerAlertHandlers(alert);
}
}