Improve updates JSON format

This commit is contained in:
Clément Renault 2019-11-12 16:18:53 +01:00
parent f355280250
commit a98949ff1d
4 changed files with 43 additions and 46 deletions

View File

@ -68,7 +68,7 @@ fn update_awaiter(receiver: UpdateEvents, env: heed::Env, update_fn: Arc<ArcSwap
let status = break_try!(result, "update task failed"); let status = break_try!(result, "update task failed");
// commit the nested transaction if the update was successful, abort it otherwise // commit the nested transaction if the update was successful, abort it otherwise
if status.result.is_ok() { if status.error.is_none() {
break_try!(nested_writer.commit(), "commit nested transaction failed"); break_try!(nested_writer.commit(), "commit nested transaction failed");
} else { } else {
nested_writer.abort() nested_writer.abort()
@ -323,7 +323,7 @@ mod tests {
let reader = env.read_txn().unwrap(); let reader = env.read_txn().unwrap();
let result = index.update_status(&reader, update_id).unwrap(); let result = index.update_status(&reader, update_id).unwrap();
assert_matches!(result, UpdateStatus::Processed(status) if status.result.is_ok()); assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_none());
} }
#[test] #[test]
@ -384,11 +384,11 @@ mod tests {
let reader = env.read_txn().unwrap(); let reader = env.read_txn().unwrap();
let result = index.update_status(&reader, update_id).unwrap(); let result = index.update_status(&reader, update_id).unwrap();
assert_matches!(result, UpdateStatus::Processed(status) if status.result.is_err()); assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_some());
} }
#[test] #[test]
fn ignored_words_to_long() { fn ignored_words_too_long() {
let dir = tempfile::tempdir().unwrap(); let dir = tempfile::tempdir().unwrap();
let database = Database::open_or_create(dir.path()).unwrap(); let database = Database::open_or_create(dir.path()).unwrap();
@ -434,7 +434,7 @@ mod tests {
let reader = env.read_txn().unwrap(); let reader = env.read_txn().unwrap();
let result = index.update_status(&reader, update_id).unwrap(); let result = index.update_status(&reader, update_id).unwrap();
assert_matches!(result, UpdateStatus::Processed(status) if status.result.is_ok()); assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_none());
} }
#[test] #[test]
@ -524,7 +524,7 @@ mod tests {
// check if it has been accepted // check if it has been accepted
let reader = env.read_txn().unwrap(); let reader = env.read_txn().unwrap();
let result = index.update_status(&reader, update_id).unwrap(); let result = index.update_status(&reader, update_id).unwrap();
assert_matches!(result, UpdateStatus::Processed(status) if status.result.is_ok()); assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_none());
reader.abort(); reader.abort();
let mut additions = index.documents_addition(); let mut additions = index.documents_addition();
@ -558,7 +558,7 @@ mod tests {
// check if it has been accepted // check if it has been accepted
let reader = env.read_txn().unwrap(); let reader = env.read_txn().unwrap();
let result = index.update_status(&reader, update_id).unwrap(); let result = index.update_status(&reader, update_id).unwrap();
assert_matches!(result, UpdateStatus::Processed(status) if status.result.is_ok()); assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_none());
// even try to search for a document // even try to search for a document
let results = index.query_builder().query(&reader, "21 ", 0..20).unwrap(); let results = index.query_builder().query(&reader, "21 ", 0..20).unwrap();
@ -604,7 +604,7 @@ mod tests {
// check if it has been accepted // check if it has been accepted
let reader = env.read_txn().unwrap(); let reader = env.read_txn().unwrap();
let result = index.update_status(&reader, update_id).unwrap(); let result = index.update_status(&reader, update_id).unwrap();
assert_matches!(result, UpdateStatus::Processed(status) if status.result.is_err()); assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_some());
} }
#[test] #[test]
@ -668,7 +668,7 @@ mod tests {
let reader = env.read_txn().unwrap(); let reader = env.read_txn().unwrap();
let result = index.update_status(&reader, update_id).unwrap(); let result = index.update_status(&reader, update_id).unwrap();
assert_matches!(result, UpdateStatus::Processed(status) if status.result.is_ok()); assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_none());
let document: Option<IgnoredAny> = index.document(&reader, None, DocumentId(25)).unwrap(); let document: Option<IgnoredAny> = index.document(&reader, None, DocumentId(25)).unwrap();
assert!(document.is_none()); assert!(document.is_none());
@ -748,7 +748,7 @@ mod tests {
let reader = env.read_txn().unwrap(); let reader = env.read_txn().unwrap();
let result = index.update_status(&reader, update_id).unwrap(); let result = index.update_status(&reader, update_id).unwrap();
assert_matches!(result, UpdateStatus::Processed(status) if status.result.is_ok()); assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_none());
let document: Option<IgnoredAny> = index.document(&reader, None, DocumentId(25)).unwrap(); let document: Option<IgnoredAny> = index.document(&reader, None, DocumentId(25)).unwrap();
assert!(document.is_none()); assert!(document.is_none());
@ -791,7 +791,7 @@ mod tests {
let reader = env.read_txn().unwrap(); let reader = env.read_txn().unwrap();
let result = index.update_status(&reader, update_id).unwrap(); let result = index.update_status(&reader, update_id).unwrap();
assert_matches!(result, UpdateStatus::Processed(status) if status.result.is_ok()); assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_none());
let document: Option<serde_json::Value> = index let document: Option<serde_json::Value> = index
.document(&reader, None, DocumentId(7900334843754999545)) .document(&reader, None, DocumentId(7900334843754999545))

