Add acknowledge all alerts feature

Add POST /api/alerts/acknowledge-all endpoint to bulk acknowledge all
unacknowledged alerts. Add "Ack All" button to alerts page header with
confirmation dialog for quick dismissal of all pending alerts.
This commit is contained in:
2025-11-20 09:35:13 -06:00
parent 91507cc8f8
commit 7c26824aa1
2 changed files with 78 additions and 3 deletions

View File

@@ -146,6 +146,47 @@ def acknowledge_alert(alert_id):
}), 400 }), 400
@bp.route('/acknowledge-all', methods=['POST'])
@api_auth_required
def acknowledge_all_alerts():
"""
Acknowledge all unacknowledged alerts.
Returns:
JSON response with count of acknowledged alerts
"""
acknowledged_by = request.json.get('acknowledged_by', 'api') if request.json else 'api'
try:
# Get all unacknowledged alerts
unacked_alerts = current_app.db_session.query(Alert).filter(
Alert.acknowledged == False
).all()
count = 0
for alert in unacked_alerts:
alert.acknowledged = True
alert.acknowledged_at = datetime.now(timezone.utc)
alert.acknowledged_by = acknowledged_by
count += 1
current_app.db_session.commit()
return jsonify({
'status': 'success',
'message': f'Acknowledged {count} alerts',
'count': count,
'acknowledged_by': acknowledged_by
})
except Exception as e:
current_app.db_session.rollback()
return jsonify({
'status': 'error',
'message': f'Failed to acknowledge alerts: {str(e)}'
}), 500
@bp.route('/rules', methods=['GET']) @bp.route('/rules', methods=['GET'])
@api_auth_required @api_auth_required
def list_alert_rules(): def list_alert_rules():

View File

@@ -6,10 +6,15 @@
<div class="row mt-4"> <div class="row mt-4">
<div class="col-12 d-flex justify-content-between align-items-center mb-4"> <div class="col-12 d-flex justify-content-between align-items-center mb-4">
<h1>Alert History</h1> <h1>Alert History</h1>
<div>
<button class="btn btn-success me-2" onclick="acknowledgeAllAlerts()">
<i class="bi bi-check-all"></i> Ack All
</button>
<a href="{{ url_for('main.alert_rules') }}" class="btn btn-primary"> <a href="{{ url_for('main.alert_rules') }}" class="btn btn-primary">
<i class="bi bi-gear"></i> Manage Alert Rules <i class="bi bi-gear"></i> Manage Alert Rules
</a> </a>
</div> </div>
</div>
</div> </div>
<!-- Alert Statistics --> <!-- Alert Statistics -->
@@ -265,5 +270,34 @@ function acknowledgeAlert(alertId) {
alert('Failed to acknowledge alert'); alert('Failed to acknowledge alert');
}); });
} }
function acknowledgeAllAlerts() {
if (!confirm('Acknowledge all unacknowledged alerts?')) {
return;
}
fetch('/api/alerts/acknowledge-all', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': localStorage.getItem('api_key') || ''
},
body: JSON.stringify({
acknowledged_by: 'web_user'
})
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
location.reload();
} else {
alert('Failed to acknowledge alerts: ' + (data.message || 'Unknown error'));
}
})
.catch(error => {
console.error('Error:', error);
alert('Failed to acknowledge alerts');
});
}
</script> </script>
{% endblock %} {% endblock %}