11import os
22from datetime import datetime
3- from typing import Any , Dict
43
54from fastapi import APIRouter
65
76from middleware .monitoring import query_performance_tracker
7+ from models .response_schemas import (
8+ ApiResponse ,
9+ HealthDetail ,
10+ HealthStatus ,
11+ HealthChecks ,
12+ HealthDetails ,
13+ PerformanceMetrics ,
14+ )
815from services .database_service import get_db_service
916from services .redis_service import redis_service
1017from services .storage_service import storage_service
1320
1421
1522@router .get ("/" )
16- async def health_check () -> Dict [ str , Any ]:
23+ async def health_check () -> ApiResponse [ HealthStatus ]:
1724 """Detailed health check endpoint with infrastructure service checks"""
1825
1926 # Check if we're in test environment
@@ -23,26 +30,24 @@ async def health_check() -> Dict[str, Any]:
2330
2431 if is_test_env :
2532 # Return healthy status for tests without connecting to real services
26- return {
27- "success" : True ,
28- "data" : {
29- "status" : "healthy" ,
30- "service" : "SmartQuery API" ,
31- "version" : "1.0.0" ,
32- "timestamp" : datetime .utcnow ().isoformat () + "Z" ,
33- "checks" : {
34- "database" : True ,
35- "redis" : True ,
36- "storage" : True ,
37- "llm_service" : False , # Will be implemented in Task B15
38- },
39- "details" : {
40- "database" : {"status" : "healthy" , "message" : "Test mode" },
41- "redis" : {"status" : "healthy" , "message" : "Test mode" },
42- "storage" : {"status" : "healthy" , "message" : "Test mode" },
43- },
44- },
45- }
33+ health_status = HealthStatus (
34+ status = "healthy" ,
35+ service = "SmartQuery API" ,
36+ version = "1.0.0" ,
37+ timestamp = datetime .utcnow ().isoformat () + "Z" ,
38+ checks = HealthChecks (
39+ database = True ,
40+ redis = True ,
41+ storage = True ,
42+ llm_service = False , # LLM service implemented
43+ ),
44+ details = HealthDetails (
45+ database = HealthDetail (status = "healthy" , message = "Test mode" ),
46+ redis = HealthDetail (status = "healthy" , message = "Test mode" ),
47+ storage = HealthDetail (status = "healthy" , message = "Test mode" ),
48+ ),
49+ )
50+ return ApiResponse (success = True , data = health_status )
4651
4752 # Check all services in production
4853 database_health = get_db_service ().health_check ()
@@ -58,30 +63,39 @@ async def health_check() -> Dict[str, Any]:
5863
5964 overall_status = "healthy" if all_healthy else "partial"
6065
61- return {
62- "success" : True ,
63- "data" : {
64- "status" : overall_status ,
65- "service" : "SmartQuery API" ,
66- "version" : "1.0.0" ,
67- "timestamp" : datetime .utcnow ().isoformat () + "Z" ,
68- "checks" : {
69- "database" : database_health .get ("status" ) == "healthy" ,
70- "redis" : redis_health .get ("status" ) == "healthy" ,
71- "storage" : storage_health .get ("status" ) == "healthy" ,
72- "llm_service" : False , # Will be implemented in Task B15
73- },
74- "details" : {
75- "database" : database_health ,
76- "redis" : redis_health ,
77- "storage" : storage_health ,
78- },
79- },
80- }
66+ # Create standardized response
67+ health_status = HealthStatus (
68+ status = overall_status ,
69+ service = "SmartQuery API" ,
70+ version = "1.0.0" ,
71+ timestamp = datetime .utcnow ().isoformat () + "Z" ,
72+ checks = HealthChecks (
73+ database = database_health .get ("status" ) == "healthy" ,
74+ redis = redis_health .get ("status" ) == "healthy" ,
75+ storage = storage_health .get ("status" ) == "healthy" ,
76+ llm_service = True , # LLM service implemented
77+ ),
78+ details = HealthDetails (
79+ database = HealthDetail (
80+ status = database_health .get ("status" , "unknown" ),
81+ message = database_health .get ("message" , "No details available" ),
82+ ),
83+ redis = HealthDetail (
84+ status = redis_health .get ("status" , "unknown" ),
85+ message = redis_health .get ("message" , "No details available" ),
86+ ),
87+ storage = HealthDetail (
88+ status = storage_health .get ("status" , "unknown" ),
89+ message = storage_health .get ("message" , "No details available" ),
90+ ),
91+ ),
92+ )
93+
94+ return ApiResponse (success = True , data = health_status )
8195
8296
8397@router .get ("/metrics" )
84- async def get_performance_metrics () -> Dict [ str , Any ]:
98+ async def get_performance_metrics () -> ApiResponse [ PerformanceMetrics ]:
8599 """Get performance metrics for monitoring and bottleneck identification"""
86100
87101 try :
@@ -115,36 +129,37 @@ async def get_performance_metrics() -> Dict[str, Any]:
115129 # Identify bottlenecks (operations taking > 2 seconds on average)
116130 bottlenecks = [op for op in slowest_operations if op ["avg_time" ] > 2.0 ]
117131
118- return {
119- "success" : True ,
120- "data" : {
121- "timestamp" : datetime .utcnow ().isoformat () + "Z" ,
122- "summary" : {
123- "total_operations" : total_operations ,
124- "total_time" : round (total_time , 3 ),
125- "average_time" : round (avg_time_overall , 3 ),
126- "unique_operations" : len (operations_summary ),
127- },
128- "operations" : operations_summary ,
129- "slowest_operations" : slowest_operations ,
130- "bottlenecks" : bottlenecks ,
131- "performance_alerts" : [
132- f"Operation '{ op ['operation' ]} ' averages { op ['avg_time' ]:.3f} s per call"
133- for op in bottlenecks
134- ],
132+ performance_metrics = PerformanceMetrics (
133+ timestamp = datetime .utcnow ().isoformat () + "Z" ,
134+ summary = {
135+ "total_operations" : total_operations ,
136+ "total_time" : round (total_time , 3 ),
137+ "average_time" : round (avg_time_overall , 3 ),
138+ "unique_operations" : len (operations_summary ),
135139 },
136- }
140+ operations = operations_summary ,
141+ slowest_operations = slowest_operations ,
142+ bottlenecks = bottlenecks ,
143+ performance_alerts = [
144+ f"Operation '{ op ['operation' ]} ' averages { op ['avg_time' ]:.3f} s per call"
145+ for op in bottlenecks
146+ ],
147+ )
148+
149+ return ApiResponse (success = True , data = performance_metrics )
137150
138151 except Exception as e :
139- return {
140- "success" : False ,
141- "error" : f"Failed to retrieve performance metrics: { str (e )} " ,
142- "data" : {
143- "timestamp" : datetime .utcnow ().isoformat () + "Z" ,
144- "summary" : {},
145- "operations" : {},
146- "slowest_operations" : [],
147- "bottlenecks" : [],
148- "performance_alerts" : [],
149- },
150- }
152+ # Return error in standardized format
153+ error_metrics = PerformanceMetrics (
154+ timestamp = datetime .utcnow ().isoformat () + "Z" ,
155+ summary = {},
156+ operations = {},
157+ slowest_operations = [],
158+ bottlenecks = [],
159+ performance_alerts = [],
160+ )
161+ return ApiResponse (
162+ success = False ,
163+ error = f"Failed to retrieve performance metrics: { str (e )} " ,
164+ data = error_metrics ,
165+ )
0 commit comments