forge-runtime 0.0.2-alpha

Runtime executors and gateway for the Forge framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
//! Alert storage and evaluation engine.

use std::collections::HashMap;
use std::time::Duration;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgRow;
use sqlx::Row;
use uuid::Uuid;

use forge_core::Result;

/// Alert severity levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AlertSeverity {
    Info,
    Warning,
    Critical,
}

impl std::fmt::Display for AlertSeverity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AlertSeverity::Info => write!(f, "info"),
            AlertSeverity::Warning => write!(f, "warning"),
            AlertSeverity::Critical => write!(f, "critical"),
        }
    }
}

impl std::str::FromStr for AlertSeverity {
    type Err = String;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "info" => Ok(AlertSeverity::Info),
            "warning" => Ok(AlertSeverity::Warning),
            "critical" => Ok(AlertSeverity::Critical),
            _ => Err(format!("Unknown severity: {}", s)),
        }
    }
}

/// Alert condition operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AlertCondition {
    /// Greater than
    Gt,
    /// Greater than or equal
    Gte,
    /// Less than
    Lt,
    /// Less than or equal
    Lte,
    /// Equal
    Eq,
    /// Not equal
    Ne,
}

impl AlertCondition {
    /// Evaluate the condition.
    pub fn evaluate(&self, value: f64, threshold: f64) -> bool {
        match self {
            AlertCondition::Gt => value > threshold,
            AlertCondition::Gte => value >= threshold,
            AlertCondition::Lt => value < threshold,
            AlertCondition::Lte => value <= threshold,
            AlertCondition::Eq => (value - threshold).abs() < f64::EPSILON,
            AlertCondition::Ne => (value - threshold).abs() >= f64::EPSILON,
        }
    }
}

impl std::fmt::Display for AlertCondition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AlertCondition::Gt => write!(f, "gt"),
            AlertCondition::Gte => write!(f, "gte"),
            AlertCondition::Lt => write!(f, "lt"),
            AlertCondition::Lte => write!(f, "lte"),
            AlertCondition::Eq => write!(f, "eq"),
            AlertCondition::Ne => write!(f, "ne"),
        }
    }
}

impl std::str::FromStr for AlertCondition {
    type Err = String;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "gt" | ">" => Ok(AlertCondition::Gt),
            "gte" | ">=" => Ok(AlertCondition::Gte),
            "lt" | "<" => Ok(AlertCondition::Lt),
            "lte" | "<=" => Ok(AlertCondition::Lte),
            "eq" | "==" => Ok(AlertCondition::Eq),
            "ne" | "!=" => Ok(AlertCondition::Ne),
            _ => Err(format!("Unknown condition: {}", s)),
        }
    }
}

/// Alert status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AlertStatus {
    Firing,
    Resolved,
}

impl std::fmt::Display for AlertStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AlertStatus::Firing => write!(f, "firing"),
            AlertStatus::Resolved => write!(f, "resolved"),
        }
    }
}

/// Alert rule definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertRule {
    pub id: Uuid,
    pub name: String,
    pub description: Option<String>,
    pub metric_name: String,
    pub condition: AlertCondition,
    pub threshold: f64,
    pub duration_seconds: i32,
    pub severity: AlertSeverity,
    pub enabled: bool,
    pub labels: HashMap<String, String>,
    pub notification_channels: Vec<String>,
    pub cooldown_seconds: i32,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

impl AlertRule {
    /// Create a new alert rule.
    pub fn new(
        name: impl Into<String>,
        metric_name: impl Into<String>,
        condition: AlertCondition,
        threshold: f64,
    ) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4(),
            name: name.into(),
            description: None,
            metric_name: metric_name.into(),
            condition,
            threshold,
            duration_seconds: 0,
            severity: AlertSeverity::Warning,
            enabled: true,
            labels: HashMap::new(),
            notification_channels: Vec::new(),
            cooldown_seconds: 300,
            created_at: now,
            updated_at: now,
        }
    }

    /// Set description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Set severity.
    pub fn with_severity(mut self, severity: AlertSeverity) -> Self {
        self.severity = severity;
        self
    }

    /// Set duration (seconds that condition must be true).
    pub fn with_duration(mut self, seconds: i32) -> Self {
        self.duration_seconds = seconds;
        self
    }
}

