|
| 1 | +//! Webhook alert delivery. |
| 2 | +//! |
| 3 | +//! Dispatches HTTP POST notifications to registered webhook targets when a |
| 4 | +//! fee spike is detected. Every delivery attempt — successful or not — is |
| 5 | +//! persisted in the `alert_events` table via [`FeeRepository::log_alert_event`]. |
| 6 | +//! |
| 7 | +//! This module is the integration point for Issue #31 (webhook delivery) and |
| 8 | +//! Issue #32 (alert history). The `dispatch` function is called by the |
| 9 | +//! scheduler / insights engine whenever a spike crosses an alert threshold. |
| 10 | +
|
| 11 | +use std::sync::Arc; |
| 12 | + |
| 13 | +use chrono::Utc; |
| 14 | + |
| 15 | +use crate::repository::{AlertEvent, FeeRepository}; |
| 16 | + |
| 17 | +/// Payload describing a triggered fee-spike alert. |
| 18 | +#[derive(Debug, Clone)] |
| 19 | +pub struct AlertPayload { |
| 20 | + /// The alert config row id that triggered this dispatch (if known). |
| 21 | + pub config_id: Option<i64>, |
| 22 | + /// Severity label, e.g. "Minor", "Major", "Critical". |
| 23 | + pub severity: String, |
| 24 | + /// Highest fee observed during the spike window (in stroops). |
| 25 | + pub peak_fee: i64, |
| 26 | + /// Rolling baseline fee used for comparison. |
| 27 | + pub baseline_fee: f64, |
| 28 | + /// `peak_fee / baseline_fee`. |
| 29 | + pub spike_ratio: f64, |
| 30 | + /// Destination webhook URL. |
| 31 | + pub webhook_url: String, |
| 32 | +} |
| 33 | + |
| 34 | +/// Dispatch a webhook notification and log the outcome to the database. |
| 35 | +/// |
| 36 | +/// The HTTP client (`reqwest`) is not yet wired up in this stub — the |
| 37 | +/// `delivered` flag defaults to `false` until Issue #31 lands and the full |
| 38 | +/// HTTP POST is implemented. The repository logging is fully functional. |
| 39 | +pub async fn dispatch(payload: AlertPayload, repository: Arc<FeeRepository>) { |
| 40 | + // TODO (Issue #31): perform the actual HTTP POST here and capture success. |
| 41 | + let delivered = false; |
| 42 | + |
| 43 | + let event = AlertEvent { |
| 44 | + id: None, |
| 45 | + config_id: payload.config_id, |
| 46 | + severity: payload.severity.clone(), |
| 47 | + peak_fee: payload.peak_fee, |
| 48 | + baseline_fee: payload.baseline_fee, |
| 49 | + spike_ratio: payload.spike_ratio, |
| 50 | + webhook_url: payload.webhook_url.clone(), |
| 51 | + delivered, |
| 52 | + triggered_at: Utc::now().to_rfc3339(), |
| 53 | + }; |
| 54 | + |
| 55 | + if let Err(err) = repository.log_alert_event(&event).await { |
| 56 | + tracing::error!( |
| 57 | + "Failed to log alert event for webhook {}: {}", |
| 58 | + payload.webhook_url, |
| 59 | + err |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use super::*; |
| 67 | + use crate::db::create_pool; |
| 68 | + |
| 69 | + #[tokio::test] |
| 70 | + async fn dispatch_logs_event_to_database() { |
| 71 | + let pool = create_pool("sqlite::memory:").await.unwrap(); |
| 72 | + let repo = Arc::new(FeeRepository::new(pool)); |
| 73 | + |
| 74 | + let payload = AlertPayload { |
| 75 | + config_id: None, |
| 76 | + severity: "Major".to_string(), |
| 77 | + peak_fee: 8000, |
| 78 | + baseline_fee: 130.5, |
| 79 | + spike_ratio: 61.3, |
| 80 | + webhook_url: "https://hooks.example.com/test".to_string(), |
| 81 | + }; |
| 82 | + |
| 83 | + dispatch(payload, repo.clone()).await; |
| 84 | + |
| 85 | + let events = repo.query_alert_history(10, None, None).await.unwrap(); |
| 86 | + assert_eq!(events.len(), 1); |
| 87 | + assert_eq!(events[0].severity, "Major"); |
| 88 | + assert_eq!(events[0].peak_fee, 8000); |
| 89 | + // delivered = false until Issue #31 implements the HTTP POST |
| 90 | + assert!(!events[0].delivered); |
| 91 | + } |
| 92 | +} |
0 commit comments