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

336 lines
9.0 KiB
Rust
Raw Normal View History

2019-11-20 17:28:46 +01:00
use std::collections::HashMap;
use actix_web::web;
2020-04-24 15:00:52 +02:00
use actix_web::HttpResponse;
use actix_web_macros::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::error::{Error, ResponseError};
use crate::helpers::Authentication;
use crate::routes::IndexParam;
2020-04-10 19:05:05 +02:00
use crate::Data;
2019-10-31 15:00:36 +01: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")]
struct IndexStatsResponse {
2019-10-31 15:00:36 +01:00
number_of_documents: u64,
is_indexing: bool,
fields_distribution: HashMap<String, usize>,
2019-10-31 15:00:36 +01:00
}
#[get("/indexes/{index_uid}/stats", wrap = "Authentication::Private")]
async fn index_stats(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
2020-04-10 19:05:05 +02:00
let index = data
.db
.open_index(&path.index_uid)
2020-05-19 18:20:29 +02:00
.ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?;
let number_of_documents = index.main.number_of_documents(&reader)?;
let fields_distribution = index.main.fields_distribution(&reader)?.unwrap_or_default();
let update_reader = data.db.update_read_txn()?;
2020-04-24 15:00:52 +02:00
let is_indexing =
2020-05-22 12:35:23 +02:00
data.db.is_indexing(&update_reader, &path.index_uid)?
2020-05-19 18:20:29 +02:00
.ok_or(Error::internal(
2020-04-24 15:00:52 +02:00
"Impossible to know if the database is indexing",
))?;
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_distribution,
}))
2019-10-31 15:00:36 +01:00
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
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", wrap = "Authentication::Private")]
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();
let reader = data.db.main_read_txn()?;
let update_reader = data.db.update_read_txn()?;
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)?;
let fields_distribution = index.main.fields_distribution(&reader)?.unwrap_or_default();
2019-11-20 17:28:46 +01:00
2020-05-22 12:35:23 +02:00
let is_indexing = data.db.is_indexing(&update_reader, &index_uid)?.ok_or(
2020-05-19 18:20:29 +02:00
Error::internal("Impossible to know if the database is indexing"),
2020-04-24 15:00:52 +02:00
)?;
2019-11-20 17:28:46 +01:00
let response = IndexStatsResponse {
number_of_documents,
is_indexing,
fields_distribution,
2019-11-20 17:28:46 +01:00
};
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)
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.db.last_update(&reader)?;
2020-04-24 15:00:52 +02:00
Ok(HttpResponse::Ok().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")]
struct VersionResponse {
2019-10-31 15:00:36 +01:00
commit_sha: String,
build_date: String,
pkg_version: String,
}
#[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(),
})
2019-10-31 15:00:36 +01:00
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
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")]
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")]
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", 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()
.into_iter()
.map(|(_, n)| n.get_received())
.sum::<u64>();
2020-04-10 19:05:05 +02:00
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();
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")]
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")]
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")]
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", 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
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
}