from sqlalchemy.orm import Session from app.database import ActionLog from typing import Optional import json class LoggingService: @staticmethod def log_action( db: Session, action_type: str, action: str, success: bool = True, message: Optional[str] = None, target_id: Optional[int] = None, target_name: Optional[str] = None, details: Optional[dict] = None ): """Log an action to the unified action log""" log_entry = ActionLog( action_type=action_type, target_id=target_id, target_name=target_name, action=action, success=success, message=message, details=json.dumps(details) if details else None ) db.add(log_entry) db.commit() @staticmethod def log_wol_action(db: Session, server_id: int, server_name: str, success: bool, message: str): """Log WOL action""" LoggingService.log_action( db=db, action_type="wol", action="wake", success=success, message=message, target_id=server_id, target_name=server_name ) @staticmethod def log_proxmox_vm_action(db: Session, action: str, vmid: str, vm_name: str, node: str, success: bool, message: str): """Log Proxmox VM actions (start/stop)""" LoggingService.log_action( db=db, action_type="proxmox", action=action, success=success, message=message, target_id=int(vmid) if vmid.isdigit() else None, target_name=vm_name, details={"node": node, "vmid": vmid} ) @staticmethod def log_proxmox_cluster_action(db: Session, action: str, cluster_id: int, cluster_name: str, success: bool, message: str): """Log Proxmox cluster actions (create/delete)""" LoggingService.log_action( db=db, action_type="proxmox", action=action, success=success, message=message, target_id=cluster_id, target_name=cluster_name ) @staticmethod def log_server_action(db: Session, action: str, server_id: int, server_name: str, success: bool, message: str): """Log server actions (create/update/delete)""" LoggingService.log_action( db=db, action_type="server", action=action, success=success, message=message, target_id=server_id, target_name=server_name ) @staticmethod def log_host_action(db: Session, action: str, host_id: int, host_name: str, success: bool, message: str): """Log Proxmox host actions (create/update/delete/wake/shutdown)""" LoggingService.log_action( db=db, action_type="host", action=action, success=success, message=message, target_id=host_id, target_name=host_name )