brainy/src/distributed/healthMonitor.ts

301 lines
7.8 KiB
TypeScript
Raw Normal View History

🧠 Brainy 2.0.0 - Zero-Configuration AI Database with Triple Intelligence™ MAJOR RELEASE: Complete evolution of Brainy with groundbreaking features and performance. 🎯 KEY FEATURES: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ✨ Triple Intelligence™ Engine - Unified Vector + Metadata + Graph search - O(log n) performance on all operations - 3ms average search latency at any scale ✨ API Consolidation - 15+ search methods → 2 clean APIs - search() for vector similarity - find() for natural language queries ✨ Natural Language Processing - 220+ pre-computed NLP patterns - Instant context understanding - "Show me recent React components with tests" ✨ Zero Configuration - Works instantly, no setup required - Built-in embedding models (no API keys) - Smart defaults for everything - Automatic optimization ✨ Enterprise Features (Free for Everyone) - Scales to 10M+ items - Write-Ahead Logging (WAL) for durability - Distributed architecture with sharding - Read/write separation - Connection pooling & request deduplication - Built-in monitoring & health checks ✨ Universal Compatibility - Node.js, Browser, Edge Workers - 4 Storage Adapters (Memory, FileSystem, OPFS, S3) - TypeScript with full type safety - Worker-based embeddings 📦 WHAT'S INCLUDED: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ • Core AI Database with HNSW indexing • 19 Production-ready augmentations • Universal Memory Manager • Complete CLI with all commands • Brain Cloud integration (soulcraft.com) • Comprehensive documentation • 52 test files with 400+ tests • Migration guide from 1.x 📊 PERFORMANCE: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ • Initialize: 450ms (24MB memory) • Search: 3ms average (up to 10M items) • Metadata Filter: 0.8ms (O(log n)) • Bulk Import: 2.3s per 1000 items • Production Scale: 5.8ms at 10M items 🔧 TECHNICAL IMPROVEMENTS: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ • TypeScript compilation: 153 errors → 0 • Memory usage: 200MB → 24MB baseline • Circular dependencies resolved • Worker thread communication fixed • Storage adapter consistency • Request coalescing for 3x performance 🛠️ CLI FEATURES: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ • brainy add - Smart data ingestion • brainy find - Natural language search • brainy search - Vector similarity • brainy chat - AI conversation mode • brainy cloud - Brain Cloud integration • brainy augment - Manage extensions • 100% API compatibility 📚 DOCUMENTATION: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ • Professional README with examples • Quick Start guide (5 minutes) • Enterprise Features guide • Migration guide from 1.x • API reference • Architecture documentation 🌟 USE CASES: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ • AI memory layer for chatbots • Semantic document search • Code intelligence platforms • Knowledge management systems • Real-time recommendation engines • Customer support automation MIT License - Enterprise features included free for everyone. No premium tiers, no paywalls, no limits. Built with ❤️ by the Brainy community. Visit https://soulcraft.com for Brain Cloud integration.
2025-08-26 12:32:21 -07:00
/**
* Health Monitor
* Monitors and reports instance health in distributed deployments
*/
import { DistributedConfigManager } from './configManager.js'
import { InstanceInfo } from '../types/distributedTypes.js'
export interface HealthMetrics {
vectorCount: number
cacheHitRate: number
memoryUsage: number
cpuUsage?: number
requestsPerSecond?: number
averageLatency?: number
errorRate?: number
}
export interface HealthStatus {
status: 'healthy' | 'degraded' | 'unhealthy'
instanceId: string
role: string
uptime: number
lastCheck: string
metrics: HealthMetrics
warnings?: string[]
errors?: string[]
}
export class HealthMonitor {
private configManager: DistributedConfigManager
private startTime: number
private requestCount: number = 0
private errorCount: number = 0
private totalLatency: number = 0
private cacheHits: number = 0
private cacheMisses: number = 0
private vectorCount: number = 0
private checkInterval: number = 30000 // 30 seconds
private healthCheckTimer?: NodeJS.Timeout
private metricsWindow: number[] = [] // Sliding window for RPS calculation
private latencyWindow: number[] = [] // Sliding window for latency
private windowSize: number = 60000 // 1 minute window
constructor(configManager: DistributedConfigManager) {
this.configManager = configManager
this.startTime = Date.now()
}
/**
* Start health monitoring
*/
start(): void {
// Initial health update
this.updateHealth()
// Schedule periodic health checks
this.healthCheckTimer = setInterval(() => {
this.updateHealth()
}, this.checkInterval)
}
/**
* Stop health monitoring
*/
stop(): void {
if (this.healthCheckTimer) {
clearInterval(this.healthCheckTimer)
this.healthCheckTimer = undefined
}
}
/**
* Update health status and metrics
*/
private async updateHealth(): Promise<void> {
const metrics = this.collectMetrics()
// Update config with latest metrics
await this.configManager.updateMetrics({
vectorCount: metrics.vectorCount,
cacheHitRate: metrics.cacheHitRate,
memoryUsage: metrics.memoryUsage,
cpuUsage: metrics.cpuUsage
})
// Clean sliding windows
this.cleanWindows()
}
/**
* Collect current metrics
*/
private collectMetrics(): HealthMetrics {
const memUsage = process.memoryUsage()
return {
vectorCount: this.vectorCount,
cacheHitRate: this.calculateCacheHitRate(),
memoryUsage: memUsage.heapUsed,
cpuUsage: this.getCPUUsage(),
requestsPerSecond: this.calculateRPS(),
averageLatency: this.calculateAverageLatency(),
errorRate: this.calculateErrorRate()
}
}
/**
* Calculate cache hit rate
*/
private calculateCacheHitRate(): number {
const total = this.cacheHits + this.cacheMisses
if (total === 0) return 0
return this.cacheHits / total
}
/**
* Calculate requests per second
*/
private calculateRPS(): number {
const now = Date.now()
const recentRequests = this.metricsWindow.filter(
timestamp => now - timestamp < this.windowSize
)
return recentRequests.length / (this.windowSize / 1000)
}
/**
* Calculate average latency
*/
private calculateAverageLatency(): number {
if (this.latencyWindow.length === 0) return 0
const sum = this.latencyWindow.reduce((a, b) => a + b, 0)
return sum / this.latencyWindow.length
}
/**
* Calculate error rate
*/
private calculateErrorRate(): number {
if (this.requestCount === 0) return 0
return this.errorCount / this.requestCount
}
/**
* Get CPU usage (simplified)
*/
private getCPUUsage(): number {
// Simplified CPU usage based on process time
const usage = process.cpuUsage()
const total = usage.user + usage.system
const seconds = (Date.now() - this.startTime) / 1000
return Math.min(100, (total / 1000000 / seconds) * 100)
}
/**
* Clean old entries from sliding windows
*/
private cleanWindows(): void {
const now = Date.now()
const cutoff = now - this.windowSize
this.metricsWindow = this.metricsWindow.filter(t => t > cutoff)
// Keep only recent latency measurements
if (this.latencyWindow.length > 100) {
this.latencyWindow = this.latencyWindow.slice(-100)
}
}
/**
* Record a request
* @param latency - Request latency in milliseconds
* @param error - Whether the request resulted in an error
*/
recordRequest(latency: number, error: boolean = false): void {
this.requestCount++
this.metricsWindow.push(Date.now())
this.latencyWindow.push(latency)
if (error) {
this.errorCount++
}
}
/**
* Record cache access
* @param hit - Whether it was a cache hit
*/
recordCacheAccess(hit: boolean): void {
if (hit) {
this.cacheHits++
} else {
this.cacheMisses++
}
}
/**
* Update vector count
* @param count - New vector count
*/
updateVectorCount(count: number): void {
this.vectorCount = count
}
/**
* Get current health status
* @returns Health status object
*/
getHealthStatus(): HealthStatus {
const metrics = this.collectMetrics()
const uptime = Date.now() - this.startTime
const warnings: string[] = []
const errors: string[] = []
// Check for warnings
if (metrics.memoryUsage > 1024 * 1024 * 1024) { // > 1GB
warnings.push('High memory usage detected')
}
if (metrics.cacheHitRate < 0.5) {
warnings.push('Low cache hit rate')
}
if (metrics.errorRate && metrics.errorRate > 0.05) {
warnings.push('High error rate detected')
}
if (metrics.averageLatency && metrics.averageLatency > 1000) {
warnings.push('High latency detected')
}
// Check for errors
if (metrics.memoryUsage > 2 * 1024 * 1024 * 1024) { // > 2GB
errors.push('Critical memory usage')
}
if (metrics.errorRate && metrics.errorRate > 0.2) {
errors.push('Critical error rate')
}
// Determine overall status
let status: 'healthy' | 'degraded' | 'unhealthy' = 'healthy'
if (errors.length > 0) {
status = 'unhealthy'
} else if (warnings.length > 0) {
status = 'degraded'
}
return {
status,
instanceId: this.configManager.getInstanceId(),
role: this.configManager.getRole(),
uptime,
lastCheck: new Date().toISOString(),
metrics,
warnings: warnings.length > 0 ? warnings : undefined,
errors: errors.length > 0 ? errors : undefined
}
}
/**
* Get health check endpoint data
* @returns JSON-serializable health data
*/
getHealthEndpointData(): Record<string, any> {
const status = this.getHealthStatus()
return {
status: status.status,
instanceId: status.instanceId,
role: status.role,
uptime: Math.floor(status.uptime / 1000), // Convert to seconds
lastCheck: status.lastCheck,
metrics: {
vectorCount: status.metrics.vectorCount,
cacheHitRate: Math.round(status.metrics.cacheHitRate * 100) / 100,
memoryUsageMB: Math.round(status.metrics.memoryUsage / 1024 / 1024),
cpuUsagePercent: Math.round(status.metrics.cpuUsage || 0),
requestsPerSecond: Math.round(status.metrics.requestsPerSecond || 0),
averageLatencyMs: Math.round(status.metrics.averageLatency || 0),
errorRate: Math.round((status.metrics.errorRate || 0) * 100) / 100
},
warnings: status.warnings,
errors: status.errors
}
}
/**
* Reset metrics (useful for testing)
*/
resetMetrics(): void {
this.requestCount = 0
this.errorCount = 0
this.totalLatency = 0
this.cacheHits = 0
this.cacheMisses = 0
this.metricsWindow = []
this.latencyWindow = []
}
}