Add function to extract logs

This commit is contained in:
Pierre HUBERT 2024-10-01 22:27:34 +02:00
parent 63bdeed952
commit 75753051f9
5 changed files with 55 additions and 2 deletions

View File

@ -39,3 +39,20 @@ pub fn save_log(
Ok(())
}
/// Make a logs extraction
pub fn get_logs(day: u64) -> anyhow::Result<Vec<LogEntry>> {
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::<Result<Vec<_>, _>>()?;
Ok(content)
}

View File

@ -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,

View File

@ -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",

View File

@ -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<u64>,
min_severity: Option<LogSeverity>,
device: Option<DeviceId>,
}
/// Get some logs
pub async fn get_log(req: web::Query<LogRequest>) -> 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))
}

View File

@ -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;