51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { BaseMigration } from './base-migration.ts';
|
|
import { logger } from '../logger.ts';
|
|
|
|
/**
|
|
* Migration from v4.2 to v4.3
|
|
*
|
|
* Changes:
|
|
* 1. Adds `runtimeUnit` to SNMP configs based on existing `upsModel`
|
|
* 2. Bumps version from '4.2' to '4.3'
|
|
*/
|
|
export class MigrationV4_2ToV4_3 extends BaseMigration {
|
|
readonly fromVersion = '4.2';
|
|
readonly toVersion = '4.3';
|
|
|
|
shouldRun(config: Record<string, unknown>): boolean {
|
|
return config.version === '4.2';
|
|
}
|
|
|
|
migrate(config: Record<string, unknown>): Record<string, unknown> {
|
|
logger.info(`${this.getName()}: Adding runtimeUnit to SNMP configs...`);
|
|
|
|
const devices = (config.upsDevices as Array<Record<string, unknown>>) || [];
|
|
const migratedDevices = devices.map((device) => {
|
|
const snmp = device.snmp as Record<string, unknown> | undefined;
|
|
if (snmp && !snmp.runtimeUnit) {
|
|
const model = snmp.upsModel as string | undefined;
|
|
if (model === 'cyberpower') {
|
|
snmp.runtimeUnit = 'ticks';
|
|
} else if (model === 'eaton') {
|
|
snmp.runtimeUnit = 'seconds';
|
|
} else {
|
|
snmp.runtimeUnit = 'minutes';
|
|
}
|
|
logger.dim(` → ${device.name}: Set runtimeUnit to '${snmp.runtimeUnit}'`);
|
|
}
|
|
return device;
|
|
});
|
|
|
|
const result = {
|
|
...config,
|
|
version: this.toVersion,
|
|
upsDevices: migratedDevices,
|
|
};
|
|
|
|
logger.success(
|
|
`${this.getName()}: Migration complete (${migratedDevices.length} devices updated)`,
|
|
);
|
|
return result;
|
|
}
|
|
}
|