spawn_access_control/
alert_storage.rs

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
use mongodb::{Collection, bson};
use chrono::{DateTime, Utc};
use serde_json::Value;
use uuid::Uuid;
use serde::{Serialize, Deserialize};
use futures::TryStreamExt;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StoredAlert {
    pub id: Uuid,
    pub incident_id: String,
    pub severity: String,
    pub message: String,
    pub metric_name: String,
    pub current_value: f64,
    pub threshold: f64,
    pub created_at: DateTime<Utc>,
    pub resolved_at: Option<DateTime<Utc>>,
    pub metadata: Value,
}

pub struct AlertStorage {
    collection: Collection<StoredAlert>,
}

impl AlertStorage {
    pub fn new(collection: Collection<StoredAlert>) -> Self {
        Self { collection }
    }

    pub async fn get_alerts_in_range(&self, start: DateTime<Utc>, end: DateTime<Utc>) 
        -> Result<Vec<StoredAlert>, mongodb::error::Error> 
    {
        let start_bson = bson::DateTime::from_millis(start.timestamp_millis());
        let end_bson = bson::DateTime::from_millis(end.timestamp_millis());

        self.collection
            .find(bson::doc! {
                "created_at": {
                    "$gte": start_bson,
                    "$lte": end_bson
                }
            }, None)
            .await?
            .try_collect()
            .await
    }
}