Data Processor¶
data_processor ¶
Data Processor - Orchestrate Data Processing and Merge Operations.
Provides the main orchestrator for processing user data, coordinating dataset merging with multiple databases, and preparing results. Acts as the central coordinator for the entire processing pipeline.
Implements defensive programming with try/except per stage, circuit breaker, DataFrame validation, timeout handling, retry logic, and structured logging. All settings are loaded from config.settings and can be overridden via environment variables.
Classes:
| Name | Description |
|---|---|
DataProcessor | Orchestrates multi-stage data processing workflow |
CircuitBreaker | Simple circuit breaker for fault tolerance |
Classes¶
CircuitBreaker ¶
CircuitBreaker(failure_threshold: int = CIRCUIT_BREAKER_THRESHOLD, timeout: int = CIRCUIT_BREAKER_TIMEOUT)
Simple circuit breaker to prevent cascading failures.
Attributes:
| Name | Type | Description |
|---|---|---|
failure_threshold | int | Number of failures before opening circuit |
timeout | int | Seconds before attempting to close circuit |
failures | int | Current failure count |
last_failure_time | float | Timestamp of last failure |
is_open | bool | Whether circuit is currently open |
Initialize circuit breaker.
Source code in src/application/core/data_processor.py
DataProcessor ¶
DataProcessor(cache_service: CacheService, progress_tracker: ProgressTracker, merge_service: Optional[MergeService] = None, timeout_seconds: int = DEFAULT_TIMEOUT_SECONDS)
Orchestrate data processing with robust error handling.
Coordinates the entire processing pipeline including cache checking, progress tracking, database merging, and result preparation with defensive programming practices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_service | CacheService | Service for caching merge results | required |
progress_tracker | ProgressTracker | Tracker for progress updates | required |
merge_service | Optional[MergeService] | Domain merge service (uses default if None) | None |
timeout_seconds | int | Timeout for processing operations (default: 60) | DEFAULT_TIMEOUT_SECONDS |
Attributes:
| Name | Type | Description |
|---|---|---|
_cache | CacheService | Cache service instance |
_tracker | ProgressTracker | Progress tracker instance |
_merge_service | MergeService | Domain merge service |
_timeout_seconds | int | Timeout for operations |
_circuit_breakers | dict | Circuit breakers for each database |
Methods:
| Name | Description |
|---|---|
process | Process dataset through full pipeline |
_merge_with_retry | Execute merge with retry logic |
_validate_dataframe | Validate DataFrame is not empty |
Notes
Processing pipeline with error handling: 1. Cache check 2. BioRemPP merge (with retry + circuit breaker) 3. KEGG merge (with retry + circuit breaker) 4. HADEG merge (with retry + circuit breaker) 5. ToxCSM merge (with retry + circuit breaker) 6. Result preparation + validation 7. Cache storage
Uses dependency injection for testability.
Initialize DataProcessor with dependencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_service | CacheService | Cache service for merge results | required |
progress_tracker | ProgressTracker | Progress tracker for UI updates | required |
merge_service | Optional[MergeService] | Merge service (uses default if None) | None |
timeout_seconds | int | Timeout for operations | DEFAULT_TIMEOUT_SECONDS |
Source code in src/application/core/data_processor.py
Functions¶
process ¶
Process dataset through complete pipeline with error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset | Dataset | Validated dataset to process | required |
session_id | str | Session identifier for tracking | required |
Returns:
| Type | Description |
|---|---|
MergedDataDTO | Result DTO with merged data |
Raises:
| Type | Description |
|---|---|
DataProcessingTimeoutError | If processing exceeds timeout |
StageProcessingError | If a critical stage fails |
Notes
Pipeline stages with error handling: 1. Check cache 2. BioRemPP merge (critical - with retry) 3. KEGG merge (with retry) 4. HADEG merge (with retry) 5. ToxCSM merge (with retry) 6. Result preparation + validation 7. Cache storage
Source code in src/application/core/data_processor.py
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 | |
prepare_result ¶
Prepare final result DTO from merge results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
merge_results | dict | Dictionary with merge results | required |
cache_key | str | Cache key for this result | required |
processing_time | float | Time taken for processing | required |
Returns:
| Type | Description |
|---|---|
MergedDataDTO | Final result DTO |
Source code in src/application/core/data_processor.py
Functions¶
timeout_handler ¶
Context manager for timeout handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds | int | Timeout in seconds | DEFAULT_TIMEOUT_SECONDS |
Raises:
| Type | Description |
|---|---|
DataProcessingTimeoutError | If operation exceeds timeout |
Notes
- Uses signal.alarm for Unix-like systems
- For Windows, timeout is advisory only