fix(cli,daemon,snmp): normalize CLI argument parsing and extract daemon monitoring helpers with stronger SNMP typing
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import type { IUpsStatus as IProtocolUpsStatus } from './snmp/types.ts';
|
||||
|
||||
export interface IShutdownMonitoringRow extends Record<string, string> {
|
||||
name: string;
|
||||
battery: string;
|
||||
runtime: string;
|
||||
status: string;
|
||||
}
|
||||
|
||||
export interface IShutdownRowFormatters {
|
||||
battery: (batteryCapacity: number) => string;
|
||||
runtime: (batteryRuntime: number) => string;
|
||||
ok: (text: string) => string;
|
||||
critical: (text: string) => string;
|
||||
error: (text: string) => string;
|
||||
}
|
||||
|
||||
export interface IShutdownEmergencyCandidate<TUps> {
|
||||
ups: TUps;
|
||||
status: IProtocolUpsStatus;
|
||||
}
|
||||
|
||||
export function isEmergencyRuntime(
|
||||
batteryRuntime: number,
|
||||
emergencyRuntimeMinutes: number,
|
||||
): boolean {
|
||||
return batteryRuntime < emergencyRuntimeMinutes;
|
||||
}
|
||||
|
||||
export function buildShutdownStatusRow(
|
||||
upsName: string,
|
||||
status: IProtocolUpsStatus,
|
||||
emergencyRuntimeMinutes: number,
|
||||
formatters: IShutdownRowFormatters,
|
||||
): { row: IShutdownMonitoringRow; isCritical: boolean } {
|
||||
const isCritical = isEmergencyRuntime(status.batteryRuntime, emergencyRuntimeMinutes);
|
||||
|
||||
return {
|
||||
row: {
|
||||
name: upsName,
|
||||
battery: formatters.battery(status.batteryCapacity),
|
||||
runtime: formatters.runtime(status.batteryRuntime),
|
||||
status: isCritical ? formatters.critical('CRITICAL!') : formatters.ok('OK'),
|
||||
},
|
||||
isCritical,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildShutdownErrorRow(
|
||||
upsName: string,
|
||||
errorFormatter: (text: string) => string,
|
||||
): IShutdownMonitoringRow {
|
||||
return {
|
||||
name: upsName,
|
||||
battery: errorFormatter('N/A'),
|
||||
runtime: errorFormatter('N/A'),
|
||||
status: errorFormatter('ERROR'),
|
||||
};
|
||||
}
|
||||
|
||||
export function selectEmergencyCandidate<TUps>(
|
||||
currentCandidate: IShutdownEmergencyCandidate<TUps> | null,
|
||||
ups: TUps,
|
||||
status: IProtocolUpsStatus,
|
||||
emergencyRuntimeMinutes: number,
|
||||
): IShutdownEmergencyCandidate<TUps> | null {
|
||||
if (currentCandidate || !isEmergencyRuntime(status.batteryRuntime, emergencyRuntimeMinutes)) {
|
||||
return currentCandidate;
|
||||
}
|
||||
|
||||
return { ups, status };
|
||||
}
|
||||
Reference in New Issue
Block a user