@ahmedhegazee/nestjs-telescope
Version:
Advanced observability and monitoring solution for NestJS applications with ML-powered analytics, enterprise features, and production-ready scaling
72 lines (71 loc) • 2.69 kB
TypeScript
import { OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { Observable } from 'rxjs';
export interface MemoryRetentionPolicy {
maxSize: number;
maxAge: number;
compressionThreshold: number;
evictionStrategy: 'fifo' | 'lifo' | 'lru' | 'lfu' | 'ttl';
checkInterval: number;
enabled: boolean;
}
export interface MemoryCollectionConfig {
id: string;
policy: MemoryRetentionPolicy;
itemSizeEstimator?: (item: any) => number;
itemAgeExtractor?: (item: any) => Date;
onEviction?: (items: any[]) => void;
onCompression?: (items: any[]) => any[];
}
export interface MemoryUsageStats {
totalCollections: number;
totalItems: number;
estimatedSize: number;
collectionsOverThreshold: number;
lastCleanup: Date | null;
cleanupCount: number;
evictedItems: number;
compressedItems: number;
}
export interface CollectionStats {
id: string;
itemCount: number;
estimatedSize: number;
oldestItem: Date | null;
newestItem: Date | null;
lastEviction: Date | null;
evictedCount: number;
compressionRatio: number;
policy: MemoryRetentionPolicy;
}
export declare class MemoryManagerService implements OnModuleInit, OnModuleDestroy {
private readonly logger;
private readonly collections;
private readonly destroy$;
private readonly statsSubject;
private cleanupCount;
private lastCleanup;
constructor();
onModuleInit(): Promise<void>;
onModuleDestroy(): void;
createCollection<T>(config: MemoryCollectionConfig): string;
addToCollection<T>(collectionId: string, item: T): void;
addBatchToCollection<T>(collectionId: string, items: T[]): void;
getFromCollection<T>(collectionId: string): T[];
getRecentFromCollection<T>(collectionId: string, count: number): T[];
filterCollection<T>(collectionId: string, predicate: (item: T) => boolean): T[];
clearCollection(collectionId: string): void;
removeCollection(collectionId: string): boolean;
getCollectionStats(collectionId: string): CollectionStats | null;
getAllCollectionStats(): CollectionStats[];
getMemoryUsage(): MemoryUsageStats;
getMemoryUsageStream(): Observable<MemoryUsageStats>;
forceCleanup(): void;
static createTimelinePolicy(maxItems?: number, maxHours?: number): MemoryRetentionPolicy;
static createMetricsPolicy(maxItems?: number, maxDays?: number): MemoryRetentionPolicy;
static createAlertPolicy(maxItems?: number, maxDays?: number): MemoryRetentionPolicy;
private startMemoryMonitoring;
private updateStats;
private checkMemoryPressure;
private performPeriodicCleanup;
private getInitialStats;
}