added feat
This commit is contained in:
+31
-1
@@ -14,9 +14,10 @@ from pathlib import Path
|
||||
|
||||
# Third-Party Library Imports
|
||||
import aiofiles
|
||||
import aiosqlite
|
||||
|
||||
# Local Module Imports
|
||||
from .utils import STORAGE_DIR, get_bot
|
||||
from .utils import STORAGE_DIR, SQ_BATTLES_DB_PATH, get_bot
|
||||
|
||||
HEALTH_PATH = STORAGE_DIR / "bot_health.json"
|
||||
|
||||
@@ -130,3 +131,32 @@ async def get_health_snapshot() -> dict:
|
||||
_health_state["games_processed_24h"] = games_24h
|
||||
|
||||
return dict(_health_state)
|
||||
|
||||
|
||||
async def get_recent_ttl_stats(limit: int = 30) -> dict:
|
||||
"""Return receive-delay stats for the most recent completed games."""
|
||||
async with aiosqlite.connect(SQ_BATTLES_DB_PATH, timeout=10.0) as db:
|
||||
rows = list(await db.execute_fetchall(
|
||||
"SELECT endtime_unix, received_unix FROM match_summary "
|
||||
"WHERE received_unix IS NOT NULL AND endtime_unix IS NOT NULL "
|
||||
"ORDER BY endtime_unix DESC LIMIT ?",
|
||||
(limit,),
|
||||
))
|
||||
|
||||
if not rows:
|
||||
return {
|
||||
"sample_size": 0,
|
||||
"avg_delay": None,
|
||||
"min_delay": None,
|
||||
"max_delay": None,
|
||||
"last_received_ts": None,
|
||||
}
|
||||
|
||||
delays = [max(int(received) - int(ended), 0) for ended, received in rows]
|
||||
return {
|
||||
"sample_size": len(delays),
|
||||
"avg_delay": int(sum(delays) / len(delays)),
|
||||
"min_delay": min(delays),
|
||||
"max_delay": max(delays),
|
||||
"last_received_ts": max(int(received) for _, received in rows),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user