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

59 lines
1.6 KiB
Rust
Raw Normal View History

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 serde::Serialize;
2020-12-22 21:02:41 +08:00
use crate::error::ResponseError;
2020-12-12 20:32:06 +08:00
use crate::helpers::Authentication;
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);
}
#[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-15 00:55:04 +08:00
let response = data.get_index_stats(path.index_uid.clone()).await?;
2021-04-01 22:44:42 +08:00
Ok(HttpResponse::Ok().json(response))
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-15 00:55:04 +08:00
let response = data.get_all_stats().await?;
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,
commit_date: String,
2020-12-12 20:32:06 +08:00
pkg_version: String,
}
#[get("/version", wrap = "Authentication::Private")]
async fn get_version() -> HttpResponse {
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,
};
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,
};
HttpResponse::Ok().json(VersionResponse {
commit_sha: commit_sha.to_string(),
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
}