File size: 665 Bytes
08c1d37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# usage_logger.py
import os, json, time

STATS_FILE = "/data/ai_stats.json"

def log_request(user_id: str):
    if os.path.exists(STATS_FILE):
        with open(STATS_FILE, "r") as f:
            stats = json.load(f)
    else:
        stats = {"total": 0, "users": {}}

    stats["total"] += 1
    stats["users"][user_id] = stats["users"].get(user_id, 0) + 1
    stats["last_request"] = time.strftime("%Y-%m-%d %H:%M:%S")

    with open(STATS_FILE, "w") as f:
        json.dump(stats, f, indent=2)

def get_stats():
    if os.path.exists(STATS_FILE):
        with open(STATS_FILE, "r") as f:
            return json.load(f)
    return {"message": "No stats found."}