omni_orchestrator/schemas/v1/api/instances/
count.rs

1use std::sync::Arc;
2use crate::DatabaseManager;
3use rocket::get;
4use rocket::http::Status;
5use rocket::serde::json::{json, Json, Value};
6use rocket::State;
7use crate::schemas::v1::db::queries::{self as db};
8
9/// Count all instances across all applications
10#[get("/platform/<platform_id>/instance-count")]
11pub async fn count_instances(
12    platform_id: i64,
13    db_manager: &State<Arc<DatabaseManager>>,
14) -> Result<Json<Value>, (Status, Json<Value>)> {
15    // Get platform information
16    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
17        Ok(platform) => platform,
18        Err(_) => {
19            return Err((
20                Status::NotFound,
21                Json(json!({
22                    "error": "Platform not found",
23                    "message": format!("Platform with ID {} does not exist", platform_id)
24                }))
25            ));
26        }
27    };
28
29    // Get platform-specific database pool
30    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
31        Ok(pool) => pool,
32        Err(_) => {
33            return Err((
34                Status::InternalServerError,
35                Json(json!({
36                    "error": "Database error",
37                    "message": "Failed to connect to platform database"
38                }))
39            ));
40        }
41    };
42
43    let count = match db::instance::count_instances(&pool).await {
44        Ok(count) => count,
45        Err(_) => {
46            return Err((
47                Status::InternalServerError,
48                Json(json!({
49                    "error": "Database error",
50                    "message": "Failed to count instances"
51                }))
52            ));
53        }
54    };
55    
56    Ok(Json(json!({ "count": count })))
57}