omni_orchestrator/schemas/v1/api/metrics/
get.rs1use std::sync::Arc;
2use crate::DatabaseManager;
3use crate::schemas::v1::db::queries::{self as db};
4use rocket::{get, http::Status, serde::json::{json, Json, Value}, State};
5
6use libomni::types::db::v1 as types;
7use types::metrics::Metric;
8
9#[get("/platform/<platform_id>/metrics/<instance_id>")]
10pub async fn get_metrics_by_app_id(
11 platform_id: i64,
12 instance_id: Option<i64>,
13 db_manager: &State<Arc<DatabaseManager>>,
14) -> Result<Json<Value>, (Status, Json<Value>)> {
15 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 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 instance_id = instance_id.or(Some(0)); match db::metrics::get_metrics_by_app_id(&pool, instance_id).await {
46 Ok(metrics) => Ok(Json(json!({ "metrics": metrics }))),
47 Err(_) => {
48 Err((
49 Status::InternalServerError,
50 Json(json!({
51 "error": "Database error",
52 "message": "Failed to retrieve metrics"
53 }))
54 ))
55 }
56 }
57}
58
59#[get("/platform/<platform_id>/metrics")]
60pub async fn get_metrics(
61 platform_id: i64,
62 db_manager: &State<Arc<DatabaseManager>>,
63) -> Result<Json<Value>, (Status, Json<Value>)> {
64 let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
66 Ok(platform) => platform,
67 Err(_) => {
68 return Err((
69 Status::NotFound,
70 Json(json!({
71 "error": "Platform not found",
72 "message": format!("Platform with ID {} does not exist", platform_id)
73 }))
74 ));
75 }
76 };
77
78 let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
80 Ok(pool) => pool,
81 Err(_) => {
82 return Err((
83 Status::InternalServerError,
84 Json(json!({
85 "error": "Database error",
86 "message": "Failed to connect to platform database"
87 }))
88 ));
89 }
90 };
91
92 match db::metrics::get_metrics_by_app_id(&pool, None).await {
93 Ok(metrics) => Ok(Json(json!({ "metrics": metrics }))),
94 Err(_) => {
95 Err((
96 Status::InternalServerError,
97 Json(json!({
98 "error": "Database error",
99 "message": "Failed to retrieve metrics"
100 }))
101 ))
102 }
103 }
104}