Download modules/stats.py from deanpowers/blind-monkeys: direct link, hf CLI and curl.
- Browser
- Download file 972 Bytes
-
https://huggingface.co/deanpowers/blind-monkeys/resolve/main/modules/stats.py
- Command line
-
hf download hf://deanpowers/blind-monkeys/modules/stats.py
-
curl -L -o stats.py https://huggingface.co/deanpowers/blind-monkeys/resolve/main/modules/stats.py
972 Bytes
| 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 | |