Spaces:
Sleeping
Sleeping
Create logs.py
Browse files
logs.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
from collections import defaultdict
|
| 3 |
+
|
| 4 |
+
# Store logs (replace with DB later)
|
| 5 |
+
USAGE_LOGS = defaultdict(list)
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def log_request(api_key: str, endpoint: str):
|
| 9 |
+
USAGE_LOGS[api_key].append({
|
| 10 |
+
"endpoint": endpoint,
|
| 11 |
+
"time": int(time.time())
|
| 12 |
+
})
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def get_usage(api_key: str):
|
| 16 |
+
return USAGE_LOGS.get(api_key, [])
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def stats():
|
| 20 |
+
report = {}
|
| 21 |
+
for key, logs in USAGE_LOGS.items():
|
| 22 |
+
report[key] = {
|
| 23 |
+
"total_requests": len(logs),
|
| 24 |
+
"last_used": logs[-1]["time"] if logs else None
|
| 25 |
+
}
|
| 26 |
+
return report
|