Spaces:
Sleeping
Sleeping
Delete stats.py
Browse files
stats.py
DELETED
|
@@ -1,100 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
stats.py — in-memory join/leave tracker + error logger.
|
| 3 |
-
Resets counters every 24 h and posts a summary to the logs channel.
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import json
|
| 7 |
-
import os
|
| 8 |
-
from datetime import datetime, timezone
|
| 9 |
-
from pathlib import Path
|
| 10 |
-
|
| 11 |
-
STATS_FILE = Path("data/stats.json")
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def _load() -> dict:
|
| 15 |
-
if STATS_FILE.exists():
|
| 16 |
-
try:
|
| 17 |
-
return json.loads(STATS_FILE.read_text())
|
| 18 |
-
except Exception:
|
| 19 |
-
pass
|
| 20 |
-
return _default()
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def _default() -> dict:
|
| 24 |
-
now = datetime.now(timezone.utc).isoformat()
|
| 25 |
-
return {
|
| 26 |
-
"day_start": now,
|
| 27 |
-
"joins": 0,
|
| 28 |
-
"leaves": 0,
|
| 29 |
-
"errors": 0,
|
| 30 |
-
"total_joins": 0,
|
| 31 |
-
"total_leaves": 0,
|
| 32 |
-
"total_errors": 0,
|
| 33 |
-
"history": [] # list of daily summaries
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def _save(data: dict):
|
| 38 |
-
STATS_FILE.parent.mkdir(parents=True, exist_ok=True)
|
| 39 |
-
STATS_FILE.write_text(json.dumps(data, ensure_ascii=False, indent=2))
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
class StatsTracker:
|
| 43 |
-
def __init__(self):
|
| 44 |
-
self._data = _load()
|
| 45 |
-
|
| 46 |
-
def record_join(self):
|
| 47 |
-
self._data["joins"] += 1
|
| 48 |
-
self._data["total_joins"] += 1
|
| 49 |
-
_save(self._data)
|
| 50 |
-
|
| 51 |
-
def record_leave(self):
|
| 52 |
-
self._data["leaves"] += 1
|
| 53 |
-
self._data["total_leaves"] += 1
|
| 54 |
-
_save(self._data)
|
| 55 |
-
|
| 56 |
-
def record_error(self):
|
| 57 |
-
self._data["errors"] += 1
|
| 58 |
-
self._data["total_errors"] += 1
|
| 59 |
-
_save(self._data)
|
| 60 |
-
|
| 61 |
-
def get_daily(self) -> dict:
|
| 62 |
-
return {
|
| 63 |
-
"joins": self._data["joins"],
|
| 64 |
-
"leaves": self._data["leaves"],
|
| 65 |
-
"errors": self._data["errors"],
|
| 66 |
-
"day_start": self._data["day_start"],
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
def get_totals(self) -> dict:
|
| 70 |
-
return {
|
| 71 |
-
"total_joins": self._data["total_joins"],
|
| 72 |
-
"total_leaves": self._data["total_leaves"],
|
| 73 |
-
"total_errors": self._data["total_errors"],
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
def reset_daily(self) -> dict:
|
| 77 |
-
"""Archive current day and reset counters. Returns the archived summary."""
|
| 78 |
-
summary = {
|
| 79 |
-
"date": self._data["day_start"],
|
| 80 |
-
"joins": self._data["joins"],
|
| 81 |
-
"leaves": self._data["leaves"],
|
| 82 |
-
"errors": self._data["errors"],
|
| 83 |
-
"growth": self._data["joins"] - self._data["leaves"],
|
| 84 |
-
}
|
| 85 |
-
self._data["history"].append(summary)
|
| 86 |
-
# keep last 30 days
|
| 87 |
-
self._data["history"] = self._data["history"][-30:]
|
| 88 |
-
self._data["day_start"] = datetime.now(timezone.utc).isoformat()
|
| 89 |
-
self._data["joins"] = 0
|
| 90 |
-
self._data["leaves"] = 0
|
| 91 |
-
self._data["errors"] = 0
|
| 92 |
-
_save(self._data)
|
| 93 |
-
return summary
|
| 94 |
-
|
| 95 |
-
def get_history(self, days: int = 7) -> list:
|
| 96 |
-
return self._data["history"][-days:]
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
# Singleton
|
| 100 |
-
tracker = StatsTracker()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|