update usage-dashboard

This commit is contained in:
2026-06-07 21:23:38 +07:00
parent 15fe48fe45
commit 524214e8e9
2 changed files with 123 additions and 98 deletions
+92 -91
View File
File diff suppressed because one or more lines are too long
+31 -7
View File
@@ -292,10 +292,13 @@ def refresh_quota(force=False):
age = latest_quota_age()
if not force and age is not None and age < cfg["quota_refresh_seconds"]:
return 0
# Quét lại thư mục để lấy danh sách file JSON hiện tại (không dùng cache)
files = auth_files()
# ✅ LOG 1: Số lượng auth files tìm thấy
print(f"refresh_quota: found {len(files)} auth files in {JSON_DIR}", flush=True)
print(f"refresh_quota: rescanned {JSON_DIR} — found {len(files)} auth file(s)", flush=True)
if not files:
print(f"refresh_quota: no auth files found, nothing to fetch", flush=True)
return 0
now = dt.datetime.now(dt.timezone.utc)
inserted = 0
@@ -508,18 +511,39 @@ def query_summary(range_name):
}
def active_emails():
"""Trả về tập hợp email (hoặc basename) của các file JSON còn tồn tại trong JSON_DIR."""
emails = set()
for path in auth_files():
try:
auth = json.load(open(path))
email = auth.get("email") or os.path.basename(path)
emails.add(email)
except Exception:
emails.add(os.path.basename(path))
return emails
def latest_quotas(force=False):
if force:
refresh_quota(force=True)
# Chỉ lấy quota của những account có file JSON còn tồn tại hiện tại
emails = active_emails()
if not emails:
return []
placeholders = ",".join("?" * len(emails))
with db_connect() as conn:
rows = conn.execute(
"""
f"""
SELECT q.* FROM quota_snapshots q
JOIN (
SELECT email, MAX(ts_epoch) ts FROM quota_snapshots GROUP BY email
SELECT email, MAX(ts_epoch) ts FROM quota_snapshots
WHERE email IN ({placeholders})
GROUP BY email
) latest ON latest.email = q.email AND latest.ts = q.ts_epoch
ORDER BY email
"""
ORDER BY q.email
""",
list(emails),
).fetchall()
return [dict(row) for row in rows]