spawn_access_control/
alert_storage.rsuse 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
}
}