node-os-utils
Version:
Advanced cross-platform operating system monitoring utilities with TypeScript support
107 lines • 3.14 kB
TypeScript
/// <reference types="node" />
import { EventEmitter } from 'events';
import { MonitorResult, MonitorConfig, MonitorSubscription } from '../types';
import { PlatformAdapter } from '../types/platform';
import { CacheManager } from './cache-manager';
import { MonitorError } from '../types/errors';
/**
* 基础监控器抽象类
*
* 提供所有监控器的通用功能,包括缓存、事件、配置管理等
*/
export declare abstract class BaseMonitor<T> extends EventEmitter {
protected config: MonitorConfig;
protected adapter: PlatformAdapter;
protected cache: CacheManager;
protected subscriptions: Set<MonitorSubscription>;
constructor(adapter: PlatformAdapter, config?: MonitorConfig, cache?: CacheManager);
/**
* 获取监控信息
*/
abstract info(): Promise<MonitorResult<T>>;
/**
* 获取默认配置
*/
protected abstract getDefaultConfig(): MonitorConfig;
/**
* 配置监控器
*/
withConfig(config: Partial<MonitorConfig>): this;
/**
* 配置缓存
*/
withCaching(enabled: boolean, ttl?: number): this;
/**
* 实时监控
*/
monitor(interval: number, callback: (data: T) => void): MonitorSubscription;
/**
* 获取配置
*/
getConfig(): MonitorConfig;
/**
* 获取缓存统计
*/
getCacheStats(): import("./cache-manager").CacheStats | null;
/**
* 清空缓存
*/
clearCache(): void;
/**
* 停止所有监控订阅
*/
stopAllMonitoring(): void;
/**
* 获取活跃订阅数量
*/
getActiveSubscriptions(): number;
/**
* 销毁监控器
*/
destroy(): void;
/**
* 获取缓存结果
*/
protected getCachedResult<R>(key: string): R | undefined;
/**
* 设置缓存结果
*/
protected setCachedResult<R>(key: string, result: R, ttl?: number): void;
/**
* 创建成功结果
*/
protected createSuccessResult<R>(data: R, cached?: boolean): MonitorResult<R>;
/**
* 创建失败结果
*/
protected createErrorResult<R>(error: MonitorError): MonitorResult<R>;
/**
* 处理错误并转换为 MonitorResult
*/
protected handleError<R>(error: any): MonitorResult<R>;
/**
* 执行带缓存的操作
*/
protected executeWithCache<R>(cacheKey: string, operation: () => Promise<R>, ttl?: number): Promise<MonitorResult<R>>;
/**
* 验证平台支持
*/
protected validatePlatformSupport(feature: string): void;
/**
* 创建不支持功能的错误
*/
protected createUnsupportedError(feature: string): MonitorError;
/**
* 安全执行异步操作
*/
protected safeExecute<R>(operation: () => Promise<R>, fallback?: R): Promise<R>;
/**
* 创建超时 Promise
*/
protected createTimeoutPromise<R>(timeoutMs: number, errorMessage?: string): Promise<R>;
/**
* 带超时执行操作
*/
protected executeWithTimeout<R>(operation: () => Promise<R>, timeoutMs?: number): Promise<R>;
}
//# sourceMappingURL=base-monitor.d.ts.map