File size: 6,160 Bytes
0231daa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
"""
Health check and system information endpoints.

This module provides routes for monitoring the API health
and getting system information.
"""

from typing import Dict, Any
from fastapi import APIRouter, Depends
from loguru import logger

from src.models.schemas import RootResponse, HealthStatus
from src.core.manager import ModelManager
from src.core.cache import EmbeddingCache
from src.api.dependencies import get_model_manager, get_cache_if_enabled
from src.config.settings import get_settings


router = APIRouter(tags=["system"])


@router.get(
    "/",
    response_model=RootResponse,
    summary="API root",
    description="Get basic API information"
)
async def root(settings = Depends(get_settings)):
    """
    API root endpoint.
    
    Returns basic information about the API including
    version and documentation URL.
    
    Args:
        settings: Application settings
        
    Returns:
        RootResponse with API information
    """
    return RootResponse(
        message=f"{settings.APP_NAME} - Dense & Sparse Embeddings",
        version=settings.VERSION,
        docs_url="/docs"
    )


@router.get(
    "/health",
    response_model=HealthStatus,
    summary="Health check",
    description="Check API health status"
)
async def health_check(
    manager: ModelManager = Depends(get_model_manager)
):
    """
    Health check endpoint.
    
    Returns the current health status of the API including:
    - Overall status (ok/error)
    - Total configured models
    - Currently loaded models
    - Startup completion status
    
    Args:
        manager: Model manager dependency
        
    Returns:
        HealthStatus with health information
    """
    try:
        memory_info = manager.get_memory_usage()
        
        return HealthStatus(
            status="ok",
            total_models=memory_info["total_available"],
            loaded_models=memory_info["loaded_count"],
            startup_complete=memory_info["preload_complete"]
        )
        
    except Exception as e:
        logger.exception("Health check failed")
        return HealthStatus(
            status="error",
            total_models=0,
            loaded_models=0,
            startup_complete=False
        )


@router.get(
    "/info",
    summary="System information",
    description="Get detailed system and cache information"
)
async def system_info(
    manager: ModelManager = Depends(get_model_manager),
    cache: EmbeddingCache = Depends(get_cache_if_enabled),
    settings = Depends(get_settings)
) -> Dict[str, Any]:
    """
    Get detailed system information.
    
    Returns comprehensive information about:
    - API configuration
    - Model statistics
    - Cache statistics (if enabled)
    - System settings
    
    Args:
        manager: Model manager dependency
        cache: Cache dependency (if enabled)
        settings: Application settings
        
    Returns:
        Dictionary with system information
    """
    try:
        memory_info = manager.get_memory_usage()
        
        info = {
            "api": {
                "name": settings.APP_NAME,
                "version": settings.VERSION,
                "environment": settings.ENVIRONMENT,
                "debug": settings.DEBUG
            },
            "models": {
                "total": memory_info["total_available"],
                "loaded": memory_info["loaded_count"],
                "preload_complete": memory_info["preload_complete"],
                "loaded_models": memory_info["loaded_models"]
            },
            "limits": {
                "max_text_length": settings.MAX_TEXT_LENGTH,
                "max_batch_size": settings.MAX_BATCH_SIZE,
                "request_timeout": settings.REQUEST_TIMEOUT
            },
            "cache": {
                "enabled": settings.ENABLE_CACHE
            }
        }
        
        # Add cache stats if enabled
        if cache is not None:
            info["cache"]["stats"] = cache.stats
        
        return info
        
    except Exception as e:
        logger.exception("Failed to get system info")
        return {
            "error": "Failed to retrieve system information",
            "detail": str(e)
        }


@router.get(
    "/cache/stats",
    summary="Cache statistics",
    description="Get detailed cache statistics (if caching is enabled)"
)
async def cache_stats(
    cache: EmbeddingCache = Depends(get_cache_if_enabled)
) -> Dict[str, Any]:
    """
    Get cache statistics.
    
    Returns detailed information about the embedding cache including:
    - Current size
    - Hit/miss counts
    - Hit rate
    - TTL configuration
    
    Args:
        cache: Cache dependency (if enabled)
        
    Returns:
        Dictionary with cache statistics or disabled message
    """
    if cache is None:
        return {
            "enabled": False,
            "message": "Caching is disabled"
        }
    
    try:
        return {
            "enabled": True,
            **cache.stats
        }
    except Exception as e:
        logger.exception("Failed to get cache stats")
        return {
            "enabled": True,
            "error": str(e)
        }


@router.post(
    "/cache/clear",
    summary="Clear cache",
    description="Clear all cached embeddings (if caching is enabled)"
)
async def clear_cache(
    cache: EmbeddingCache = Depends(get_cache_if_enabled)
) -> Dict[str, Any]:
    """
    Clear all cached embeddings.
    
    This endpoint removes all entries from the cache, forcing
    fresh computation for subsequent requests.
    
    Args:
        cache: Cache dependency (if enabled)
        
    Returns:
        Dictionary with operation status
    """
    if cache is None:
        return {
            "success": False,
            "message": "Caching is disabled"
        }
    
    try:
        cache.clear()
        return {
            "success": True,
            "message": "Cache cleared successfully"
        }
    except Exception as e:
        logger.exception("Failed to clear cache")
        return {
            "success": False,
            "error": str(e)
        }