还是随项目启动。解决对象传递问题
This commit is contained in:
@@ -10,7 +10,6 @@ from datetime import datetime
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
|
||||
|
||||
from flask import Flask, render_template, request, jsonify, redirect, url_for, session, send_from_directory
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
|
||||
from db.connection import DBConnectionManager
|
||||
from db.message_storage import MessageStorageDB
|
||||
@@ -22,6 +21,7 @@ from robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
|
||||
import toml
|
||||
import os.path
|
||||
|
||||
|
||||
class DashboardServer:
|
||||
"""统计看板服务器"""
|
||||
|
||||
@@ -30,16 +30,16 @@ class DashboardServer:
|
||||
robot_instance=None):
|
||||
# 加载配置文件
|
||||
self.config = self._load_dashboard_config()
|
||||
|
||||
|
||||
# 优先使用传入的参数,其次使用配置文件中的参数
|
||||
self.host = host or self.config.get("server", {}).get("host", "0.0.0.0")
|
||||
self.port = port or self.config.get("server", {}).get("port", 8888)
|
||||
self.username = username or self.config.get("auth", {}).get("username", "admin")
|
||||
self.password = password or self.config.get("auth", {}).get("password", "admin123")
|
||||
|
||||
|
||||
self.logger = logging.getLogger("DashboardServer")
|
||||
self.logger.info(f"Dashboard配置加载完成: 服务器将运行在 {self.host}:{self.port}")
|
||||
|
||||
|
||||
# 如果提供了robot实例,则使用其对象
|
||||
if robot_instance:
|
||||
self.db_manager = robot_instance.db_manager
|
||||
@@ -53,11 +53,11 @@ class DashboardServer:
|
||||
# 获取联系人管理器实例
|
||||
self.contact_manager = ContactManager.get_instance()
|
||||
self.logger.info("独立初始化数据库和联系人管理器")
|
||||
|
||||
|
||||
self.app = self._create_app()
|
||||
self._stop_event = threading.Event()
|
||||
self._server = None # 存储服务器实例
|
||||
|
||||
|
||||
def _load_dashboard_config(self):
|
||||
"""加载Dashboard配置文件"""
|
||||
try:
|
||||
@@ -88,10 +88,10 @@ class DashboardServer:
|
||||
# 获取项目根目录的config.yaml文件路径
|
||||
config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'config.yaml'))
|
||||
self.logger.info(f"正在加载配置文件: {config_path}")
|
||||
|
||||
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
config = yaml.safe_load(f)
|
||||
|
||||
|
||||
return config
|
||||
except Exception as e:
|
||||
self.logger.error(f"加载配置文件失败: {e}")
|
||||
@@ -122,41 +122,41 @@ class DashboardServer:
|
||||
try:
|
||||
# 加载配置
|
||||
config = self._load_config()
|
||||
|
||||
|
||||
# 获取数据库配置
|
||||
db_config = config.get("db_config", {})
|
||||
redis_config = config.get("redis_config", {})
|
||||
|
||||
|
||||
self.logger.info("正在初始化数据库连接...")
|
||||
self.logger.info(f"数据库主机: {db_config.get('host')}, 数据库: {db_config.get('database')}")
|
||||
|
||||
|
||||
# 初始化数据库连接管理器 - 按照DBConnectionManager的接口要求传递参数
|
||||
self.db_manager = DBConnectionManager.get_instance(
|
||||
mysql_config=db_config,
|
||||
redis_config=redis_config
|
||||
)
|
||||
|
||||
|
||||
# 初始化数据库操作类
|
||||
self.stats_db = StatsDBOperator(self.db_manager)
|
||||
self.message_storage = MessageStorageDB(self.db_manager)
|
||||
|
||||
|
||||
self.logger.info("数据库连接初始化成功")
|
||||
except Exception as e:
|
||||
self.logger.error(f"初始化数据库连接失败: {e}")
|
||||
raise
|
||||
|
||||
|
||||
def _create_app(self) -> Flask:
|
||||
"""创建Flask应用"""
|
||||
app = Flask(__name__)
|
||||
app.secret_key = "stats_dashboard_secret_key"
|
||||
|
||||
|
||||
# 实现基本的身份验证
|
||||
def check_auth():
|
||||
auth = request.authorization
|
||||
if not auth or auth.username != self.username or auth.password != self.password:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
# 静态文件目录
|
||||
static_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
|
||||
|
||||
@@ -169,7 +169,7 @@ class DashboardServer:
|
||||
def favicon():
|
||||
return send_from_directory(os.path.join(app.root_path, 'static'),
|
||||
'favicon.ico', mimetype='image/vnd.microsoft.icon')
|
||||
|
||||
|
||||
# 登录页面
|
||||
@app.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
@@ -177,30 +177,31 @@ class DashboardServer:
|
||||
if request.method == 'POST':
|
||||
username = request.form['username']
|
||||
password = request.form['password']
|
||||
|
||||
|
||||
if username == self.username and password == self.password:
|
||||
session['logged_in'] = True
|
||||
return redirect(url_for('index'))
|
||||
else:
|
||||
error = '用户名或密码错误'
|
||||
|
||||
|
||||
return render_template('login.html', error=error)
|
||||
|
||||
|
||||
# 登出
|
||||
@app.route('/logout')
|
||||
def logout():
|
||||
session.pop('logged_in', None)
|
||||
return redirect(url_for('login'))
|
||||
|
||||
|
||||
# 登录检查装饰器
|
||||
def login_required(f):
|
||||
def decorated_function(*args, **kwargs):
|
||||
if not session.get('logged_in'):
|
||||
return redirect(url_for('login'))
|
||||
return f(*args, **kwargs)
|
||||
|
||||
decorated_function.__name__ = f.__name__
|
||||
return decorated_function
|
||||
|
||||
|
||||
@app.route('/')
|
||||
@login_required
|
||||
def index():
|
||||
@@ -238,7 +239,7 @@ class DashboardServer:
|
||||
# 获取所有群组列表
|
||||
groups = GroupBotManager.get_group_list()
|
||||
group_data = []
|
||||
|
||||
|
||||
for group_id in groups:
|
||||
group_name = self.contact_manager.get_nickname(group_id)
|
||||
robot_status = GroupBotManager.get_group_permission(group_id, Feature.ROBOT)
|
||||
@@ -247,7 +248,7 @@ class DashboardServer:
|
||||
"group_name": group_name,
|
||||
"robot_status": robot_status.value
|
||||
})
|
||||
|
||||
|
||||
return jsonify({"success": True, "data": group_data})
|
||||
|
||||
@app.route('/api/robot/group/<group_id>/permissions')
|
||||
@@ -255,7 +256,7 @@ class DashboardServer:
|
||||
def api_robot_group_permissions(group_id):
|
||||
permissions = GroupBotManager.list_group_permissions(group_id)
|
||||
permission_data = []
|
||||
|
||||
|
||||
for feature, status in permissions.items():
|
||||
permission_data.append({
|
||||
"feature_id": feature.value,
|
||||
@@ -263,7 +264,7 @@ class DashboardServer:
|
||||
"feature_description": feature.description,
|
||||
"status": status.value
|
||||
})
|
||||
|
||||
|
||||
return jsonify({"success": True, "data": permission_data})
|
||||
|
||||
@app.route('/api/robot/group/<group_id>/permissions', methods=['POST'])
|
||||
@@ -273,11 +274,11 @@ class DashboardServer:
|
||||
data = request.json
|
||||
feature_id = data.get('feature_id')
|
||||
status = data.get('status')
|
||||
|
||||
|
||||
try:
|
||||
feature = Feature(int(feature_id))
|
||||
new_status = PermissionStatus(status)
|
||||
|
||||
|
||||
# 特殊处理ROBOT功能
|
||||
if feature == Feature.ROBOT:
|
||||
r = self.db_manager.get_redis_connection()
|
||||
@@ -287,7 +288,7 @@ class DashboardServer:
|
||||
else:
|
||||
GroupBotManager.local_cache["group_list"].remove(group_id)
|
||||
r.srem("group:list", group_id)
|
||||
|
||||
|
||||
GroupBotManager.set_group_permission(group_id, feature, new_status)
|
||||
return jsonify({"success": True})
|
||||
except Exception as e:
|
||||
@@ -301,15 +302,15 @@ class DashboardServer:
|
||||
data = request.json
|
||||
operation = data.get('operation')
|
||||
group_ids = data.get('group_ids', [])
|
||||
|
||||
|
||||
results = {}
|
||||
|
||||
|
||||
try:
|
||||
if operation == 'remove_groups':
|
||||
for group_id in group_ids:
|
||||
result = GroupBotManager.remove_group(group_id)
|
||||
results[group_id] = result
|
||||
|
||||
|
||||
return jsonify({"success": True, "results": results})
|
||||
else:
|
||||
return jsonify({"success": False, "error": "不支持的操作类型"}), 400
|
||||
@@ -323,13 +324,13 @@ class DashboardServer:
|
||||
days = request.args.get('days', 7, type=int)
|
||||
limit = request.args.get('limit', 10, type=int)
|
||||
stats = self.stats_db.get_user_stats(days, limit)
|
||||
|
||||
|
||||
# 将用户ID转换为名称
|
||||
for item in stats:
|
||||
if 'user_id' in item:
|
||||
user_id = item['user_id']
|
||||
item['user_name'] = self.contact_manager.get_nickname(user_id)
|
||||
|
||||
|
||||
return jsonify({"success": True, "data": stats})
|
||||
|
||||
@app.route('/api/group_stats')
|
||||
@@ -338,13 +339,13 @@ class DashboardServer:
|
||||
days = request.args.get('days', 7, type=int)
|
||||
limit = request.args.get('limit', 10, type=int)
|
||||
stats = self.stats_db.get_group_stats(days, limit)
|
||||
|
||||
|
||||
# 将群ID转换为名称
|
||||
for item in stats:
|
||||
if 'group_id' in item:
|
||||
group_id = item['group_id']
|
||||
item['group_name'] = self.contact_manager.get_nickname(group_id)
|
||||
|
||||
|
||||
return jsonify({"success": True, "data": stats})
|
||||
|
||||
@app.route('/api/plugin_stats')
|
||||
@@ -368,7 +369,7 @@ class DashboardServer:
|
||||
# 获取系统信息
|
||||
import platform
|
||||
import psutil
|
||||
|
||||
|
||||
system_info = {
|
||||
"os": platform.system(),
|
||||
"os_version": platform.version(),
|
||||
@@ -379,7 +380,7 @@ class DashboardServer:
|
||||
"uptime": time.time() - psutil.boot_time(),
|
||||
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
}
|
||||
|
||||
|
||||
return jsonify({"success": True, "data": system_info})
|
||||
|
||||
@app.route('/api/dashboard_summary')
|
||||
@@ -388,18 +389,18 @@ class DashboardServer:
|
||||
try:
|
||||
days = request.args.get('days', 7, type=int)
|
||||
summary = self.stats_db.get_dashboard_summary(days)
|
||||
|
||||
|
||||
# 转换用户和群组ID为名称
|
||||
if 'top_users' in summary:
|
||||
for user in summary['top_users']:
|
||||
if 'user_id' in user:
|
||||
user['user_name'] = self.contact_manager.get_nickname(user['user_id'])
|
||||
|
||||
|
||||
if 'top_groups' in summary:
|
||||
for group in summary['top_groups']:
|
||||
if 'group_id' in group:
|
||||
group['group_name'] = self.contact_manager.get_nickname(group['group_id'])
|
||||
|
||||
|
||||
return jsonify({"success": True, "data": summary})
|
||||
except Exception as e:
|
||||
self.logger.error(f"获取仪表盘摘要数据出错: {e}")
|
||||
@@ -419,7 +420,7 @@ class DashboardServer:
|
||||
try:
|
||||
days = request.args.get('days', 7, type=int)
|
||||
trend_data = self.message_storage.get_message_trend(group_id, days)
|
||||
|
||||
|
||||
# 格式化数据为前端需要的格式
|
||||
dates = []
|
||||
counts = []
|
||||
@@ -429,10 +430,10 @@ class DashboardServer:
|
||||
date_str = item['date'].strftime('%Y-%m-%d')
|
||||
else:
|
||||
date_str = str(item['date'])
|
||||
|
||||
|
||||
dates.append(date_str)
|
||||
counts.append(item['message_count'])
|
||||
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'data': {
|
||||
@@ -456,29 +457,29 @@ class DashboardServer:
|
||||
try:
|
||||
data = request.json
|
||||
group_id = data.get('group_id')
|
||||
|
||||
|
||||
if not group_id or not group_id.strip():
|
||||
return jsonify({"success": False, "error": "群组ID不能为空"}), 400
|
||||
|
||||
|
||||
group_id = group_id.strip()
|
||||
|
||||
|
||||
# 检查群组是否已存在
|
||||
if group_id in GroupBotManager.local_cache["group_list"]:
|
||||
return jsonify({"success": False, "error": "该群组已存在"}), 400
|
||||
|
||||
|
||||
# 添加群组到列表并启用机器人功能
|
||||
GroupBotManager.local_cache["group_list"].add(group_id)
|
||||
r = self.db_manager.get_redis_connection()
|
||||
r.sadd("group:list", group_id)
|
||||
|
||||
|
||||
# 设置ROBOT功能为启用状态
|
||||
GroupBotManager.set_group_permission(group_id, Feature.ROBOT, PermissionStatus.ENABLED)
|
||||
|
||||
|
||||
# 获取群组名称(如果可能)
|
||||
group_name = self.contact_manager.get_nickname(group_id)
|
||||
|
||||
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"success": True,
|
||||
"message": f"群组 {group_id} 已成功添加",
|
||||
"group": {
|
||||
"group_id": group_id,
|
||||
@@ -495,7 +496,7 @@ class DashboardServer:
|
||||
def run(self):
|
||||
"""运行服务器"""
|
||||
from werkzeug.serving import make_server
|
||||
|
||||
|
||||
self.logger.info(f"启动服务器: {self.host}:{self.port}")
|
||||
try:
|
||||
# 修改:使用线程安全的方式运行服务器
|
||||
@@ -509,10 +510,9 @@ class DashboardServer:
|
||||
"""停止服务器"""
|
||||
self.logger.info("正在停止服务器...")
|
||||
self._stop_event.set()
|
||||
|
||||
|
||||
# 修改:使用werkzeug服务器的关闭方法
|
||||
if self._server:
|
||||
self._server.shutdown()
|
||||
|
||||
self.logger.info("服务器已停止")
|
||||
|
||||
self.logger.info("服务器已停止")
|
||||
|
||||
Reference in New Issue
Block a user