Spaces:
Sleeping
Sleeping
| import time | |
| from collections import defaultdict | |
| # Store logs (replace with DB later) | |
| USAGE_LOGS = defaultdict(list) | |
| def log_request(api_key: str, endpoint: str): | |
| USAGE_LOGS[api_key].append({ | |
| "endpoint": endpoint, | |
| "time": int(time.time()) | |
| }) | |
| def get_usage(api_key: str): | |
| return USAGE_LOGS.get(api_key, []) | |
| def stats(): | |
| report = {} | |
| for key, logs in USAGE_LOGS.items(): | |
| report[key] = { | |
| "total_requests": len(logs), | |
| "last_used": logs[-1]["time"] if logs else None | |
| } | |
| return report | |