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:
@@ -5,7 +5,7 @@ use super::{
|
||||
use crate::state::HubState;
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{rejection::JsonRejection, ConnectInfo, Path, State},
|
||||
extract::{rejection::JsonRejection, ConnectInfo, Path, Query, State},
|
||||
http::{header, HeaderMap, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
routing::{get, post},
|
||||
@@ -29,7 +29,7 @@ pub(super) fn routes() -> Router<HubState> {
|
||||
.route("/api/v1/restore-targets", post(register))
|
||||
.route("/api/v1/capture-profiles/:id/restore-targets", get(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/restore-jobs/:id", get(job).delete(cancel))
|
||||
.route("/api/v1/restore-jobs/:id/reconcile", post(reconcile))
|
||||
@@ -222,6 +222,26 @@ async fn history(
|
||||
.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(
|
||||
ConnectInfo(peer): ConnectInfo<SocketAddr>,
|
||||
State(state): State<HubState>,
|
||||
|
||||
@@ -181,6 +181,15 @@ impl Db {
|
||||
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> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
Ok(conn.query_row(
|
||||
@@ -544,6 +553,9 @@ mod tests {
|
||||
.unwrap());
|
||||
let interrupted = db.restore_job(job.id, late).unwrap().unwrap();
|
||||
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!(db.snapshot_in_restore(snapshot).unwrap());
|
||||
assert!(db.snapshot_in_restore(safety).unwrap());
|
||||
|
||||
Reference in New Issue
Block a user