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84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
"""
Monitoring and health check utilities for the Knowledge Assistant RAG application.
Provides comprehensive service monitoring, alerting, and health status tracking.
"""

import asyncio
import logging
import time
import psutil
import os
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Any
from dataclasses import dataclass, asdict
from enum import Enum
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import text, select, func
from .database import User, DocumentMetadata, get_async_session
from .vector_store import get_qdrant_client
from .gemini_llm import get_gemini_client, generate_response
from .processing import get_embedding_model

logger = logging.getLogger(__name__)


class HealthStatus(Enum):
    """Health status enumeration"""
    HEALTHY = "healthy"
    DEGRADED = "degraded"
    UNHEALTHY = "unhealthy"
    UNKNOWN = "unknown"


@dataclass
class ServiceHealth:
    """Service health status data class"""
    name: str
    status: HealthStatus
    response_time_ms: Optional[float] = None
    error_message: Optional[str] = None
    metadata: Optional[Dict[str, Any]] = None
    last_check: Optional[datetime] = None

    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary for JSON serialization"""
        result = asdict(self)
        result['status'] = self.status.value
        if self.last_check:
            result['last_check'] = self.last_check.isoformat()
        return result


@dataclass
class SystemMetrics:
    """System resource metrics"""
    cpu_percent: float
    memory_percent: float
    disk_percent: float
    disk_free_gb: float
    uptime_seconds: float
    timestamp: datetime

    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary for JSON serialization"""
        result = asdict(self)
        result['timestamp'] = self.timestamp.isoformat()
        return result


