File size: 972 Bytes
5f20dc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import json
from datetime import datetime, timezone

STATS_FILE = os.environ.get("ADMIN_STATS_FILE", "admin_stats.jsonl")

from modules.storage import load_text, append_text

def log_admin_stat(stat_type: str, count: int, extra: dict = None) -> None:
    """Append a stat record to the JSONL log."""
    record = {
        "ts": datetime.now(timezone.utc).isoformat(),
        "type": stat_type,
        "count": count,
    }
    if extra:
        record.update(extra)
    ok = append_text(STATS_FILE, json.dumps(record) + "\n")
    if not ok:
        print("admin_stats write failed")

def get_admin_stats(limit: int = 100) -> list:
    """Read recent stats (oldest first)."""
    raw = load_text(STATS_FILE, default="")
    if not raw:
        return []
    lines = [l for l in raw.splitlines() if l.strip()]
    out = []
    for l in lines[-limit:]:
        try:
            out.append(json.loads(l))
        except Exception:
            pass
    return out