diff options
| author | Ellie Huxtable <ellie@elliehuxtable.com> | 2023-03-26 17:48:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-26 17:48:41 +0100 |
| commit | c30b457fc507171d55d7179c049ba5e3ad8f6b2f (patch) | |
| tree | 27d90a1137a68eace1ea4115c2475a7cbdfbe991 /atuin-server/src | |
| parent | Bind keys in vi mode too (#811) (diff) | |
| download | atuin-c30b457fc507171d55d7179c049ba5e3ad8f6b2f.zip | |
Account for user not yet having count cache (#812)
* Account for user not yet having count cache
* Make clippy happy
Diffstat (limited to 'atuin-server/src')
| -rw-r--r-- | atuin-server/src/handlers/status.rs | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/atuin-server/src/handlers/status.rs b/atuin-server/src/handlers/status.rs index 9c7ef779..090d2c3d 100644 --- a/atuin-server/src/handlers/status.rs +++ b/atuin-server/src/handlers/status.rs @@ -14,16 +14,22 @@ pub async fn status<DB: Database>( ) -> Result<Json<StatusResponse>, 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 })) } |
