omni_orchestrator/schemas/v1/api/alerts/
search.rs1use 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("/platform/<platform_id>/alerts/search?<query>&<org_id>&<page>&<per_page>")]
10pub async fn search_alerts(
11 platform_id: i64,
12 query: String,
13 org_id: Option<i64>,
14 page: Option<i64>,
15 per_page: Option<i64>,
16 db_manager: &State<Arc<DatabaseManager>>,
17) -> Result<Json<Value>, (Status, Json<Value>)> {
18 let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
20 Ok(platform) => platform,
21 Err(_) => {
22 return Err((
23 Status::NotFound,
24 Json(json!({
25 "error": "Platform not found",
26 "message": format!("Platform with ID {} does not exist", platform_id)
27 }))
28 ));
29 }
30 };
31
32 let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
34 Ok(pool) => pool,
35 Err(_) => {
36 return Err((
37 Status::InternalServerError,
38 Json(json!({
39 "error": "Database error",
40 "message": "Failed to connect to platform database"
41 }))
42 ));
43 }
44 };
45
46 let page = page.unwrap_or(0);
47 let per_page = per_page.unwrap_or(20);
48
49 let alerts = match db::alert::search_alerts(
50 &pool,
51 &query,
52 org_id,
53 page,
54 per_page,
55 ).await {
56 Ok(alerts) => alerts,
57 Err(e) => {
58 log::error!("Failed to search alerts: {}", e);
59 return Err((
60 Status::InternalServerError,
61 Json(json!({
62 "error": "Database error",
63 "message": "Failed to search alerts"
64 }))
65 ));
66 }
67 };
68
69 let total_count = match db::alert::count_search_alerts(
70 &pool,
71 &query,
72 org_id,
73 ).await {
74 Ok(count) => count,
75 Err(e) => {
76 log::error!("Failed to count search results: {}", e);
77 return Err((
78 Status::InternalServerError,
79 Json(json!({
80 "error": "Database error",
81 "message": "Failed to count search results"
82 }))
83 ));
84 }
85 };
86
87 let total_pages = (total_count as f64 / per_page as f64).ceil() as i64;
88
89 Ok(Json(json!({
90 "alerts": alerts,
91 "pagination": {
92 "page": page,
93 "per_page": per_page,
94 "total_count": total_count,
95 "total_pages": total_pages
96 }
97 })))
98}