use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use axum::Json;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use second_brain_core::keys::{moved_aside_dirs, open_moved_aside};
use serde::Deserialize;
use super::internal_error;
use crate::AppState;
use crate::backup::{Backup, BackupError};
use crate::maintenance::{DrainRequest, RepairRequest, run_repair};
use crate::store_encryption::PLAINTEXT_COPY_LIFETIME;
#[derive(Deserialize, Default)]
pub(super) struct RepairBody {
#[serde(default)]
dry_run: bool,
}
pub(super) async fn maintenance_repair(
State(state): State<Arc<AppState>>,
body: Option<Json<RepairBody>>,
) -> impl IntoResponse {
let dry_run = body.map(|Json(b)| b.dry_run).unwrap_or(false);
let result = tokio::task::spawn_blocking(move || match &state.repair_tx {
Some(tx) => {
let (reply_tx, reply_rx) = std::sync::mpsc::channel();
tx.send(DrainRequest::Repair(RepairRequest {
dry_run,
reply: reply_tx,
}))
.map_err(|_| anyhow::anyhow!("the index drain thread is not running"))?;
reply_rx
.recv_timeout(std::time::Duration::from_secs(3600))
.map_err(|e| anyhow::anyhow!("waiting for the drain thread: {e}"))?
}
None => {
let summary = run_repair(&state.sqlite, None, state.embedder.as_deref(), dry_run)?;
if !dry_run {
let mut guard = match state.maintenance.write() {
Ok(g) => g,
Err(poisoned) => poisoned.into_inner(),
};
*guard = Some(summary.clone());
}
Ok(summary)
}
})
.await;
match result {
Ok(Ok(summary)) => Json(summary).into_response(),
Ok(Err(e)) => internal_error(e),
Err(e) => internal_error(e),
}
}
pub(super) async fn maintenance_vacuum(State(state): State<Arc<AppState>>) -> impl IntoResponse {
let result = tokio::task::spawn_blocking(move || {
crate::store_compaction::vacuum(&state.sqlite, &state.gate, &state.health)
})
.await;
match result {
Ok(Ok((before, after))) => Json(serde_json::json!({
"bytes_before": before,
"bytes_after": after,
}))
.into_response(),
Ok(Err(e)) => internal_error(e),
Err(e) => internal_error(e),
}
}
#[derive(Deserialize)]
pub(super) struct BackupBody {
dest: PathBuf,
}
pub(super) async fn maintenance_backup(
State(state): State<Arc<AppState>>,
Json(body): Json<BackupBody>,
) -> impl IntoResponse {
let result = tokio::task::spawn_blocking(move || {
Backup {
store: &state.sqlite,
data_dir: &state.data_dir,
gate: &state.gate,
drain: state.repair_tx.as_ref(),
archive: state.archive.as_deref(),
}
.write(&body.dest)
})
.await;
let (status, message) = match result {
Ok(Ok(summary)) => return Json(summary).into_response(),
Ok(Err(BackupError::BadDestination(m))) => (StatusCode::BAD_REQUEST, m),
Ok(Err(BackupError::Refused(m))) => (StatusCode::CONFLICT, m),
Ok(Err(BackupError::Failed(e))) => return internal_error(e),
Err(e) => return internal_error(e),
};
(status, Json(serde_json::json!({ "error": message }))).into_response()
}
#[derive(Deserialize, Default)]
pub(super) struct PlaintextCopyBody {
#[serde(default)]
aside: Option<String>,
}
pub(super) async fn maintenance_plaintext_copy(
State(state): State<Arc<AppState>>,
body: Option<Json<PlaintextCopyBody>>,
) -> impl IntoResponse {
let aside = body.and_then(|Json(b)| b.aside);
let dir = state.data_dir.clone();
let sqlite = state.sqlite.clone();
let named = aside.clone();
let result = tokio::task::spawn_blocking(move || match named {
None => sqlite.write_plaintext_copy(&dir).map(Some),
Some(name) => match find_moved_aside(&dir, &name)? {
Some(path) => open_moved_aside(&path)?
.write_plaintext_copy(&dir)
.map(Some),
None => Ok(None),
},
})
.await;
match result {
Ok(Ok(Some(path))) => {
let mut body = serde_json::json!({
"path": path,
"removed_after_secs": PLAINTEXT_COPY_LIFETIME.as_secs(),
});
if let Some(name) = aside {
body["aside"] = name.into();
}
Json(body).into_response()
}
Ok(Ok(None)) => (
StatusCode::NOT_FOUND,
Json(serde_json::json!({
"error": format!(
"{} is not a moved-aside data dir; /health lists them under moved_aside",
aside.unwrap_or_default()
),
})),
)
.into_response(),
Ok(Err(e)) => internal_error(e),
Err(e) => internal_error(e),
}
}
fn find_moved_aside(data_dir: &Path, name: &str) -> anyhow::Result<Option<PathBuf>> {
Ok(moved_aside_dirs(data_dir)?
.into_iter()
.map(|aside| aside.path)
.find(|path| path.file_name() == Some(OsStr::new(name))))
}
pub(super) async fn backfill_project_paths(
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
let store = state.store.clone();
let result = tokio::task::spawn_blocking(move || store.backfill_project_paths()).await;
match result {
Ok(Ok(count)) => Json(serde_json::json!({"backfilled": count})).into_response(),
Ok(Err(e)) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
}
}