/** * Dashboard Service Implementation * Provides HTTP API and WebSocket for real-time cache monitoring * Requirements: 9.7, 9.8 */ import { CacheEngine } from '../cache/CacheEngine.js'; import { TierManager } from '../tier/TierManager.js'; import { AccessTracker } from '../tracking/AccessTracker.js'; /** * Dashboard metrics returned by the API */ export interface DashboardMetrics { /** RAM tier usage in bytes */ ramUsageBytes: number; /** RAM tier capacity in bytes */ ramCapacityBytes: number; /** SSD tier usage in bytes */ ssdUsageBytes: number; /** SSD tier capacity in bytes */ ssdCapacityBytes: number; /** DB tier usage in bytes */ dbUsageBytes: number; /** Cache hit rate (0-1) */ hitRate: number; /** Cache miss rate (0-1) */ missRate: number; /** Operations per second */ opsPerSecond: number; /** Estimated RAM savings in bytes compared to pure RAM solution */ estimatedRamSavings: number; } /** * Key metrics for hottest keys endpoint */ export interface KeyMetrics { key: string; accessCount: number; frequency: number; lastAccess: number; sizeBytes?: number; tier?: string; } /** * Tier distribution statistics */ export interface TierDistribution { ram: { keys: number; bytes: number; percentage: number; }; ssd: { keys: number; bytes: number; percentage: number; }; db: { keys: number; bytes: number; percentage: number; }; } /** * Optimization suggestion returned by the dashboard * Requirement 9.6: Provide auto-optimization suggestions based on current metrics */ export interface OptimizationSuggestion { /** Type of suggestion */ type: 'ram_size' | 'ssd_size' | 'threshold' | 'eviction' | 'prediction'; /** Priority level (high, medium, low) */ priority: 'high' | 'medium' | 'low'; /** Human-readable suggestion message */ message: string; /** Current value that triggered the suggestion */ currentValue?: number | string; /** Suggested value to improve performance */ suggestedValue?: number | string; } /** * Cost metrics for the cache system * Requirements: 14.1, 14.2, 14.3 */ export interface CostMetrics { /** Estimated RAM savings in bytes compared to pure RAM solution */ estimatedRamSavings: number; /** Percentage of RAM saved compared to pure RAM solution */ ramSavingsPercentage: number; /** Cost per operation based on tier access patterns (relative units) */ costPerOperation: number; /** Cost comparison ratio (tiered vs non-tiered, lower is better) */ costComparisonRatio: number; /** RAM tier utilization percentage */ ramUtilizationPercentage: number; } /** * Historical tier utilization snapshot * Requirement 14.4: Store historical tier utilization for trend analysis */ export interface HistoricalSnapshot { /** Timestamp of the snapshot */ timestamp: number; /** RAM tier utilization percentage */ ramUtilization: number; /** SSD tier utilization percentage */ ssdUtilization: number; /** DB tier utilization percentage */ dbUtilization: number; /** Total keys at this point */ totalKeys: number; /** Total bytes at this point */ totalBytes: number; /** Hit rate at this point */ hitRate: number; } /** * DashboardService provides HTTP API and WebSocket for monitoring the cache system. * * Requirements: * - 9.7: Expose dashboard via HTTP API * - 9.8: Support real-time updates via WebSocket */ export declare class DashboardService { private server; private wsClients; private metricsInterval; private historyInterval; private port; private isRunning; private hitCount; private missCount; private opsCount; private lastOpsResetTime; private historicalSnapshots; private maxHistorySize; private historyIntervalMs; private cacheEngine; private tierManager; private accessTracker; private ramTierCapacity; private ssdTierCapacity; constructor(); /** * Set the CacheEngine dependency */ setCacheEngine(engine: CacheEngine): void; /** * Set dependencies directly (alternative to setCacheEngine) */ setDependencies(tierManager: TierManager, accessTracker: AccessTracker): void; /** * Set the storage tier capacities for accurate utilization calculations */ setTierCapacities(ramCapacityBytes: number, ssdCapacityBytes: number): void; /** * Record a cache hit for metrics */ recordHit(): void; /** * Record a cache miss for metrics */ recordMiss(): void; /** * Start the dashboard HTTP server * * Requirement 9.7: Expose dashboard via HTTP API * Requirement 9.8: Support real-time updates via WebSocket * * @param port - Port to listen on (default: 8080) */ start(port?: number): Promise; /** * Stop the dashboard HTTP server */ stop(): Promise; /** * Check if the service is running */ isServiceRunning(): boolean; /** * Handle incoming HTTP requests */ private handleRequest; /** * Handle GET /metrics endpoint */ private handleMetricsRequest; /** * Handle GET /keys/hot endpoint */ private handleHotKeysRequest; /** * Handle GET /tiers endpoint */ private handleTiersRequest; /** * Handle GET /health endpoint */ private handleHealthRequest; /** * Handle GET /suggestions endpoint * Requirement 9.6: Provide auto-optimization suggestions based on current metrics */ private handleSuggestionsRequest; /** * Handle GET /cost endpoint * Requirements: 14.1, 14.2, 14.3 */ private handleCostRequest; /** * Handle GET /history endpoint * Requirement 14.4: Store historical tier utilization for trend analysis */ private handleHistoryRequest; /** * Send JSON response */ private sendJson; /** * Handle WebSocket upgrade request * * Requirement 9.8: Support real-time updates via WebSocket */ private handleWebSocketUpgrade; /** * Generate WebSocket accept key for handshake */ private generateWebSocketAcceptKey; /** * Handle incoming WebSocket data */ private handleWebSocketData; /** * Send WebSocket pong frame */ private sendWebSocketPong; /** * Send WebSocket message to a client */ private sendWebSocketMessage; /** * Create WebSocket frame for sending data */ private createWebSocketFrame; /** * Start periodic metrics broadcast to WebSocket clients */ private startMetricsBroadcast; /** * Start periodic history collection for trend analysis * Requirement 14.4: Store historical tier utilization for trend analysis */ private startHistoryCollection; /** * Collect a historical snapshot of tier utilization * Requirement 14.4: Store historical tier utilization for trend analysis */ private collectHistorySnapshot; /** * Broadcast metrics to all connected WebSocket clients */ private broadcastMetrics; /** * Get current dashboard metrics * * Requirements: 9.1, 9.4, 14.1, 14.2 */ getMetrics(): DashboardMetrics; /** * Get the N hottest keys by access frequency * * Requirement 9.2: Display the top N hottest keys by access frequency */ getHottestKeys(n: number): KeyMetrics[]; /** * Get tier distribution statistics * * Requirement 9.5: Display tier distribution showing percentage of data in each tier */ getTierDistribution(): TierDistribution; /** * Get optimization suggestions based on current metrics * * Requirement 9.6: Provide auto-optimization suggestions based on current metrics */ getOptimizationSuggestions(): OptimizationSuggestion[]; /** * Get cost metrics for the cache system * * Requirements: 14.1, 14.2, 14.3 */ getCostMetrics(): CostMetrics; /** * Get historical tier utilization metrics * * Requirement 14.4: Store historical tier utilization for trend analysis * * @param limit - Maximum number of snapshots to return (most recent) */ getHistoricalMetrics(limit?: number): HistoricalSnapshot[]; /** * Calculate RAM tier utilization percentage * Uses actual capacity if available */ private calculateRamUtilization; /** * Configure history collection settings * * @param intervalMs - Interval between snapshots in milliseconds * @param maxSize - Maximum number of snapshots to retain */ setHistoryConfig(intervalMs: number, maxSize: number): void; /** * Clear historical metrics */ clearHistory(): void; /** * Manually add a history snapshot (useful for testing) */ addHistorySnapshot(snapshot: HistoricalSnapshot): void; /** * Get the number of connected WebSocket clients */ getConnectedClients(): number; /** * Reset statistics counters */ resetStats(): void; /** * Subscribe to metrics updates (for programmatic use) * Returns an unsubscribe function */ subscribeToMetrics(callback: (metrics: DashboardMetrics) => void): () => void; } //# sourceMappingURL=DashboardService.d.ts.map