2022-10-24 14:49:39 +02:00
|
|
|
use actix_web::web::Data;
|
2022-11-28 16:27:41 +01:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
2023-02-14 13:12:42 +01:00
|
|
|
use deserr::actix_web::AwebJson;
|
2023-02-13 18:45:13 +01:00
|
|
|
use deserr::Deserr;
|
2022-10-24 14:49:39 +02:00
|
|
|
use index_scheduler::IndexScheduler;
|
2023-01-16 16:59:26 +01:00
|
|
|
use meilisearch_types::deserr::DeserrJsonError;
|
2023-01-11 12:33:56 +01:00
|
|
|
use meilisearch_types::error::deserr_codes::InvalidSwapIndexes;
|
2023-01-16 16:59:26 +01:00
|
|
|
use meilisearch_types::error::ResponseError;
|
2023-01-17 13:51:07 +01:00
|
|
|
use meilisearch_types::index_uid::IndexUid;
|
2022-10-26 12:57:29 +02:00
|
|
|
use meilisearch_types::tasks::{IndexSwap, KindWithContent};
|
2024-10-16 17:16:33 +02:00
|
|
|
use serde::Serialize;
|
2022-10-24 14:49:39 +02:00
|
|
|
|
2024-02-21 11:21:26 +01:00
|
|
|
use super::{get_task_id, is_dry_run, SummarizedTaskView};
|
2024-10-16 17:16:33 +02:00
|
|
|
use crate::analytics::{Aggregate, Analytics};
|
2022-10-24 17:51:30 +02:00
|
|
|
use crate::error::MeilisearchHttpError;
|
2022-10-26 13:43:57 +02:00
|
|
|
use crate::extractors::authentication::policies::*;
|
|
|
|
use crate::extractors::authentication::{AuthenticationError, GuardedData};
|
2022-10-24 14:49:39 +02:00
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
2024-02-20 11:24:44 +01:00
|
|
|
use crate::Opt;
|
2022-10-24 14:49:39 +02:00
|
|
|
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(web::resource("").route(web::post().to(SeqHandler(swap_indexes))));
|
|
|
|
}
|
2022-12-14 13:00:43 +01:00
|
|
|
|
2023-02-13 18:45:13 +01:00
|
|
|
#[derive(Deserr, Debug, Clone, PartialEq, Eq)]
|
2023-01-12 13:55:53 +01:00
|
|
|
#[deserr(error = DeserrJsonError, rename_all = camelCase, deny_unknown_fields)]
|
2022-10-24 14:49:39 +02:00
|
|
|
pub struct SwapIndexesPayload {
|
2023-01-16 16:59:26 +01:00
|
|
|
#[deserr(error = DeserrJsonError<InvalidSwapIndexes>, missing_field_error = DeserrJsonError::missing_swap_indexes)]
|
2023-01-17 13:51:07 +01:00
|
|
|
indexes: Vec<IndexUid>,
|
2022-10-24 14:49:39 +02:00
|
|
|
}
|
|
|
|
|
2024-10-16 17:16:33 +02:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct IndexSwappedAnalytics {
|
|
|
|
swap_operation_number: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Aggregate for IndexSwappedAnalytics {
|
|
|
|
fn event_name(&self) -> &'static str {
|
|
|
|
"Indexes Swapped"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn aggregate(self, other: Self) -> Self {
|
|
|
|
Self { swap_operation_number: self.swap_operation_number.max(other.swap_operation_number) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_event(self) -> impl Serialize {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 14:49:39 +02:00
|
|
|
pub async fn swap_indexes(
|
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_SWAP }>, Data<IndexScheduler>>,
|
2023-02-14 13:12:42 +01:00
|
|
|
params: AwebJson<Vec<SwapIndexesPayload>, DeserrJsonError>,
|
2022-11-28 16:27:41 +01:00
|
|
|
req: HttpRequest,
|
2024-02-20 11:24:44 +01:00
|
|
|
opt: web::Data<Opt>,
|
2024-10-16 17:16:33 +02:00
|
|
|
analytics: web::Data<Analytics>,
|
2022-10-24 14:49:39 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-12-14 13:00:43 +01:00
|
|
|
let params = params.into_inner();
|
2024-10-16 17:16:33 +02:00
|
|
|
analytics.publish(IndexSwappedAnalytics { swap_operation_number: params.len() }, &req);
|
2023-02-20 09:25:29 +01:00
|
|
|
let filters = index_scheduler.filters();
|
2022-10-24 14:49:39 +02:00
|
|
|
|
|
|
|
let mut swaps = vec![];
|
2022-12-14 13:00:43 +01:00
|
|
|
for SwapIndexesPayload { indexes } in params.into_iter() {
|
2023-01-12 13:55:53 +01:00
|
|
|
// TODO: switch to deserr
|
2022-10-26 12:57:29 +02:00
|
|
|
let (lhs, rhs) = match indexes.as_slice() {
|
|
|
|
[lhs, rhs] => (lhs, rhs),
|
|
|
|
_ => {
|
|
|
|
return Err(MeilisearchHttpError::SwapIndexPayloadWrongLength(indexes).into());
|
|
|
|
}
|
|
|
|
};
|
2023-02-20 09:25:29 +01:00
|
|
|
if !filters.is_index_authorized(lhs) || !filters.is_index_authorized(rhs) {
|
2022-10-27 09:41:32 +02:00
|
|
|
return Err(AuthenticationError::InvalidToken.into());
|
2022-10-24 14:49:39 +02:00
|
|
|
}
|
2023-01-17 13:51:07 +01:00
|
|
|
swaps.push(IndexSwap { indexes: (lhs.to_string(), rhs.to_string()) });
|
2022-10-26 13:30:37 +02:00
|
|
|
}
|
2022-10-24 14:49:39 +02:00
|
|
|
|
|
|
|
let task = KindWithContent::IndexSwap { swaps };
|
2024-02-20 11:24:44 +01:00
|
|
|
let uid = get_task_id(&req, &opt)?;
|
2024-02-21 11:21:26 +01:00
|
|
|
let dry_run = is_dry_run(&req, &opt)?;
|
2023-09-07 11:37:02 +02:00
|
|
|
let task: SummarizedTaskView =
|
2024-02-21 11:21:26 +01:00
|
|
|
tokio::task::spawn_blocking(move || index_scheduler.register(task, uid, dry_run))
|
|
|
|
.await??
|
|
|
|
.into();
|
2022-10-27 00:56:34 +02:00
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2022-10-24 14:49:39 +02:00
|
|
|
}
|