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

1use super::super::super::db::queries as db;
2use rocket::http::Status;
3use rocket::serde::json::{json, Json, Value};
4use rocket::{get, State};
5use std::sync::Arc;
6use crate::DatabaseManager;
7
8/// Get alerts needing escalation
9#[get("/platform/<platform_id>/alerts/needing-escalation?<org_id>&<hours_threshold>")]
10pub async fn get_alerts_needing_escalation(
11    platform_id: i64,
12    org_id: Option<i64>,
13    hours_threshold: Option<i64>,
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 hours_threshold = hours_threshold.unwrap_or(4); // Default to 4 hours
45    
46    let alerts = match db::alert::get_alerts_needing_escalation(
47        &pool,
48        org_id,
49        hours_threshold,
50    ).await {
51        Ok(alerts) => alerts,
52        Err(e) => {
53            log::error!("Failed to fetch alerts needing escalation: {}", e);
54            return Err((
55                Status::InternalServerError,
56                Json(json!({
57                    "error": "Database error",
58                    "message": "Failed to fetch alerts needing escalation"
59                }))
60            ));
61        }
62    };
63
64    Ok(Json(json!({ "alerts": alerts })))
65}