feat: config history (diff/rollback) + friendly backup scheduling (v0.12.3-beta)
Configuration History: - Per-device snapshots from the full RouterOS /export (.rsc) — config-only, so counters/operational state no longer cause phantom changes; deduped by content hash - New Config History tab: snapshot timeline, readable line diff, on-demand Capture (with honest 'no changes' feedback), one-click rollback via the linked .rsc backup - Snapshot and its backup are one artifact: badged 'Config Snapshot' on the Backups page, FK ON DELETE CASCADE keeps them in lockstep; delete is gated by a warning modal - New config_drift alert event (off by default); periodic snapshot poll with configurable cadence/retention (config_snapshot_interval_min/_retention) - New endpoints under /api/config-history (list/get/diff/capture/rollback/delete) Scheduled Backups: - Replaced the raw cron textbox in Settings with Frequency (Daily/Weekly/Monthly) + time + day pickers; cron is generated/parsed behind the scenes - Fixed cronMatchesNow to evaluate day-of-month/month/day-of-week (it previously only checked minute/hour, so weekly/monthly schedules ran every day) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
4ff5ed805c
commit
367034c2d6
@@ -0,0 +1,75 @@
|
||||
// Friendly <-> cron translation for the Scheduled Backups setting. The backend
|
||||
// still consumes a 5-field cron string (backup_schedule_cron); this keeps the UI
|
||||
// to simple Frequency / time / day choices and hides cron from the user.
|
||||
|
||||
export type BackupFrequency = 'daily' | 'weekly' | 'monthly';
|
||||
|
||||
export interface BackupSchedule {
|
||||
frequency: BackupFrequency;
|
||||
hour: number; // 0-23
|
||||
minute: number; // 0-59
|
||||
weekday: number; // 0=Sun .. 6=Sat (used when weekly)
|
||||
dayOfMonth: number; // 1-28 (used when monthly)
|
||||
}
|
||||
|
||||
export const WEEKDAY_NAMES = [
|
||||
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
|
||||
];
|
||||
|
||||
const DEFAULT_SCHEDULE: BackupSchedule = {
|
||||
frequency: 'daily',
|
||||
hour: 2,
|
||||
minute: 0,
|
||||
weekday: 0,
|
||||
dayOfMonth: 1,
|
||||
};
|
||||
|
||||
function intOr(field: string, fallback: number): number {
|
||||
return /^\d+$/.test(field) ? parseInt(field, 10) : fallback;
|
||||
}
|
||||
|
||||
/** Parse a stored cron string into friendly schedule fields (best effort). */
|
||||
export function parseBackupCron(cron: string | undefined | null): BackupSchedule {
|
||||
if (!cron) return { ...DEFAULT_SCHEDULE };
|
||||
const parts = cron.trim().split(/\s+/);
|
||||
if (parts.length < 5) return { ...DEFAULT_SCHEDULE };
|
||||
|
||||
const [m, h, dom, , dow] = parts;
|
||||
const minute = intOr(m, DEFAULT_SCHEDULE.minute);
|
||||
const hour = intOr(h, DEFAULT_SCHEDULE.hour);
|
||||
|
||||
if (dow !== '*' && /^\d+$/.test(dow)) {
|
||||
return { ...DEFAULT_SCHEDULE, frequency: 'weekly', hour, minute, weekday: parseInt(dow, 10) % 7 };
|
||||
}
|
||||
if (dom !== '*' && /^\d+$/.test(dom)) {
|
||||
return { ...DEFAULT_SCHEDULE, frequency: 'monthly', hour, minute, dayOfMonth: parseInt(dom, 10) };
|
||||
}
|
||||
return { ...DEFAULT_SCHEDULE, frequency: 'daily', hour, minute };
|
||||
}
|
||||
|
||||
/** Build a 5-field cron string from friendly schedule fields. */
|
||||
export function scheduleToCron(s: BackupSchedule): string {
|
||||
if (s.frequency === 'weekly') return `${s.minute} ${s.hour} * * ${s.weekday}`;
|
||||
if (s.frequency === 'monthly') return `${s.minute} ${s.hour} ${s.dayOfMonth} * *`;
|
||||
return `${s.minute} ${s.hour} * * *`;
|
||||
}
|
||||
|
||||
export function ordinal(n: number): string {
|
||||
const s = ['th', 'st', 'nd', 'rd'];
|
||||
const v = n % 100;
|
||||
return n + (s[(v - 20) % 10] || s[v] || s[0]);
|
||||
}
|
||||
|
||||
export function formatTime(hour: number, minute: number): string {
|
||||
const ampm = hour < 12 ? 'AM' : 'PM';
|
||||
const hr = hour % 12 === 0 ? 12 : hour % 12;
|
||||
return `${hr}:${String(minute).padStart(2, '0')} ${ampm}`;
|
||||
}
|
||||
|
||||
/** Human description, e.g. "Every Sunday at 3:00 AM". */
|
||||
export function describeBackupSchedule(s: BackupSchedule): string {
|
||||
const t = formatTime(s.hour, s.minute);
|
||||
if (s.frequency === 'weekly') return `Every ${WEEKDAY_NAMES[s.weekday]} at ${t}`;
|
||||
if (s.frequency === 'monthly') return `Monthly on the ${ordinal(s.dayOfMonth)} at ${t}`;
|
||||
return `Every day at ${t}`;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// Minimal, dependency-free unified line diff (LCS based). Config snapshots are
|
||||
// modest in size (hundreds of lines), so the O(n*m) table is fine here.
|
||||
|
||||
export type DiffRowType = 'add' | 'del' | 'ctx';
|
||||
export interface DiffRow {
|
||||
type: DiffRowType;
|
||||
text: string;
|
||||
}
|
||||
|
||||
export function lineDiff(oldText: string, newText: string): DiffRow[] {
|
||||
const a = oldText.split('\n');
|
||||
const b = newText.split('\n');
|
||||
const n = a.length;
|
||||
const m = b.length;
|
||||
|
||||
// dp[i][j] = length of LCS of a[i..] and b[j..]
|
||||
const dp: number[][] = Array.from({ length: n + 1 }, () => new Array<number>(m + 1).fill(0));
|
||||
for (let i = n - 1; i >= 0; i--) {
|
||||
for (let j = m - 1; j >= 0; j--) {
|
||||
dp[i][j] = a[i] === b[j] ? dp[i + 1][j + 1] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
const rows: DiffRow[] = [];
|
||||
let i = 0;
|
||||
let j = 0;
|
||||
while (i < n && j < m) {
|
||||
if (a[i] === b[j]) {
|
||||
rows.push({ type: 'ctx', text: a[i] });
|
||||
i++;
|
||||
j++;
|
||||
} else if (dp[i + 1][j] >= dp[i][j + 1]) {
|
||||
rows.push({ type: 'del', text: a[i] });
|
||||
i++;
|
||||
} else {
|
||||
rows.push({ type: 'add', text: b[j] });
|
||||
j++;
|
||||
}
|
||||
}
|
||||
while (i < n) rows.push({ type: 'del', text: a[i++] });
|
||||
while (j < m) rows.push({ type: 'add', text: b[j++] });
|
||||
return rows;
|
||||
}
|
||||
Reference in New Issue
Block a user