Spaces:
Running
Running
Create usage_logger.py
Browse files- usage_logger.py +24 -0
usage_logger.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# usage_logger.py
|
| 2 |
+
import os, json, time
|
| 3 |
+
|
| 4 |
+
STATS_FILE = "/data/ai_stats.json"
|
| 5 |
+
|
| 6 |
+
def log_request(user_id: str):
|
| 7 |
+
if os.path.exists(STATS_FILE):
|
| 8 |
+
with open(STATS_FILE, "r") as f:
|
| 9 |
+
stats = json.load(f)
|
| 10 |
+
else:
|
| 11 |
+
stats = {"total": 0, "users": {}}
|
| 12 |
+
|
| 13 |
+
stats["total"] += 1
|
| 14 |
+
stats["users"][user_id] = stats["users"].get(user_id, 0) + 1
|
| 15 |
+
stats["last_request"] = time.strftime("%Y-%m-%d %H:%M:%S")
|
| 16 |
+
|
| 17 |
+
with open(STATS_FILE, "w") as f:
|
| 18 |
+
json.dump(stats, f, indent=2)
|
| 19 |
+
|
| 20 |
+
def get_stats():
|
| 21 |
+
if os.path.exists(STATS_FILE):
|
| 22 |
+
with open(STATS_FILE, "r") as f:
|
| 23 |
+
return json.load(f)
|
| 24 |
+
return {"message": "No stats found."}
|