class HealthMonitor:
    """Comprehensive health monitoring system"""
    
    def __init__(self):
        self.service_checks: Dict[str, ServiceHealth] = {}
        self.system_metrics_history: List[SystemMetrics] = []
        self.max_history_size = 100
        self.alert_thresholds = {
            'cpu_percent': 80.0,
            'memory_percent': 85.0,
            'disk_percent': 90.0,
            'response_time_ms': 5000.0
        }
    
    async def check_database_health(self, session: AsyncSession) -> ServiceHealth:
        """Check database connectivity and performance"""
        start_time = time.time()
        
        try:
            # Test basic connectivity
            await session.execute(text("SELECT 1"))
            
            # Test user table access
            user_count = await session.execute(select(func.count(User.id)))
            user_count = user_count.scalar()
            
            # Test document table access
            doc_count = await session.execute(select(func.count(DocumentMetadata.id)))
            doc_count = doc_count.scalar()
            
            response_time = (time.time() - start_time) * 1000
            
            return ServiceHealth(
                name="database",
                status=HealthStatus.HEALTHY,
                response_time_ms=response_time,
                metadata={
                    "type": "sqlite",
                    "user_count": user_count,
                    "document_count": doc_count
                },
                last_check=datetime.utcnow()
            )
            
        except Exception as e:
            response_time = (time.time() - start_time) * 1000
            logger.error(f"Database health check failed: {str(e)}")
            
            return ServiceHealth(
                name="database",
                status=HealthStatus.UNHEALTHY,
                response_time_ms=response_time,
                error_message=str(e),
                last_check=datetime.utcnow()
            )
    
    async def check_qdrant_health(self) -> ServiceHealth:
        """Check Qdrant vector database health"""
        start_time = time.time()
        
        try:
            client = get_qdrant_client()
            
            # Get collections info
            collections = client.get_collections()
            collection_count = len(collections.collections)
            
            # Get cluster info if available
            try:
                cluster_info = client.get_cluster_info()
                cluster_status = "healthy"
            except:
                cluster_info = None
                cluster_status = "unknown"
            
            response_time = (time.time() - start_time) * 1000
            
            return ServiceHealth(
                name="qdrant",
                status=HealthStatus.HEALTHY,
                response_time_ms=response_time,
                metadata={
                    "collections_count": collection_count,
                    "cluster_status": cluster_status,
                    "collections": [col.name for col in collections.collections]
                },
                last_check=datetime.utcnow()
            )
            
        except Exception as e:
            response_time = (time.time() - start_time) * 1000
            logger.error(f"Qdrant health check failed: {str(e)}")
            
            return ServiceHealth(
                name="qdrant",
                status=HealthStatus.UNHEALTHY,
                response_time_ms=response_time,
                error_message=str(e),
                last_check=datetime.utcnow()
            )
    
    async def check_gemini_health(self) -> ServiceHealth:
        """Check Google Gemini API health"""
        start_time = time.time()
        
        try:
            client = get_gemini_client()
            
            # Test with a simple prompt
            test_response = generate_response(client, "Hello, respond with 'OK' if you're working.")
            
            response_time = (time.time() - start_time) * 1000
            
            # Check if response is reasonable
            if test_response and len(test_response.strip()) > 0:
                status = HealthStatus.HEALTHY
            else:
                status = HealthStatus.DEGRADED
                
            return ServiceHealth(
                name="gemini",
                status=status,
                response_time_ms=response_time,
                metadata={
                    "model": "gemini-pro",
                    "test_response_length": len(test_response) if test_response else 0
                },
                last_check=datetime.utcnow()
            )
            
        except Exception as e:
            response_time = (time.time() - start_time) * 1000
            logger.error(f"Gemini health check failed: {str(e)}")
            
            return ServiceHealth(
                name="gemini",
                status=HealthStatus.UNHEALTHY,
                response_time_ms=response_time,
                error_message=str(e),
                last_check=datetime.utcnow()
            )
    
    async def check_embedding_model_health(self) -> ServiceHealth:
        """Check embedding model health"""
        start_time = time.time()
        
        try:
            model = get_embedding_model()
            
            # Test embedding generation
            test_embedding = model.encode("test health check")
            
            response_time = (time.time() - start_time) * 1000
            
            return ServiceHealth(
                name="embedding_model",
                status=HealthStatus.HEALTHY,
                response_time_ms=response_time,
                metadata={
                    "embedding_dimension": len(test_embedding),
                    "model_type": type(model).__name__
                },
                last_check=datetime.utcnow()
            )
            
        except Exception as e:
            response_time = (time.time() - start_time) * 1000
            logger.error(f"Embedding model health check failed: {str(e)}")
            
            return ServiceHealth(
                name="embedding_model",
                status=HealthStatus.UNHEALTHY,
                response_time_ms=response_time,
                error_message=str(e),
                last_check=datetime.utcnow()
            )
    
    def get_system_metrics(self) -> SystemMetrics:
        """Get current system resource metrics"""
        try:
            # CPU usage
            cpu_percent = psutil.cpu_percent(interval=1)
            
            # Memory usage
            memory = psutil.virtual_memory()
            memory_percent = memory.percent
            
            # Disk usage
            disk = psutil.disk_usage('/')
            disk_percent = (disk.used / disk.total) * 100
            disk_free_gb = disk.free / (1024**3)
            
            # System uptime
            boot_time = psutil.boot_time()
            uptime_seconds = time.time() - boot_time
            
            return SystemMetrics(
                cpu_percent=cpu_percent,
                memory_percent=memory_percent,
                disk_percent=disk_percent,
                disk_free_gb=disk_free_gb,
                uptime_seconds=uptime_seconds,
                timestamp=datetime.utcnow()
            )
            
        except Exception as e:
            logger.error(f"Failed to get system metrics: {str(e)}")
            return SystemMetrics(
                cpu_percent=0.0,
                memory_percent=0.0,
                disk_percent=0.0,
                disk_free_gb=0.0,
                uptime_seconds=0.0,
                timestamp=datetime.utcnow()
            )
    
    async def perform_comprehensive_health_check(self, session: AsyncSession) -> Dict[str, Any]:
        """Perform comprehensive health check of all services"""
        logger.info("Starting comprehensive health check...")
        
        # Run all health checks concurrently
        health_checks = await asyncio.gather(
            self.check_database_health(session),
            self.check_qdrant_health(),
            self.check_gemini_health(),
            self.check_embedding_model_health(),
            return_exceptions=True
        )
        
        # Process results
        services = {}
        overall_status = HealthStatus.HEALTHY
        
        for check in health_checks:
            if isinstance(check, Exception):
                logger.error(f"Health check failed with exception: {str(check)}")
                continue
                
            services[check.name] = check.to_dict()
            self.service_checks[check.name] = check
            
            # Update overall status
            if check.status == HealthStatus.UNHEALTHY:
                overall_status = HealthStatus.UNHEALTHY
            elif check.status == HealthStatus.DEGRADED and overall_status == HealthStatus.HEALTHY:
                overall_status = HealthStatus.DEGRADED
        
        # Get system metrics
        system_metrics = self.get_system_metrics()
        self.system_metrics_history.append(system_metrics)
        
        # Keep history size manageable
        if len(self.system_metrics_history) > self.max_history_size:
            self.system_metrics_history = self.system_metrics_history[-self.max_history_size:]
        
        # Check for system resource alerts
        alerts = self.check_system_alerts(system_metrics)
        
        return {
            "status": overall_status.value,
            "timestamp": datetime.utcnow().isoformat(),
            "services": services,
            "system_metrics": system_metrics.to_dict(),
            "alerts": alerts,
            "summary": {
                "total_services": len(services),
                "healthy_services": len([s for s in services.values() if s["status"] == "healthy"]),
                "degraded_services": len([s for s in services.values() if s["status"] == "degraded"]),
                "unhealthy_services": len([s for s in services.values() if s["status"] == "unhealthy"])
            }
        }
    
    def check_system_alerts(self, metrics: SystemMetrics) -> List[Dict[str, Any]]:
        """Check system metrics against alert thresholds"""
        alerts = []
        
        if metrics.cpu_percent > self.alert_thresholds['cpu_percent']:
            alerts.append({
                "type": "high_cpu_usage",
                "severity": "warning",
                "message": f"CPU usage is {metrics.cpu_percent:.1f}% (threshold: {self.alert_thresholds['cpu_percent']}%)",
                "value": metrics.cpu_percent,
                "threshold": self.alert_thresholds['cpu_percent']
            })
        
        if metrics.memory_percent > self.alert_thresholds['memory_percent']:
            alerts.append({
                "type": "high_memory_usage",
                "severity": "warning",
                "message": f"Memory usage is {metrics.memory_percent:.1f}% (threshold: {self.alert_thresholds['memory_percent']}%)",
                "value": metrics.memory_percent,
                "threshold": self.alert_thresholds['memory_percent']
            })
        
        if metrics.disk_percent > self.alert_thresholds['disk_percent']:
            alerts.append({
                "type": "high_disk_usage",
                "severity": "critical",
                "message": f"Disk usage is {metrics.disk_percent:.1f}% (threshold: {self.alert_thresholds['disk_percent']}%)",
                "value": metrics.disk_percent,
                "threshold": self.alert_thresholds['disk_percent']
            })
        
        if metrics.disk_free_gb < 1.0:
            alerts.append({
                "type": "low_disk_space",
                "severity": "critical",
                "message": f"Only {metrics.disk_free_gb:.2f} GB free disk space remaining",
                "value": metrics.disk_free_gb,
                "threshold": 1.0
            })
        
        return alerts
    
    def get_service_status_dashboard(self) -> Dict[str, Any]:
        """Get service status dashboard data"""
        dashboard_data = {
            "timestamp": datetime.utcnow().isoformat(),
            "services": {},
            "system_metrics": None,
            "recent_alerts": []
        }
        
        # Add service statuses
        for name, health in self.service_checks.items():
            dashboard_data["services"][name] = health.to_dict()
        
        # Add latest system metrics
        if self.system_metrics_history:
            dashboard_data["system_metrics"] = self.system_metrics_history[-1].to_dict()
            
            # Get recent alerts from last few metrics
            recent_metrics = self.system_metrics_history[-5:]  # Last 5 checks
            for metrics in recent_metrics:
                alerts = self.check_system_alerts(metrics)
                dashboard_data["recent_alerts"].extend(alerts)
        
        return dashboard_data


# Global health monitor instance
health_monitor = HealthMonitor()


async def get_health_status(session: AsyncSession) -> Dict[str, Any]:
    """Get comprehensive health status - main entry point"""
    return await health_monitor.perform_comprehensive_health_check(session)


def get_service_dashboard() -> Dict[str, Any]:
    """Get service status dashboard - main entry point"""
    return health_monitor.get_service_status_dashboard()