diff --git a/central_backend/src/logs/logs_manager.rs b/central_backend/src/logs/logs_manager.rs index ba3cb8a..fb0d1f8 100644 --- a/central_backend/src/logs/logs_manager.rs +++ b/central_backend/src/logs/logs_manager.rs @@ -39,3 +39,20 @@ pub fn save_log( Ok(()) } + +/// Make a logs extraction +pub fn get_logs(day: u64) -> anyhow::Result> { + let file = AppConfig::get().log_of_day(day); + + if !file.exists() { + return Ok(Vec::new()); + } + + let content = std::fs::read_to_string(file)? + .split('\n') + .filter(|l| !l.is_empty()) + .map(serde_json::from_str) + .collect::, _>>()?; + + Ok(content) +} diff --git a/central_backend/src/logs/severity.rs b/central_backend/src/logs/severity.rs index 9f461c2..6265bd3 100644 --- a/central_backend/src/logs/severity.rs +++ b/central_backend/src/logs/severity.rs @@ -1,6 +1,6 @@ -#[derive(serde::Serialize, serde::Deserialize, Copy, Clone, Debug)] +#[derive(serde::Serialize, serde::Deserialize, Copy, Clone, Debug, PartialOrd, Eq, PartialEq)] pub enum LogSeverity { - Debug, + Debug = 0, Info, Warn, Error, diff --git a/central_backend/src/server/servers.rs b/central_backend/src/server/servers.rs index 46d54ed..44b0a6d 100644 --- a/central_backend/src/server/servers.rs +++ b/central_backend/src/server/servers.rs @@ -180,6 +180,11 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()> "/web_api/device/{id}", web::delete().to(devices_controller::delete_device), ) + // Logging controller API + .route( + "/web_api/logging/logs", + web::get().to(logging_controller::get_log), + ) // Relays API .route( "/web_api/relays/list", diff --git a/central_backend/src/server/web_api/logging_controller.rs b/central_backend/src/server/web_api/logging_controller.rs new file mode 100644 index 0000000..dc48e32 --- /dev/null +++ b/central_backend/src/server/web_api/logging_controller.rs @@ -0,0 +1,30 @@ +use crate::devices::device::DeviceId; +use crate::logs::logs_manager; +use crate::logs::severity::LogSeverity; +use crate::server::custom_error::HttpResult; +use crate::utils::time_utils::curr_day_number; +use actix_web::{web, HttpResponse}; + +#[derive(serde::Deserialize)] +pub struct LogRequest { + // Day number + day: Option, + min_severity: Option, + device: Option, +} + +/// Get some logs +pub async fn get_log(req: web::Query) -> HttpResult { + let day = req.day.unwrap_or_else(curr_day_number); + let mut logs = logs_manager::get_logs(day)?; + + if let Some(min_severity) = req.min_severity { + logs.retain(|d| d.severity >= min_severity); + } + + if let Some(dev_id) = &req.device { + logs.retain(|d| d.device_id.as_ref() == Some(dev_id)); + } + + Ok(HttpResponse::Ok().json(logs)) +} diff --git a/central_backend/src/server/web_api/mod.rs b/central_backend/src/server/web_api/mod.rs index eb48e75..5b75fab 100644 --- a/central_backend/src/server/web_api/mod.rs +++ b/central_backend/src/server/web_api/mod.rs @@ -1,5 +1,6 @@ pub mod auth_controller; pub mod devices_controller; pub mod energy_controller; +pub mod logging_controller; pub mod relays_controller; pub mod server_controller;