Files
ems-core/backend/app/services/audit.py
Du Wenbo 92ec910a13 ems-core v1.0.0: Standard EMS platform core
Shared backend + frontend for multi-customer EMS deployments.
- 12 enterprise modules: quota, cost, charging, maintenance, analysis, etc.
- 120+ API endpoints, 37 database tables
- Customer config mechanism (CUSTOMER env var + YAML config)
- Collectors: Modbus TCP, MQTT, HTTP API, Sungrow iSolarCloud
- Frontend: React 19 + Ant Design + ECharts + Three.js
- Infrastructure: Redis cache, rate limiting, aggregation engine

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 18:14:11 +08:00

33 lines
940 B
Python

from sqlalchemy.ext.asyncio import AsyncSession
from app.models.user import AuditLog
async def log_audit(
db: AsyncSession,
user_id: int | None,
action: str,
resource: str,
detail: str = "",
ip_address: str | None = None,
):
"""Write an audit log entry.
Args:
db: async database session (must be flushed/committed by caller)
user_id: ID of the acting user (None for system actions)
action: one of login, create, update, delete, export, view,
acknowledge, resolve
resource: one of user, device, alarm, report, system, auth
detail: human-readable description
ip_address: client IP if available
"""
entry = AuditLog(
user_id=user_id,
action=action,
resource=resource,
detail=detail,
ip_address=ip_address,
)
db.add(entry)
# Don't flush here — let the caller's transaction handle it