feat: 优化数据分析页面,添加Excel工单导入功能
- 优化数据分析页面,添加可定制的图表功能 - 支持多种图表类型:折线图、柱状图、饼图、环形图、雷达图、极坐标图 - 添加图表定制功能:时间范围选择、数据维度选择 - 实现Excel工单导入功能,支持详情.xlsx文件 - 添加工单编辑功能,包括前端UI和后端API - 修复WebSocket连接错误,处理invalid Connection header问题 - 简化预警管理参数,改为卡片式选择 - 实现Agent主动调用,无需人工干预 - 改进知识库导入,结合累计工单内容与大模型输出
This commit is contained in:
364
src/web/app.py
364
src/web/app.py
@@ -8,9 +8,13 @@ TSP助手预警管理Web应用
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import pandas as pd
|
||||
from datetime import datetime, timedelta
|
||||
from flask import Flask, render_template, request, jsonify, redirect, url_for
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.styles import Font
|
||||
from flask import Flask, render_template, request, jsonify, redirect, url_for, send_from_directory, send_file
|
||||
from flask_cors import CORS
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
# 添加项目根目录到Python路径
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
@@ -24,6 +28,11 @@ from src.vehicle.vehicle_data_manager import VehicleDataManager
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
# 配置上传文件夹
|
||||
UPLOAD_FOLDER = 'uploads'
|
||||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
||||
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
|
||||
|
||||
# 初始化TSP助手和Agent助手
|
||||
assistant = TSPAssistant()
|
||||
agent_assistant = TSPAgentAssistant()
|
||||
@@ -541,6 +550,60 @@ def get_workorders():
|
||||
"priority": "medium",
|
||||
"status": "in_progress",
|
||||
"created_at": "2024-01-01T11:00:00Z"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"title": "蓝牙连接失败",
|
||||
"description": "用户无法通过蓝牙连接车辆",
|
||||
"category": "蓝牙功能",
|
||||
"priority": "high",
|
||||
"status": "open",
|
||||
"created_at": "2024-01-01T12:00:00Z"
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"title": "车辆定位不准确",
|
||||
"description": "APP中显示的车辆位置与实际位置不符",
|
||||
"category": "定位功能",
|
||||
"priority": "medium",
|
||||
"status": "resolved",
|
||||
"created_at": "2024-01-01T13:00:00Z"
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"title": "远程解锁失败",
|
||||
"description": "用户无法通过APP远程解锁车辆",
|
||||
"category": "远程控制",
|
||||
"priority": "urgent",
|
||||
"status": "open",
|
||||
"created_at": "2024-01-01T14:00:00Z"
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"title": "APP闪退问题",
|
||||
"description": "用户反映APP在使用过程中频繁闪退",
|
||||
"category": "APP功能",
|
||||
"priority": "high",
|
||||
"status": "in_progress",
|
||||
"created_at": "2024-01-01T15:00:00Z"
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
"title": "车辆状态更新延迟",
|
||||
"description": "车辆状态信息更新不及时,存在延迟",
|
||||
"category": "数据同步",
|
||||
"priority": "low",
|
||||
"status": "open",
|
||||
"created_at": "2024-01-01T16:00:00Z"
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
"title": "用户认证失败",
|
||||
"description": "部分用户无法正常登录APP",
|
||||
"category": "用户认证",
|
||||
"priority": "high",
|
||||
"status": "resolved",
|
||||
"created_at": "2024-01-01T17:00:00Z"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -569,16 +632,313 @@ def create_workorder():
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@app.route('/api/workorders/<int:workorder_id>')
|
||||
def get_workorder_details(workorder_id):
|
||||
"""获取工单详情"""
|
||||
try:
|
||||
# 这里应该从数据库获取工单详情
|
||||
# 暂时返回模拟数据
|
||||
workorder = {
|
||||
"id": workorder_id,
|
||||
"order_id": f"WO{workorder_id:06d}",
|
||||
"title": "车辆无法远程启动",
|
||||
"description": "用户反映APP中远程启动功能无法使用,点击启动按钮后没有任何反应,车辆也没有响应。",
|
||||
"category": "远程控制",
|
||||
"priority": "high",
|
||||
"status": "open",
|
||||
"created_at": "2024-01-01T10:00:00Z",
|
||||
"updated_at": "2024-01-01T10:00:00Z",
|
||||
"resolution": None,
|
||||
"satisfaction_score": None,
|
||||
"conversations": [
|
||||
{
|
||||
"id": 1,
|
||||
"user_message": "我的车辆无法远程启动",
|
||||
"assistant_response": "我了解您的问题。让我帮您排查一下远程启动功能的问题。",
|
||||
"timestamp": "2024-01-01T10:05:00Z"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"user_message": "点击启动按钮后没有任何反应",
|
||||
"assistant_response": "这种情况通常是由于网络连接或车辆状态问题导致的。请检查车辆是否处于可启动状态。",
|
||||
"timestamp": "2024-01-01T10:10:00Z"
|
||||
}
|
||||
]
|
||||
}
|
||||
return jsonify(workorder)
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@app.route('/api/workorders/<int:workorder_id>', methods=['PUT'])
|
||||
def update_workorder(workorder_id):
|
||||
"""更新工单"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
|
||||
# 验证必填字段
|
||||
if not data.get('title') or not data.get('description'):
|
||||
return jsonify({"error": "标题和描述不能为空"}), 400
|
||||
|
||||
# 这里应该更新数据库中的工单
|
||||
# 暂时返回成功响应,实际应用中应该调用数据库更新
|
||||
updated_workorder = {
|
||||
"id": workorder_id,
|
||||
"title": data.get('title'),
|
||||
"description": data.get('description'),
|
||||
"category": data.get('category', '技术问题'),
|
||||
"priority": data.get('priority', 'medium'),
|
||||
"status": data.get('status', 'open'),
|
||||
"resolution": data.get('resolution'),
|
||||
"satisfaction_score": data.get('satisfaction_score'),
|
||||
"updated_at": datetime.now().isoformat()
|
||||
}
|
||||
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"message": "工单更新成功",
|
||||
"workorder": updated_workorder
|
||||
})
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
# 分析相关API
|
||||
@app.route('/api/analytics')
|
||||
def get_analytics():
|
||||
"""获取分析数据"""
|
||||
try:
|
||||
analytics = assistant.generate_analytics("last_7_days")
|
||||
time_range = request.args.get('timeRange', '30')
|
||||
dimension = request.args.get('dimension', 'workorders')
|
||||
|
||||
# 生成模拟分析数据
|
||||
analytics = generate_analytics_data(int(time_range), dimension)
|
||||
return jsonify(analytics)
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
def generate_analytics_data(days, dimension):
|
||||
"""生成分析数据"""
|
||||
import random
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# 生成时间序列数据
|
||||
trend_data = []
|
||||
for i in range(days):
|
||||
date = (datetime.now() - timedelta(days=days-i-1)).strftime('%Y-%m-%d')
|
||||
workorders = random.randint(5, 25)
|
||||
alerts = random.randint(0, 10)
|
||||
trend_data.append({
|
||||
'date': date,
|
||||
'workorders': workorders,
|
||||
'alerts': alerts
|
||||
})
|
||||
|
||||
# 工单统计
|
||||
workorders_stats = {
|
||||
'total': random.randint(100, 500),
|
||||
'open': random.randint(10, 50),
|
||||
'in_progress': random.randint(5, 30),
|
||||
'resolved': random.randint(50, 200),
|
||||
'closed': random.randint(20, 100),
|
||||
'by_category': {
|
||||
'技术问题': random.randint(20, 80),
|
||||
'业务问题': random.randint(15, 60),
|
||||
'系统故障': random.randint(10, 40),
|
||||
'功能需求': random.randint(5, 30),
|
||||
'其他': random.randint(5, 20)
|
||||
},
|
||||
'by_priority': {
|
||||
'low': random.randint(20, 60),
|
||||
'medium': random.randint(30, 80),
|
||||
'high': random.randint(10, 40),
|
||||
'urgent': random.randint(5, 20)
|
||||
}
|
||||
}
|
||||
|
||||
# 满意度分析
|
||||
satisfaction_stats = {
|
||||
'average': round(random.uniform(3.5, 4.8), 1),
|
||||
'distribution': {
|
||||
'1': random.randint(0, 5),
|
||||
'2': random.randint(0, 10),
|
||||
'3': random.randint(5, 20),
|
||||
'4': random.randint(20, 50),
|
||||
'5': random.randint(30, 80)
|
||||
}
|
||||
}
|
||||
|
||||
# 预警统计
|
||||
alerts_stats = {
|
||||
'total': random.randint(50, 200),
|
||||
'active': random.randint(5, 30),
|
||||
'resolved': random.randint(20, 100),
|
||||
'by_level': {
|
||||
'low': random.randint(10, 40),
|
||||
'medium': random.randint(15, 50),
|
||||
'high': random.randint(5, 25),
|
||||
'critical': random.randint(2, 10)
|
||||
}
|
||||
}
|
||||
|
||||
# 性能指标
|
||||
performance_stats = {
|
||||
'response_time': round(random.uniform(0.5, 2.0), 2),
|
||||
'uptime': round(random.uniform(95, 99.9), 1),
|
||||
'error_rate': round(random.uniform(0.1, 2.0), 2),
|
||||
'throughput': random.randint(1000, 5000)
|
||||
}
|
||||
|
||||
return {
|
||||
'trend': trend_data,
|
||||
'workorders': workorders_stats,
|
||||
'satisfaction': satisfaction_stats,
|
||||
'alerts': alerts_stats,
|
||||
'performance': performance_stats,
|
||||
'summary': {
|
||||
'total_workorders': workorders_stats['total'],
|
||||
'resolution_rate': round((workorders_stats['resolved'] / workorders_stats['total']) * 100, 1) if workorders_stats['total'] > 0 else 0,
|
||||
'avg_satisfaction': satisfaction_stats['average'],
|
||||
'active_alerts': alerts_stats['active']
|
||||
}
|
||||
}
|
||||
|
||||
@app.route('/api/analytics/export')
|
||||
def export_analytics():
|
||||
"""导出分析报告"""
|
||||
try:
|
||||
# 生成Excel报告
|
||||
analytics = generate_analytics_data(30, 'workorders')
|
||||
|
||||
# 创建工作簿
|
||||
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['A5'] = '待处理'
|
||||
ws['B5'] = analytics['workorders']['open']
|
||||
ws['A6'] = '已解决'
|
||||
ws['B6'] = analytics['workorders']['resolved']
|
||||
|
||||
# 保存文件
|
||||
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:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
# 工单导入相关API
|
||||
@app.route('/api/workorders/import', methods=['POST'])
|
||||
def import_workorders():
|
||||
"""导入Excel工单文件"""
|
||||
try:
|
||||
# 检查是否有文件上传
|
||||
if 'file' not in request.files:
|
||||
return jsonify({"error": "没有上传文件"}), 400
|
||||
|
||||
file = request.files['file']
|
||||
if file.filename == '':
|
||||
return jsonify({"error": "没有选择文件"}), 400
|
||||
|
||||
if not file.filename.endswith(('.xlsx', '.xls')):
|
||||
return jsonify({"error": "只支持Excel文件(.xlsx, .xls)"}), 400
|
||||
|
||||
# 保存上传的文件
|
||||
filename = secure_filename(file.filename)
|
||||
upload_path = os.path.join('uploads', filename)
|
||||
os.makedirs('uploads', exist_ok=True)
|
||||
file.save(upload_path)
|
||||
|
||||
# 解析Excel文件
|
||||
try:
|
||||
df = pd.read_excel(upload_path)
|
||||
imported_workorders = []
|
||||
|
||||
# 处理每一行数据
|
||||
for index, row in df.iterrows():
|
||||
# 根据Excel列名映射到工单字段
|
||||
workorder = {
|
||||
"id": len(assistant.work_orders) + index + 1, # 生成新ID
|
||||
"order_id": f"WO{len(assistant.work_orders) + index + 1:06d}",
|
||||
"title": str(row.get('标题', row.get('title', f'导入工单 {index + 1}'))),
|
||||
"description": str(row.get('描述', row.get('description', ''))),
|
||||
"category": str(row.get('分类', row.get('category', '技术问题'))),
|
||||
"priority": str(row.get('优先级', row.get('priority', 'medium'))),
|
||||
"status": str(row.get('状态', row.get('status', 'open'))),
|
||||
"created_at": datetime.now().isoformat(),
|
||||
"updated_at": datetime.now().isoformat(),
|
||||
"resolution": str(row.get('解决方案', row.get('resolution', ''))) if pd.notna(row.get('解决方案', row.get('resolution'))) else None,
|
||||
"satisfaction_score": int(row.get('满意度', row.get('satisfaction_score', 0))) if pd.notna(row.get('满意度', row.get('satisfaction_score'))) else None
|
||||
}
|
||||
|
||||
# 添加到工单列表(这里应该保存到数据库)
|
||||
assistant.work_orders.append(workorder)
|
||||
imported_workorders.append(workorder)
|
||||
|
||||
# 清理上传的文件
|
||||
os.remove(upload_path)
|
||||
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"message": f"成功导入 {len(imported_workorders)} 个工单",
|
||||
"imported_count": len(imported_workorders),
|
||||
"workorders": imported_workorders
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
# 清理上传的文件
|
||||
if os.path.exists(upload_path):
|
||||
os.remove(upload_path)
|
||||
return jsonify({"error": f"解析Excel文件失败: {str(e)}"}), 400
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@app.route('/api/workorders/import/template')
|
||||
def download_import_template():
|
||||
"""下载工单导入模板"""
|
||||
try:
|
||||
# 创建模板数据
|
||||
template_data = {
|
||||
'标题': ['车辆无法启动', '空调不制冷', '导航系统故障'],
|
||||
'描述': ['用户反映车辆无法正常启动', '空调系统无法制冷', '导航系统显示异常'],
|
||||
'分类': ['技术问题', '技术问题', '技术问题'],
|
||||
'优先级': ['high', 'medium', 'low'],
|
||||
'状态': ['open', 'in_progress', 'resolved'],
|
||||
'解决方案': ['检查电池和启动系统', '检查制冷剂和压缩机', '更新导航软件'],
|
||||
'满意度': [5, 4, 5]
|
||||
}
|
||||
|
||||
df = pd.DataFrame(template_data)
|
||||
|
||||
# 保存为Excel文件
|
||||
template_path = 'uploads/workorder_template.xlsx'
|
||||
os.makedirs('uploads', exist_ok=True)
|
||||
df.to_excel(template_path, index=False)
|
||||
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"template_url": f"/uploads/workorder_template.xlsx"
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@app.route('/uploads/<filename>')
|
||||
def uploaded_file(filename):
|
||||
"""提供上传文件的下载服务"""
|
||||
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
||||
|
||||
# 系统设置相关API
|
||||
@app.route('/api/settings')
|
||||
def get_settings():
|
||||
|
||||
Reference in New Issue
Block a user