260707_1_대시보드정리 1차
This commit is contained in:
@@ -7,6 +7,7 @@ from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
import aiomysql
|
||||
import psutil
|
||||
|
||||
from config.config_db import get_db_pool
|
||||
|
||||
@@ -372,9 +373,11 @@ async def list_audit_logs(limit: int, offset: int) -> dict[str, Any]:
|
||||
return {"total": total, "items": list(await cursor.fetchall())}
|
||||
|
||||
|
||||
async def get_system_resources() -> dict[str, Any]:
|
||||
async def get_system_resources(days: int = 30) -> dict[str, Any]:
|
||||
total, used, _free = shutil.disk_usage(".")
|
||||
disk_percent = used / total * 100 if total else 0
|
||||
cpu_percent = psutil.cpu_percent(interval=0.1)
|
||||
memory_percent = psutil.virtual_memory().percent
|
||||
pool = get_db_pool()
|
||||
async with pool.acquire() as connection, connection.cursor(aiomysql.DictCursor) as cursor:
|
||||
await cursor.execute(
|
||||
@@ -385,18 +388,31 @@ async def get_system_resources() -> dict[str, Any]:
|
||||
)
|
||||
stats = await cursor.fetchone()
|
||||
current = {
|
||||
"cpu_usage_percent": None,
|
||||
"memory_usage_percent": None,
|
||||
"cpu_usage_percent": round(cpu_percent, 2),
|
||||
"memory_usage_percent": round(memory_percent, 2),
|
||||
"disk_usage_percent": round(disk_percent, 2),
|
||||
"active_user_count": stats["active_user_count"],
|
||||
"active_project_count": stats["active_project_count"],
|
||||
"total_storage_mb": round(used / 1024 / 1024, 2),
|
||||
}
|
||||
since = datetime.utcnow() - timedelta(days=days)
|
||||
# 다운샘플링: 조회 구간을 최대 TARGET_POINTS개 시간버킷으로 나눠 평균.
|
||||
# 계측 간격(2분)보다 버킷이 작으면 원본 그대로(버킷=간격) 반환.
|
||||
target_points = 300
|
||||
bucket_seconds = max(120, (days * 86400) // target_points)
|
||||
await cursor.execute(
|
||||
"""SELECT timestamp, cpu_usage_percent, memory_usage_percent, disk_usage_percent,
|
||||
active_user_count, active_project_count, total_storage_mb
|
||||
"""SELECT
|
||||
FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(timestamp) / %s) * %s) AS timestamp,
|
||||
ROUND(AVG(cpu_usage_percent), 2) AS cpu_usage_percent,
|
||||
ROUND(AVG(memory_usage_percent), 2) AS memory_usage_percent,
|
||||
ROUND(AVG(disk_usage_percent), 2) AS disk_usage_percent,
|
||||
MAX(active_user_count) AS active_user_count,
|
||||
MAX(active_project_count) AS active_project_count,
|
||||
ROUND(AVG(total_storage_mb), 2) AS total_storage_mb
|
||||
FROM system_resources
|
||||
WHERE timestamp >= %s ORDER BY timestamp""",
|
||||
(datetime.utcnow() - timedelta(hours=24),),
|
||||
WHERE timestamp >= %s
|
||||
GROUP BY FLOOR(UNIX_TIMESTAMP(timestamp) / %s)
|
||||
ORDER BY timestamp""",
|
||||
(bucket_seconds, bucket_seconds, since, bucket_seconds),
|
||||
)
|
||||
return {"current": current, "history": list(await cursor.fetchall()), "stats": current}
|
||||
|
||||
Reference in New Issue
Block a user