View File

@ -1,12 +1,12 @@
use super::BEU64; use super::BEU64;
use crate::update::ProcessedUpdateResult; use crate::update::ProcessedUpdateResult;
use heed::types::{OwnedType, SerdeBincode}; use heed::types::{OwnedType, SerdeJson};
use heed::Result as ZResult; use heed::Result as ZResult;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct UpdatesResults { pub struct UpdatesResults {
pub(crate) updates_results: pub(crate) updates_results:
heed::Database<OwnedType<BEU64>, SerdeBincode<ProcessedUpdateResult>>, heed::Database<OwnedType<BEU64>, SerdeJson<ProcessedUpdateResult>>,
} }
impl UpdatesResults { impl UpdatesResults {

View File

@ -22,7 +22,7 @@ pub use self::synonyms_deletion::{apply_synonyms_deletion, SynonymsDeletion};
use std::cmp; use std::cmp;
use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::time::{Duration, Instant}; use std::time::Instant;
use heed::Result as ZResult; use heed::Result as ZResult;
use log::debug; use log::debug;
@ -49,9 +49,7 @@ impl Update {
pub fn update_type(&self) -> UpdateType { pub fn update_type(&self) -> UpdateType {
match self { match self {
Update::ClearAll => UpdateType::ClearAll, Update::ClearAll => UpdateType::ClearAll,
Update::Schema(schema) => UpdateType::Schema { Update::Schema(_) => UpdateType::Schema,
schema: schema.clone(),
},
Update::Customs(_) => UpdateType::Customs, Update::Customs(_) => UpdateType::Customs,
Update::DocumentsAddition(addition) => UpdateType::DocumentsAddition { Update::DocumentsAddition(addition) => UpdateType::DocumentsAddition {
number: addition.len(), number: addition.len(),
@ -79,9 +77,10 @@ impl Update {
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "name")]
pub enum UpdateType { pub enum UpdateType {
ClearAll, ClearAll,
Schema { schema: Schema }, Schema,
Customs, Customs,
DocumentsAddition { number: usize }, DocumentsAddition { number: usize },
DocumentsPartial { number: usize }, DocumentsPartial { number: usize },
@ -92,17 +91,14 @@ pub enum UpdateType {
StopWordsDeletion { number: usize }, StopWordsDeletion { number: usize },
} }
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetailedDuration {
pub main: Duration,
}
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessedUpdateResult { pub struct ProcessedUpdateResult {
pub update_id: u64, pub update_id: u64,
#[serde(rename = "type")]
pub update_type: UpdateType, pub update_type: UpdateType,
pub result: Result<(), String>, #[serde(skip_serializing_if = "Option::is_none")]
pub detailed_duration: DetailedDuration, pub error: Option<String>,
pub duration: f64, // in seconds
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -112,9 +108,16 @@ pub struct EnqueuedUpdateResult {
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "status")]
pub enum UpdateStatus { pub enum UpdateStatus {
Enqueued(EnqueuedUpdateResult), Enqueued {
Processed(ProcessedUpdateResult), #[serde(flatten)]
content: EnqueuedUpdateResult
},
Processed {
#[serde(flatten)]
content: ProcessedUpdateResult
},
Unknown, Unknown,
} }
@ -125,13 +128,13 @@ pub fn update_status(
update_id: u64, update_id: u64,
) -> MResult<UpdateStatus> { ) -> MResult<UpdateStatus> {
match updates_results_store.update_result(reader, update_id)? { match updates_results_store.update_result(reader, update_id)? {
Some(result) => Ok(UpdateStatus::Processed(result)), Some(result) => Ok(UpdateStatus::Processed { content: result }),
None => { None => {
if let Some(update) = updates_store.get(reader, update_id)? { if let Some(update) = updates_store.get(reader, update_id)? {
Ok(UpdateStatus::Enqueued(EnqueuedUpdateResult { Ok(UpdateStatus::Enqueued { content: EnqueuedUpdateResult {
update_id, update_id,
update_type: update.update_type(), update_type: update.update_type(),
})) }})
} else { } else {
Ok(UpdateStatus::Unknown) Ok(UpdateStatus::Unknown)
} }
@ -183,9 +186,7 @@ pub fn update_task<'a, 'b>(
Update::Schema(schema) => { Update::Schema(schema) => {
let start = Instant::now(); let start = Instant::now();
let update_type = UpdateType::Schema { let update_type = UpdateType::Schema;
schema: schema.clone(),
};
let result = apply_schema_update( let result = apply_schema_update(
writer, writer,
&schema, &schema,
@ -323,12 +324,11 @@ pub fn update_task<'a, 'b>(
update_id, update_type, result update_id, update_type, result
); );
let detailed_duration = DetailedDuration { main: duration };
let status = ProcessedUpdateResult { let status = ProcessedUpdateResult {
update_id, update_id,
update_type, update_type,
result: result.map_err(|e| e.to_string()), error: result.map_err(|e| e.to_string()).err(),
detailed_duration, duration: duration.as_secs_f64(),
}; };
Ok(status) Ok(status)

View File

@ -150,19 +150,16 @@ pub async fn get_update_status(ctx: Context<Data>) -> SResult<Response> {
.map_err(ResponseError::internal)?; .map_err(ResponseError::internal)?;
let response = match status { let response = match status {
UpdateStatus::Enqueued(data) => { UpdateStatus::Unknown => {
tide::response::json(json!({ "status": "enqueued", "data": data })) tide::response::json(json!({ "message": "unknown update id" }))
.with_status(StatusCode::NOT_FOUND)
.into_response()
}
status => {
tide::response::json(status)
.with_status(StatusCode::OK) .with_status(StatusCode::OK)
.into_response() .into_response()
} }
UpdateStatus::Processed(data) => {
tide::response::json(json!({ "status": "processed", "data": data }))
.with_status(StatusCode::OK)
.into_response()
}
UpdateStatus::Unknown => tide::response::json(json!({ "message": "unknown update id" }))
.with_status(StatusCode::NOT_FOUND)
.into_response(),
}; };
Ok(response) Ok(response)