omni_orchestrator/schemas/v1/api/alerts/
app_alerts.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 for a specific application
9#[get("/platform/<platform_id>/apps/<app_id>/alerts?<limit>&<include_resolved>")]
10pub async fn get_app_alerts(
11    platform_id: i64,
12    app_id: i64,
13    limit: Option<i64>,
14    include_resolved: Option<bool>,
15    db_manager: &State<Arc<DatabaseManager>>,
16) -> Result<Json<Value>, (Status, Json<Value>)> {
17    // Get platform information
18    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
19        Ok(platform) => platform,
20        Err(_) => {
21            return Err((
22                Status::NotFound,
23                Json(json!({
24                    "error": "Platform not found",
25                    "message": format!("Platform with ID {} does not exist", platform_id)
26                }))
27            ));
28        }
29    };
30
31    // Get platform-specific database pool
32    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
33        Ok(pool) => pool,
34        Err(_) => {
35            return Err((
36                Status::InternalServerError,
37                Json(json!({
38                    "error": "Database error",
39                    "message": "Failed to connect to platform database"
40                }))
41            ));
42        }
43    };
44
45    let limit = limit.unwrap_or(20);
46    let include_resolved = include_resolved.unwrap_or(false);
47    
48    let alerts = match db::alert::get_recent_app_alerts(
49        &pool,
50        app_id,
51        limit,
52        include_resolved,
53    ).await {
54        Ok(alerts) => alerts,
55        Err(e) => {
56            log::error!("Failed to fetch app alerts: {}", e);
57            return Err((
58                Status::InternalServerError,
59                Json(json!({
60                    "error": "Database error",
61                    "message": "Failed to fetch application alerts"
62                }))
63            ));
64        }
65    };
66
67    Ok(Json(json!({ "alerts": alerts })))
68}