perf: async Proxmox API, optimized IPAM queries, log cleanup scheduling, background template import

- proxmox_client.py: add ThreadPoolExecutor(max_workers=20) + async wrapper
  methods (async_get_nodes, async_get_vms, async_get_containers,
  async_get_node_status, async_get_vm_status, async_get_container_status,
  async_get_storages, async_get_all_resources) to avoid blocking event loop
- ipam_service.py: replace full-network allocation scan + Python IP loop with
  per-pool targeted SQL query — only loads IPs for the specific pool being searched
- api/ipam.py: fix N+1 full-table scans on GET /api/allocations — use IN-filtered
  queries on only the network/pool IDs present in the result set
- monitoring_worker.py: wire LoggingService.cleanup_old_logs(days=30) into the
  existing run_cleanup_expired() job so audit logs don't grow unboundedly
- api/templates.py: convert auto_import_templates to async endpoint using
  BackgroundTasks — returns immediately, heavy Proxmox I/O runs in background

https://claude.ai/code/session_019rK2wvaWg5zXFwmAPPEB7R
This commit is contained in:
Claude
2026-03-07 11:48:53 +00:00
parent f4b6b36f3b
commit e05650cc9b
5 changed files with 165 additions and 117 deletions

View File

@@ -18,6 +18,7 @@ try:
from backend.app.db import SessionLocal
from backend.app.models import User, ProxmoxServer, PanelSettings, VMInstance, IPAMAllocation, Notification
from backend.app.services.notification_service import NotificationService
from backend.app.logging_service import LoggingService
from backend.app.proxmox_client import ProxmoxClient
from backend.app.schemas import NotificationCreate
from backend.app.i18n import t
@@ -25,6 +26,7 @@ except ImportError:
from app.db import SessionLocal
from app.models import User, ProxmoxServer, PanelSettings, VMInstance, IPAMAllocation, Notification
from app.services.notification_service import NotificationService
from app.logging_service import LoggingService
from app.proxmox_client import ProxmoxClient
from app.schemas import NotificationCreate
from app.i18n import t
@@ -531,7 +533,7 @@ class MonitoringWorker:
db.close()
def run_cleanup_expired(self):
"""Clean up expired notifications"""
"""Clean up expired notifications and old audit logs"""
db = SessionLocal()
try:
count = NotificationService.cleanup_expired(db)
@@ -541,6 +543,14 @@ class MonitoringWorker:
logger.error(f"Error cleaning up notifications: {e}")
finally:
db.close()
db = SessionLocal()
try:
LoggingService.cleanup_old_logs(db, days=30)
except Exception as e:
logger.error(f"Error cleaning up old audit logs: {e}")
finally:
db.close()
def sync_vm_cache(self):
"""