Add function to extract logs
This commit is contained in:
parent
63bdeed952
commit
75753051f9
@ -39,3 +39,20 @@ pub fn save_log(
|
|||||||
|
|
||||||
Ok(())
|
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)
|
||||||
|
}
|
||||||
|
@ -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 {
|
pub enum LogSeverity {
|
||||||
Debug,
|
Debug = 0,
|
||||||
Info,
|
Info,
|
||||||
Warn,
|
Warn,
|
||||||
Error,
|
Error,
|
||||||
|
@ -180,6 +180,11 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
|
|||||||
"/web_api/device/{id}",
|
"/web_api/device/{id}",
|
||||||
web::delete().to(devices_controller::delete_device),
|
web::delete().to(devices_controller::delete_device),
|
||||||
)
|
)
|
||||||
|
// Logging controller API
|
||||||
|
.route(
|
||||||
|
"/web_api/logging/logs",
|
||||||
|
web::get().to(logging_controller::get_log),
|
||||||
|
)
|
||||||
// Relays API
|
// Relays API
|
||||||
.route(
|
.route(
|
||||||
"/web_api/relays/list",
|
"/web_api/relays/list",
|
||||||
|
30
central_backend/src/server/web_api/logging_controller.rs
Normal file
30
central_backend/src/server/web_api/logging_controller.rs
Normal 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))
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
pub mod auth_controller;
|
pub mod auth_controller;
|
||||||
pub mod devices_controller;
|
pub mod devices_controller;
|
||||||
pub mod energy_controller;
|
pub mod energy_controller;
|
||||||
|
pub mod logging_controller;
|
||||||
pub mod relays_controller;
|
pub mod relays_controller;
|
||||||
pub mod server_controller;
|
pub mod server_controller;
|
||||||
|
Loading…
Reference in New Issue
Block a user