Test / utils /quota.py
Hfzune's picture
Create utils/quota.py
9515268 verified
Raw History Blame Contribute Delete
719 Bytes
import logging
from datetime import datetime, timezone
from db.init import get_conn
log = logging.getLogger("quota")
SECONDS_LIMIT = 300 # free tier approximation
def _today_prefix():
return datetime.now(timezone.utc).date().isoformat()
def quota_snapshot() -> dict:
prefix = _today_prefix()
with get_conn() as conn:
row = conn.execute(
"SELECT COALESCE(SUM(duration_ms),0) AS ms, COUNT(*) AS c "
"FROM gpu_log WHERE timestamp LIKE ?",
(prefix + "%",),
).fetchone()
ms = row["ms"] or 0
return {
"seconds_used": ms / 1000.0,
"seconds_limit": SECONDS_LIMIT,
"turns_today": row["c"] or 0,
"date": prefix,
}