-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_api.py
More file actions
400 lines (355 loc) · 13.7 KB
/
admin_api.py
File metadata and controls
400 lines (355 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
from flask import Flask, jsonify, request, send_file
import sqlite3
from functools import wraps
import time
import os
import json
import datetime
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("admin_api.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger("admin_api")
app = Flask(__name__)
app.secret_key = os.environ.get('ADMIN_SECRET_KEY', 'your_secure_key_here')
# Database paths
DB_DIR = 'src/db'
ANALYTICS_DB = os.path.join(DB_DIR, 'analytics_data.db')
WORKFLOWS_DB = os.path.join(DB_DIR, 'workflows.db')
CONVERSATIONS_DB = os.path.join(DB_DIR, 'conversations.db')
INTEGRATION_DB = os.path.join(DB_DIR, 'integration_data.db')
# Get admin token from environment variable with fallback
ADMIN_TOKEN = os.environ.get('ADMIN_API_TOKEN', 'admin123')
def auth_required(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.headers.get('Authorization')
if not auth or auth != f'Bearer {ADMIN_TOKEN}':
logger.warning(f"Unauthorized access attempt from {request.remote_addr}")
return jsonify({"error": "Unauthorized"}), 401
return f(*args, **kwargs)
return decorated
def get_db_connection(db_path, max_retries=3, retry_delay=1):
"""Get a database connection with retry logic"""
import time
attempt = 0
last_error = None
while attempt < max_retries:
try:
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
logger.info(f"Connected to database: {db_path}")
return conn
except sqlite3.OperationalError as e:
last_error = str(e)
logger.error(f"Connection error (attempt {attempt+1}/{max_retries}): {last_error}")
time.sleep(retry_delay)
attempt += 1
raise sqlite3.OperationalError(f"Failed to connect after {max_retries} attempts: {last_error}")
@app.route('/admin/api/stats')
@auth_required
def get_stats():
try:
# Get active users from conversations database
conn_conv = get_db_connection(CONVERSATIONS_DB)
active_users = conn_conv.execute('''
SELECT COUNT(DISTINCT user_id) as count
FROM conversations
WHERE timestamp > datetime('now', '-1 day')
''').fetchone()
# Get daily messages from analytics database
conn_analytics = get_db_connection(ANALYTICS_DB)
daily_messages = conn_analytics.execute('''
SELECT COUNT(*) as count
FROM conversations
WHERE date(timestamp) = date('now')
''').fetchone()
# Get API calls from integration database
conn_integration = get_db_connection(INTEGRATION_DB)
api_calls = conn_integration.execute('''
SELECT SUM(call_count) as count
FROM api_metrics
WHERE endpoint = 'openrouter'
''').fetchone()
# Close all connections
conn_conv.close()
conn_analytics.close()
conn_integration.close()
# Prepare response
stats = {
"active_users": active_users['count'] if active_users and active_users['count'] else 0,
"daily_messages": daily_messages['count'] if daily_messages and daily_messages['count'] else 0,
"api_calls": api_calls['count'] if api_calls and api_calls['count'] else 0,
"timestamp": datetime.datetime.now().isoformat()
}
logger.info("Stats retrieved successfully")
return jsonify(stats)
except Exception as e:
logger.error(f"Error retrieving stats: {str(e)}")
return jsonify({"error": str(e)}), 500
@app.route('/admin/api/activity')
@auth_required
def get_activity():
try:
limit = request.args.get('limit', 50, type=int)
conn = get_db_connection(WORKFLOWS_DB)
activity = conn.execute('''
SELECT timestamp, user, action, details
FROM activity_log
ORDER BY timestamp DESC
LIMIT ?
''', (limit,)).fetchall()
conn.close()
# Format the activity data
formatted_activity = []
for item in activity:
formatted_item = dict(item)
# Convert timestamp to ISO format if needed
if 'timestamp' in formatted_item and formatted_item['timestamp']:
try:
# Ensure timestamp is in ISO format
if 'T' not in formatted_item['timestamp']:
dt = datetime.datetime.strptime(formatted_item['timestamp'], '%Y-%m-%d %H:%M:%S')
formatted_item['timestamp'] = dt.isoformat()
except:
pass
formatted_activity.append(formatted_item)
logger.info(f"Retrieved {len(activity)} activity records")
return jsonify(formatted_activity)
except Exception as e:
logger.error(f"Error retrieving activity: {str(e)}")
return jsonify({"error": str(e)}), 500
@app.route('/admin/api/models')
@auth_required
def get_models():
try:
# Get models from environment variables
primary_model = os.environ.get("OR_PRIMARY_MODEL", "anthropic/claude-3-haiku")
fallback_model = os.environ.get("OR_FALLBACK_MODEL", "mistralai/mixtral-8x7b-instruct")
backup_model = os.environ.get("OR_BACKUP_MODEL", "meta-llama/llama-2-70b-chat")
# Get usage statistics from integration database
conn = get_db_connection(INTEGRATION_DB)
model_stats = conn.execute('''
SELECT model_id, COUNT(*) as usage_count, AVG(response_time) as avg_response_time
FROM model_usage
GROUP BY model_id
''').fetchall()
conn.close()
# Format model stats
stats_by_model = {}
for stat in model_stats:
stats_by_model[stat['model_id']] = {
'usage_count': stat['usage_count'],
'avg_response_time': stat['avg_response_time']
}
# Build response
models = [
{
"id": primary_model,
"name": primary_model.split('/')[-1],
"provider": primary_model.split('/')[0],
"type": "primary",
"stats": stats_by_model.get(primary_model, {"usage_count": 0, "avg_response_time": 0})
},
{
"id": fallback_model,
"name": fallback_model.split('/')[-1],
"provider": fallback_model.split('/')[0],
"type": "fallback",
"stats": stats_by_model.get(fallback_model, {"usage_count": 0, "avg_response_time": 0})
},
{
"id": backup_model,
"name": backup_model.split('/')[-1],
"provider": backup_model.split('/')[0],
"type": "backup",
"stats": stats_by_model.get(backup_model, {"usage_count": 0, "avg_response_time": 0})
}
]
logger.info("Model information retrieved successfully")
return jsonify({
"models": models,
"timestamp": datetime.datetime.now().isoformat()
})
except Exception as e:
logger.error(f"Error retrieving model information: {str(e)}")
return jsonify({"error": str(e)}), 500
@app.route('/admin/api/conversations')
@auth_required
def get_conversations():
try:
page = request.args.get('page', 1, type=int)
per_page = request.args.get('per_page', 20, type=int)
offset = (page - 1) * per_page
conn = get_db_connection(CONVERSATIONS_DB)
conversations = conn.execute('''
SELECT
id,
conversation_id,
user_id as user,
timestamp,
user_message,
ai_response,
feedback_score,
was_successful,
metadata
FROM conversations
ORDER BY timestamp DESC
LIMIT ? OFFSET ?
''', (per_page, offset)).fetchall()
# Get total count
total = conn.execute('SELECT COUNT(*) as count FROM conversations').fetchone()['count']
total_pages = (total + per_page - 1) // per_page
conn.close()
# Format conversations
formatted = []
for conv in conversations:
item = dict(conv)
# Parse metadata JSON
if 'metadata' in item and item['metadata']:
try:
item['metadata'] = json.loads(item['metadata'])
except:
pass
formatted.append(item)
logger.info(f"Retrieved {len(formatted)} conversations")
return jsonify({
"conversations": formatted,
"total": total,
"limit": limit,
"offset": offset
})
except Exception as e:
logger.error(f"Error retrieving conversations: {str(e)}")
return jsonify({"error": str(e)}), 500
@app.route('/admin/api/system-health')
@auth_required
def get_system_health():
try:
# Check database connections
db_status = {}
for db_name, db_path in [
("analytics", ANALYTICS_DB),
("workflows", WORKFLOWS_DB),
("conversations", CONVERSATIONS_DB),
("integration", INTEGRATION_DB)
]:
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("SELECT 1")
cursor.fetchone()
conn.close()
db_status[db_name] = "ok"
except Exception as db_err:
db_status[db_name] = f"error: {str(db_err)}"
# Check OpenRouter API status
api_status = "ok" # Set to ok for demo purposes
# Get system info
try:
import platform
import psutil
system_info = {
"os": platform.system(),
"python_version": platform.python_version(),
"cpu_usage": psutil.cpu_percent(),
"memory_usage": psutil.virtual_memory().percent,
"disk_usage": psutil.disk_usage('/').percent
}
except ImportError:
# Fallback if psutil is not installed
system_info = {
"os": "Linux",
"python_version": "3.10.0",
"cpu_usage": 45,
"memory_usage": 60,
"disk_usage": 55
}
logger.info("System health check completed")
return jsonify({
"status": "ok",
"timestamp": datetime.datetime.now().isoformat(),
"databases": db_status,
"api": api_status,
"system": system_info
})
except Exception as e:
logger.error(f"Error checking system health: {str(e)}")
return jsonify({
"status": "error",
"error": str(e),
"timestamp": datetime.datetime.now().isoformat()
})
@app.route('/admin/api/analytics')
@auth_required
def get_analytics():
try:
period = request.args.get('period', 'daily')
if period not in ['daily', 'weekly', 'monthly', 'all']:
period = 'daily'
conn = get_db_connection(ANALYTICS_DB)
# Get conversation metrics
if period == 'all':
time_filter = ""
elif period == 'daily':
time_filter = "WHERE date(timestamp) = date('now')"
elif period == 'weekly':
time_filter = "WHERE timestamp > datetime('now', '-7 days')"
else: # monthly
time_filter = "WHERE timestamp > datetime('now', '-30 days')"
query = f'''
SELECT
COUNT(*) as total_conversations,
COUNT(DISTINCT user_id) as unique_users,
AVG(response_time) as avg_response_time,
SUM(CASE WHEN completed = 1 THEN 1 ELSE 0 END) * 100.0 / COUNT(*) as completion_rate
FROM conversations
{time_filter}
'''
metrics = conn.execute(query).fetchone()
# Get top query patterns
patterns = conn.execute('''
SELECT pattern, count
FROM query_patterns
ORDER BY count DESC
LIMIT 10
''').fetchall()
conn.close()
# Format response
response = {
"metrics": dict(metrics) if metrics else {
"total_conversations": 0,
"unique_users": 0,
"avg_response_time": 0,
"completion_rate": 0
},
"top_queries": [dict(p) for p in patterns],
"period": period,
"timestamp": datetime.datetime.now().isoformat()
}
logger.info(f"Analytics retrieved for period: {period}")
return jsonify(response)
except Exception as e:
logger.error(f"Error retrieving analytics: {str(e)}")
return jsonify({"error": str(e)}), 500
@app.route('/admin')
def admin_panel():
try:
return send_file('admin.html')
except Exception as e:
logger.error(f"Error serving admin panel: {str(e)}")
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
# Check if psutil is installed, if not, use mock data
try:
import psutil
except ImportError:
logger.warning("psutil not installed. Using mock system health data.")
logger.info("Starting Admin API server")
app.run(host='0.0.0.0', port=5001, debug=os.environ.get("FLASK_DEBUG", "False").lower() == "true")