Refactor database operations and enhance repository structure
- Introduced a modular repository structure by creating separate repository classes for `Bot`, `MarketData`, and `RawTrade`, improving code organization and maintainability. - Updated the `DatabaseOperations` class to utilize the new repository classes, enhancing the abstraction of database interactions. - Refactored the `.env` file to update database connection parameters and add new logging and health monitoring configurations. - Modified the `okx_config.json` to change default timeframes for trading pairs, aligning with updated requirements. - Added comprehensive unit tests for the new repository classes, ensuring robust functionality and reliability. These changes improve the overall architecture of the database layer, making it more scalable and easier to manage.
This commit is contained in:
@@ -37,11 +37,11 @@ The Database Operations module (`database/operations.py`) provides a clean, cent
|
||||
│ └─────────────────────────────────────────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌─────────────────┐ ┌─────────────────┐ ┌──────────────┐ │
|
||||
│ │MarketDataRepo │ │RawTradeRepo │ │ Future │ │
|
||||
│ │ │ │ │ │ Repositories │ │
|
||||
│ │ • upsert_candle │ │ • insert_data │ │ • OrderBook │ │
|
||||
│ │ • get_candles │ │ • get_trades │ │ • UserTrades │ │
|
||||
│ │ • get_latest │ │ • raw_websocket │ │ • Positions │ │
|
||||
│ │MarketDataRepo │ │RawTradeRepo │ │ BotRepo │ │
|
||||
│ │ │ │ │ │ │ │
|
||||
│ │ • upsert_candle │ │ • insert_data │ │ • add │ │
|
||||
│ │ • get_candles │ │ • get_trades │ │ • get_by_id │ │
|
||||
│ │ • get_latest │ │ • raw_websocket │ │ • update/delete│ │
|
||||
│ └─────────────────┘ └─────────────────┘ └──────────────┘ │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
@@ -118,8 +118,9 @@ async def main():
|
||||
|
||||
# Check statistics
|
||||
stats = db.get_stats()
|
||||
print(f"Total bots: {stats['bot_count']}")
|
||||
print(f"Total candles: {stats['candle_count']}")
|
||||
print(f"Total raw trades: {stats['trade_count']}")
|
||||
print(f"Total raw trades: {stats['raw_trade_count']}")
|
||||
|
||||
asyncio.run(main())
|
||||
```
|
||||
@@ -148,8 +149,9 @@ Get comprehensive database statistics.
|
||||
|
||||
```python
|
||||
stats = db.get_stats()
|
||||
print(f"Bots: {stats['bot_count']:,}")
|
||||
print(f"Candles: {stats['candle_count']:,}")
|
||||
print(f"Raw trades: {stats['trade_count']:,}")
|
||||
print(f"Raw trades: {stats['raw_trade_count']:,}")
|
||||
print(f"Health: {stats['healthy']}")
|
||||
```
|
||||
|
||||
@@ -212,6 +214,81 @@ else:
|
||||
print("No candles found")
|
||||
```
|
||||
|
||||
### BotRepository
|
||||
|
||||
Repository for `bots` table operations.
|
||||
|
||||
#### Methods
|
||||
|
||||
##### `add(bot_data: Dict[str, Any]) -> Bot`
|
||||
|
||||
Adds a new bot to the database.
|
||||
|
||||
**Parameters:**
|
||||
- `bot_data`: Dictionary containing the bot's attributes (`name`, `strategy_name`, etc.)
|
||||
|
||||
**Returns:** The newly created `Bot` object.
|
||||
|
||||
```python
|
||||
from decimal import Decimal
|
||||
|
||||
bot_data = {
|
||||
"name": "MyTestBot",
|
||||
"strategy_name": "SimpleMACD",
|
||||
"symbol": "BTC-USDT",
|
||||
"timeframe": "1h",
|
||||
"status": "inactive",
|
||||
"virtual_balance": Decimal("10000"),
|
||||
}
|
||||
new_bot = db.bots.add(bot_data)
|
||||
print(f"Added bot with ID: {new_bot.id}")
|
||||
```
|
||||
|
||||
##### `get_by_id(bot_id: int) -> Optional[Bot]`
|
||||
|
||||
Retrieves a bot by its unique ID.
|
||||
|
||||
```python
|
||||
bot = db.bots.get_by_id(1)
|
||||
if bot:
|
||||
print(f"Found bot: {bot.name}")
|
||||
```
|
||||
|
||||
##### `get_by_name(name: str) -> Optional[Bot]`
|
||||
|
||||
Retrieves a bot by its unique name.
|
||||
|
||||
```python
|
||||
bot = db.bots.get_by_name("MyTestBot")
|
||||
if bot:
|
||||
print(f"Found bot with ID: {bot.id}")
|
||||
```
|
||||
|
||||
##### `update(bot_id: int, update_data: Dict[str, Any]) -> Optional[Bot]`
|
||||
|
||||
Updates an existing bot's attributes.
|
||||
|
||||
```python
|
||||
from datetime import datetime, timezone
|
||||
|
||||
update_payload = {"status": "active", "last_heartbeat": datetime.now(timezone.utc)}
|
||||
updated_bot = db.bots.update(1, update_payload)
|
||||
if updated_bot:
|
||||
print(f"Bot status updated to: {updated_bot.status}")
|
||||
```
|
||||
|
||||
##### `delete(bot_id: int) -> bool`
|
||||
|
||||
Deletes a bot from the database.
|
||||
|
||||
**Returns:** `True` if deletion was successful, `False` otherwise.
|
||||
|
||||
```python
|
||||
success = db.bots.delete(1)
|
||||
if success:
|
||||
print("Bot deleted successfully.")
|
||||
```
|
||||
|
||||
### RawTradeRepository
|
||||
|
||||
Repository for `raw_trades` table operations (raw WebSocket data).
|
||||
|
||||
Reference in New Issue
Block a user