Files
aitbc/cli/handlers/pool_hub.py
aitbc a7b6e39cdf fix: replace all print() with click.echo() or logger in CLI production code
- 55 CLI files: handlers/, aitbc_cli/commands/, cli/core/, cli/utils/, top-level scripts
- Click-based files: print() -> click.echo()
- Library modules: print() -> logger.info/error/warning
- Fixed pre-existing indentation bugs in monitor.py dashboard function
- Fixed bare print() -> logger.info('') in chain_manager.py
- 0 remaining print() in production CLI code
- All files compile cleanly
2026-05-25 13:53:49 +02:00

193 lines
7.8 KiB
Python

"""Pool hub SLA and capacity management handlers."""
from aitbc import AITBCHTTPClient, NetworkError
import logging
logger = logging.getLogger(__name__)
def handle_pool_hub_sla_metrics(args):
"""Get SLA metrics for a miner or all miners."""
try:
from commands.legacy.pool_hub import get_config as get_pool_hub_config
config = get_pool_hub_config()
if args.test_mode:
logger.info(" SLA Metrics (test mode):")
logger.info(" Uptime: 97.5%")
logger.info(" Response Time: 850ms")
logger.info(" Job Completion Rate: 92.3%")
return
pool_hub_url = getattr(config, "pool_hub_url", "http://localhost:8012")
miner_id = getattr(args, "miner_id", None)
http_client = AITBCHTTPClient(base_url=pool_hub_url, timeout=30)
if miner_id:
metrics = http_client.get(f"/v1/sla/metrics/{miner_id}")
else:
metrics = http_client.get("/v1/sla/metrics")
logger.info(" SLA Metrics:")
for key, value in metrics.items():
logger.info(f" {key}: {value}")
except NetworkError as e:
logger.error(f"❌ Failed to get SLA metrics: {e}")
except Exception as e:
logger.error(f"❌ Error getting SLA metrics: {e}")
def handle_pool_hub_sla_violations(args):
"""Get SLA violations across all miners."""
try:
from commands.legacy.pool_hub import get_config as get_pool_hub_config
config = get_pool_hub_config()
if args.test_mode:
logger.info("⚠️ SLA Violations (test mode):")
logger.info(" miner_001: response_time violation")
return
pool_hub_url = getattr(config, "pool_hub_url", "http://localhost:8012")
http_client = AITBCHTTPClient(base_url=pool_hub_url, timeout=30)
violations = http_client.get("/v1/sla/violations")
logger.info("⚠️ SLA Violations:")
for v in violations:
logger.info(f" {v}")
except NetworkError as e:
logger.error(f"❌ Failed to get violations: {e}")
except Exception as e:
logger.error(f"❌ Error getting violations: {e}")
def handle_pool_hub_capacity_snapshots(args):
"""Get capacity planning snapshots."""
try:
from commands.legacy.pool_hub import get_config as get_pool_hub_config
config = get_pool_hub_config()
if args.test_mode:
logger.info("📊 Capacity Snapshots (test mode):")
logger.info(" Total Capacity: 1250 GPU")
logger.info(" Available: 320 GPU")
return
pool_hub_url = getattr(config, "pool_hub_url", "http://localhost:8012")
http_client = AITBCHTTPClient(base_url=pool_hub_url, timeout=30)
snapshots = http_client.get("/v1/sla/capacity/snapshots")
logger.info("📊 Capacity Snapshots:")
for s in snapshots:
logger.info(f" {s}")
except NetworkError as e:
logger.error(f"❌ Failed to get snapshots: {e}")
except Exception as e:
logger.error(f"❌ Error getting snapshots: {e}")
def handle_pool_hub_capacity_forecast(args):
"""Get capacity forecast."""
try:
from commands.legacy.pool_hub import get_config as get_pool_hub_config
config = get_pool_hub_config()
if args.test_mode:
logger.info("🔮 Capacity Forecast (test mode):")
logger.info(" Projected Capacity: 1400 GPU")
logger.info(" Growth Rate: 12%")
return
pool_hub_url = getattr(config, "pool_hub_url", "http://localhost:8012")
http_client = AITBCHTTPClient(base_url=pool_hub_url, timeout=30)
forecast = http_client.get("/v1/sla/capacity/forecast")
logger.info("🔮 Capacity Forecast:")
for key, value in forecast.items():
logger.info(f" {key}: {value}")
except NetworkError as e:
logger.error(f"❌ Failed to get forecast: {e}")
except Exception as e:
logger.error(f"❌ Error getting forecast: {e}")
def handle_pool_hub_capacity_recommendations(args):
"""Get scaling recommendations."""
try:
from commands.legacy.pool_hub import get_config as get_pool_hub_config
config = get_pool_hub_config()
if args.test_mode:
logger.info("💡 Capacity Recommendations (test mode):")
logger.info(" Type: scale_up")
logger.info(" Action: Add 50 GPU capacity")
return
pool_hub_url = getattr(config, "pool_hub_url", "http://localhost:8012")
http_client = AITBCHTTPClient(base_url=pool_hub_url, timeout=30)
recommendations = http_client.get("/v1/sla/capacity/recommendations")
logger.info("💡 Capacity Recommendations:")
for r in recommendations:
logger.info(f" {r}")
except NetworkError as e:
logger.error(f"❌ Failed to get recommendations: {e}")
except Exception as e:
logger.error(f"❌ Error getting recommendations: {e}")
def handle_pool_hub_billing_usage(args):
"""Get billing usage data."""
try:
from commands.legacy.pool_hub import get_config as get_pool_hub_config
config = get_pool_hub_config()
if args.test_mode:
logger.info("💰 Billing Usage (test mode):")
logger.info(" Total GPU Hours: 45678")
logger.info(" Total Cost: $12500.50")
return
pool_hub_url = getattr(config, "pool_hub_url", "http://localhost:8012")
http_client = AITBCHTTPClient(base_url=pool_hub_url, timeout=30)
usage = http_client.get("/v1/sla/billing/usage")
logger.info("💰 Billing Usage:")
for key, value in usage.items():
logger.info(f" {key}: {value}")
except NetworkError as e:
logger.error(f"❌ Failed to get billing usage: {e}")
except Exception as e:
logger.error(f"❌ Error getting billing usage: {e}")
def handle_pool_hub_billing_sync(args):
"""Trigger billing sync with coordinator-api."""
try:
from commands.legacy.pool_hub import get_config as get_pool_hub_config
config = get_pool_hub_config()
if args.test_mode:
logger.info("🔄 Billing sync triggered (test mode)")
logger.info("✅ Sync completed successfully")
return
pool_hub_url = getattr(config, "pool_hub_url", "http://localhost:8012")
http_client = AITBCHTTPClient(base_url=pool_hub_url, timeout=60)
result = http_client.post("/v1/sla/billing/sync")
logger.info("🔄 Billing sync triggered")
logger.info(f"{result.get('message', 'Success')}")
except NetworkError as e:
logger.error(f"❌ Billing sync failed: {e}")
except Exception as e:
logger.error(f"❌ Error triggering billing sync: {e}")
def handle_pool_hub_collect_metrics(args):
"""Trigger SLA metrics collection."""
try:
from commands.legacy.pool_hub import get_config as get_pool_hub_config
config = get_pool_hub_config()
if args.test_mode:
logger.info("📊 SLA metrics collection triggered (test mode)")
logger.info("✅ Collection completed successfully")
return
pool_hub_url = getattr(config, "pool_hub_url", "http://localhost:8012")
http_client = AITBCHTTPClient(base_url=pool_hub_url, timeout=60)
result = http_client.post("/v1/sla/metrics/collect")
logger.info("📊 SLA metrics collection triggered")
logger.info(f"{result.get('message', 'Success')}")
except NetworkError as e:
logger.error(f"❌ Metrics collection failed: {e}")
except Exception as e:
logger.error(f"❌ Error triggering metrics collection: {e}")