/// A fired alert.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Alert {
    pub id: Uuid,
    pub rule_id: Uuid,
    pub rule_name: String,
    pub metric_value: f64,
    pub threshold: f64,
    pub severity: AlertSeverity,
    pub status: AlertStatus,
    pub triggered_at: DateTime<Utc>,
    pub resolved_at: Option<DateTime<Utc>>,
    pub acknowledged_at: Option<DateTime<Utc>>,
    pub acknowledged_by: Option<String>,
    pub labels: HashMap<String, String>,
    pub annotations: HashMap<String, String>,
}

impl Alert {
    /// Create a new firing alert.
    pub fn firing(rule: &AlertRule, metric_value: f64) -> Self {
        Self {
            id: Uuid::new_v4(),
            rule_id: rule.id,
            rule_name: rule.name.clone(),
            metric_value,
            threshold: rule.threshold,
            severity: rule.severity,
            status: AlertStatus::Firing,
            triggered_at: Utc::now(),
            resolved_at: None,
            acknowledged_at: None,
            acknowledged_by: None,
            labels: rule.labels.clone(),
            annotations: HashMap::new(),
        }
    }
}

/// Alert store for persistence.
pub struct AlertStore {
    pool: sqlx::PgPool,
}

impl AlertStore {
    /// Create a new alert store.
    pub fn new(pool: sqlx::PgPool) -> Self {
        Self { pool }
    }

    // ==================== Alert Rules ====================

