2021-04-13 16:58:22 +08:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::iter::FromIterator;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-03-16 01:11:10 +08:00
|
|
|
use actix_web::get;
|
2020-12-12 20:32:06 +08:00
|
|
|
use actix_web::web;
|
|
|
|
use actix_web::HttpResponse;
|
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use serde::Serialize;
|
|
|
|
|
2021-04-13 16:58:22 +08:00
|
|
|
use crate::data::Stats;
|
2020-12-22 21:02:41 +08:00
|
|
|
use crate::error::ResponseError;
|
2020-12-12 20:32:06 +08:00
|
|
|
use crate::helpers::Authentication;
|
2021-04-01 22:44:42 +08:00
|
|
|
use crate::index_controller::IndexStats;
|
2020-12-12 20:32:06 +08:00
|
|
|
use crate::routes::IndexParam;
|
|
|
|
use crate::Data;
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
2021-04-01 22:44:42 +08:00
|
|
|
cfg.service(get_index_stats)
|
2020-12-12 20:32:06 +08:00
|
|
|
.service(get_stats)
|
|
|
|
.service(get_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct IndexStatsResponse {
|
|
|
|
number_of_documents: u64,
|
|
|
|
is_indexing: bool,
|
2021-04-13 16:58:22 +08:00
|
|
|
fields_distribution: BTreeMap<String, u64>,
|
2021-04-01 22:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<IndexStats> for IndexStatsResponse {
|
|
|
|
fn from(stats: IndexStats) -> Self {
|
|
|
|
Self {
|
|
|
|
number_of_documents: stats.number_of_documents,
|
|
|
|
is_indexing: stats.is_indexing,
|
2021-04-13 16:58:22 +08:00
|
|
|
fields_distribution: BTreeMap::from_iter(stats.fields_distribution.into_iter()),
|
2021-04-01 22:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/stats", wrap = "Authentication::Private")]
|
2021-04-01 22:44:42 +08:00
|
|
|
async fn get_index_stats(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-04-08 21:35:28 +08:00
|
|
|
let response: IndexStatsResponse = data.get_index_stats(path.index_uid.clone()).await?.into();
|
2021-04-01 22:44:42 +08:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(response))
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-04-01 22:44:42 +08:00
|
|
|
struct StatsResponse {
|
2020-12-12 20:32:06 +08:00
|
|
|
database_size: u64,
|
|
|
|
last_update: Option<DateTime<Utc>>,
|
2021-04-13 16:58:22 +08:00
|
|
|
indexes: BTreeMap<String, IndexStatsResponse>,
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-04-08 21:35:28 +08:00
|
|
|
impl From<Stats> for StatsResponse {
|
|
|
|
fn from(stats: Stats) -> Self {
|
|
|
|
Self {
|
|
|
|
database_size: stats.database_size,
|
|
|
|
last_update: stats.last_update,
|
2021-04-13 16:58:22 +08:00
|
|
|
indexes: stats
|
|
|
|
.indexes
|
2021-04-08 21:35:28 +08:00
|
|
|
.into_iter()
|
|
|
|
.map(|(uid, index_stats)| (uid, index_stats.into()))
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 20:32:06 +08:00
|
|
|
#[get("/stats", wrap = "Authentication::Private")]
|
2021-04-01 22:44:42 +08:00
|
|
|
async fn get_stats(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
|
2021-04-08 21:35:28 +08:00
|
|
|
let response: StatsResponse = data.get_stats().await?.into();
|
2021-04-01 22:44:42 +08:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(response))
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct VersionResponse {
|
|
|
|
commit_sha: String,
|
2021-04-09 14:03:25 +08:00
|
|
|
commit_date: String,
|
2020-12-12 20:32:06 +08:00
|
|
|
pkg_version: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/version", wrap = "Authentication::Private")]
|
|
|
|
async fn get_version() -> HttpResponse {
|
2021-03-31 01:03:13 +08:00
|
|
|
let commit_sha = match option_env!("COMMIT_SHA") {
|
|
|
|
Some("") | None => env!("VERGEN_SHA"),
|
2021-04-01 22:44:42 +08:00
|
|
|
Some(commit_sha) => commit_sha,
|
2021-03-31 01:03:13 +08:00
|
|
|
};
|
|
|
|
let commit_date = match option_env!("COMMIT_DATE") {
|
|
|
|
Some("") | None => env!("VERGEN_COMMIT_DATE"),
|
2021-04-01 22:44:42 +08:00
|
|
|
Some(commit_date) => commit_date,
|
2021-03-31 01:03:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
HttpResponse::Ok().json(VersionResponse {
|
|
|
|
commit_sha: commit_sha.to_string(),
|
2021-04-09 14:03:25 +08:00
|
|
|
commit_date: commit_date.to_string(),
|
2021-03-16 02:08:19 +08:00
|
|
|
pkg_version: env!("CARGO_PKG_VERSION").to_string(),
|
|
|
|
})
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|