重构: 切换存储至SQLite,启用INI配置与API Key校验
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
"""
|
||||
Statistics Blueprint
|
||||
Statistics Blueprintoutput_dir
|
||||
Provides API statistics and monitoring endpoints.
|
||||
"""
|
||||
|
||||
@ -7,7 +7,7 @@ import time
|
||||
from flask import Blueprint, current_app
|
||||
|
||||
|
||||
from ..shared import _format_response, log_performance, logger,stats_collector, task_status
|
||||
from ..shared import _format_response, log_performance, logger, stats_collector
|
||||
|
||||
# Create blueprint
|
||||
stats_bp = Blueprint('stats', __name__, url_prefix='/stats')
|
||||
@ -46,17 +46,33 @@ def get_stats():
|
||||
logger.warning(f"Failed to collect system metrics: {e}")
|
||||
stats_data['system'] = {'error': str(e)}
|
||||
|
||||
# Add recent task information
|
||||
# Add recent task information from database
|
||||
recent_tasks = []
|
||||
current_time = time.time()
|
||||
for task_id, task_info in list(task_status.items())[-20:]: # Last 20 tasks
|
||||
age = current_time - task_info.get('updated_at', 0)
|
||||
recent_tasks.append({
|
||||
'task_id': task_id,
|
||||
'status': task_info.get('status'),
|
||||
'age_seconds': round(age, 1),
|
||||
'message': task_info.get('message', '')[:100] # Truncate long messages
|
||||
})
|
||||
try:
|
||||
from ..db import get_db
|
||||
db = get_db()
|
||||
current_time = time.time()
|
||||
|
||||
rows = db.execute("""
|
||||
SELECT task_id, status, message, updated_at
|
||||
FROM tasks
|
||||
WHERE deleted_at IS NULL
|
||||
ORDER BY updated_at DESC
|
||||
LIMIT 20
|
||||
""").fetchall()
|
||||
|
||||
for row in rows:
|
||||
updated_at = row[3] if row[3] else 0
|
||||
age = current_time - updated_at
|
||||
recent_tasks.append({
|
||||
'task_id': row[0], # task_id
|
||||
'status': row[1], # status
|
||||
'age_seconds': round(age, 1),
|
||||
'message': (row[2] or '')[:100] # message, truncate long messages
|
||||
})
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to get recent tasks from database: {e}")
|
||||
recent_tasks = []
|
||||
|
||||
stats_data['recent_tasks'] = recent_tasks
|
||||
|
||||
|
||||
Reference in New Issue
Block a user