2019-11-20 17:28:46 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
use actix_web::web;
|
2020-04-24 15:00:52 +02:00
|
|
|
use actix_web::HttpResponse;
|
2020-04-22 17:43:51 +02:00
|
|
|
use actix_web_macros::get;
|
2019-11-20 11:21:55 +01:00
|
|
|
use chrono::{DateTime, Utc};
|
2019-11-20 17:28:46 +01:00
|
|
|
use log::error;
|
2019-10-31 15:00:36 +01:00
|
|
|
use pretty_bytes::converter::convert;
|
|
|
|
use serde::Serialize;
|
2020-04-08 14:13:45 +02:00
|
|
|
use sysinfo::{NetworkExt, ProcessExt, ProcessorExt, System, SystemExt};
|
2019-10-31 15:00:36 +01:00
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
2020-04-08 14:13:45 +02:00
|
|
|
use crate::error::ResponseError;
|
2020-04-22 17:43:51 +02:00
|
|
|
use crate::helpers::Authentication;
|
2020-04-09 11:11:48 +02:00
|
|
|
use crate::routes::IndexParam;
|
2020-04-10 19:05:05 +02:00
|
|
|
use crate::Data;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(index_stats)
|
|
|
|
.service(get_stats)
|
|
|
|
.service(get_version)
|
|
|
|
.service(get_sys_info)
|
|
|
|
.service(get_sys_info_pretty);
|
|
|
|
}
|
|
|
|
|
2019-10-31 15:00:36 +01:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-04-22 17:43:51 +02:00
|
|
|
struct IndexStatsResponse {
|
2019-10-31 15:00:36 +01:00
|
|
|
number_of_documents: u64,
|
|
|
|
is_indexing: bool,
|
|
|
|
fields_frequency: HashMap<String, usize>,
|
|
|
|
}
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
#[get("/indexes/{index_uid}/stats", wrap = "Authentication::Private")]
|
|
|
|
async fn index_stats(
|
2020-04-08 14:13:45 +02:00
|
|
|
data: web::Data<Data>,
|
2020-04-09 11:11:48 +02:00
|
|
|
path: web::Path<IndexParam>,
|
2020-04-24 15:00:52 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
2020-04-17 14:52:13 +02:00
|
|
|
.open_index(&path.index_uid)
|
|
|
|
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
|
2020-04-08 14:13:45 +02:00
|
|
|
|
2020-04-17 14:52:13 +02:00
|
|
|
let reader = data.db.main_read_txn()?;
|
2020-04-08 14:13:45 +02:00
|
|
|
|
2020-04-17 14:52:13 +02:00
|
|
|
let number_of_documents = index.main.number_of_documents(&reader)?;
|
|
|
|
|
|
|
|
let fields_frequency = index.main.fields_frequency(&reader)?.unwrap_or_default();
|
|
|
|
|
|
|
|
let update_reader = data.db.update_read_txn()?;
|
2020-04-08 14:13:45 +02:00
|
|
|
|
2020-04-24 15:00:52 +02:00
|
|
|
let is_indexing =
|
|
|
|
data.is_indexing(&update_reader, &path.index_uid)?
|
|
|
|
.ok_or(ResponseError::internal(
|
|
|
|
"Impossible to know if the database is indexing",
|
|
|
|
))?;
|
2020-04-08 14:13:45 +02:00
|
|
|
|
2020-04-24 15:00:52 +02:00
|
|
|
Ok(HttpResponse::Ok().json(IndexStatsResponse {
|
2019-10-31 15:00:36 +01:00
|
|
|
number_of_documents,
|
|
|
|
is_indexing,
|
|
|
|
fields_frequency,
|
2020-04-08 14:13:45 +02:00
|
|
|
}))
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-04-22 17:43:51 +02:00
|
|
|
struct StatsResult {
|
2019-10-31 15:00:36 +01:00
|
|
|
database_size: u64,
|
2019-11-20 11:21:55 +01:00
|
|
|
last_update: Option<DateTime<Utc>>,
|
2019-10-31 15:00:36 +01:00
|
|
|
indexes: HashMap<String, IndexStatsResponse>,
|
|
|
|
}
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
#[get("/stats", wrap = "Authentication::Private")]
|
2020-04-24 15:00:52 +02:00
|
|
|
async fn get_stats(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
|
2019-10-31 15:00:36 +01:00
|
|
|
let mut index_list = HashMap::new();
|
|
|
|
|
2020-04-17 14:52:13 +02:00
|
|
|
let reader = data.db.main_read_txn()?;
|
|
|
|
let update_reader = data.db.update_read_txn()?;
|
2019-11-20 11:21:55 +01:00
|
|
|
|
2020-04-08 14:13:45 +02:00
|
|
|
let indexes_set = data.db.indexes_uids();
|
2019-11-20 11:21:55 +01:00
|
|
|
for index_uid in indexes_set {
|
2020-04-08 14:13:45 +02:00
|
|
|
let index = data.db.open_index(&index_uid);
|
2019-11-20 17:28:46 +01:00
|
|
|
match index {
|
|
|
|
Some(index) => {
|
2020-04-17 14:52:13 +02:00
|
|
|
let number_of_documents = index.main.number_of_documents(&reader)?;
|
|
|
|
|
|
|
|
let fields_frequency = index.main.fields_frequency(&reader)?.unwrap_or_default();
|
2019-11-20 17:28:46 +01:00
|
|
|
|
2020-04-24 15:00:52 +02:00
|
|
|
let is_indexing = data.is_indexing(&update_reader, &index_uid)?.ok_or(
|
|
|
|
ResponseError::internal("Impossible to know if the database is indexing"),
|
|
|
|
)?;
|
2019-11-20 17:28:46 +01:00
|
|
|
|
|
|
|
let response = IndexStatsResponse {
|
|
|
|
number_of_documents,
|
|
|
|
is_indexing,
|
|
|
|
fields_frequency,
|
|
|
|
};
|
|
|
|
index_list.insert(index_uid, response);
|
|
|
|
}
|
|
|
|
None => error!(
|
|
|
|
"Index {:?} is referenced in the indexes list but cannot be found",
|
|
|
|
index_uid
|
|
|
|
),
|
|
|
|
}
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-17 14:52:13 +02:00
|
|
|
let database_size = WalkDir::new(&data.db_path)
|
2019-10-31 15:00:36 +01:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|entry| entry.ok())
|
|
|
|
.filter_map(|entry| entry.metadata().ok())
|
|
|
|
.filter(|metadata| metadata.is_file())
|
|
|
|
.fold(0, |acc, m| acc + m.len());
|
|
|
|
|
2020-04-17 14:52:13 +02:00
|
|
|
let last_update = data.last_update(&reader)?;
|
2019-11-20 11:21:55 +01:00
|
|
|
|
2020-04-24 15:00:52 +02:00
|
|
|
Ok(HttpResponse::Ok().json(StatsResult {
|
2019-10-31 15:00:36 +01:00
|
|
|
database_size,
|
2019-11-20 11:21:55 +01:00
|
|
|
last_update,
|
2019-10-31 15:00:36 +01:00
|
|
|
indexes: index_list,
|
2020-04-08 14:13:45 +02:00
|
|
|
}))
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-04-22 17:43:51 +02:00
|
|
|
struct VersionResponse {
|
2019-10-31 15:00:36 +01:00
|
|
|
commit_sha: String,
|
|
|
|
build_date: String,
|
|
|
|
pkg_version: String,
|
|
|
|
}
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
#[get("/version", wrap = "Authentication::Private")]
|
2020-04-24 15:00:52 +02:00
|
|
|
async fn get_version() -> HttpResponse {
|
|
|
|
HttpResponse::Ok().json(VersionResponse {
|
2019-10-31 15:00:36 +01:00
|
|
|
commit_sha: env!("VERGEN_SHA").to_string(),
|
|
|
|
build_date: env!("VERGEN_BUILD_TIMESTAMP").to_string(),
|
|
|
|
pkg_version: env!("CARGO_PKG_VERSION").to_string(),
|
2020-04-08 14:13:45 +02:00
|
|
|
})
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-04-22 17:43:51 +02:00
|
|
|
struct SysGlobal {
|
2019-10-31 15:00:36 +01:00
|
|
|
total_memory: u64,
|
|
|
|
used_memory: u64,
|
|
|
|
total_swap: u64,
|
|
|
|
used_swap: u64,
|
|
|
|
input_data: u64,
|
|
|
|
output_data: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SysGlobal {
|
|
|
|
fn new() -> SysGlobal {
|
|
|
|
SysGlobal {
|
|
|
|
total_memory: 0,
|
|
|
|
used_memory: 0,
|
|
|
|
total_swap: 0,
|
|
|
|
used_swap: 0,
|
|
|
|
input_data: 0,
|
|
|
|
output_data: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-04-22 17:43:51 +02:00
|
|
|
struct SysProcess {
|
2019-10-31 15:00:36 +01:00
|
|
|
memory: u64,
|
|
|
|
cpu: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SysProcess {
|
|
|
|
fn new() -> SysProcess {
|
|
|
|
SysProcess {
|
|
|
|
memory: 0,
|
|
|
|
cpu: 0.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-04-22 17:43:51 +02:00
|
|
|
struct SysInfo {
|
2019-10-31 15:00:36 +01:00
|
|
|
memory_usage: f64,
|
|
|
|
processor_usage: Vec<f32>,
|
|
|
|
global: SysGlobal,
|
|
|
|
process: SysProcess,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SysInfo {
|
|
|
|
fn new() -> SysInfo {
|
|
|
|
SysInfo {
|
|
|
|
memory_usage: 0.0,
|
|
|
|
processor_usage: Vec::new(),
|
|
|
|
global: SysGlobal::new(),
|
|
|
|
process: SysProcess::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
#[get("/sys-info", wrap = "Authentication::Private")]
|
2020-04-24 15:00:52 +02:00
|
|
|
async fn get_sys_info(data: web::Data<Data>) -> HttpResponse {
|
2019-10-31 15:00:36 +01:00
|
|
|
let mut sys = System::new();
|
|
|
|
let mut info = SysInfo::new();
|
|
|
|
|
|
|
|
info.memory_usage = sys.get_used_memory() as f64 / sys.get_total_memory() as f64 * 100.0;
|
|
|
|
|
2020-04-02 18:36:04 +02:00
|
|
|
for processor in sys.get_processors() {
|
2019-10-31 15:00:36 +01:00
|
|
|
info.processor_usage.push(processor.get_cpu_usage() * 100.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
info.global.total_memory = sys.get_total_memory();
|
|
|
|
info.global.used_memory = sys.get_used_memory();
|
|
|
|
info.global.total_swap = sys.get_total_swap();
|
|
|
|
info.global.used_swap = sys.get_used_swap();
|
2020-04-10 19:05:05 +02:00
|
|
|
info.global.input_data = sys
|
|
|
|
.get_networks()
|
2020-04-08 14:13:45 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|(_, n)| n.get_received())
|
|
|
|
.sum::<u64>();
|
2020-04-10 19:05:05 +02:00
|
|
|
info.global.output_data = sys
|
|
|
|
.get_networks()
|
2020-04-08 14:13:45 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|(_, n)| n.get_transmitted())
|
|
|
|
.sum::<u64>();
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-08 14:13:45 +02:00
|
|
|
if let Some(process) = sys.get_process(data.server_pid) {
|
2019-10-31 15:00:36 +01:00
|
|
|
info.process.memory = process.memory();
|
|
|
|
info.process.cpu = process.cpu_usage() * 100.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.refresh_all();
|
2020-04-24 15:00:52 +02:00
|
|
|
HttpResponse::Ok().json(info)
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-04-22 17:43:51 +02:00
|
|
|
struct SysGlobalPretty {
|
2019-10-31 15:00:36 +01:00
|
|
|
total_memory: String,
|
|
|
|
used_memory: String,
|
|
|
|
total_swap: String,
|
|
|
|
used_swap: String,
|
|
|
|
input_data: String,
|
|
|
|
output_data: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SysGlobalPretty {
|
|
|
|
fn new() -> SysGlobalPretty {
|
|
|
|
SysGlobalPretty {
|
|
|
|
total_memory: "None".to_owned(),
|
|
|
|
used_memory: "None".to_owned(),
|
|
|
|
total_swap: "None".to_owned(),
|
|
|
|
used_swap: "None".to_owned(),
|
|
|
|
input_data: "None".to_owned(),
|
|
|
|
output_data: "None".to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-04-22 17:43:51 +02:00
|
|
|
struct SysProcessPretty {
|
2019-10-31 15:00:36 +01:00
|
|
|
memory: String,
|
|
|
|
cpu: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SysProcessPretty {
|
|
|
|
fn new() -> SysProcessPretty {
|
|
|
|
SysProcessPretty {
|
|
|
|
memory: "None".to_owned(),
|
|
|
|
cpu: "None".to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-04-22 17:43:51 +02:00
|
|
|
struct SysInfoPretty {
|
2019-10-31 15:00:36 +01:00
|
|
|
memory_usage: String,
|
|
|
|
processor_usage: Vec<String>,
|
|
|
|
global: SysGlobalPretty,
|
|
|
|
process: SysProcessPretty,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SysInfoPretty {
|
|
|
|
fn new() -> SysInfoPretty {
|
|
|
|
SysInfoPretty {
|
|
|
|
memory_usage: "None".to_owned(),
|
|
|
|
processor_usage: Vec::new(),
|
|
|
|
global: SysGlobalPretty::new(),
|
|
|
|
process: SysProcessPretty::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
#[get("/sys-info/pretty", wrap = "Authentication::Private")]
|
2020-04-24 15:00:52 +02:00
|
|
|
async fn get_sys_info_pretty(data: web::Data<Data>) -> HttpResponse {
|
2019-10-31 15:00:36 +01:00
|
|
|
let mut sys = System::new();
|
|
|
|
let mut info = SysInfoPretty::new();
|
|
|
|
|
|
|
|
info.memory_usage = format!(
|
|
|
|
"{:.1} %",
|
|
|
|
sys.get_used_memory() as f64 / sys.get_total_memory() as f64 * 100.0
|
|
|
|
);
|
|
|
|
|
2020-04-02 18:36:04 +02:00
|
|
|
for processor in sys.get_processors() {
|
2019-10-31 15:00:36 +01:00
|
|
|
info.processor_usage
|
|
|
|
.push(format!("{:.1} %", processor.get_cpu_usage() * 100.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
info.global.total_memory = convert(sys.get_total_memory() as f64 * 1024.0);
|
|
|
|
info.global.used_memory = convert(sys.get_used_memory() as f64 * 1024.0);
|
|
|
|
info.global.total_swap = convert(sys.get_total_swap() as f64 * 1024.0);
|
|
|
|
info.global.used_swap = convert(sys.get_used_swap() as f64 * 1024.0);
|
2020-04-10 19:05:05 +02:00
|
|
|
info.global.input_data = convert(
|
|
|
|
sys.get_networks()
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, n)| n.get_received())
|
|
|
|
.sum::<u64>() as f64,
|
|
|
|
);
|
|
|
|
info.global.output_data = convert(
|
|
|
|
sys.get_networks()
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, n)| n.get_transmitted())
|
|
|
|
.sum::<u64>() as f64,
|
|
|
|
);
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-08 14:13:45 +02:00
|
|
|
if let Some(process) = sys.get_process(data.server_pid) {
|
2019-10-31 15:00:36 +01:00
|
|
|
info.process.memory = convert(process.memory() as f64 * 1024.0);
|
|
|
|
info.process.cpu = format!("{:.1} %", process.cpu_usage() * 100.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.refresh_all();
|
|
|
|
|
2020-04-24 15:00:52 +02:00
|
|
|
HttpResponse::Ok().json(info)
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|