omni_orchestrator/schemas/v1/api/
metrics.rs1use std::sync::Arc;
2use crate::DatabaseManager;
3use crate::schemas::v1::db::queries::{self as db};
4use crate::models::metrics::Metric;
5use rocket::{get, http::Status, serde::json::{json, Json, Value}, State};
6
7#[get("/platform/<platform_id>/metrics/<instance_id>")]
8pub async fn get_metrics_by_app_id(
9 platform_id: i64,
10 instance_id: Option<i64>,
11 db_manager: &State<Arc<DatabaseManager>>,
12) -> Result<Json<Value>, (Status, Json<Value>)> {
13 let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
15 Ok(platform) => platform,
16 Err(_) => {
17 return Err((
18 Status::NotFound,
19 Json(json!({
20 "error": "Platform not found",
21 "message": format!("Platform with ID {} does not exist", platform_id)
22 }))
23 ));
24 }
25 };
26
27 let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
29 Ok(pool) => pool,
30 Err(_) => {
31 return Err((
32 Status::InternalServerError,
33 Json(json!({
34 "error": "Database error",
35 "message": "Failed to connect to platform database"
36 }))
37 ));
38 }
39 };
40
41 let instance_id = instance_id.or(Some(0)); match db::metrics::get_metrics_by_app_id(&pool, instance_id).await {
44 Ok(metrics) => Ok(Json(json!({ "metrics": metrics }))),
45 Err(_) => {
46 Err((
47 Status::InternalServerError,
48 Json(json!({
49 "error": "Database error",
50 "message": "Failed to retrieve metrics"
51 }))
52 ))
53 }
54 }
55}
56
57#[get("/platform/<platform_id>/metrics")]
58pub async fn get_metrics(
59 platform_id: i64,
60 db_manager: &State<Arc<DatabaseManager>>,
61) -> Result<Json<Value>, (Status, Json<Value>)> {
62 let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
64 Ok(platform) => platform,
65 Err(_) => {
66 return Err((
67 Status::NotFound,
68 Json(json!({
69 "error": "Platform not found",
70 "message": format!("Platform with ID {} does not exist", platform_id)
71 }))
72 ));
73 }
74 };
75
76 let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
78 Ok(pool) => pool,
79 Err(_) => {
80 return Err((
81 Status::InternalServerError,
82 Json(json!({
83 "error": "Database error",
84 "message": "Failed to connect to platform database"
85 }))
86 ));
87 }
88 };
89
90 match db::metrics::get_metrics_by_app_id(&pool, None).await {
91 Ok(metrics) => Ok(Json(json!({ "metrics": metrics }))),
92 Err(_) => {
93 Err((
94 Status::InternalServerError,
95 Json(json!({
96 "error": "Database error",
97 "message": "Failed to retrieve metrics"
98 }))
99 ))
100 }
101 }
102}