2023-03-20 03:26:54 -06:00
|
|
|
use axum::{extract::State, Json};
|
|
|
|
use http::StatusCode;
|
|
|
|
use tracing::instrument;
|
|
|
|
|
|
|
|
use super::{ErrorResponse, ErrorResponseStatus, RespExt};
|
|
|
|
use crate::{database::Database, models::User, router::AppState};
|
|
|
|
|
|
|
|
use atuin_common::api::*;
|
|
|
|
|
|
|
|
#[instrument(skip_all, fields(user.id = user.id))]
|
|
|
|
pub async fn status<DB: Database>(
|
|
|
|
user: User,
|
|
|
|
state: State<AppState<DB>>,
|
|
|
|
) -> Result<Json<StatusResponse>, ErrorResponseStatus<'static>> {
|
|
|
|
let db = &state.0.database;
|
|
|
|
|
2023-03-26 10:48:41 -06:00
|
|
|
let deleted = db.deleted_history(&user).await.unwrap_or(vec![]);
|
2023-03-20 03:26:54 -06:00
|
|
|
|
2023-03-26 10:48:41 -06:00
|
|
|
let count = match db.count_history_cached(&user).await {
|
|
|
|
// By default read out the cached value
|
|
|
|
Ok(count) => count,
|
2023-03-20 03:26:54 -06:00
|
|
|
|
2023-03-26 10:48:41 -06:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-03-29 23:45:49 -06:00
|
|
|
Ok(Json(StatusResponse {
|
|
|
|
count,
|
|
|
|
deleted,
|
|
|
|
username: user.username,
|
|
|
|
}))
|
2023-03-20 03:26:54 -06:00
|
|
|
}
|