forked from ruvnet/RuView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
310 lines (247 loc) · 10.7 KB
/
config.py
File metadata and controls
310 lines (247 loc) · 10.7 KB
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
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
"""
Centralized configuration management for WiFi-DensePose API
"""
import os
import logging
from pathlib import Path
from typing import Dict, Any, Optional, List
from functools import lru_cache
from src.config.settings import Settings, get_settings
from src.config.domains import DomainConfig, get_domain_config
logger = logging.getLogger(__name__)
class ConfigManager:
"""Centralized configuration manager."""
def __init__(self):
self._settings: Optional[Settings] = None
self._domain_config: Optional[DomainConfig] = None
self._environment_overrides: Dict[str, Any] = {}
@property
def settings(self) -> Settings:
"""Get application settings."""
if self._settings is None:
self._settings = get_settings()
return self._settings
@property
def domain_config(self) -> DomainConfig:
"""Get domain configuration."""
if self._domain_config is None:
self._domain_config = get_domain_config()
return self._domain_config
def reload_settings(self) -> Settings:
"""Reload settings from environment."""
self._settings = None
return self.settings
def reload_domain_config(self) -> DomainConfig:
"""Reload domain configuration."""
self._domain_config = None
return self.domain_config
def set_environment_override(self, key: str, value: Any):
"""Set environment variable override."""
self._environment_overrides[key] = value
os.environ[key] = str(value)
def get_environment_override(self, key: str, default: Any = None) -> Any:
"""Get environment variable override."""
return self._environment_overrides.get(key, os.environ.get(key, default))
def clear_environment_overrides(self):
"""Clear all environment overrides."""
for key in self._environment_overrides:
if key in os.environ:
del os.environ[key]
self._environment_overrides.clear()
def get_database_config(self) -> Dict[str, Any]:
"""Get database configuration."""
settings = self.settings
config = {
"url": settings.get_database_url(),
"pool_size": settings.database_pool_size,
"max_overflow": settings.database_max_overflow,
"echo": settings.is_development and settings.debug,
"pool_pre_ping": True,
"pool_recycle": 3600, # 1 hour
}
return config
def get_redis_config(self) -> Optional[Dict[str, Any]]:
"""Get Redis configuration."""
settings = self.settings
redis_url = settings.get_redis_url()
if not redis_url:
return None
config = {
"url": redis_url,
"password": settings.redis_password,
"db": settings.redis_db,
"decode_responses": True,
"socket_connect_timeout": 5,
"socket_timeout": 5,
"retry_on_timeout": True,
"health_check_interval": 30,
}
return config
def get_logging_config(self) -> Dict[str, Any]:
"""Get logging configuration."""
return self.settings.get_logging_config()
def get_cors_config(self) -> Dict[str, Any]:
"""Get CORS configuration."""
return self.settings.get_cors_config()
def get_security_config(self) -> Dict[str, Any]:
"""Get security configuration."""
settings = self.settings
config = {
"secret_key": settings.secret_key,
"jwt_algorithm": settings.jwt_algorithm,
"jwt_expire_hours": settings.jwt_expire_hours,
"allowed_hosts": settings.allowed_hosts,
"enable_authentication": settings.enable_authentication,
}
return config
def get_hardware_config(self) -> Dict[str, Any]:
"""Get hardware configuration."""
settings = self.settings
domain_config = self.domain_config
config = {
"wifi_interface": settings.wifi_interface,
"csi_buffer_size": settings.csi_buffer_size,
"polling_interval": settings.hardware_polling_interval,
"mock_hardware": settings.mock_hardware,
"routers": [router.dict() for router in domain_config.routers],
}
return config
def get_pose_config(self) -> Dict[str, Any]:
"""Get pose estimation configuration."""
settings = self.settings
domain_config = self.domain_config
config = {
"model_path": settings.pose_model_path,
"confidence_threshold": settings.pose_confidence_threshold,
"batch_size": settings.pose_processing_batch_size,
"max_persons": settings.pose_max_persons,
"mock_pose_data": settings.mock_pose_data,
"models": [model.dict() for model in domain_config.pose_models],
}
return config
def get_streaming_config(self) -> Dict[str, Any]:
"""Get streaming configuration."""
settings = self.settings
domain_config = self.domain_config
config = {
"fps": settings.stream_fps,
"buffer_size": settings.stream_buffer_size,
"websocket_ping_interval": settings.websocket_ping_interval,
"websocket_timeout": settings.websocket_timeout,
"enable_websockets": settings.enable_websockets,
"enable_real_time_processing": settings.enable_real_time_processing,
"max_connections": domain_config.streaming.max_connections,
"compression": domain_config.streaming.compression,
}
return config
def get_storage_config(self) -> Dict[str, Any]:
"""Get storage configuration."""
settings = self.settings
config = {
"data_path": Path(settings.data_storage_path),
"model_path": Path(settings.model_storage_path),
"temp_path": Path(settings.temp_storage_path),
"max_size_gb": settings.max_storage_size_gb,
"enable_historical_data": settings.enable_historical_data,
}
# Ensure directories exist
for path in [config["data_path"], config["model_path"], config["temp_path"]]:
path.mkdir(parents=True, exist_ok=True)
return config
def get_monitoring_config(self) -> Dict[str, Any]:
"""Get monitoring configuration."""
settings = self.settings
config = {
"metrics_enabled": settings.metrics_enabled,
"health_check_interval": settings.health_check_interval,
"performance_monitoring": settings.performance_monitoring,
"log_level": settings.log_level,
"log_file": settings.log_file,
}
return config
def get_rate_limiting_config(self) -> Dict[str, Any]:
"""Get rate limiting configuration."""
settings = self.settings
config = {
"enabled": settings.enable_rate_limiting,
"requests": settings.rate_limit_requests,
"authenticated_requests": settings.rate_limit_authenticated_requests,
"window": settings.rate_limit_window,
}
return config
def validate_configuration(self) -> List[str]:
"""Validate complete configuration and return issues."""
issues = []
try:
# Validate settings
from src.config.settings import validate_settings
settings_issues = validate_settings(self.settings)
issues.extend(settings_issues)
# Validate database configuration
try:
db_config = self.get_database_config()
if not db_config["url"]:
issues.append("Database URL is not configured")
except Exception as e:
issues.append(f"Database configuration error: {e}")
# Validate storage paths
try:
storage_config = self.get_storage_config()
for name, path in storage_config.items():
if name.endswith("_path") and not path.exists():
issues.append(f"Storage path does not exist: {path}")
except Exception as e:
issues.append(f"Storage configuration error: {e}")
# Validate hardware configuration
try:
hw_config = self.get_hardware_config()
if not hw_config["routers"]:
issues.append("No routers configured")
except Exception as e:
issues.append(f"Hardware configuration error: {e}")
# Validate pose configuration
try:
pose_config = self.get_pose_config()
if not pose_config["models"]:
issues.append("No pose models configured")
except Exception as e:
issues.append(f"Pose configuration error: {e}")
except Exception as e:
issues.append(f"Configuration validation error: {e}")
return issues
def get_full_config(self) -> Dict[str, Any]:
"""Get complete configuration dictionary."""
return {
"settings": self.settings.dict(),
"domain_config": self.domain_config.to_dict(),
"database": self.get_database_config(),
"redis": self.get_redis_config(),
"security": self.get_security_config(),
"hardware": self.get_hardware_config(),
"pose": self.get_pose_config(),
"streaming": self.get_streaming_config(),
"storage": self.get_storage_config(),
"monitoring": self.get_monitoring_config(),
"rate_limiting": self.get_rate_limiting_config(),
}
# Global configuration manager instance
@lru_cache()
def get_config_manager() -> ConfigManager:
"""Get cached configuration manager instance."""
return ConfigManager()
# Convenience functions
def get_app_settings() -> Settings:
"""Get application settings."""
return get_config_manager().settings
def get_app_domain_config() -> DomainConfig:
"""Get domain configuration."""
return get_config_manager().domain_config
def validate_app_configuration() -> List[str]:
"""Validate application configuration."""
return get_config_manager().validate_configuration()
def reload_configuration():
"""Reload all configuration."""
config_manager = get_config_manager()
config_manager.reload_settings()
config_manager.reload_domain_config()
logger.info("Configuration reloaded")