2022-09-27 16:33:37 +02:00
|
|
|
use actix_web::web::Data;
|
2021-10-13 20:56:28 +02:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
2022-09-27 16:33:37 +02:00
|
|
|
use index_scheduler::{IndexScheduler, KindWithContent};
|
2021-06-23 12:18:34 +02:00
|
|
|
use log::debug;
|
2022-06-06 12:38:46 +02:00
|
|
|
use meilisearch_types::error::ResponseError;
|
2021-05-31 16:03:39 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-10-12 14:46:35 +02:00
|
|
|
use serde_json::json;
|
2022-02-14 15:32:41 +01:00
|
|
|
use time::OffsetDateTime;
|
2020-12-12 13:32:06 +01:00
|
|
|
|
2021-10-12 14:46:35 +02:00
|
|
|
use crate::analytics::Analytics;
|
2022-09-06 15:13:09 +02:00
|
|
|
use crate::extractors::authentication::{policies::*, AuthenticationError, GuardedData};
|
2022-03-04 20:12:44 +01:00
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
2022-09-22 12:14:51 +02:00
|
|
|
use index_scheduler::task::TaskView;
|
2020-12-12 13:32:06 +01:00
|
|
|
|
2022-06-02 13:31:46 +02:00
|
|
|
use super::Pagination;
|
|
|
|
|
2021-07-07 16:20:22 +02:00
|
|
|
pub mod documents;
|
|
|
|
pub mod search;
|
|
|
|
pub mod settings;
|
2021-07-05 14:29:20 +02:00
|
|
|
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
2021-06-24 15:33:21 +02:00
|
|
|
cfg.service(
|
2021-07-05 14:29:20 +02:00
|
|
|
web::resource("")
|
2021-06-24 15:33:21 +02:00
|
|
|
.route(web::get().to(list_indexes))
|
2022-03-04 20:12:44 +01:00
|
|
|
.route(web::post().to(SeqHandler(create_index))),
|
2021-06-24 15:33:21 +02:00
|
|
|
)
|
|
|
|
.service(
|
2021-07-05 14:29:20 +02:00
|
|
|
web::scope("/{index_uid}")
|
|
|
|
.service(
|
|
|
|
web::resource("")
|
2022-03-04 20:12:44 +01:00
|
|
|
.route(web::get().to(SeqHandler(get_index)))
|
2022-06-02 11:48:59 +02:00
|
|
|
.route(web::patch().to(SeqHandler(update_index)))
|
2022-03-04 20:12:44 +01:00
|
|
|
.route(web::delete().to(SeqHandler(delete_index))),
|
2021-07-05 14:29:20 +02:00
|
|
|
)
|
2022-03-04 20:12:44 +01:00
|
|
|
.service(web::resource("/stats").route(web::get().to(SeqHandler(get_index_stats))))
|
2021-07-05 14:29:20 +02:00
|
|
|
.service(web::scope("/documents").configure(documents::configure))
|
|
|
|
.service(web::scope("/search").configure(search::configure))
|
2021-09-24 14:55:57 +02:00
|
|
|
.service(web::scope("/settings").configure(settings::configure)),
|
2021-06-24 15:33:21 +02:00
|
|
|
);
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
2021-09-28 22:22:59 +02:00
|
|
|
pub async fn list_indexes(
|
2022-09-27 16:33:37 +02:00
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_GET }>, Data<IndexScheduler>>,
|
2022-06-02 13:31:46 +02:00
|
|
|
paginate: web::Query<Pagination>,
|
2021-09-28 22:22:59 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-09-27 16:33:37 +02:00
|
|
|
let search_rules = &index_scheduler.filters().search_rules;
|
|
|
|
let indexes: Vec<_> = index_scheduler.indexes()?;
|
2022-05-24 11:29:03 +02:00
|
|
|
let nb_indexes = indexes.len();
|
2022-06-02 13:31:46 +02:00
|
|
|
let iter = indexes
|
2022-01-12 15:35:33 +01:00
|
|
|
.into_iter()
|
2022-09-22 12:14:51 +02:00
|
|
|
.filter(|index| search_rules.is_index_authorized(&index.name));
|
|
|
|
/*
|
2022-09-27 16:33:37 +02:00
|
|
|
TODO: TAMO: implements me. It's missing a kind of IndexView or something
|
2022-06-02 13:31:46 +02:00
|
|
|
let ret = paginate
|
|
|
|
.into_inner()
|
|
|
|
.auto_paginate_unsized(nb_indexes, iter);
|
2022-09-22 12:14:51 +02:00
|
|
|
*/
|
|
|
|
let ret = todo!();
|
2021-11-08 18:31:27 +01:00
|
|
|
|
2022-06-02 13:31:46 +02:00
|
|
|
debug!("returns: {:?}", ret);
|
|
|
|
Ok(HttpResponse::Ok().json(ret))
|
2021-07-05 14:29:20 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:32:06 +01:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
2021-07-07 16:20:22 +02:00
|
|
|
pub struct IndexCreateRequest {
|
2021-02-18 17:48:37 +01:00
|
|
|
uid: String,
|
2020-12-12 13:32:06 +01:00
|
|
|
primary_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-09-28 18:10:09 +02:00
|
|
|
pub async fn create_index(
|
2022-09-27 16:33:37 +02:00
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_CREATE }>, Data<IndexScheduler>>,
|
2021-09-28 18:10:09 +02:00
|
|
|
body: web::Json<IndexCreateRequest>,
|
2021-10-13 20:56:28 +02:00
|
|
|
req: HttpRequest,
|
2021-10-29 16:10:58 +02:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2021-09-28 18:10:09 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-09-22 12:14:51 +02:00
|
|
|
let IndexCreateRequest { primary_key, uid } = body.into_inner();
|
2021-10-12 14:46:35 +02:00
|
|
|
|
2022-09-06 15:13:09 +02:00
|
|
|
let allow_index_creation = meilisearch.filters().search_rules.is_index_authorized(&uid);
|
|
|
|
if allow_index_creation {
|
|
|
|
analytics.publish(
|
|
|
|
"Index Created".to_string(),
|
|
|
|
json!({ "primary_key": primary_key }),
|
|
|
|
Some(&req),
|
|
|
|
);
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2022-09-22 12:14:51 +02:00
|
|
|
let task = KindWithContent::IndexCreation {
|
|
|
|
index_uid: uid,
|
|
|
|
primary_key,
|
|
|
|
};
|
2022-09-27 16:33:37 +02:00
|
|
|
let task = tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??;
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2022-09-06 15:13:09 +02:00
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
|
|
|
} else {
|
|
|
|
Err(AuthenticationError::InvalidToken.into())
|
|
|
|
}
|
2021-09-28 18:10:09 +02:00
|
|
|
}
|
2021-07-05 14:29:20 +02:00
|
|
|
|
2020-12-12 13:32:06 +01:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
2021-12-02 16:03:26 +01:00
|
|
|
#[allow(dead_code)]
|
2021-07-07 16:20:22 +02:00
|
|
|
pub struct UpdateIndexRequest {
|
2021-03-15 14:43:47 +01:00
|
|
|
uid: Option<String>,
|
2020-12-12 13:32:06 +01:00
|
|
|
primary_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-04-28 16:43:49 +02:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct UpdateIndexResponse {
|
|
|
|
name: String,
|
|
|
|
uid: String,
|
2022-02-14 15:32:41 +01:00
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
|
|
|
created_at: OffsetDateTime,
|
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
|
|
|
updated_at: OffsetDateTime,
|
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
|
|
|
primary_key: OffsetDateTime,
|
2021-04-28 16:43:49 +02:00
|
|
|
}
|
2021-07-05 10:10:17 +02:00
|
|
|
|
2021-07-07 16:20:22 +02:00
|
|
|
pub async fn get_index(
|
2022-09-27 16:33:37 +02:00
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_GET }>, Data<IndexScheduler>>,
|
|
|
|
index_uid: web::Path<String>,
|
2021-06-24 15:33:21 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-09-27 16:33:37 +02:00
|
|
|
let meta = index_scheduler.index(&index_uid)?;
|
2021-06-24 15:33:21 +02:00
|
|
|
debug!("returns: {:?}", meta);
|
2022-09-22 12:14:51 +02:00
|
|
|
|
|
|
|
// TODO: TAMO: do this as well
|
|
|
|
todo!()
|
|
|
|
// Ok(HttpResponse::Ok().json(meta))
|
2021-06-24 15:33:21 +02:00
|
|
|
}
|
|
|
|
|
2021-07-07 16:20:22 +02:00
|
|
|
pub async fn update_index(
|
2022-09-27 16:33:37 +02:00
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_UPDATE }>, Data<IndexScheduler>>,
|
2021-12-02 16:03:26 +01:00
|
|
|
path: web::Path<String>,
|
2021-02-09 11:26:52 +01:00
|
|
|
body: web::Json<UpdateIndexRequest>,
|
2021-10-13 20:56:28 +02:00
|
|
|
req: HttpRequest,
|
2021-10-29 16:10:58 +02:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-06-23 12:18:34 +02:00
|
|
|
debug!("called with params: {:?}", body);
|
2021-03-15 16:52:05 +01:00
|
|
|
let body = body.into_inner();
|
2021-10-12 15:00:04 +02:00
|
|
|
analytics.publish(
|
|
|
|
"Index Updated".to_string(),
|
2021-10-20 14:29:08 +02:00
|
|
|
json!({ "primary_key": body.primary_key}),
|
2021-10-13 20:56:28 +02:00
|
|
|
Some(&req),
|
2021-10-12 15:00:04 +02:00
|
|
|
);
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2022-09-22 12:14:51 +02:00
|
|
|
let task = KindWithContent::IndexUpdate {
|
|
|
|
index_uid: path.into_inner(),
|
2021-09-21 13:23:22 +02:00
|
|
|
primary_key: body.primary_key,
|
|
|
|
};
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2022-09-27 16:33:37 +02:00
|
|
|
let task = tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??;
|
2021-12-02 16:03:26 +01:00
|
|
|
|
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
2021-09-28 18:10:09 +02:00
|
|
|
pub async fn delete_index(
|
2022-09-27 16:33:37 +02:00
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_DELETE }>, Data<IndexScheduler>>,
|
|
|
|
index_uid: web::Path<String>,
|
2021-09-28 18:10:09 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-09-27 16:33:37 +02:00
|
|
|
let task = KindWithContent::IndexDeletion {
|
|
|
|
index_uid: index_uid.into_inner(),
|
|
|
|
};
|
|
|
|
let task = tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??;
|
2021-12-02 16:03:26 +01:00
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-28 18:10:09 +02:00
|
|
|
}
|
2020-12-12 13:32:06 +01:00
|
|
|
|
2021-07-07 16:20:22 +02:00
|
|
|
pub async fn get_index_stats(
|
2022-09-27 16:33:37 +02:00
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::STATS_GET }>, Data<IndexScheduler>>,
|
|
|
|
index_uid: web::Path<String>,
|
2022-08-17 16:12:26 +02:00
|
|
|
req: HttpRequest,
|
|
|
|
analytics: web::Data<dyn Analytics>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-08-17 16:12:26 +02:00
|
|
|
analytics.publish(
|
|
|
|
"Stats Seen".to_string(),
|
|
|
|
json!({ "per_index_uid": true }),
|
|
|
|
Some(&req),
|
|
|
|
);
|
2022-09-27 16:33:37 +02:00
|
|
|
let index = index_scheduler.index(&index_uid)?;
|
|
|
|
// TODO: TAMO: Bring the index_stats in meilisearch-http
|
|
|
|
// let response = index.get_index_stats()?;
|
|
|
|
let response = todo!();
|
2021-04-29 19:31:58 +02:00
|
|
|
|
2021-07-05 14:29:20 +02:00
|
|
|
debug!("returns: {:?}", response);
|
|
|
|
Ok(HttpResponse::Ok().json(response))
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|