fix: 数据分析模块修复 添加 API 端点 + 租户维度
1. 新增 GET /api/analytics 端点(之前不存在,前端一直请求 404) 2. query_optimizer.get_analytics_optimized 支持 tenant_id 参数 3. 工单/预警/对话查询按 tenant_id 过滤 4. 数据分析控制面板新增租户筛选下拉框 5. updateCharts 传递 tenant_id 参数 6. populateTenantSelectors 填充分析页租户筛选器
This commit is contained in:
@@ -4,49 +4,63 @@
|
||||
处理数据分析、报告生成等功能
|
||||
"""
|
||||
|
||||
from flask import Blueprint, request, jsonify, send_file
|
||||
import os
|
||||
import logging
|
||||
from flask import Blueprint, request, jsonify, send_file
|
||||
from src.core.query_optimizer import query_optimizer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
analytics_bp = Blueprint('analytics', __name__, url_prefix='/api/analytics')
|
||||
|
||||
|
||||
@analytics_bp.route('')
|
||||
def get_analytics():
|
||||
"""获取分析数据(支持租户筛选和时间范围)"""
|
||||
try:
|
||||
time_range = request.args.get('timeRange', '30')
|
||||
tenant_id = request.args.get('tenant_id')
|
||||
|
||||
try:
|
||||
days = int(time_range)
|
||||
except (ValueError, TypeError):
|
||||
days = 30
|
||||
|
||||
analytics = query_optimizer.get_analytics_optimized(days, tenant_id=tenant_id)
|
||||
return jsonify(analytics)
|
||||
except Exception as e:
|
||||
logger.error(f"获取分析数据失败: {e}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@analytics_bp.route('/export')
|
||||
def export_analytics():
|
||||
"""导出分析报告"""
|
||||
try:
|
||||
from src.web.service_manager import service_manager
|
||||
from src.core.query_optimizer import query_optimizer
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.styles import Font
|
||||
|
||||
# 生成Excel报告(使用数据库真实数据)
|
||||
analytics = query_optimizer.get_analytics_optimized(30)
|
||||
|
||||
# 创建工作簿
|
||||
wb = Workbook()
|
||||
ws = wb.active
|
||||
ws.title = "分析报告"
|
||||
|
||||
# 添加标题
|
||||
ws['A1'] = 'TSP智能助手分析报告'
|
||||
ws['A1'].font = Font(size=16, bold=True)
|
||||
|
||||
# 添加工单统计
|
||||
ws['A3'] = '工单统计'
|
||||
ws['A3'].font = Font(bold=True)
|
||||
ws['A4'] = '总工单数'
|
||||
ws['B4'] = analytics['workorders']['total']
|
||||
ws['B4'] = analytics.get('workorders', {}).get('total', 0)
|
||||
ws['A5'] = '待处理'
|
||||
ws['B5'] = analytics['workorders']['open']
|
||||
ws['B5'] = analytics.get('workorders', {}).get('open', 0)
|
||||
ws['A6'] = '已解决'
|
||||
ws['B6'] = analytics['workorders']['resolved']
|
||||
ws['B6'] = analytics.get('workorders', {}).get('resolved', 0)
|
||||
|
||||
# 保存文件
|
||||
report_path = 'uploads/analytics_report.xlsx'
|
||||
os.makedirs('uploads', exist_ok=True)
|
||||
wb.save(report_path)
|
||||
|
||||
return send_file(report_path, as_attachment=True, download_name='analytics_report.xlsx')
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"导出分析报告失败: {e}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
Reference in New Issue
Block a user