    /// Create an alert rule.
    pub async fn create_rule(&self, rule: &AlertRule) -> Result<()> {
        let labels = serde_json::to_value(&rule.labels).unwrap_or_default();

        sqlx::query(
            r#"
            INSERT INTO forge_alert_rules
            (id, name, description, metric_name, condition, threshold, duration_seconds,
             severity, enabled, labels, notification_channels, cooldown_seconds,
             created_at, updated_at)
            VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
            "#,
        )
        .bind(rule.id)
        .bind(&rule.name)
        .bind(&rule.description)
        .bind(&rule.metric_name)
        .bind(rule.condition.to_string())
        .bind(rule.threshold)
        .bind(rule.duration_seconds)
        .bind(rule.severity.to_string())
        .bind(rule.enabled)
        .bind(labels)
        .bind(&rule.notification_channels)
        .bind(rule.cooldown_seconds)
        .bind(rule.created_at)
        .bind(rule.updated_at)
        .execute(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(())
    }

    /// List all alert rules.
    pub async fn list_rules(&self) -> Result<Vec<AlertRule>> {
        let rows = sqlx::query(
            r#"
            SELECT id, name, description, metric_name, condition, threshold,
                   duration_seconds, severity, enabled, labels, notification_channels,
                   cooldown_seconds, created_at, updated_at
            FROM forge_alert_rules
            ORDER BY name
            "#,
        )
        .fetch_all(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(rows.into_iter().map(parse_alert_rule_row).collect())
    }

    /// List enabled alert rules.
    pub async fn list_enabled_rules(&self) -> Result<Vec<AlertRule>> {
        let rows = sqlx::query(
            r#"
            SELECT id, name, description, metric_name, condition, threshold,
                   duration_seconds, severity, enabled, labels, notification_channels,
                   cooldown_seconds, created_at, updated_at
            FROM forge_alert_rules
            WHERE enabled = TRUE
            ORDER BY name
            "#,
        )
        .fetch_all(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(rows.into_iter().map(parse_alert_rule_row).collect())
    }

    /// Get a rule by ID.
    pub async fn get_rule(&self, id: Uuid) -> Result<Option<AlertRule>> {
        let row = sqlx::query(
            r#"
            SELECT id, name, description, metric_name, condition, threshold,
                   duration_seconds, severity, enabled, labels, notification_channels,
                   cooldown_seconds, created_at, updated_at
            FROM forge_alert_rules
            WHERE id = $1
            "#,
        )
        .bind(id)
        .fetch_optional(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(row.map(parse_alert_rule_row))
    }

    /// Update an alert rule.
    pub async fn update_rule(&self, rule: &AlertRule) -> Result<()> {
        let labels = serde_json::to_value(&rule.labels).unwrap_or_default();

        sqlx::query(
            r#"
            UPDATE forge_alert_rules
            SET name = $2, description = $3, metric_name = $4, condition = $5,
                threshold = $6, duration_seconds = $7, severity = $8, enabled = $9,
                labels = $10, notification_channels = $11, cooldown_seconds = $12,
                updated_at = NOW()
            WHERE id = $1
            "#,
        )
        .bind(rule.id)
        .bind(&rule.name)
        .bind(&rule.description)
        .bind(&rule.metric_name)
        .bind(rule.condition.to_string())
        .bind(rule.threshold)
        .bind(rule.duration_seconds)
        .bind(rule.severity.to_string())
        .bind(rule.enabled)
        .bind(labels)
        .bind(&rule.notification_channels)
        .bind(rule.cooldown_seconds)
        .execute(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(())
    }

    /// Delete an alert rule.
    pub async fn delete_rule(&self, id: Uuid) -> Result<()> {
        sqlx::query("DELETE FROM forge_alert_rules WHERE id = $1")
            .bind(id)
            .execute(&self.pool)
            .await
            .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(())
    }

    // ==================== Alerts ====================

    /// Create an alert.
    pub async fn create_alert(&self, alert: &Alert) -> Result<()> {
        let labels = serde_json::to_value(&alert.labels).unwrap_or_default();
        let annotations = serde_json::to_value(&alert.annotations).unwrap_or_default();

        sqlx::query(
            r#"
            INSERT INTO forge_alerts
            (id, rule_id, rule_name, metric_value, threshold, severity, status,
             triggered_at, resolved_at, acknowledged_at, acknowledged_by, labels, annotations)
            VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
            "#,
        )
        .bind(alert.id)
        .bind(alert.rule_id)
        .bind(&alert.rule_name)
        .bind(alert.metric_value)
        .bind(alert.threshold)
        .bind(alert.severity.to_string())
        .bind(alert.status.to_string())
        .bind(alert.triggered_at)
        .bind(alert.resolved_at)
        .bind(alert.acknowledged_at)
        .bind(&alert.acknowledged_by)
        .bind(labels)
        .bind(annotations)
        .execute(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(())
    }

    /// List active (firing) alerts.
    pub async fn list_active_alerts(&self) -> Result<Vec<Alert>> {
        let rows = sqlx::query(
            r#"
            SELECT id, rule_id, rule_name, metric_value, threshold, severity, status,
                   triggered_at, resolved_at, acknowledged_at, acknowledged_by, labels, annotations
            FROM forge_alerts
            WHERE status = 'firing'
            ORDER BY triggered_at DESC
            "#,
        )
        .fetch_all(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(rows.into_iter().map(parse_alert_row).collect())
    }

    /// List recent alerts (both firing and resolved).
    pub async fn list_recent_alerts(&self, limit: i64) -> Result<Vec<Alert>> {
        let rows = sqlx::query(
            r#"
            SELECT id, rule_id, rule_name, metric_value, threshold, severity, status,
                   triggered_at, resolved_at, acknowledged_at, acknowledged_by, labels, annotations
            FROM forge_alerts
            ORDER BY triggered_at DESC
            LIMIT $1
            "#,
        )
        .bind(limit)
        .fetch_all(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(rows.into_iter().map(parse_alert_row).collect())
    }

    /// Resolve an alert.
    pub async fn resolve_alert(&self, id: Uuid) -> Result<()> {
        sqlx::query(
            r#"
            UPDATE forge_alerts
            SET status = 'resolved', resolved_at = NOW()
            WHERE id = $1
            "#,
        )
        .bind(id)
        .execute(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(())
    }

    /// Acknowledge an alert.
    pub async fn acknowledge_alert(&self, id: Uuid, by: &str) -> Result<()> {
        sqlx::query(
            r#"
            UPDATE forge_alerts
            SET acknowledged_at = NOW(), acknowledged_by = $2
            WHERE id = $1
            "#,
        )
        .bind(id)
        .bind(by)
        .execute(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(())
    }

    /// Get last alert for a rule (to check cooldown).
    pub async fn get_last_alert_for_rule(&self, rule_id: Uuid) -> Result<Option<Alert>> {
        let row = sqlx::query(
            r#"
            SELECT id, rule_id, rule_name, metric_value, threshold, severity, status,
                   triggered_at, resolved_at, acknowledged_at, acknowledged_by, labels, annotations
            FROM forge_alerts
            WHERE rule_id = $1
            ORDER BY triggered_at DESC
            LIMIT 1
            "#,
        )
        .bind(rule_id)
        .fetch_optional(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(row.map(parse_alert_row))
    }

    /// Cleanup old resolved alerts.
    pub async fn cleanup(&self, retention: Duration) -> Result<u64> {
        let cutoff = Utc::now() - chrono::Duration::from_std(retention).unwrap_or_default();

        let result = sqlx::query(
            r#"
            DELETE FROM forge_alerts
            WHERE status = 'resolved' AND resolved_at < $1
            "#,
        )
        .bind(cutoff)
        .execute(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(result.rows_affected())
    }
}

/// Alert evaluator that periodically checks rules against metrics.
pub struct AlertEvaluator {
    alert_store: Arc<AlertStore>,
    #[allow(dead_code)]
    metrics_store: Arc<super::MetricsStore>,
    pool: sqlx::PgPool,
    shutdown: Arc<RwLock<bool>>,
}

impl AlertEvaluator {
    /// Create a new alert evaluator.
    pub fn new(
        alert_store: Arc<AlertStore>,
        metrics_store: Arc<super::MetricsStore>,
        pool: sqlx::PgPool,
    ) -> Self {
        Self {
            alert_store,
            metrics_store,
            pool,
            shutdown: Arc::new(RwLock::new(false)),
        }
    }

    /// Start the evaluation loop.
    ///
    /// This runs in the background and evaluates all enabled rules every `interval`.
    pub async fn run(&self, interval: Duration) {
        tracing::info!("Alert evaluator started");

        let mut ticker = tokio::time::interval(interval);
        loop {
            ticker.tick().await;

            if *self.shutdown.read().await {
                break;
            }

            if let Err(e) = self.evaluate_all_rules().await {
                tracing::error!("Alert evaluation error: {}", e);
            }
        }

        tracing::info!("Alert evaluator stopped");
    }

    /// Stop the evaluator.
    pub async fn stop(&self) {
        let mut shutdown = self.shutdown.write().await;
        *shutdown = true;
    }

    /// Evaluate all enabled rules.
    async fn evaluate_all_rules(&self) -> Result<()> {
        let rules = self.alert_store.list_enabled_rules().await?;

        for rule in rules {
            if let Err(e) = self.evaluate_rule(&rule).await {
                tracing::warn!("Failed to evaluate rule {}: {}", rule.name, e);
            }
        }

        Ok(())
    }

    /// Evaluate a single rule.
    async fn evaluate_rule(&self, rule: &AlertRule) -> Result<()> {
        // Get the latest metric value for this rule
        let metric_value = self
            .get_latest_metric_value(&rule.metric_name, &rule.labels)
            .await?;

        let metric_value = match metric_value {
            Some(v) => v,
            None => return Ok(()), // No metric data yet
        };

        // Evaluate the condition
        let condition_met = rule.condition.evaluate(metric_value, rule.threshold);

        // Check if there's an existing firing alert for this rule
        let existing_alert = self.alert_store.get_last_alert_for_rule(rule.id).await?;

        match (condition_met, existing_alert) {
            (true, None) => {
                // Condition met and no existing alert - create new alert
                let alert = Alert::firing(rule, metric_value);
                self.alert_store.create_alert(&alert).await?;
                tracing::warn!(
                    rule = rule.name,
                    value = metric_value,
                    threshold = rule.threshold,
                    severity = ?rule.severity,
                    "Alert triggered"
                );
            }
            (true, Some(existing)) if existing.status == AlertStatus::Resolved => {
                // Condition met again after resolution - check cooldown
                let cooldown = chrono::Duration::seconds(rule.cooldown_seconds as i64);
                let since_resolved = existing
                    .resolved_at
                    .map(|t| Utc::now() - t)
                    .unwrap_or(cooldown);

                if since_resolved >= cooldown {
                    // Cooldown passed - create new alert
                    let alert = Alert::firing(rule, metric_value);
                    self.alert_store.create_alert(&alert).await?;
                    tracing::warn!(
                        rule = rule.name,
                        value = metric_value,
                        threshold = rule.threshold,
                        "Alert re-triggered after cooldown"
                    );
                }
            }
            (false, Some(existing)) if existing.status == AlertStatus::Firing => {
                // Condition no longer met - resolve alert
                self.alert_store.resolve_alert(existing.id).await?;
                tracing::info!(rule = rule.name, value = metric_value, "Alert resolved");
            }
            _ => {
                // No action needed
            }
        }

        Ok(())
    }

    /// Get the latest metric value matching the rule's criteria.
    async fn get_latest_metric_value(
        &self,
        metric_name: &str,
        _labels: &HashMap<String, String>,
    ) -> Result<Option<f64>> {
        // Query the latest metric value
        let row: Option<(f64,)> = sqlx::query_as(
            r#"
            SELECT value
            FROM forge_metrics
            WHERE name = $1
            ORDER BY timestamp DESC
            LIMIT 1
            "#,
        )
        .bind(metric_name)
        .fetch_optional(&self.pool)
        .await
        .map_err(|e| forge_core::ForgeError::Database(e.to_string()))?;

        Ok(row.map(|(v,)| v))
    }
}

use std::sync::Arc;
use tokio::sync::RwLock;

// Manual row parsing functions
fn parse_alert_rule_row(row: PgRow) -> AlertRule {
    let labels_json: serde_json::Value = row.get("labels");
    let labels: HashMap<String, String> = serde_json::from_value(labels_json).unwrap_or_default();
    let condition_str: String = row.get("condition");
    let severity_str: String = row.get("severity");

    AlertRule {
        id: row.get("id"),
        name: row.get("name"),
        description: row.get("description"),
        metric_name: row.get("metric_name"),
        condition: condition_str.parse().unwrap_or(AlertCondition::Gt),
        threshold: row.get("threshold"),
        duration_seconds: row.get("duration_seconds"),
        severity: severity_str.parse().unwrap_or(AlertSeverity::Warning),
        enabled: row.get("enabled"),
        labels,
        notification_channels: row.get("notification_channels"),
        cooldown_seconds: row.get("cooldown_seconds"),
        created_at: row.get("created_at"),
        updated_at: row.get("updated_at"),
    }
}

fn parse_alert_row(row: PgRow) -> Alert {
    let labels_json: serde_json::Value = row.get("labels");
    let annotations_json: serde_json::Value = row.get("annotations");
    let labels: HashMap<String, String> = serde_json::from_value(labels_json).unwrap_or_default();
    let annotations: HashMap<String, String> =
        serde_json::from_value(annotations_json).unwrap_or_default();
    let severity_str: String = row.get("severity");
    let status_str: String = row.get("status");

    Alert {
        id: row.get("id"),
        rule_id: row.get("rule_id"),
        rule_name: row.get("rule_name"),
        metric_value: row.get("metric_value"),
        threshold: row.get("threshold"),
        severity: severity_str.parse().unwrap_or(AlertSeverity::Warning),
        status: if status_str == "firing" {
            AlertStatus::Firing
        } else {
            AlertStatus::Resolved
        },
        triggered_at: row.get("triggered_at"),
        resolved_at: row.get("resolved_at"),
        acknowledged_at: row.get("acknowledged_at"),
        acknowledged_by: row.get("acknowledged_by"),
        labels,
        annotations,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_alert_condition_evaluate() {
        assert!(AlertCondition::Gt.evaluate(10.0, 5.0));
        assert!(!AlertCondition::Gt.evaluate(5.0, 10.0));

        assert!(AlertCondition::Gte.evaluate(10.0, 10.0));
        assert!(AlertCondition::Gte.evaluate(10.0, 5.0));

        assert!(AlertCondition::Lt.evaluate(5.0, 10.0));
        assert!(!AlertCondition::Lt.evaluate(10.0, 5.0));

        assert!(AlertCondition::Lte.evaluate(10.0, 10.0));
        assert!(AlertCondition::Lte.evaluate(5.0, 10.0));

        assert!(AlertCondition::Eq.evaluate(10.0, 10.0));
        assert!(!AlertCondition::Eq.evaluate(10.0, 5.0));

        assert!(AlertCondition::Ne.evaluate(10.0, 5.0));
        assert!(!AlertCondition::Ne.evaluate(10.0, 10.0));
    }

    #[test]
    fn test_alert_rule_builder() {
        let rule = AlertRule::new("high_cpu", "cpu_usage_percent", AlertCondition::Gt, 90.0)
            .with_description("Alert when CPU usage exceeds 90%")
            .with_severity(AlertSeverity::Critical)
            .with_duration(60);

        assert_eq!(rule.name, "high_cpu");
        assert_eq!(rule.metric_name, "cpu_usage_percent");
        assert_eq!(rule.threshold, 90.0);
        assert_eq!(rule.severity, AlertSeverity::Critical);
        assert_eq!(rule.duration_seconds, 60);
    }

    #[test]
    fn test_alert_firing() {
        let rule = AlertRule::new("test", "metric", AlertCondition::Gt, 50.0);
        let alert = Alert::firing(&rule, 75.0);

        assert_eq!(alert.rule_name, "test");
        assert_eq!(alert.metric_value, 75.0);
        assert_eq!(alert.threshold, 50.0);
        assert_eq!(alert.status, AlertStatus::Firing);
    }
}