10th_fix status
This commit is contained in:
122
app/api/admin.py
122
app/api/admin.py
@@ -75,8 +75,41 @@ def get_system_stats():
|
||||
'total_cost': float(ranking.total_cost or 0.0)
|
||||
})
|
||||
|
||||
# 簡化的每日統計 - 只返回空數組
|
||||
# 計算每日統計
|
||||
period = request.args.get('period', 'month')
|
||||
days = {'week': 7, 'month': 30, 'quarter': 90}.get(period, 30)
|
||||
|
||||
# 取得指定期間的每日統計
|
||||
daily_stats = []
|
||||
for i in range(days):
|
||||
target_date = (datetime.utcnow() - timedelta(days=i)).date()
|
||||
|
||||
# 當日任務統計
|
||||
daily_jobs = TranslationJob.query.filter(
|
||||
func.date(TranslationJob.created_at) == target_date
|
||||
).count()
|
||||
|
||||
daily_completed = TranslationJob.query.filter(
|
||||
func.date(TranslationJob.created_at) == target_date,
|
||||
TranslationJob.status == 'COMPLETED'
|
||||
).count()
|
||||
|
||||
# 當日成本統計
|
||||
daily_cost = db.session.query(
|
||||
func.sum(TranslationJob.total_cost)
|
||||
).filter(
|
||||
func.date(TranslationJob.created_at) == target_date
|
||||
).scalar() or 0.0
|
||||
|
||||
daily_stats.append({
|
||||
'date': target_date.strftime('%Y-%m-%d'),
|
||||
'jobs': daily_jobs,
|
||||
'completed': daily_completed,
|
||||
'cost': float(daily_cost)
|
||||
})
|
||||
|
||||
# 反轉順序,最早的日期在前
|
||||
daily_stats.reverse()
|
||||
|
||||
return jsonify(create_response(
|
||||
success=True,
|
||||
@@ -84,8 +117,8 @@ def get_system_stats():
|
||||
'overview': overview,
|
||||
'daily_stats': daily_stats,
|
||||
'user_rankings': user_rankings_data,
|
||||
'period': 'month',
|
||||
'start_date': format_taiwan_time(datetime.utcnow(), "%Y-%m-%d %H:%M:%S"),
|
||||
'period': period,
|
||||
'start_date': format_taiwan_time(datetime.utcnow() - timedelta(days=days), "%Y-%m-%d %H:%M:%S"),
|
||||
'end_date': format_taiwan_time(datetime.utcnow(), "%Y-%m-%d %H:%M:%S")
|
||||
}
|
||||
))
|
||||
@@ -377,7 +410,8 @@ def get_system_health():
|
||||
# 資料庫檢查
|
||||
try:
|
||||
from app import db
|
||||
db.session.execute('SELECT 1')
|
||||
from sqlalchemy import text
|
||||
db.session.execute(text('SELECT 1'))
|
||||
status['services']['database'] = {'status': 'healthy'}
|
||||
except Exception as e:
|
||||
status['services']['database'] = {
|
||||
@@ -386,14 +420,16 @@ def get_system_health():
|
||||
}
|
||||
status['status'] = 'unhealthy'
|
||||
|
||||
# 基本統計
|
||||
# 翻譯服務統計
|
||||
try:
|
||||
total_jobs = TranslationJob.query.count()
|
||||
pending_jobs = TranslationJob.query.filter_by(status='PENDING').count()
|
||||
processing_jobs = TranslationJob.query.filter_by(status='PROCESSING').count()
|
||||
status['services']['translation_service'] = {
|
||||
'status': 'healthy',
|
||||
'total_jobs': total_jobs,
|
||||
'pending_jobs': pending_jobs
|
||||
'pending_jobs': pending_jobs,
|
||||
'processing_jobs': processing_jobs
|
||||
}
|
||||
except Exception as e:
|
||||
status['services']['translation_service'] = {
|
||||
@@ -402,6 +438,79 @@ def get_system_health():
|
||||
}
|
||||
status['status'] = 'unhealthy'
|
||||
|
||||
# Celery 工作者檢查
|
||||
try:
|
||||
from celery_app import celery
|
||||
from celery.app.control import Control
|
||||
|
||||
# 檢查 Celery 工作者狀態
|
||||
control = Control(celery)
|
||||
inspect_obj = control.inspect(timeout=2.0) # 設置較短超時
|
||||
|
||||
# 獲取活躍工作者
|
||||
active_workers = inspect_obj.active()
|
||||
|
||||
if active_workers and len(active_workers) > 0:
|
||||
worker_count = len(active_workers)
|
||||
status['services']['celery'] = {
|
||||
'status': 'healthy',
|
||||
'active_workers': worker_count,
|
||||
'workers': list(active_workers.keys())
|
||||
}
|
||||
else:
|
||||
# Celery 工作者沒有運行,但不一定表示系統異常
|
||||
status['services']['celery'] = {
|
||||
'status': 'warning',
|
||||
'message': 'No active Celery workers found',
|
||||
'active_workers': 0
|
||||
}
|
||||
# 不設置整體系統為異常,只是警告
|
||||
|
||||
except Exception as e:
|
||||
# Celery 連接失敗,但不一定表示系統異常
|
||||
status['services']['celery'] = {
|
||||
'status': 'warning',
|
||||
'message': f'Cannot connect to Celery workers: {str(e)[:100]}'
|
||||
}
|
||||
# 不設置整體系統為異常,只是警告
|
||||
|
||||
# 檔案系統檢查
|
||||
try:
|
||||
import os
|
||||
from app.config import Config
|
||||
|
||||
# 檢查上傳目錄
|
||||
upload_dir = getattr(Config, 'UPLOAD_FOLDER', 'uploads')
|
||||
if os.path.exists(upload_dir) and os.access(upload_dir, os.W_OK):
|
||||
status['services']['file_system'] = {'status': 'healthy'}
|
||||
else:
|
||||
status['services']['file_system'] = {
|
||||
'status': 'unhealthy',
|
||||
'error': f'Upload directory {upload_dir} not accessible'
|
||||
}
|
||||
status['status'] = 'unhealthy'
|
||||
except Exception as e:
|
||||
status['services']['file_system'] = {
|
||||
'status': 'unhealthy',
|
||||
'error': str(e)
|
||||
}
|
||||
|
||||
# 重新評估整體系統狀態
|
||||
unhealthy_services = [service for service, info in status['services'].items()
|
||||
if info.get('status') == 'unhealthy']
|
||||
|
||||
if unhealthy_services:
|
||||
status['status'] = 'unhealthy'
|
||||
status['unhealthy_services'] = unhealthy_services
|
||||
else:
|
||||
warning_services = [service for service, info in status['services'].items()
|
||||
if info.get('status') == 'warning']
|
||||
if warning_services:
|
||||
status['status'] = 'warning'
|
||||
status['warning_services'] = warning_services
|
||||
else:
|
||||
status['status'] = 'healthy'
|
||||
|
||||
return jsonify(create_response(
|
||||
success=True,
|
||||
data=status
|
||||
@@ -592,7 +701,6 @@ def export_report(report_type):
|
||||
try:
|
||||
from io import BytesIO
|
||||
import pandas as pd
|
||||
from datetime import datetime, timedelta
|
||||
from app import db
|
||||
|
||||
# 驗證報表類型
|
||||
|
Reference in New Issue
Block a user