omni_orchestrator/schemas/v1/api/apps/
get.rs

1use super::super::super::db::queries as db;
2use super::types::AppStats;
3use rocket::http::Status;
4use rocket::serde::json::{json, Json, Value};
5use rocket::{get, State};
6use std::sync::Arc;
7
8use crate::DatabaseManager;
9
10use libomni::types::db::v1 as types;
11use types::app::{App, AppWithInstances};
12
13/// Get app with instances
14#[get("/platform/<platform_id>/app_with_instances/<app_id>")]
15pub async fn get_app_with_instances(
16    db_manager: &State<Arc<DatabaseManager>>, 
17    platform_id: i64,
18    app_id: i64
19) -> Result<Json<AppWithInstances>, (Status, Json<Value>)> {
20    // Get platform information
21    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
22        Ok(platform) => platform,
23        Err(_) => {
24            return Err((
25                Status::NotFound,
26                Json(json!({
27                    "error": "Platform not found",
28                    "message": format!("Platform with ID {} does not exist", platform_id)
29                }))
30            ));
31        }
32    };
33
34    // Get platform-specific database pool
35    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
36        Ok(pool) => pool,
37        Err(_) => {
38            return Err((
39                Status::InternalServerError,
40                Json(json!({
41                    "error": "Database error",
42                    "message": "Failed to connect to platform database"
43                }))
44            ));
45        }
46    };
47
48    match db::app::get_app_with_instances(&pool, app_id).await {
49        Ok(app_with_instances) => {
50            Ok(Json(app_with_instances))
51        }
52        Err(_) => Err((
53            Status::InternalServerError,
54            Json(json!({
55                "error": "Failed to fetch app with instances",
56                "message": "An error occurred while retrieving the application data"
57            })),
58        )),
59    }
60}
61
62/// Get a specific application by ID.
63///
64/// # Arguments
65///
66/// * `platform_id` - Platform identifier
67/// * `app_id` - The ID of the application to retrieve
68/// * `db_manager` - Database manager for accessing platform-specific pools
69///
70/// # Returns
71///
72/// The application if found, or None if not found
73#[get("/platform/<platform_id>/apps/<app_id>")]
74pub async fn get_app(
75    platform_id: i64,
76    app_id: i64, 
77    db_manager: &State<Arc<DatabaseManager>>
78) -> Result<Json<App>, (Status, Json<Value>)> {
79    // Get platform information
80    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
81        Ok(platform) => platform,
82        Err(_) => {
83            return Err((
84                Status::NotFound,
85                Json(json!({
86                    "error": "Platform not found",
87                    "message": format!("Platform with ID {} does not exist", platform_id)
88                }))
89            ));
90        }
91    };
92
93    // Get platform-specific database pool
94    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
95        Ok(pool) => pool,
96        Err(_) => {
97            return Err((
98                Status::InternalServerError,
99                Json(json!({
100                    "error": "Database error",
101                    "message": "Failed to connect to platform database"
102                }))
103            ));
104        }
105    };
106
107    match db::app::get_app_by_id(&pool, app_id).await {
108        Ok(app) => Ok(Json(app)),
109        Err(_) => {
110            Err((
111                Status::NotFound,
112                Json(json!({
113                    "error": "App not found",
114                    "message": format!("App with ID {} could not be found", app_id)
115                }))
116            ))
117        }
118    }
119}
120
121/// Get statistics for a specific application.
122///
123/// # Arguments
124///
125/// * `platform_id` - Platform identifier
126/// * `app_id` - The ID of the application to get statistics for
127/// * `db_manager` - Database manager for accessing platform-specific pools
128///
129/// # Returns
130///
131/// Statistics for the application
132#[get("/platform/<platform_id>/apps/<app_id>/stats")]
133pub async fn get_app_stats(
134    platform_id: i64,
135    app_id: String, 
136    db_manager: &State<Arc<DatabaseManager>>
137) -> Result<Json<AppStats>, (Status, Json<Value>)> {
138    // Get platform information
139    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
140        Ok(platform) => platform,
141        Err(_) => {
142            return Err((
143                Status::NotFound,
144                Json(json!({
145                    "error": "Platform not found",
146                    "message": format!("Platform with ID {} does not exist", platform_id)
147                }))
148            ));
149        }
150    };
151
152    // Get platform-specific database pool (we'll need this for future implementations)
153    let _pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
154        Ok(pool) => pool,
155        Err(_) => {
156            return Err((
157                Status::InternalServerError,
158                Json(json!({
159                    "error": "Database error",
160                    "message": "Failed to connect to platform database"
161                }))
162            ));
163        }
164    };
165
166    // For now, return placeholder stats as in the original implementation
167    let app_stats = AppStats {
168        cpu_usage: 0.0,
169        memory_usage: 0,
170        disk_usage: 0,
171        requests_per_second: 0.0,
172        response_time_ms: 0,
173    };
174    Ok(Json(app_stats))
175}