- Extracted `OHLCVData` and validation logic into a new `common/ohlcv_data.py` module, promoting better organization and reusability. - Updated `BaseDataCollector` to utilize the new `validate_ohlcv_data` function for improved data validation, enhancing code clarity and maintainability. - Refactored imports in `data/__init__.py` to reflect the new structure, ensuring consistent access to common data types and exceptions. - Removed redundant data validation logic from `BaseDataCollector`, streamlining its responsibilities. - Added unit tests for `OHLCVData` and validation functions to ensure correctness and reliability. These changes improve the architecture of the data module, aligning with project standards for maintainability and performance.
6.1 KiB
6.1 KiB
Relevant Files
data/base_collector.py- The main file to be refactored, whereBaseDataCollectoris defined.data/collector/collector_state_telemetry.py- New file for managing collector status, health, and statistics.data/collector/collector_connection_manager.py- New file for handling connection, disconnection, and reconnection logic.data/collector/collector_callback_dispatcher.py- New file for managing data callbacks and notifications.data/ohlcv_data.py- Potential new file forOHLCVDataand related validation if deemed beneficial.tests/data/test_base_collector.py- Existing test file forBaseDataCollector.tests/data/collector/test_collector_state_telemetry.py- New test file forCollectorStateAndTelemetryclass.tests/data/collector/test_collector_connection_manager.py- New test file forConnectionManagerclass.tests/data/collector/test_collector_callback_dispatcher.py- New test file forCallbackDispatcherclass.tests/data/test_ohlcv_data.py- New test file forOHLCVDataand validation.
Notes
- Unit tests should typically be placed alongside the code files they are testing (e.g.,
MyComponent.tsxandMyComponent.test.tsxin the same directory). - Each refactoring step will be small and verified with existing tests, and new tests will be created for extracted components.
Tasks
-
0.0 Create
data/collectordirectory -
1.0 Extract
CollectorStateAndTelemetryClass- 1.1 Create
data/collector/collector_state_telemetry.py. - 1.2 Move
CollectorStatusenum todata/collector/collector_state_telemetry.py. - 1.3 Move
_statsinitialization and related helper methods (_log_debug,_log_info,_log_warning,_log_error,_log_critical) toCollectorStateAndTelemetry. - 1.4 Move
get_statusandget_health_statusmethods toCollectorStateAndTelemetry. - 1.5 Implement a constructor for
CollectorStateAndTelemetryto receive logger and initial parameters. - 1.6 Add necessary imports to both
data/base_collector.pyanddata/collector/collector_state_telemetry.py. - 1.7 Create
tests/data/collector/test_collector_state_telemetry.pyand add initial tests for the new class.
- 1.1 Create
-
2.0 Extract
ConnectionManagerClass- 2.1 Create
data/collector/collector_connection_manager.py. - 2.2 Move connection-related attributes (
_connection,_reconnect_attempts,_max_reconnect_attempts,_reconnect_delay) toConnectionManager. - 2.3 Move
connect,disconnect,_handle_connection_errormethods toConnectionManager. - 2.4 Implement a constructor for
ConnectionManagerto receive logger and other necessary parameters. - 2.5 Add necessary imports to both
data/base_collector.pyanddata/collector/collector_connection_manager.py. - 2.6 Create
tests/data/collector/test_collector_connection_manager.pyand add initial tests for the new class.
- 2.1 Create
-
3.0 Extract
CallbackDispatcherClass- 3.1 Create
data/collector/collector_callback_dispatcher.py. - 3.2 Move
_data_callbacksattribute toCallbackDispatcher. - 3.3 Move
add_data_callback,remove_data_callback,_notify_callbacksmethods toCallbackDispatcher. - 3.4 Implement a constructor for
CallbackDispatcherto receive logger. - 3.5 Add necessary imports to both
data/base_collector.pyanddata/collector/collector_callback_dispatcher.py. - 3.6 Create
tests/data/collector/test_collector_callback_dispatcher.pyand add initial tests for the new class.
- 3.1 Create
-
4.0 Refactor
BaseDataCollectorto use new components- 4.1 Update
BaseDataCollector.__init__to instantiate and useCollectorStateAndTelemetry,ConnectionManager, andCallbackDispatcherinstances. - 4.2 Replace direct access to moved attributes/methods with calls to the new component instances (e.g.,
self.logger.infobecomesself._state_telemetry.log_info). - 4.3 Modify
start,stop,restart,_message_loop,_health_monitorto interact with the new components, delegating responsibilities appropriately. - 4.4 Update
get_statusandget_health_statusinBaseDataCollectorto delegate toCollectorStateAndTelemetry. - 4.5 Review and update abstract methods and their calls as needed, ensuring they interact correctly with the new components.
- 4.6 Ensure all existing tests for
BaseDataCollectorstill pass after refactoring. - 4.7 Update
data/exchanges/okx/collector.pyto use the newCollectorStateAndTelemetryandConnectionManagerclasses for logging, status updates, and connection handling. - 4.8 Update
data/collector_manager.pyto interact with the newCollectorStateAndTelemetryclass for health checks and status retrieval fromBaseDataCollectorinstances.
- 4.1 Update
-
5.0 Review and potentially extract
OHLCVDataand related validation- 5.1 Analyze if
OHLCVDataandvalidate_ohlcv_dataare frequently used outside ofdata/base_collector.py. - 5.2 If analysis indicates external usage or clear separation benefits, move
OHLCVDataclass andDataValidationErrorto a newdata/ohlcv_data.pyfile. - 5.3 Update imports in
data/base_collector.pyand any other affected files. - 5.4 If
OHLCVDatais extracted, createtests/data/test_ohlcv_data.pywith tests for its structure and validation logic.
- 5.1 Analyze if
-
6.0 Update Module Imports
- 6.1 Update imports in
data/__init__.pyto reflect the new locations ofCollectorStatus,DataCollectorError,DataValidationError,DataType,MarketDataPoint, andOHLCVData(if moved). - 6.2 Update imports in
data/common/data_types.pyforDataTypeandMarketDataPoint. - 6.3 Review and update imports in all test files (
tests/test_refactored_okx.py,tests/test_real_storage.py,tests/test_okx_collector.py,tests/test_exchange_factory.py,tests/test_data_collection_aggregation.py,tests/test_collector_manager.py,tests/test_base_collector.py,tests/database/test_database_operations.py) and scripts (scripts/production_clean.py) that import directly fromdata.base_collector.
- 6.1 Update imports in