a
This commit is contained in:
parent
59b8e7b44f
commit
e092a36640
|
|
@ -16,6 +16,9 @@ router = APIRouter(prefix="/api/system")
|
||||||
|
|
||||||
LOG_GROUPS = ("error", "warning", "info")
|
LOG_GROUPS = ("error", "warning", "info")
|
||||||
|
|
||||||
|
# 避免 Redis 异常刷屏(前端可能自动刷新)
|
||||||
|
_last_logs_redis_err_ts: float = 0.0
|
||||||
|
|
||||||
|
|
||||||
def _logs_prefix() -> str:
|
def _logs_prefix() -> str:
|
||||||
return (os.getenv("REDIS_LOG_LIST_PREFIX", "ats:logs").strip() or "ats:logs")
|
return (os.getenv("REDIS_LOG_LIST_PREFIX", "ats:logs").strip() or "ats:logs")
|
||||||
|
|
@ -249,7 +252,15 @@ def _get_redis_client_for_logs():
|
||||||
client = redis.from_url(redis_url, **kwargs)
|
client = redis.from_url(redis_url, **kwargs)
|
||||||
client.ping()
|
client.ping()
|
||||||
return client
|
return client
|
||||||
except Exception:
|
except Exception as e:
|
||||||
|
# 把错误尽量打到 api.log(你现在看的文件)
|
||||||
|
global _last_logs_redis_err_ts
|
||||||
|
import time as _t
|
||||||
|
|
||||||
|
now = _t.time()
|
||||||
|
if now - _last_logs_redis_err_ts > 30:
|
||||||
|
_last_logs_redis_err_ts = now
|
||||||
|
logger.warning(f"日志模块 Redis 连接失败。REDIS_URL={os.getenv('REDIS_URL', '')} err={e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -370,41 +381,51 @@ async def logs_overview(x_admin_token: Optional[str] = Header(default=None, alia
|
||||||
if client is None:
|
if client is None:
|
||||||
raise HTTPException(status_code=503, detail="Redis 不可用,无法读取日志概览")
|
raise HTTPException(status_code=503, detail="Redis 不可用,无法读取日志概览")
|
||||||
|
|
||||||
cfg = _read_logs_config(client)
|
try:
|
||||||
|
cfg = _read_logs_config(client)
|
||||||
|
|
||||||
day = _beijing_yyyymmdd()
|
day = _beijing_yyyymmdd()
|
||||||
stats_prefix = _logs_stats_prefix()
|
stats_prefix = _logs_stats_prefix()
|
||||||
|
|
||||||
pipe = client.pipeline()
|
pipe = client.pipeline()
|
||||||
for g in LOG_GROUPS:
|
for g in LOG_GROUPS:
|
||||||
pipe.llen(_logs_key_for_group(g))
|
pipe.llen(_logs_key_for_group(g))
|
||||||
for g in LOG_GROUPS:
|
for g in LOG_GROUPS:
|
||||||
pipe.get(f"{stats_prefix}:{day}:{g}")
|
pipe.get(f"{stats_prefix}:{day}:{g}")
|
||||||
res = pipe.execute()
|
res = pipe.execute()
|
||||||
|
|
||||||
llen_vals = res[: len(LOG_GROUPS)]
|
llen_vals = res[: len(LOG_GROUPS)]
|
||||||
added_vals = res[len(LOG_GROUPS) :]
|
added_vals = res[len(LOG_GROUPS) :]
|
||||||
|
|
||||||
llen: Dict[str, int] = {}
|
llen: Dict[str, int] = {}
|
||||||
added_today: Dict[str, int] = {}
|
added_today: Dict[str, int] = {}
|
||||||
for i, g in enumerate(LOG_GROUPS):
|
for i, g in enumerate(LOG_GROUPS):
|
||||||
try:
|
try:
|
||||||
llen[g] = int(llen_vals[i] or 0)
|
llen[g] = int(llen_vals[i] or 0)
|
||||||
except Exception:
|
except Exception:
|
||||||
llen[g] = 0
|
llen[g] = 0
|
||||||
try:
|
try:
|
||||||
added_today[g] = int(added_vals[i] or 0)
|
added_today[g] = int(added_vals[i] or 0)
|
||||||
except Exception:
|
except Exception:
|
||||||
added_today[g] = 0
|
added_today[g] = 0
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"config": cfg,
|
"config": cfg,
|
||||||
"stats": {
|
"stats": {
|
||||||
"day": day,
|
"day": day,
|
||||||
"llen": llen,
|
"llen": llen,
|
||||||
"added_today": added_today,
|
"added_today": added_today,
|
||||||
},
|
},
|
||||||
}
|
"meta": {
|
||||||
|
"redis_url": os.getenv("REDIS_URL", ""),
|
||||||
|
"keys": {g: _logs_key_for_group(g) for g in LOG_GROUPS},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"logs_overview 失败: {e}", exc_info=True)
|
||||||
|
raise HTTPException(status_code=500, detail=f"logs_overview failed: {e}")
|
||||||
|
|
||||||
|
|
||||||
@router.put("/logs/config")
|
@router.put("/logs/config")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user