From afd026d08c252b263ca7f05d2449527a978eb81e Mon Sep 17 00:00:00 2001 From: Juergen Kunz Date: Mon, 20 Oct 2025 12:03:14 +0000 Subject: [PATCH] refactor(cli, ups-handler, daemon, migrations): remove thresholds handling and update migration order logic --- ts/cli.ts | 12 ++++----- ts/cli/ups-handler.ts | 14 ++--------- ts/daemon.ts | 20 +-------------- ts/migrations/base-migration.ts | 33 +++++++++++++++++-------- ts/migrations/migration-runner.ts | 4 +-- ts/migrations/migration-v1-to-v2.ts | 1 - ts/migrations/migration-v3-to-v4.ts | 1 - ts/migrations/migration-v4.0-to-v4.1.ts | 1 - 8 files changed, 33 insertions(+), 53 deletions(-) diff --git a/ts/cli.ts b/ts/cli.ts index b9c0b22..a1e8634 100644 --- a/ts/cli.ts +++ b/ts/cli.ts @@ -335,7 +335,7 @@ export class NupstCli { id: theme.dim(ups.id), host: `${ups.snmp.host}:${ups.snmp.port}`, model: ups.snmp.upsModel || 'cyberpower', - thresholds: `${ups.thresholds.battery}% / ${ups.thresholds.runtime}min`, + actions: `${(ups.actions || []).length} configured`, groups: ups.groups.length > 0 ? ups.groups.join(', ') : theme.dim('None'), })); @@ -344,7 +344,7 @@ export class NupstCli { { header: 'ID', key: 'id', align: 'left' }, { header: 'Host:Port', key: 'host', align: 'left', color: theme.info }, { header: 'Model', key: 'model', align: 'left' }, - { header: 'Battery/Runtime', key: 'thresholds', align: 'left' }, + { header: 'Actions', key: 'actions', align: 'left' }, { header: 'Groups', key: 'groups', align: 'left' }, ]; @@ -389,9 +389,9 @@ export class NupstCli { } else { // === Legacy Single UPS Configuration === - if (!config.snmp || !config.thresholds) { + if (!config.snmp) { logger.logBox('Configuration Error', [ - 'Error: Legacy configuration missing SNMP or threshold settings', + 'Error: Legacy configuration missing SNMP settings', ], 60, 'error'); return; } @@ -435,9 +435,7 @@ export class NupstCli { : [] ), '', - theme.dim('Thresholds:'), - ` Battery: ${theme.highlight(String(config.thresholds.battery))}%`, - ` Runtime: ${theme.highlight(String(config.thresholds.runtime))} minutes`, + ` Check Interval: ${config.checkInterval / 1000} seconds`, '', theme.dim('Configuration File:'), diff --git a/ts/cli/ups-handler.ts b/ts/cli/ups-handler.ts index 7de1e47..f92d542 100644 --- a/ts/cli/ups-handler.ts +++ b/ts/cli/ups-handler.ts @@ -559,10 +559,6 @@ export class UpsHandler { ); logger.logBoxLine(` Battery Runtime: ${snmpConfig.customOIDs.BATTERY_RUNTIME || 'Not set'}`); } - logger.logBoxLine('Thresholds:'); - logger.logBoxLine(` Battery: ${thresholds.battery}%`); - logger.logBoxLine(` Runtime: ${thresholds.runtime} minutes`); - // Show group assignments if this is a UPS config if (config.groups && Array.isArray(config.groups)) { logger.logBoxLine( @@ -586,7 +582,6 @@ export class UpsHandler { try { // Create a test config with a short timeout const snmpConfig = config.snmp ? config.snmp : config.snmp; - const thresholds = config.thresholds ? config.thresholds : config.thresholds; const testConfig = { ...snmpConfig, @@ -603,10 +598,7 @@ export class UpsHandler { logger.logBoxLine(` Runtime Remaining: ${status.batteryRuntime} minutes`); logger.logBoxEnd(); - // Check status against thresholds if on battery - if (status.powerStatus === 'onBattery') { - this.analyzeThresholds(status, thresholds); - } + } catch (error) { const errorBoxWidth = 45; logger.logBoxTitle(`Connection Failed: ${upsName}`, errorBoxWidth); @@ -1083,9 +1075,7 @@ export class UpsHandler { logger.logBoxLine(`SNMP Host: ${ups.snmp.host}:${ups.snmp.port}`); logger.logBoxLine(`SNMP Version: ${ups.snmp.version}`); logger.logBoxLine(`UPS Model: ${ups.snmp.upsModel}`); - logger.logBoxLine( - `Thresholds: ${ups.thresholds.battery}% battery, ${ups.thresholds.runtime} min runtime`, - ); + if (ups.groups && ups.groups.length > 0) { logger.logBoxLine(`Groups: ${ups.groups.join(', ')}`); } else { diff --git a/ts/daemon.ts b/ts/daemon.ts index 0e689ff..e5406eb 100644 --- a/ts/daemon.ts +++ b/ts/daemon.ts @@ -304,8 +304,6 @@ export class NupstDaemon { batteryRuntime: 999, // High value as default lastStatusChange: Date.now(), lastCheckTime: 0, - thresholdsExceeded: false, - lastThresholdCrossing: 0, }); } @@ -553,23 +551,7 @@ export class NupstDaemon { - // Check threshold conditions - if ( - status.batteryCapacity < ups.thresholds.battery || - status.batteryRuntime < ups.thresholds.runtime - ) { - logger.logBoxTitle(`UPS Shutdown Required: ${ups.name}`, 50); - logger.logBoxLine( - `Battery: ${status.batteryCapacity}% (threshold: ${ups.thresholds.battery}%)`, - ); - logger.logBoxLine( - `Runtime: ${status.batteryRuntime} min (threshold: ${ups.thresholds.runtime} min)`, - ); - logger.logBoxEnd(); - - await this.initiateShutdown(`UPS "${ups.name}" battery or runtime below threshold`); - } - } + /** * Build action context from UPS state diff --git a/ts/migrations/base-migration.ts b/ts/migrations/base-migration.ts index 799a20d..38ef72d 100644 --- a/ts/migrations/base-migration.ts +++ b/ts/migrations/base-migration.ts @@ -5,16 +5,14 @@ * Migrations run in order based on the `order` field, allowing users to jump * multiple versions (e.g., v1 → v4 runs migrations 2, 3, and 4). */ +/** + * Abstract base class for configuration migrations + * + * Each migration represents an upgrade from one config version to another. + * Migrations run in order based on the `toVersion` field, allowing users to jump + * multiple versions (e.g., v1 → v4 runs migrations 2, 3, and 4). + */ export abstract class BaseMigration { - /** - * Migration order number - * - Order 2: v1 → v2 - * - Order 3: v2 → v3 - * - Order 4: v3 → v4 - * etc. - */ - abstract readonly order: number; - /** * Source version this migration upgrades from * e.g., "1.x", "3.x" @@ -23,7 +21,7 @@ export abstract class BaseMigration { /** * Target version this migration upgrades to - * e.g., "2.0", "4.0" + * e.g., "2.0", "4.0", "4.1" */ abstract readonly toVersion: string; @@ -51,4 +49,19 @@ export abstract class BaseMigration { getName(): string { return `Migration ${this.fromVersion} → ${this.toVersion}`; } + + /** + * Parse version string into a comparable number + * Supports formats like "2.0", "4.1", etc. + * Returns a number like 2.0, 4.1 for sorting + * + * @returns Parsed version number for ordering + */ + getVersionOrder(): number { + const parsed = parseFloat(this.toVersion); + if (isNaN(parsed)) { + throw new Error(`Invalid version format: ${this.toVersion}`); + } + return parsed; + } } diff --git a/ts/migrations/migration-runner.ts b/ts/migrations/migration-runner.ts index d01c703..a719a97 100644 --- a/ts/migrations/migration-runner.ts +++ b/ts/migrations/migration-runner.ts @@ -22,8 +22,8 @@ export class MigrationRunner { // Add future migrations here (v4.2, v4.3, etc.) ]; - // Sort by order to ensure they run in sequence - this.migrations.sort((a, b) => a.order - b.order); + // Sort by version order to ensure they run in sequence + this.migrations.sort((a, b) => a.getVersionOrder() - b.getVersionOrder()); } /** diff --git a/ts/migrations/migration-v1-to-v2.ts b/ts/migrations/migration-v1-to-v2.ts index 04d8357..914caf8 100644 --- a/ts/migrations/migration-v1-to-v2.ts +++ b/ts/migrations/migration-v1-to-v2.ts @@ -20,7 +20,6 @@ import { logger } from '../logger.ts'; * } */ export class MigrationV1ToV2 extends BaseMigration { - readonly order = 2; readonly fromVersion = '1.x'; readonly toVersion = '2.0'; diff --git a/ts/migrations/migration-v3-to-v4.ts b/ts/migrations/migration-v3-to-v4.ts index 7faab71..e662f90 100644 --- a/ts/migrations/migration-v3-to-v4.ts +++ b/ts/migrations/migration-v3-to-v4.ts @@ -39,7 +39,6 @@ import { logger } from '../logger.ts'; * } */ export class MigrationV3ToV4 extends BaseMigration { - readonly order = 4; readonly fromVersion = '3.x'; readonly toVersion = '4.0'; diff --git a/ts/migrations/migration-v4.0-to-v4.1.ts b/ts/migrations/migration-v4.0-to-v4.1.ts index 5664e28..f13dd0f 100644 --- a/ts/migrations/migration-v4.0-to-v4.1.ts +++ b/ts/migrations/migration-v4.0-to-v4.1.ts @@ -46,7 +46,6 @@ import { logger } from '../logger.ts'; * } */ export class MigrationV4_0ToV4_1 extends BaseMigration { - readonly order = 5; readonly fromVersion = '4.0'; readonly toVersion = '4.1';