From e8cc7f9cee818ecc18fff3c65f7b7566fb75a836 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Wed, 17 Mar 2021 14:32:00 +0100 Subject: [PATCH] Expose a route in the http-ui to update the WordsLevelPositions --- http-ui/src/main.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/http-ui/src/main.rs b/http-ui/src/main.rs index b091985f3..dbf7aadce 100644 --- a/http-ui/src/main.rs +++ b/http-ui/src/main.rs @@ -229,6 +229,7 @@ enum UpdateMeta { Settings(Settings), Facets(Facets), WordsPrefixes(WordsPrefixes), + WordsLevelPositions(WordsLevelPositions), } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -281,6 +282,22 @@ struct WordsPrefixes { max_prefix_length: Option, } +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +#[serde(rename_all = "camelCase")] +struct WordsLevelPositions { + level_group_size: Option, + min_level_size: Option, +} + +// Any value that is present is considered Some value, including null. +fn deserialize_some<'de, T, D>(deserializer: D) -> Result, D::Error> +where T: Deserialize<'de>, + D: Deserializer<'de> +{ + Deserialize::deserialize(deserializer).map(Some) +} + #[tokio::main] async fn main() -> anyhow::Result<()> { let opt = Opt::from_args(); @@ -493,6 +510,21 @@ async fn main() -> anyhow::Result<()> { Ok(()) => wtxn.commit().map_err(Into::into), Err(e) => Err(e) } + }, + UpdateMeta::WordsLevelPositions(levels) => { + // We must use the write transaction of the update here. + let mut wtxn = index_cloned.write_txn()?; + let mut builder = update_builder.words_level_positions(&mut wtxn, &index_cloned); + if let Some(value) = levels.level_group_size { + builder.level_group_size(value); + } + if let Some(value) = levels.min_level_size { + builder.min_level_size(value); + } + match builder.execute() { + Ok(()) => wtxn.commit().map_err(Into::into), + Err(e) => Err(e.into()) + } } }; @@ -923,6 +955,19 @@ async fn main() -> anyhow::Result<()> { warp::reply() }); + let update_store_cloned = update_store.clone(); + let update_status_sender_cloned = update_status_sender.clone(); + let change_words_level_positions_route = warp::filters::method::post() + .and(warp::path!("words-level-positions")) + .and(warp::body::json()) + .map(move |levels: WordsLevelPositions| { + let meta = UpdateMeta::WordsLevelPositions(levels); + let update_id = update_store_cloned.register_update(&meta, &[]).unwrap(); + let _ = update_status_sender_cloned.send(UpdateStatus::Pending { update_id, meta }); + eprintln!("update {} registered", update_id); + warp::reply() + }); + let update_store_cloned = update_store.clone(); let update_status_sender_cloned = update_status_sender.clone(); let abort_update_id_route = warp::filters::method::delete() @@ -998,6 +1043,7 @@ async fn main() -> anyhow::Result<()> { .or(change_settings_route) .or(change_facet_levels_route) .or(change_words_prefixes_route) + .or(change_words_level_positions_route) .or(update_ws_route); let addr = SocketAddr::from_str(&opt.http_listen_addr)?;