MeiliSearch/meilisearch-http/src/routes/stats.rs

331 lines
9.1 KiB
Rust
Raw Normal View History

2019-11-20 17:28:46 +01:00
use std::collections::HashMap;
2020-04-09 10:39:34 +02:00
use actix_web as aweb;
use actix_web::{web, get};
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;
use sysinfo::{NetworkExt, ProcessExt, ProcessorExt, System, SystemExt};
2019-10-31 15:00:36 +01:00
use walkdir::WalkDir;
use crate::Data;
use crate::error::ResponseError;
use crate::routes::IndexParam;
2019-10-31 15:00:36 +01:00
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct IndexStatsResponse {
2019-10-31 15:00:36 +01:00
number_of_documents: u64,
is_indexing: bool,
fields_frequency: HashMap<String, usize>,
}
#[get("/indexes/{index_uid}/stats")]
pub async fn index_stats(
data: web::Data<Data>,
path: web::Path<IndexParam>,
2020-04-09 10:39:34 +02:00
) -> aweb::Result<web::Json<IndexStatsResponse>> {
let index = data.db.open_index(path.index_uid.clone())
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
let reader = data.db.main_read_txn()
2020-04-09 10:39:34 +02:00
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let number_of_documents = index.main.number_of_documents(&reader)
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let fields_frequency = index.main.fields_frequency(&reader)
.map_err(|err| ResponseError::Internal(err.to_string()))?
.unwrap_or_default();
let update_reader = data.db.update_read_txn()
2020-04-09 10:39:34 +02:00
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let is_indexing = data
.is_indexing(&update_reader, &path.index_uid)
.map_err(|err| ResponseError::Internal(err.to_string()))?
.unwrap_or_default();
Ok(web::Json(IndexStatsResponse {
2019-10-31 15:00:36 +01:00
number_of_documents,
is_indexing,
fields_frequency,
}))
2019-10-31 15:00:36 +01:00
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StatsResult {
2019-10-31 15:00:36 +01:00
database_size: u64,
last_update: Option<DateTime<Utc>>,
2019-10-31 15:00:36 +01:00
indexes: HashMap<String, IndexStatsResponse>,
}
#[get("/stats")]
pub async fn get_stats(
data: web::Data<Data>,
2020-04-09 10:39:34 +02:00
) -> aweb::Result<web::Json<StatsResult>> {
2019-10-31 15:00:36 +01:00
let mut index_list = HashMap::new();
let reader = data.db.main_read_txn()
2020-04-09 10:39:34 +02:00
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let update_reader = data.db.update_read_txn()
2020-04-09 10:39:34 +02:00
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let indexes_set = data.db.indexes_uids();
for index_uid in indexes_set {
let index = data.db.open_index(&index_uid);
2019-11-20 17:28:46 +01:00
match index {
Some(index) => {
let number_of_documents = index.main.number_of_documents(&reader)
.map_err(|err| ResponseError::Internal(err.to_string()))?;
2019-11-20 17:28:46 +01:00
let fields_frequency = index.main.fields_frequency(&reader)
.map_err(|err| ResponseError::Internal(err.to_string()))?
.unwrap_or_default();
2019-11-20 17:28:46 +01:00
let is_indexing = data
.is_indexing(&update_reader, &index_uid)
.map_err(|err| ResponseError::Internal(err.to_string()))?
.unwrap_or_default();
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
}
let database_size = WalkDir::new(data.db_path.clone())
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());
let last_update = data.last_update(&reader)
.map_err(|err| ResponseError::Internal(err.to_string()))?;
Ok(web::Json(StatsResult {
2019-10-31 15:00:36 +01:00
database_size,
last_update,
2019-10-31 15:00:36 +01:00
indexes: index_list,
}))
2019-10-31 15:00:36 +01:00
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VersionResponse {
2019-10-31 15:00:36 +01:00
commit_sha: String,
build_date: String,
pkg_version: String,
}
#[get("/version")]
pub async fn get_version() -> web::Json<VersionResponse> {
web::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(),
})
2019-10-31 15:00:36 +01:00
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub 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")]
pub 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")]
pub 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(),
}
}
}
#[get("/sys-info")]
pub async fn get_sys_info(
data: web::Data<Data>,
) -> web::Json<SysInfo> {
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();
info.global.input_data = sys.get_networks()
.into_iter()
.map(|(_, n)| n.get_received())
.sum::<u64>();
info.global.output_data = sys.get_networks()
.into_iter()
.map(|(_, n)| n.get_transmitted())
.sum::<u64>();
2019-10-31 15:00:36 +01: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();
web::Json(info)
2019-10-31 15:00:36 +01:00
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub 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")]
pub 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")]
pub 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(),
}
}
}
#[get("/sys-info/pretty")]
pub async fn get_sys_info_pretty(
data: web::Data<Data>,
) -> web::Json<SysInfoPretty> {
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-02 18:36:04 +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
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();
web::Json(info)
2019-10-31 15:00:36 +01:00
}