6cefa620e4
重新设计权限系统所有 UI 组件的视觉风格,统一配色、圆角、阴影和交互动效: - 实时概览:统计卡片加图标和渐变色条,健康评分改为环形进度,告警改为卡片式 - 趋势分析:图表卡片加彩色图标标识,ECharts 配色升级为渐变面积填充 - 访问日志:指标卡片带图标,日志审计改为卡片式入口,IP排行前三高亮 - IP属地:工具栏重设计,排行列表前三渐变高亮,指标卡片统一新风格 - 系统级权限:从 el-table 改为自定义卡片列表,模块块独立圆角卡片 - 项目权限配置:空状态引导优化,成员表格加头像,工具栏加背景容器 - 角色概览卡片:加进度条可视化,hover 微动效 - 接口权限矩阵:工具栏分离布局,表格圆角包裹 - 角色管理抽屉:侧边栏选中态渐变,操作行 hover 高亮 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
118 lines
3.6 KiB
Python
118 lines
3.6 KiB
Python
"""底层安全访问日志后台写入器"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
import uuid
|
|
from datetime import datetime, timezone
|
|
|
|
from app.db.session import SessionLocal
|
|
from app.models.security_access_log import SecurityAccessLog
|
|
|
|
logger = logging.getLogger("ctms.security_access_log_writer")
|
|
|
|
BATCH_SIZE = 100
|
|
FLUSH_INTERVAL = 3.0
|
|
QUEUE_MAX_SIZE = 20000
|
|
|
|
|
|
class SecurityAccessLogWriter:
|
|
def __init__(self) -> None:
|
|
self._queue: asyncio.Queue[dict] = asyncio.Queue(maxsize=QUEUE_MAX_SIZE)
|
|
self._task: asyncio.Task | None = None
|
|
|
|
def enqueue(self, entry: dict) -> None:
|
|
try:
|
|
self._queue.put_nowait(entry)
|
|
except asyncio.QueueFull:
|
|
logger.warning("Security access log queue full, dropping entry")
|
|
|
|
async def start(self) -> None:
|
|
self._task = asyncio.create_task(self._flush_loop())
|
|
logger.info("SecurityAccessLogWriter started")
|
|
|
|
async def stop(self) -> None:
|
|
if self._task:
|
|
self._task.cancel()
|
|
try:
|
|
await self._task
|
|
except asyncio.CancelledError:
|
|
pass
|
|
await self._drain()
|
|
logger.info("SecurityAccessLogWriter stopped")
|
|
|
|
async def _flush_loop(self) -> None:
|
|
while True:
|
|
batch = await self._collect_batch()
|
|
if batch:
|
|
await self._write_batch(batch)
|
|
|
|
async def _collect_batch(self) -> list[dict]:
|
|
batch: list[dict] = []
|
|
try:
|
|
first = await asyncio.wait_for(self._queue.get(), timeout=FLUSH_INTERVAL)
|
|
batch.append(first)
|
|
except asyncio.TimeoutError:
|
|
return batch
|
|
|
|
while len(batch) < BATCH_SIZE:
|
|
try:
|
|
batch.append(self._queue.get_nowait())
|
|
except asyncio.QueueEmpty:
|
|
break
|
|
return batch
|
|
|
|
async def _write_batch(self, batch: list[dict]) -> None:
|
|
try:
|
|
async with SessionLocal() as session:
|
|
for entry in batch:
|
|
session.add(
|
|
SecurityAccessLog(
|
|
id=uuid.uuid4(),
|
|
method=entry["method"],
|
|
path=entry["path"],
|
|
status_code=entry["status_code"],
|
|
elapsed_ms=entry["elapsed_ms"],
|
|
client_ip=entry.get("client_ip"),
|
|
user_agent=entry.get("user_agent"),
|
|
auth_status=entry["auth_status"],
|
|
user_identifier=entry.get("user_identifier"),
|
|
created_at=entry.get("created_at", datetime.now(timezone.utc)),
|
|
)
|
|
)
|
|
await session.commit()
|
|
except Exception:
|
|
logger.exception("Failed to write security access log batch (%d entries)", len(batch))
|
|
|
|
async def _drain(self) -> None:
|
|
batch: list[dict] = []
|
|
while not self._queue.empty():
|
|
try:
|
|
batch.append(self._queue.get_nowait())
|
|
except asyncio.QueueEmpty:
|
|
break
|
|
if batch:
|
|
await self._write_batch(batch)
|
|
|
|
|
|
_writer: SecurityAccessLogWriter | None = None
|
|
|
|
|
|
def get_security_log_writer() -> SecurityAccessLogWriter | None:
|
|
return _writer
|
|
|
|
|
|
async def start_security_log_writer() -> SecurityAccessLogWriter:
|
|
global _writer
|
|
_writer = SecurityAccessLogWriter()
|
|
await _writer.start()
|
|
return _writer
|
|
|
|
|
|
async def stop_security_log_writer() -> None:
|
|
global _writer
|
|
if _writer:
|
|
await _writer.stop()
|
|
_writer = None
|