From 891dfc5777e272c40a1fa2f645a1c32a1d182481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=88=90?= Date: Thu, 18 Dec 2025 18:32:11 +0800 Subject: [PATCH] 1 --- api/controller_admin/dashboard.js | 20 +++------ api/controller_admin/device_monitor.js | 61 ++++++++++++-------------- api/middleware/mqtt/mqttDispatcher.js | 4 +- config/config.js | 2 +- 4 files changed, 37 insertions(+), 50 deletions(-) diff --git a/api/controller_admin/dashboard.js b/api/controller_admin/dashboard.js index 1b18b7e..c321d24 100644 --- a/api/controller_admin/dashboard.js +++ b/api/controller_admin/dashboard.js @@ -27,12 +27,10 @@ module.exports = { chat_records, op } = models; - const deviceManager = require('../middleware/schedule/deviceManager'); - // 设备统计(从 pla_account 和 deviceManager 获取) + // 设备统计(直接从数据库获取) const totalDevices = await pla_account.count({ where: { is_delete: 0 } }); - const deviceStatus = deviceManager.getAllDevicesStatus(); - const onlineDevices = Object.values(deviceStatus).filter(d => d.isOnline).length; + const onlineDevices = await pla_account.count({ where: { is_delete: 0, is_online: 1 } }); const runningDevices = 0; // 不再维护运行状态 // 任务统计 @@ -211,29 +209,25 @@ return ctx.success({ 'GET /dashboard/device-performance': async (ctx) => { const models = Framework.getModels(); const { pla_account, task_status, job_postings, apply_records, chat_records } = models; - const deviceManager = require('../middleware/schedule/deviceManager'); // 从 pla_account 获取所有账号 const accounts = await pla_account.findAll({ where: { is_delete: 0 }, - attributes: ['id', 'sn_code', 'name'], + attributes: ['id', 'sn_code', 'name', 'is_online'], limit: 20 }); - // 获取设备在线状态 - const deviceStatus = deviceManager.getAllDevicesStatus(); - // 为每个账号统计任务、岗位、投递、聊天数据 const performanceData = await Promise.all(accounts.map(async (account) => { const snCode = account.sn_code; - const status = deviceStatus[snCode] || { isOnline: false }; - + const isOnline = account.is_online === 1; + // 统计任务 const [completedTasks, failedTasks] = await Promise.all([ task_status.count({ where: { sn_code: snCode, status: 'completed' } }), task_status.count({ where: { sn_code: snCode, status: 'failed' } }) ]); - + // 统计岗位、投递、聊天(如果有相关字段) const [jobsSearched, applies, chats] = await Promise.all([ job_postings.count({ where: { sn_code: snCode } }).catch(() => 0), @@ -253,7 +247,7 @@ return ctx.success({ applies, chats, successRate, - healthScore: status.isOnline ? 100 : 0, + healthScore: isOnline ? 100 : 0, onlineDuration: 0 // 不再维护在线时长 }; })); diff --git a/api/controller_admin/device_monitor.js b/api/controller_admin/device_monitor.js index e8b2633..aca7185 100644 --- a/api/controller_admin/device_monitor.js +++ b/api/controller_admin/device_monitor.js @@ -102,10 +102,9 @@ module.exports = { 'POST /device/list': async (ctx) => { const models = Framework.getModels(); const { pla_account, op } = models; - const deviceManager = require('../middleware/schedule/deviceManager'); const body = ctx.getBody(); const { isOnline, healthStatus, platform, searchText} = ctx.getBody(); - + // 获取分页参数 const { limit, offset } = ctx.getPageSize(); @@ -121,6 +120,11 @@ module.exports = { ]; } + // 在线状态筛选(直接查询数据库) + if (isOnline !== undefined) { + where.is_online = isOnline ? 1 : 0; + } + const result = await pla_account.findAndCountAll({ where, limit, @@ -128,35 +132,26 @@ module.exports = { order: [['id', 'DESC']] }); - // 获取设备在线状态 - const deviceStatus = deviceManager.getAllDevicesStatus(); - - // 组合数据并过滤在线状态 - let list = result.rows.map(account => { + // 组合数据(直接从数据库读取) + const list = result.rows.map(account => { const accountData = account.toJSON(); - const status = deviceStatus[accountData.sn_code] || { isOnline: false }; return { sn_code: accountData.sn_code, device_id: accountData.device_id, deviceName: accountData.name || accountData.sn_code, platform: accountData.platform_type, - isOnline: status.isOnline || false, + isOnline: accountData.is_online === 1, isRunning: false, - lastHeartbeatTime: status.lastHeartbeat ? new Date(status.lastHeartbeat) : null, + lastHeartbeatTime: null, accountName: accountData.name, platform_type: accountData.platform_type, is_enabled: accountData.is_enabled }; }); - // 如果指定了在线状态筛选 - if (isOnline !== undefined) { - list = list.filter(item => item.isOnline === isOnline); - } - return ctx.success({ - total: list.length, - list: list.slice(0, limit) + total: result.count, + list: list }); }, @@ -174,30 +169,28 @@ module.exports = { 'GET /device/overview': async (ctx) => { const models = Framework.getModels(); const { pla_account } = models; - const deviceManager = require('../middleware/schedule/deviceManager'); - // 从 pla_account 获取账号总数 + // 从 pla_account 获取账号统计(直接查询数据库) const totalDevices = await pla_account.count({ where: { is_delete: 0 } }); - - // 从 deviceManager 获取在线设备统计 - const deviceStatus = deviceManager.getAllDevicesStatus(); - const onlineDevices = Object.values(deviceStatus).filter(d => d.isOnline).length; + const onlineDevices = await pla_account.count({ where: { is_delete: 0, is_online: 1 } }); const runningDevices = 0; // 不再维护运行状态 const healthyDevices = onlineDevices; // 简化处理,在线即健康 const warningDevices = 0; const errorDevices = 0; - // 获取最近离线的设备(从内存状态中获取) - const offlineDevicesList = Object.entries(deviceStatus) - .filter(([sn_code, status]) => !status.isOnline) - .map(([sn_code, status]) => ({ - sn_code, - deviceName: sn_code, - lastOfflineTime: status.lastHeartbeat ? new Date(status.lastHeartbeat) : null, - lastError: '' - })) - .sort((a, b) => (b.lastOfflineTime?.getTime() || 0) - (a.lastOfflineTime?.getTime() || 0)) - .slice(0, 5); + // 获取最近离线的设备(从数据库查询) + const offlineAccounts = await pla_account.findAll({ + where: { is_delete: 0, is_online: 0 }, + limit: 5, + order: [['id', 'DESC']] + }); + + const offlineDevicesList = offlineAccounts.map(account => ({ + sn_code: account.sn_code, + deviceName: account.name || account.sn_code, + lastOfflineTime: null, + lastError: '' + })); return ctx.success({ totalDevices, diff --git a/api/middleware/mqtt/mqttDispatcher.js b/api/middleware/mqtt/mqttDispatcher.js index d10b20d..54e4c29 100644 --- a/api/middleware/mqtt/mqttDispatcher.js +++ b/api/middleware/mqtt/mqttDispatcher.js @@ -278,8 +278,8 @@ class MqttDispatcher { // 更新 pla_account 表中的在线和登录状态 try { - const models = db.getModels(); - await models.pla_account.update( + const pla_account = db.getModel('pla_account'); + await pla_account.update( { is_online: 1, is_logged_in: updateData.isLoggedIn ? 1 : 0 diff --git a/config/config.js b/config/config.js index c9a608d..8f34850 100644 --- a/config/config.js +++ b/config/config.js @@ -87,7 +87,7 @@ module.exports = { enabled: true, timezone: 'Asia/Shanghai' }, - qq_map_key: "7AXBZ-Z7M3V-BZQPK-53TUZ-2QLC6-RAFKU", + qq_map_key: "VIABZ-3N6HT-4BLXK-VF3FD-TM6YF-YRFQM", };