feat: scheduled backups, availability tracking, PoE power metrics (v0.11.2 Beta)
P1 — Scheduled Backups - BackupService.createBackup() now accepts a `type` param (manual/scheduled) - PollerService: cronMatchesNow() parser + runScheduledBackups() + Redis 1h gate - Settings page: backup schedule toggle + cron expression input with examples P2 — Uptime/Availability Tracking - DB: device_availability table (went_offline_at, came_back_online_at, duration_seconds) - PollerService: inserts outage rows on offline detection, closes them on recovery - GET /api/metrics/device/:id/availability?range=30d endpoint - /api/metrics/summary now includes fleet 30-day uptime % - DeviceDetailPage: 30-day availability card (uptime %, outage count, longest outage) - DashboardPage HealthBar: Uptime 30d mini-stat P3 — PoE Power Draw Metrics - DeviceCollector.collectPoePower() queries /interface/ethernet/poe/monitor for switches - Writes poe_power measurement to InfluxDB (tags: device_id, port; fields: watts, current_ma, voltage_v) - GET /api/metrics/device/:id/poe endpoint (current per-port + 1h history) - SwitchPortDiagram: amber watt badge on PoE-active ports - HardwareTab: PoE budget section showing active ports and total watts Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
e692a1d7cd
commit
6a4ab1fc5f
@@ -96,6 +96,8 @@ export class PollerService {
|
||||
const spectralIntervalHours = (appSettings['spectral_scan_interval_hours'] as number) || 24;
|
||||
const apScanEnabled = appSettings['ap_scan_enabled'] === true;
|
||||
const apScanIntervalHours = (appSettings['ap_scan_interval_hours'] as number) || 24;
|
||||
const backupScheduleEnabled = appSettings['backup_schedule_enabled'] === true;
|
||||
const backupScheduleCron = (appSettings['backup_schedule_cron'] as string) || '0 2 * * *';
|
||||
|
||||
const devices = await query<DeviceRow>(
|
||||
`SELECT * FROM devices WHERE status != 'disabled'`
|
||||
@@ -197,6 +199,19 @@ export class PollerService {
|
||||
console.error('[Poller] Client prune error:', e)
|
||||
);
|
||||
}
|
||||
|
||||
// Scheduled backups — fire when the cron expression matches the current minute/hour.
|
||||
// Redis key with 1-hour TTL prevents double-firing within the same cron window.
|
||||
if (backupScheduleEnabled && this.cronMatchesNow(backupScheduleCron)) {
|
||||
const backupKey = 'task:scheduled_backup';
|
||||
const lastBackup = await this.getTimestamp(backupKey);
|
||||
if (now - lastBackup > 3_600_000) {
|
||||
await this.setTimestamp(backupKey, now);
|
||||
this.runScheduledBackups().catch((e) =>
|
||||
console.error('[Poller] Scheduled backup error:', e)
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Scheduler error:', err);
|
||||
}
|
||||
@@ -209,7 +224,8 @@ export class PollerService {
|
||||
WHERE key IN ('mac_scan_enabled', 'mac_scan_interval', 'reverse_dns_enabled',
|
||||
'retention_clients_days', 'spectral_scan_enabled',
|
||||
'spectral_scan_interval_hours', 'ap_scan_enabled',
|
||||
'ap_scan_interval_hours')`
|
||||
'ap_scan_interval_hours', 'backup_schedule_enabled',
|
||||
'backup_schedule_cron')`
|
||||
);
|
||||
const map: Record<string, unknown> = {};
|
||||
for (const row of rows) map[row.key] = row.value;
|
||||
@@ -219,6 +235,49 @@ export class PollerService {
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if the 5-part cron expression matches the current minute/hour.
|
||||
// Supports: *, exact numbers, comma lists, ranges (a-b), and step values (*/n).
|
||||
private cronMatchesNow(cron: string): boolean {
|
||||
const parts = cron.trim().split(/\s+/);
|
||||
if (parts.length < 5) return false;
|
||||
const [minuteField, hourField] = parts;
|
||||
const now = new Date();
|
||||
const matchField = (field: string, val: number): boolean => {
|
||||
if (field === '*') return true;
|
||||
return field.split(',').some((f) => {
|
||||
if (f.includes('/')) {
|
||||
const [base, step] = f.split('/');
|
||||
const start = base === '*' ? 0 : Number(base);
|
||||
return val >= start && (val - start) % Number(step) === 0;
|
||||
}
|
||||
if (f.includes('-')) {
|
||||
const [lo, hi] = f.split('-').map(Number);
|
||||
return val >= lo && val <= hi;
|
||||
}
|
||||
return Number(f) === val;
|
||||
});
|
||||
};
|
||||
return matchField(minuteField, now.getMinutes()) && matchField(hourField, now.getHours());
|
||||
}
|
||||
|
||||
private async runScheduledBackups(): Promise<void> {
|
||||
const { BackupService } = await import('./BackupService');
|
||||
const backupService = new BackupService();
|
||||
const devices = await query<{
|
||||
id: number; name: string; ip_address: string; ssh_port: number;
|
||||
ssh_username: string; ssh_password_encrypted: string;
|
||||
api_username: string; api_password_encrypted: string;
|
||||
}>(`SELECT id, name, ip_address, ssh_port, ssh_username, ssh_password_encrypted,
|
||||
api_username, api_password_encrypted
|
||||
FROM devices WHERE status = 'online'`);
|
||||
console.log(`[Poller] Starting scheduled backup for ${devices.length} online device(s)`);
|
||||
const results = await Promise.allSettled(
|
||||
devices.map((d) => backupService.createBackup(d, 'Scheduled backup', 'scheduled'))
|
||||
);
|
||||
const succeeded = results.filter((r) => r.status === 'fulfilled').length;
|
||||
console.log(`[Poller] Scheduled backup complete: ${succeeded}/${devices.length} succeeded`);
|
||||
}
|
||||
|
||||
private async resolveClientHostnames(): Promise<void> {
|
||||
const { reverse } = await import('dns/promises');
|
||||
|
||||
@@ -376,6 +435,16 @@ export class PollerService {
|
||||
deviceId: device.id,
|
||||
deviceName: device.name,
|
||||
}).catch(() => {});
|
||||
// Close the open outage row if one exists
|
||||
if (prevStatus === 'offline') {
|
||||
query(
|
||||
`UPDATE device_availability
|
||||
SET came_back_online_at = NOW(),
|
||||
duration_seconds = EXTRACT(EPOCH FROM (NOW() - went_offline_at))::INTEGER
|
||||
WHERE device_id = $1 AND came_back_online_at IS NULL`,
|
||||
[device.id]
|
||||
).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
this.io?.emit('device:updated', { deviceId: device.id });
|
||||
@@ -620,6 +689,11 @@ export class PollerService {
|
||||
deviceId,
|
||||
deviceName: device?.name,
|
||||
}).catch(() => {});
|
||||
// Open a new availability outage row
|
||||
query(
|
||||
`INSERT INTO device_availability (device_id, went_offline_at) VALUES ($1, NOW())`,
|
||||
[deviceId]
|
||||
).catch(() => {});
|
||||
}
|
||||
// Mark all clients for this device inactive — updateClients() never ran because connect() failed.
|
||||
await query(`UPDATE clients SET active = FALSE WHERE device_id = $1`, [deviceId]);
|
||||
|
||||
Reference in New Issue
Block a user