2022-09-27 22:33:37 +08:00
|
|
|
use actix_web::web::Data;
|
2021-10-14 17:32:55 +08:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
2022-09-27 22:33:37 +08:00
|
|
|
use index_scheduler::IndexScheduler;
|
2021-06-29 21:25:18 +08:00
|
|
|
use log::debug;
|
2022-10-13 21:02:59 +08:00
|
|
|
use meilisearch_auth::AuthController;
|
2022-06-06 18:38:46 +08:00
|
|
|
use meilisearch_types::error::ResponseError;
|
2022-10-12 09:21:25 +08:00
|
|
|
use meilisearch_types::tasks::KindWithContent;
|
2021-10-14 17:32:55 +08:00
|
|
|
use serde_json::json;
|
2022-10-13 21:02:59 +08:00
|
|
|
use time::macros::format_description;
|
|
|
|
use time::OffsetDateTime;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-10-14 17:32:55 +08:00
|
|
|
use crate::analytics::Analytics;
|
2022-10-21 00:00:07 +08:00
|
|
|
use crate::extractors::authentication::policies::*;
|
|
|
|
use crate::extractors::authentication::GuardedData;
|
2022-03-05 03:12:44 +08:00
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
2022-10-13 21:02:59 +08:00
|
|
|
use crate::routes::SummarizedTaskView;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-07-05 20:29:20 +08:00
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
2022-05-20 02:19:34 +08:00
|
|
|
cfg.service(web::resource("").route(web::post().to(SeqHandler(create_dump))));
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-29 04:22:59 +08:00
|
|
|
pub async fn create_dump(
|
2022-09-27 22:33:37 +08:00
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::DUMPS_CREATE }>, Data<IndexScheduler>>,
|
2022-10-13 21:02:59 +08:00
|
|
|
auth_controller: GuardedData<ActionPolicy<{ actions::DUMPS_CREATE }>, AuthController>,
|
2021-10-14 17:32:55 +08:00
|
|
|
req: HttpRequest,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2021-09-29 04:22:59 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-10-14 17:32:55 +08:00
|
|
|
analytics.publish("Dump Created".to_string(), json!({}), Some(&req));
|
|
|
|
|
2022-10-13 21:02:59 +08:00
|
|
|
let dump_uid = OffsetDateTime::now_utc()
|
|
|
|
.format(format_description!(
|
|
|
|
"[year repr:full][month repr:numerical][day padding:zero]-[hour padding:zero][minute padding:zero][second padding:zero][subsecond digits:3]"
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
|
2022-09-22 18:14:51 +08:00
|
|
|
let task = KindWithContent::DumpExport {
|
2022-10-13 21:02:59 +08:00
|
|
|
keys: auth_controller.list_keys()?,
|
|
|
|
instance_uid: analytics.instance_uid().cloned(),
|
|
|
|
dump_uid,
|
2022-09-22 18:14:51 +08:00
|
|
|
};
|
2022-10-13 21:02:59 +08:00
|
|
|
let task: SummarizedTaskView =
|
2022-10-21 00:00:07 +08:00
|
|
|
tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??.into();
|
2021-05-05 20:11:56 +08:00
|
|
|
|
2022-10-13 21:02:59 +08:00
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|