Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Dashboard/Services/EmailAlertService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ public async Task TrySendAlertEmailAsync(
try
{
var prefs = _preferencesService.GetPreferences();
string? sendError = null;
bool sent = false;
string notificationType = "tray";

/* Attempt email delivery if SMTP is fully configured */
if (prefs.SmtpEnabled &&
Expand All @@ -95,7 +92,8 @@ public async Task TrySendAlertEmailAsync(

if (!withinCooldown)
{
notificationType = "email";
bool sent = false;
string? sendError = null;
var subject = $"[SQL Monitor Alert] {metricName} on {serverName}";
var (htmlBody, plainTextBody) = EmailTemplateBuilder.BuildAlertEmail(
metricName, serverName, currentValue, thresholdValue, prefs.EmailCooldownMinutes, context);
Expand Down Expand Up @@ -130,18 +128,21 @@ public async Task TrySendAlertEmailAsync(
Logger.Error($"ALERT EMAIL STILL FAILING: {_consecutiveFailures} consecutive failures. Last error: {ex.Message}");
}
}

RecordAlert(serverId, serverName, metricName, currentValue, thresholdValue, sent, "email", sendError);
}
}

/* Log the alert attempt */
RecordAlert(serverId, serverName, metricName, currentValue, thresholdValue, sent, notificationType, sendError);

/* Send webhook notifications (Teams / Slack) — independent of email */
var webhookService = WebhookAlertService.Current;
if (webhookService != null)
{
await webhookService.TrySendWebhookAlertsAsync(
var webhookSent = await webhookService.TrySendWebhookAlertsAsync(
metricName, serverName, currentValue, thresholdValue, serverId, context);
if (webhookSent)
{
RecordAlert(serverId, serverName, metricName, currentValue, thresholdValue, true, "webhook");
}
}
}
catch (Exception ex)
Expand Down
7 changes: 5 additions & 2 deletions Dashboard/Services/WebhookAlertService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public WebhookAlertService(UserPreferencesService preferencesService)
/// Sends webhook alerts to all configured channels (Teams and/or Slack).
/// Respects the email cooldown setting for throttling. Never throws.
/// </summary>
public async Task TrySendWebhookAlertsAsync(
public async Task<bool> TrySendWebhookAlertsAsync(
string metricName,
string serverName,
string currentValue,
Expand All @@ -62,7 +62,7 @@ public async Task TrySendWebhookAlertsAsync(
if (_cooldowns.TryGetValue(cooldownKey, out var lastSent) &&
DateTime.UtcNow - lastSent < TimeSpan.FromMinutes(prefs.EmailCooldownMinutes))
{
return;
return false;
}

bool sent = false;
Expand All @@ -81,10 +81,13 @@ public async Task TrySendWebhookAlertsAsync(
{
_cooldowns[cooldownKey] = DateTime.UtcNow;
}

return sent;
}
catch (Exception ex)
{
Logger.Error($"TrySendWebhookAlertsAsync outer error: {ex.Message}");
return false;
}
}

Expand Down
10 changes: 9 additions & 1 deletion Lite/Services/EmailAlertService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,20 @@ public async Task TrySendAlertEmailAsync(
}

/* Send webhook notifications (Teams / Slack) alongside email */
bool webhookSent = false;
if (!muted)
{
await _webhookAlertService.TrySendWebhookAlertsAsync(
webhookSent = await _webhookAlertService.TrySendWebhookAlertsAsync(
metricName, serverName, currentValue, thresholdValue, serverId, context);
}

/* Reflect webhook delivery in notification type */
if (webhookSent)
{
notificationType = notificationType == "email" ? "email+webhook" : "webhook";
sent = true;
}

/* Always log the alert to DuckDB, regardless of email status */
var logCurrent = numericCurrentValue
?? (double.TryParse(currentValue.TrimEnd('%'), out var cv) ? cv : 0);
Expand Down
7 changes: 5 additions & 2 deletions Lite/Services/WebhookAlertService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class WebhookAlertService
/// Sends webhook alerts to all configured channels (Teams and/or Slack).
/// Respects the email cooldown setting for throttling. Never throws.
/// </summary>
public async Task TrySendWebhookAlertsAsync(
public async Task<bool> TrySendWebhookAlertsAsync(
string metricName,
string serverName,
string currentValue,
Expand All @@ -51,7 +51,7 @@ public async Task TrySendWebhookAlertsAsync(
if (_cooldowns.TryGetValue(cooldownKey, out var lastSent) &&
DateTime.UtcNow - lastSent < TimeSpan.FromMinutes(App.EmailCooldownMinutes))
{
return;
return false;
}

bool sent = false;
Expand All @@ -70,10 +70,13 @@ public async Task TrySendWebhookAlertsAsync(
{
_cooldowns[cooldownKey] = DateTime.UtcNow;
}

return sent;
}
catch (Exception ex)
{
AppLogger.Error("Webhook", $"TrySendWebhookAlertsAsync outer error: {ex.Message}");
return false;
}
}

Expand Down
Loading