From c30b457fc507171d55d7179c049ba5e3ad8f6b2f Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Sun, 26 Mar 2023 17:48:41 +0100 Subject: [PATCH] Account for user not yet having count cache (#812) * Account for user not yet having count cache * Make clippy happy --- CONTRIBUTING.md | 3 +++ atuin-server/src/handlers/status.rs | 26 ++++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..abb7031 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,3 @@ +# Contributing + +Firstly, thank you so much for considering contributing to Atuin! diff --git a/atuin-server/src/handlers/status.rs b/atuin-server/src/handlers/status.rs index 9c7ef77..090d2c3 100644 --- a/atuin-server/src/handlers/status.rs +++ b/atuin-server/src/handlers/status.rs @@ -14,16 +14,22 @@ pub async fn status( ) -> Result, ErrorResponseStatus<'static>> { let db = &state.0.database; - let history_count = db.count_history_cached(&user).await; - let deleted = db.deleted_history(&user).await; + let deleted = db.deleted_history(&user).await.unwrap_or(vec![]); - if history_count.is_err() || deleted.is_err() { - return Err(ErrorResponse::reply("failed to query history count") - .with_status(StatusCode::INTERNAL_SERVER_ERROR)); - } + let count = match db.count_history_cached(&user).await { + // By default read out the cached value + Ok(count) => count, - Ok(Json(StatusResponse { - count: history_count.unwrap(), - deleted: deleted.unwrap(), - })) + // If that fails, fallback on a full COUNT. Cache is built on a POST + // only + Err(_) => match db.count_history(&user).await { + Ok(count) => count, + Err(_) => { + return Err(ErrorResponse::reply("failed to query history count") + .with_status(StatusCode::INTERNAL_SERVER_ERROR)) + } + }, + }; + + Ok(Json(StatusResponse { count, deleted })) }