omni_orchestrator/schemas/v1/api/alerts/
auto_resolve.rs

1use super::super::super::db::queries as db;
2use rocket::http::Status;
3use rocket::serde::json::{json, Json, Value};
4use rocket::{post, State};
5use std::sync::Arc;
6use crate::DatabaseManager;
7
8/// Auto-resolve old alerts
9#[post("/platform/<platform_id>/alerts/auto-resolve?<days_threshold>&<severity_level>")]
10pub async fn auto_resolve_old_alerts(
11    platform_id: i64,
12    days_threshold: Option<i64>,
13    severity_level: Option<Vec<String>>, // Can provide multiple severity levels
14    db_manager: &State<Arc<DatabaseManager>>,
15) -> Result<Json<Value>, (Status, Json<Value>)> {
16    // Get platform information
17    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
18        Ok(platform) => platform,
19        Err(_) => {
20            return Err((
21                Status::NotFound,
22                Json(json!({
23                    "error": "Platform not found",
24                    "message": format!("Platform with ID {} does not exist", platform_id)
25                }))
26            ));
27        }
28    };
29
30    // Get platform-specific database pool
31    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
32        Ok(pool) => pool,
33        Err(_) => {
34            return Err((
35                Status::InternalServerError,
36                Json(json!({
37                    "error": "Database error",
38                    "message": "Failed to connect to platform database"
39                }))
40            ));
41        }
42    };
43
44    let days_threshold = days_threshold.unwrap_or(7); // Default to 7 days
45    
46    // Convert Vec<String> to Vec<&str>
47    let severity_refs: Option<Vec<&str>> = severity_level
48        .as_ref()
49        .map(|levels| levels.iter().map(AsRef::as_ref).collect());
50    
51    let count = match db::alert::auto_resolve_old_alerts(
52        &pool,
53        days_threshold,
54        severity_refs,
55    ).await {
56        Ok(count) => count,
57        Err(e) => {
58            log::error!("Failed to auto-resolve old alerts: {}", e);
59            return Err((
60                Status::InternalServerError,
61                Json(json!({
62                    "error": "Database error",
63                    "message": "Failed to auto-resolve old alerts"
64                }))
65            ));
66        }
67    };
68
69    Ok(Json(json!({
70        "message": "Successfully auto-resolved old alerts",
71        "count": count
72    })))
73}