Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
535 changes: 499 additions & 36 deletions bots/controllers/generic/pmm_mister.py

Large diffs are not rendered by default.

15 changes: 3 additions & 12 deletions bots/scripts/v2_with_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,12 @@ def check_max_controller_drawdown(self):
filter_func=lambda x: x.is_active and not x.is_trading,
)
self.executor_orchestrator.execute_actions(
actions=[
StopExecutorAction(controller_id=controller_id, executor_id=executor.id)
for executor in executors_order_placed
]
actions=[StopExecutorAction(controller_id=controller_id, executor_id=executor.id) for executor in executors_order_placed]
)
self.drawdown_exited_controllers.append(controller_id)

def check_max_global_drawdown(self):
current_global_pnl = sum([
self.get_performance_report(controller_id).global_pnl_quote
for controller_id in self.controllers.keys()
])
current_global_pnl = sum([self.get_performance_report(controller_id).global_pnl_quote for controller_id in self.controllers.keys()])
if current_global_pnl > self.max_global_pnl:
self.max_global_pnl = current_global_pnl
else:
Expand All @@ -103,10 +97,7 @@ def get_controller_report(self, controller_id: str) -> dict:

def send_performance_report(self):
if self.current_timestamp - self._last_performance_report_timestamp >= self.performance_report_interval and self._pub:
controller_reports = {
controller_id: self.get_controller_report(controller_id)
for controller_id in self.controllers.keys()
}
controller_reports = {controller_id: self.get_controller_report(controller_id) for controller_id in self.controllers.keys()}
self._pub(controller_reports)
self._last_performance_report_timestamp = self.current_timestamp

Expand Down
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BrokerSettings(BaseSettings):
port: int = Field(default=1883, description="MQTT broker port")
username: str = Field(default="admin", description="MQTT broker username")
password: str = Field(default="password", description="MQTT broker password")
performance_dump_interval: int = Field(default=5, description="Controller performance dump interval in minutes")

model_config = SettingsConfigDict(env_prefix="BROKER_", extra="ignore")

Expand Down
31 changes: 29 additions & 2 deletions database/repositories/bot_run_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime, timezone
from typing import Dict, List, Optional, Any

from sqlalchemy import desc, select, and_, or_, func
from sqlalchemy import delete, desc, select, and_, or_, func
from sqlalchemy.ext.asyncio import AsyncSession

from database.models import BotRun
Expand Down Expand Up @@ -188,4 +188,31 @@ async def get_bot_run_stats(self) -> Dict[str, Any]:
"active_runs": active_runs,
"strategy_type_counts": strategy_counts,
"status_counts": status_counts
}
}

async def delete_bot_run(self, bot_run_id: int) -> Optional[BotRun]:
"""Delete a bot run record by ID. Returns the deleted record or None."""
stmt = select(BotRun).where(BotRun.id == bot_run_id)
result = await self.session.execute(stmt)
bot_run = result.scalar_one_or_none()

if bot_run:
await self.session.delete(bot_run)
await self.session.flush()

return bot_run

async def delete_bot_runs_by_bot_name(self, bot_name: str) -> int:
"""Delete all bot run records for a given bot_name. Returns count deleted."""
stmt = select(BotRun).where(BotRun.bot_name == bot_name)
result = await self.session.execute(stmt)
bot_runs = result.scalars().all()

count = len(bot_runs)
for bot_run in bot_runs:
await self.session.delete(bot_run)

if count > 0:
await self.session.flush()

return count
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ async def lifespan(app: FastAPI):
broker_host=settings.broker.host,
broker_port=settings.broker.port,
broker_username=settings.broker.username,
broker_password=settings.broker.password
broker_password=settings.broker.password,
performance_dump_interval=settings.broker.performance_dump_interval
)

backtesting_service = BacktestingService()
Expand Down
33 changes: 29 additions & 4 deletions routers/archived_bots.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import logging
from typing import List, Optional

from fastapi import APIRouter, HTTPException, Query
from fastapi import APIRouter, Depends, HTTPException, Query

from database import AsyncDatabaseManager, BotRunRepository
from deps import get_database_manager
from utils.file_system import fs_util
from utils.hummingbot_database_reader import HummingbotDatabase

logger = logging.getLogger(__name__)

router = APIRouter(tags=["Archived Bots"], prefix="/archived-bots")


Expand All @@ -20,19 +25,22 @@ async def list_databases():


