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

@@ -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());