spawn_access_control/
alert_storage.rs

1use mongodb::{Collection, bson};
2use chrono::{DateTime, Utc};
3use serde_json::Value;
4use uuid::Uuid;
5use serde::{Serialize, Deserialize};
6use futures::TryStreamExt;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct StoredAlert {
10    pub id: Uuid,
11    pub incident_id: String,
12    pub severity: String,
13    pub message: String,
14    pub metric_name: String,
15    pub current_value: f64,
16    pub threshold: f64,
17    pub created_at: DateTime<Utc>,
18    pub resolved_at: Option<DateTime<Utc>>,
19    pub metadata: Value,
20}
21
22pub struct AlertStorage {
23    collection: Collection<StoredAlert>,
24}
25
26impl AlertStorage {
27    pub fn new(collection: Collection<StoredAlert>) -> Self {
28        Self { collection }
29    }
30
31    pub async fn get_alerts_in_range(&self, start: DateTime<Utc>, end: DateTime<Utc>) 
32        -> Result<Vec<StoredAlert>, mongodb::error::Error> 
33    {
34        let start_bson = bson::DateTime::from_millis(start.timestamp_millis());
35        let end_bson = bson::DateTime::from_millis(end.timestamp_millis());
36
37        self.collection
38            .find(bson::doc! {
39                "created_at": {
40                    "$gte": start_bson,
41                    "$lte": end_bson
42                }
43            }, None)
44            .await?
45            .try_collect()
46            .await
47    }
48}