@router.delete("/{db_path:path}")
async def delete_archived_bot(db_path: str):
async def delete_archived_bot(
db_path: str,
db_manager: AsyncDatabaseManager = Depends(get_database_manager)
):
"""
Delete an archived bot and its entire directory.
Also attempts to delete matching BotRun records from PostgreSQL (best-effort).

Args:
db_path: Path to the database file (as returned by list_databases)

Returns:
Confirmation message with the deleted bot name
Confirmation message with the deleted bot name and count of cleaned PG records
"""
try:
bot_name = fs_util.delete_archived_bot(db_path)
return {"message": f"Archived bot '{bot_name}' deleted successfully", "bot_name": bot_name}
except FileNotFoundError as e:
raise HTTPException(status_code=404, detail=str(e))
except ValueError as e:
Expand All @@ -42,6 +50,23 @@ async def delete_archived_bot(db_path: str):
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error deleting archived bot: {str(e)}")

# Best-effort: also clean matching BotRun records from PG
bot_runs_deleted = 0
try:
async with db_manager.get_session_context() as session:
bot_run_repo = BotRunRepository(session)
bot_runs_deleted = await bot_run_repo.delete_bot_runs_by_bot_name(bot_name)
if bot_runs_deleted > 0:
logger.info(f"Deleted {bot_runs_deleted} bot run record(s) for '{bot_name}'")
except Exception as e:
logger.warning(f"Failed to clean bot run records for '{bot_name}': {e}")

return {
"message": f"Archived bot '{bot_name}' deleted successfully",
"bot_name": bot_name,
"bot_runs_deleted": bot_runs_deleted
}


@router.get("/{db_path:path}/status")
async def get_database_status(db_path: str):
Expand Down
55 changes: 55 additions & 0 deletions routers/bot_orchestration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import logging
import os
import shutil
from datetime import datetime, timezone

from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Query
Expand Down Expand Up @@ -387,6 +388,60 @@ async def get_bot_run_by_id(
raise HTTPException(status_code=500, detail=str(e))


@router.delete("/bot-runs/{bot_run_id}")
async def delete_bot_run(
bot_run_id: int,
db_manager: AsyncDatabaseManager = Depends(get_database_manager)
):
"""
Delete a bot run record by ID.

Args:
bot_run_id: ID of the bot run to delete
db_manager: Database manager dependency

Returns:
Confirmation of deletion

Raises:
HTTPException: 404 if bot run not found
"""
try:
async with db_manager.get_session_context() as session:
bot_run_repo = BotRunRepository(session)
bot_run = await bot_run_repo.delete_bot_run(bot_run_id)

if not bot_run:
raise HTTPException(status_code=404, detail=f"Bot run {bot_run_id} not found")

# Also delete the archived bot folder if it exists
archived_dir = os.path.join('bots', 'archived', bot_run.instance_name)
archived_deleted = False
if os.path.isdir(archived_dir):
try:
import subprocess, platform
if platform.system() == 'Darwin':
# Strip macOS ACLs (Docker adds "deny delete" ACLs)
subprocess.run(['chmod', '-R', '-N', archived_dir], check=False)
shutil.rmtree(archived_dir)
archived_deleted = True
logger.info(f"Deleted archived folder: {archived_dir}")
except Exception as e:
logger.warning(f"Failed to delete archived folder {archived_dir}: {e}")

return {
"status": "success",
"message": f"Bot run {bot_run_id} deleted successfully",
"bot_name": bot_run.bot_name,
"archived_folder_deleted": archived_deleted
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Failed to delete bot run {bot_run_id}: {e}")
raise HTTPException(status_code=500, detail=str(e))


@router.get("/bot-runs/stats")
async def get_bot_run_stats(
db_manager: AsyncDatabaseManager = Depends(get_database_manager)
Expand Down
8 changes: 5 additions & 3 deletions utils/file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,11 @@ def delete_archived_bot(self, db_path: str) -> str:
if not os.path.isdir(archived_bot_dir):
raise FileNotFoundError(f"Archived bot directory '{bot_name}' not found")

shutil.rmtree(archived_bot_dir, ignore_errors=False, onerror=lambda func, path, exc: (
os.chmod(path, 0o777), func(path)
))
import subprocess, platform
if platform.system() == 'Darwin':
# Strip macOS ACLs (Docker adds "deny delete" ACLs)
subprocess.run(['chmod', '-R', '-N', archived_bot_dir], check=False)
shutil.rmtree(archived_bot_dir)
return bot_name

def list_databases(self) -> List[str]:
Expand Down
Loading