Spaces:
Running
Running
Update usage_tracker.py
Browse files- usage_tracker.py +96 -94
usage_tracker.py
CHANGED
|
@@ -1,106 +1,108 @@
|
|
| 1 |
-
import os
|
| 2 |
import json
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
class UsageTracker:
|
| 6 |
-
def __init__(self,
|
| 7 |
-
self.
|
| 8 |
-
self.
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
with open(self.
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return {
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
}
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
"total_requests": 0,
|
| 53 |
-
"first_used": datetime.now().isoformat(),
|
| 54 |
-
"last_used": datetime.now().isoformat()
|
| 55 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
self.usage_data["daily_usage"][today] = {}
|
| 64 |
-
|
| 65 |
-
# Track daily usage for model
|
| 66 |
-
if model:
|
| 67 |
-
if model not in self.usage_data["daily_usage"][today]:
|
| 68 |
-
self.usage_data["daily_usage"][today][model] = 0
|
| 69 |
-
self.usage_data["daily_usage"][today][model] += 1
|
| 70 |
-
|
| 71 |
-
# Track daily usage for endpoint
|
| 72 |
-
if endpoint:
|
| 73 |
-
if endpoint not in self.usage_data["daily_usage"][today]:
|
| 74 |
-
self.usage_data["daily_usage"][today][endpoint] = 0
|
| 75 |
-
self.usage_data["daily_usage"][today][endpoint] += 1
|
| 76 |
-
|
| 77 |
-
# Save updated usage data
|
| 78 |
-
self.save_usage_data()
|
| 79 |
|
| 80 |
-
def save_usage_data(self):
|
| 81 |
-
"""Save usage data to file"""
|
| 82 |
-
try:
|
| 83 |
-
with open(self.filename, 'w') as f:
|
| 84 |
-
json.dump(self.usage_data, f, indent=4)
|
| 85 |
-
except Exception as e:
|
| 86 |
-
print(f"Error saving usage data: {e}")
|
| 87 |
|
| 88 |
-
def get_usage_summary(self, days=7):
|
| 89 |
-
"""
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
+
import os
|
| 3 |
+
import datetime
|
| 4 |
+
import threading
|
| 5 |
+
from collections import defaultdict
|
| 6 |
|
| 7 |
class UsageTracker:
|
| 8 |
+
def __init__(self, data_file="usage_data.json"):
|
| 9 |
+
self.data_file = data_file
|
| 10 |
+
self.lock = threading.Lock()
|
| 11 |
+
self.data = self._load_data()
|
| 12 |
|
| 13 |
+
def _load_data(self):
|
| 14 |
+
"""Loads usage data from the JSON file."""
|
| 15 |
+
if os.path.exists(self.data_file):
|
| 16 |
+
try:
|
| 17 |
+
with open(self.data_file, 'r') as f:
|
| 18 |
+
data = json.load(f)
|
| 19 |
+
# Ensure all necessary keys exist, initialize if not
|
| 20 |
+
data.setdefault('total_requests', 0)
|
| 21 |
+
data.setdefault('models', {})
|
| 22 |
+
data.setdefault('api_endpoints', {})
|
| 23 |
+
data.setdefault('recent_daily_usage', {})
|
| 24 |
+
return data
|
| 25 |
+
except json.JSONDecodeError:
|
| 26 |
+
print(f"Warning: Could not decode JSON from {self.data_file}. Starting with empty data.")
|
| 27 |
+
return self._initialize_empty_data()
|
| 28 |
+
return self._initialize_empty_data()
|
| 29 |
+
|
| 30 |
+
def _initialize_empty_data(self):
|
| 31 |
+
"""Initializes an empty data structure for usage tracking."""
|
| 32 |
return {
|
| 33 |
+
'total_requests': 0,
|
| 34 |
+
'models': {},
|
| 35 |
+
'api_endpoints': {},
|
| 36 |
+
'recent_daily_usage': {}
|
| 37 |
}
|
| 38 |
|
| 39 |
+
def save_data(self):
|
| 40 |
+
"""Saves current usage data to the JSON file."""
|
| 41 |
+
with self.lock:
|
| 42 |
+
try:
|
| 43 |
+
with open(self.data_file, 'w') as f:
|
| 44 |
+
json.dump(self.data, f, indent=4)
|
| 45 |
+
except IOError as e:
|
| 46 |
+
print(f"Error saving usage data to {self.data_file}: {e}")
|
| 47 |
+
|
| 48 |
+
def record_request(self, model: str = "unknown", endpoint: str = "unknown"):
|
| 49 |
+
"""Records a single API request, updating model, endpoint, and daily usage."""
|
| 50 |
+
with self.lock:
|
| 51 |
+
now = datetime.datetime.now()
|
| 52 |
+
current_date = now.strftime("%Y-%m-%d")
|
| 53 |
+
current_time = now.strftime("%Y-%m-%d %I:%M:%S %p")
|
| 54 |
+
|
| 55 |
+
# Update total requests
|
| 56 |
+
self.data['total_requests'] += 1
|
| 57 |
+
|
| 58 |
+
# Update model usage
|
| 59 |
+
if model not in self.data['models']:
|
| 60 |
+
self.data['models'][model] = {
|
| 61 |
+
'total_requests': 0,
|
| 62 |
+
'first_used': current_time,
|
| 63 |
+
'last_used': current_time
|
| 64 |
}
|
| 65 |
+
self.data['models'][model]['total_requests'] += 1
|
| 66 |
+
self.data['models'][model]['last_used'] = current_time
|
| 67 |
+
|
| 68 |
+
# Update API endpoint usage
|
| 69 |
+
if endpoint not in self.data['api_endpoints']:
|
| 70 |
+
self.data['api_endpoints'][endpoint] = {
|
| 71 |
+
'total_requests': 0,
|
| 72 |
+
'first_used': current_time,
|
| 73 |
+
'last_used': current_time
|
|
|
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
+
self.data['api_endpoints'][endpoint]['total_requests'] += 1
|
| 76 |
+
self.data['api_endpoints'][endpoint]['last_used'] = current_time
|
| 77 |
+
|
| 78 |
+
# Update daily usage
|
| 79 |
+
if current_date not in self.data['recent_daily_usage']:
|
| 80 |
+
self.data['recent_daily_usage'][current_date] = defaultdict(int)
|
| 81 |
|
| 82 |
+
# Convert defaultdict back to dict for JSON serialization
|
| 83 |
+
self.data['recent_daily_usage'][current_date][model] += 1
|
| 84 |
+
self.data['recent_daily_usage'][current_date][endpoint] += 1
|
| 85 |
+
|
| 86 |
+
# Ensure the daily usage is stored as a regular dict
|
| 87 |
+
self.data['recent_daily_usage'][current_date] = dict(self.data['recent_daily_usage'][current_date])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
def get_usage_summary(self, days: int = 7):
|
| 91 |
+
"""Generates a summary of usage data for the last 'days'."""
|
| 92 |
+
with self.lock:
|
| 93 |
+
summary = {
|
| 94 |
+
'total_requests': self.data['total_requests'],
|
| 95 |
+
'models': self.data['models'],
|
| 96 |
+
'api_endpoints': self.data['api_endpoints'],
|
| 97 |
+
'recent_daily_usage': {}
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
# Filter daily usage for the last 'days'
|
| 101 |
+
today = datetime.date.today()
|
| 102 |
+
for i in range(days):
|
| 103 |
+
date = (today - datetime.timedelta(days=i)).strftime("%Y-%m-%d")
|
| 104 |
+
if date in self.data['recent_daily_usage']:
|
| 105 |
+
summary['recent_daily_usage'][date] = self.data['recent_daily_usage'][date]
|
| 106 |
+
|
| 107 |
+
return summary
|
| 108 |
+
|