2025-10-20 11:47:51 +00:00
|
|
|
import { BaseMigration } from './base-migration.ts';
|
|
|
|
import { logger } from '../logger.ts';
|
|
|
|
|
|
|
|
/**
|
2025-10-20 12:14:02 +00:00
|
|
|
* Migration from v4.1 to v4.2
|
2025-10-20 11:47:51 +00:00
|
|
|
*
|
|
|
|
* Major changes:
|
|
|
|
* 1. Moves thresholds from UPS level to action level
|
|
|
|
* 2. Creates default shutdown action for UPS devices that had thresholds
|
|
|
|
* 3. Adds empty actions array to UPS devices without actions
|
|
|
|
* 4. Adds empty actions array to groups
|
|
|
|
*
|
2025-10-20 12:14:02 +00:00
|
|
|
* Transforms v4.1 format (with UPS-level thresholds):
|
2025-10-20 11:47:51 +00:00
|
|
|
* {
|
2025-10-20 12:14:02 +00:00
|
|
|
* version: "4.1",
|
2025-10-20 11:47:51 +00:00
|
|
|
* upsDevices: [
|
|
|
|
* {
|
|
|
|
* id: "ups-1",
|
|
|
|
* name: "UPS 1",
|
|
|
|
* snmp: {...},
|
|
|
|
* thresholds: { battery: 60, runtime: 20 }, // UPS-level
|
2025-10-20 12:14:02 +00:00
|
|
|
* groups: [],
|
|
|
|
* actions: []
|
2025-10-20 11:47:51 +00:00
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
*
|
2025-10-20 12:14:02 +00:00
|
|
|
* To v4.2 format (with action-level thresholds):
|
2025-10-20 11:47:51 +00:00
|
|
|
* {
|
2025-10-20 12:14:02 +00:00
|
|
|
* version: "4.2",
|
2025-10-20 11:47:51 +00:00
|
|
|
* upsDevices: [
|
|
|
|
* {
|
|
|
|
* id: "ups-1",
|
|
|
|
* name: "UPS 1",
|
|
|
|
* snmp: {...},
|
|
|
|
* groups: [],
|
|
|
|
* actions: [ // Thresholds moved here
|
|
|
|
* {
|
|
|
|
* type: "shutdown",
|
|
|
|
* thresholds: { battery: 60, runtime: 20 },
|
2025-10-20 12:14:02 +00:00
|
|
|
* triggerMode: "onlyThresholds",
|
2025-10-20 11:47:51 +00:00
|
|
|
* shutdownDelay: 5
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
*/
|
2025-10-20 12:14:02 +00:00
|
|
|
export class MigrationV4_1ToV4_2 extends BaseMigration {
|
|
|
|
readonly fromVersion = '4.1';
|
|
|
|
readonly toVersion = '4.2';
|
2025-10-20 11:47:51 +00:00
|
|
|
|
|
|
|
async shouldRun(config: any): Promise<boolean> {
|
2025-10-20 12:14:02 +00:00
|
|
|
// Run if config is version 4.1
|
|
|
|
if (config.version === '4.1') {
|
2025-10-20 11:47:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2025-10-20 12:14:02 +00:00
|
|
|
// Also run if config has upsDevices with thresholds at UPS level (v4.1 format)
|
2025-10-20 11:47:51 +00:00
|
|
|
if (config.upsDevices && config.upsDevices.length > 0) {
|
|
|
|
const firstDevice = config.upsDevices[0];
|
2025-10-20 12:14:02 +00:00
|
|
|
// v4.1 has thresholds at UPS level, v4.2 has them in actions
|
|
|
|
return firstDevice.thresholds !== undefined;
|
2025-10-20 11:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async migrate(config: any): Promise<any> {
|
2025-10-20 12:14:02 +00:00
|
|
|
logger.info(`${this.getName()}: Migrating v4.1 config to v4.2 format...`);
|
2025-10-20 11:47:51 +00:00
|
|
|
logger.dim(` - Moving thresholds from UPS level to action level`);
|
|
|
|
logger.dim(` - Creating default shutdown actions from existing thresholds`);
|
|
|
|
|
|
|
|
// Migrate UPS devices
|
|
|
|
const migratedDevices = (config.upsDevices || []).map((device: any) => {
|
|
|
|
const migrated: any = {
|
|
|
|
id: device.id,
|
|
|
|
name: device.name,
|
|
|
|
snmp: device.snmp,
|
|
|
|
groups: device.groups || [],
|
|
|
|
};
|
|
|
|
|
|
|
|
// If device has thresholds at UPS level, convert to shutdown action
|
|
|
|
if (device.thresholds) {
|
|
|
|
migrated.actions = [
|
|
|
|
{
|
|
|
|
type: 'shutdown',
|
|
|
|
thresholds: {
|
|
|
|
battery: device.thresholds.battery,
|
|
|
|
runtime: device.thresholds.runtime,
|
|
|
|
},
|
|
|
|
triggerMode: 'onlyThresholds', // Preserve old behavior (only on threshold violation)
|
|
|
|
shutdownDelay: 5, // Default delay
|
|
|
|
},
|
|
|
|
];
|
|
|
|
logger.dim(
|
|
|
|
` → ${device.name}: Created shutdown action (battery: ${device.thresholds.battery}%, runtime: ${device.thresholds.runtime}min)`,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// No thresholds, just add empty actions array
|
|
|
|
migrated.actions = device.actions || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return migrated;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add actions to groups
|
|
|
|
const migratedGroups = (config.groups || []).map((group: any) => ({
|
|
|
|
...group,
|
|
|
|
actions: group.actions || [],
|
|
|
|
}));
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
version: this.toVersion,
|
|
|
|
upsDevices: migratedDevices,
|
|
|
|
groups: migratedGroups,
|
|
|
|
checkInterval: config.checkInterval || 30000,
|
|
|
|
};
|
|
|
|
|
|
|
|
logger.success(
|
|
|
|
`${this.getName()}: Migration complete (${migratedDevices.length} devices, ${migratedGroups.length} groups updated)`,
|
|
|
|
);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|