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

135 lines
3.9 KiB
Rust
Raw Normal View History

2020-10-02 13:00:55 +02:00
use std::collections::{HashMap, BTreeMap};
2019-11-20 17:28:46 +01:00
use actix_web::web;
2020-04-24 15:00:52 +02:00
use actix_web::HttpResponse;
2020-09-12 04:29:17 +02:00
use actix_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 serde::Serialize;
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)
2020-06-30 13:11:49 +02:00
.service(get_version);
}
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,
2020-10-02 13:00:55 +02:00
fields_distribution: BTreeMap<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
}