feat: list recent restore jobs across all snapshots

Add GET /api/v1/restore-jobs?limit= (admin only, newest first, default 50,
at most 200) so the activity log can show restores without asking snapshot by
snapshot. The existing history test now also covers ordering and the limit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 12:23:22 +07:00
parent 8b6efe1eaa
commit 34b2f346af
3 changed files with 35 additions and 3 deletions

View File

@@ -5,7 +5,7 @@ use super::{
use crate::state::HubState; use crate::state::HubState;
use axum::{ use axum::{
body::Body, body::Body,
extract::{rejection::JsonRejection, ConnectInfo, Path, State}, extract::{rejection::JsonRejection, ConnectInfo, Path, Query, State},
http::{header, HeaderMap, StatusCode}, http::{header, HeaderMap, StatusCode},
response::{IntoResponse, Response}, response::{IntoResponse, Response},
routing::{get, post}, routing::{get, post},
@@ -29,7 +29,7 @@ pub(super) fn routes() -> Router<HubState> {
.route("/api/v1/restore-targets", post(register)) .route("/api/v1/restore-targets", post(register))
.route("/api/v1/capture-profiles/:id/restore-targets", get(targets)) .route("/api/v1/capture-profiles/:id/restore-targets", get(targets))
.route("/api/v1/snapshots/:id/restore-targets", get(snapshot_targets)) .route("/api/v1/snapshots/:id/restore-targets", get(snapshot_targets))
.route("/api/v1/restore-jobs", post(enqueue)) .route("/api/v1/restore-jobs", post(enqueue).get(recent))
.route("/api/v1/snapshots/:id/restore-jobs", get(history)) .route("/api/v1/snapshots/:id/restore-jobs", get(history))
.route("/api/v1/restore-jobs/:id", get(job).delete(cancel)) .route("/api/v1/restore-jobs/:id", get(job).delete(cancel))
.route("/api/v1/restore-jobs/:id/reconcile", post(reconcile)) .route("/api/v1/restore-jobs/:id/reconcile", post(reconcile))
@@ -222,6 +222,26 @@ async fn history(
.map_err(internal) .map_err(internal)
} }
#[derive(Deserialize)]
struct Recent {
limit: Option<u32>,
}
/// The newest restore jobs across all snapshots. The activity log reads this instead of asking snapshot by snapshot.
async fn recent(
ConnectInfo(peer): ConnectInfo<SocketAddr>,
State(state): State<HubState>,
headers: HeaderMap,
Query(query): Query<Recent>,
) -> Result<Json<Vec<RestoreJob>>, StatusCode> {
require_admin(&state, peer, &headers)?;
state
.db
.restore_recent(query.limit.unwrap_or(50).clamp(1, 200), now())
.map(Json)
.map_err(internal)
}
async fn cancel( async fn cancel(
ConnectInfo(peer): ConnectInfo<SocketAddr>, ConnectInfo(peer): ConnectInfo<SocketAddr>,
State(state): State<HubState>, State(state): State<HubState>,

View File

@@ -181,6 +181,15 @@ impl Db {
Ok(rows.collect::<rusqlite::Result<_>>()?) Ok(rows.collect::<rusqlite::Result<_>>()?)
} }
/// The newest restore jobs across every snapshot, newest first, for the activity log.
pub fn restore_recent(&self, limit: u32, now: i64) -> Result<Vec<RestoreJob>> {
let conn = self.conn.lock().unwrap();
expire_destructive(&conn, now)?;
let mut stmt = conn.prepare(&format!("{VIEW} ORDER BY j.rowid DESC LIMIT ?1"))?;
let rows = stmt.query_map([limit], read_job)?;
Ok(rows.collect::<rusqlite::Result<_>>()?)
}
pub fn snapshot_in_restore(&self, id: Uuid) -> Result<bool> { pub fn snapshot_in_restore(&self, id: Uuid) -> Result<bool> {
let conn = self.conn.lock().unwrap(); let conn = self.conn.lock().unwrap();
Ok(conn.query_row( Ok(conn.query_row(
@@ -544,6 +553,9 @@ mod tests {
.unwrap()); .unwrap());
let interrupted = db.restore_job(job.id, late).unwrap().unwrap(); let interrupted = db.restore_job(job.id, late).unwrap().unwrap();
assert_eq!(interrupted.status, "recovery_required"); assert_eq!(interrupted.status, "recovery_required");
let recent = db.restore_recent(10, late).unwrap();
assert_eq!(recent.iter().map(|item| item.id).collect::<Vec<_>>(), vec![next.id, job.id]);
assert_eq!(db.restore_recent(1, late).unwrap().len(), 1);
assert_eq!(interrupted.safety_snapshot_id, Some(safety)); assert_eq!(interrupted.safety_snapshot_id, Some(safety));
assert!(db.snapshot_in_restore(snapshot).unwrap()); assert!(db.snapshot_in_restore(snapshot).unwrap());
assert!(db.snapshot_in_restore(safety).unwrap()); assert!(db.snapshot_in_restore(safety).unwrap());

View File

@@ -135,7 +135,7 @@ Use `{"reset":"recreate_database"}` to recreate the registered PostgreSQL databa
A MySQL target uses `"engine":"mysql"` and the same `connection` fields as a direct or SSH MySQL source. `recreate_database` drops and recreates the registered database before applying SQL. `use_target_definer` rewrites supported dump DEFINER clauses to the connected MySQL user. A MySQL target uses `"engine":"mysql"` and the same `connection` fields as a direct or SSH MySQL source. `recreate_database` drops and recreates the registered database before applying SQL. `use_target_definer` rewrites supported dump DEFINER clauses to the connected MySQL user.
Choose the snapshot and target in **Restore**. The required confirmation includes both UUIDs. The hub verifies the safety archive before issuing the single-use apply permit. If the job reports `recovery_required`, stop any old apply process and verify its database session has ended. Recover the target with the linked safety archive, verify data and permissions, then confirm reconciliation to release the lock. Session-authenticated file exports use a filename in the hub's private `exports` directory. Open the snapshot and choose its recovery options, then pick the target in **Restore**. **Activity** lists recent restore jobs across all snapshots (`GET /api/v1/restore-jobs?limit=`, newest first, at most 200) and flags any that need reconciliation. The required confirmation includes both UUIDs. The hub verifies the safety archive before issuing the single-use apply permit. If the job reports `recovery_required`, stop any old apply process and verify its database session has ended. Recover the target with the linked safety archive, verify data and permissions, then confirm reconciliation to release the lock. Session-authenticated file exports use a filename in the hub's private `exports` directory.
## Import legacy archives ## Import legacy archives