This commit is contained in:
张成
2025-12-18 18:32:11 +08:00
parent 699c1d4f55
commit 891dfc5777
4 changed files with 37 additions and 50 deletions

View File

@@ -27,12 +27,10 @@ module.exports = {
chat_records, chat_records,
op op
} = models; } = models;
const deviceManager = require('../middleware/schedule/deviceManager');
// 设备统计(从 pla_account 和 deviceManager 获取) // 设备统计(直接从数据库获取)
const totalDevices = await pla_account.count({ where: { is_delete: 0 } }); const totalDevices = await pla_account.count({ where: { is_delete: 0 } });
const deviceStatus = deviceManager.getAllDevicesStatus(); const onlineDevices = await pla_account.count({ where: { is_delete: 0, is_online: 1 } });
const onlineDevices = Object.values(deviceStatus).filter(d => d.isOnline).length;
const runningDevices = 0; // 不再维护运行状态 const runningDevices = 0; // 不再维护运行状态
// 任务统计 // 任务统计
@@ -211,29 +209,25 @@ return ctx.success({
'GET /dashboard/device-performance': async (ctx) => { 'GET /dashboard/device-performance': async (ctx) => {
const models = Framework.getModels(); const models = Framework.getModels();
const { pla_account, task_status, job_postings, apply_records, chat_records } = models; const { pla_account, task_status, job_postings, apply_records, chat_records } = models;
const deviceManager = require('../middleware/schedule/deviceManager');
// 从 pla_account 获取所有账号 // 从 pla_account 获取所有账号
const accounts = await pla_account.findAll({ const accounts = await pla_account.findAll({
where: { is_delete: 0 }, where: { is_delete: 0 },
attributes: ['id', 'sn_code', 'name'], attributes: ['id', 'sn_code', 'name', 'is_online'],
limit: 20 limit: 20
}); });
// 获取设备在线状态
const deviceStatus = deviceManager.getAllDevicesStatus();
// 为每个账号统计任务、岗位、投递、聊天数据 // 为每个账号统计任务、岗位、投递、聊天数据
const performanceData = await Promise.all(accounts.map(async (account) => { const performanceData = await Promise.all(accounts.map(async (account) => {
const snCode = account.sn_code; const snCode = account.sn_code;
const status = deviceStatus[snCode] || { isOnline: false }; const isOnline = account.is_online === 1;
// 统计任务 // 统计任务
const [completedTasks, failedTasks] = await Promise.all([ const [completedTasks, failedTasks] = await Promise.all([
task_status.count({ where: { sn_code: snCode, status: 'completed' } }), task_status.count({ where: { sn_code: snCode, status: 'completed' } }),
task_status.count({ where: { sn_code: snCode, status: 'failed' } }) task_status.count({ where: { sn_code: snCode, status: 'failed' } })
]); ]);
// 统计岗位、投递、聊天(如果有相关字段) // 统计岗位、投递、聊天(如果有相关字段)
const [jobsSearched, applies, chats] = await Promise.all([ const [jobsSearched, applies, chats] = await Promise.all([
job_postings.count({ where: { sn_code: snCode } }).catch(() => 0), job_postings.count({ where: { sn_code: snCode } }).catch(() => 0),
@@ -253,7 +247,7 @@ return ctx.success({
applies, applies,
chats, chats,
successRate, successRate,
healthScore: status.isOnline ? 100 : 0, healthScore: isOnline ? 100 : 0,
onlineDuration: 0 // 不再维护在线时长 onlineDuration: 0 // 不再维护在线时长
}; };
})); }));

View File

@@ -102,10 +102,9 @@ module.exports = {
'POST /device/list': async (ctx) => { 'POST /device/list': async (ctx) => {
const models = Framework.getModels(); const models = Framework.getModels();
const { pla_account, op } = models; const { pla_account, op } = models;
const deviceManager = require('../middleware/schedule/deviceManager');
const body = ctx.getBody(); const body = ctx.getBody();
const { isOnline, healthStatus, platform, searchText} = ctx.getBody(); const { isOnline, healthStatus, platform, searchText} = ctx.getBody();
// 获取分页参数 // 获取分页参数
const { limit, offset } = ctx.getPageSize(); 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({ const result = await pla_account.findAndCountAll({
where, where,
limit, limit,
@@ -128,35 +132,26 @@ module.exports = {
order: [['id', 'DESC']] order: [['id', 'DESC']]
}); });
// 获取设备在线状态 // 组合数据(直接从数据库读取)
const deviceStatus = deviceManager.getAllDevicesStatus(); const list = result.rows.map(account => {
// 组合数据并过滤在线状态
let list = result.rows.map(account => {
const accountData = account.toJSON(); const accountData = account.toJSON();
const status = deviceStatus[accountData.sn_code] || { isOnline: false };
return { return {
sn_code: accountData.sn_code, sn_code: accountData.sn_code,
device_id: accountData.device_id, device_id: accountData.device_id,
deviceName: accountData.name || accountData.sn_code, deviceName: accountData.name || accountData.sn_code,
platform: accountData.platform_type, platform: accountData.platform_type,
isOnline: status.isOnline || false, isOnline: accountData.is_online === 1,
isRunning: false, isRunning: false,
lastHeartbeatTime: status.lastHeartbeat ? new Date(status.lastHeartbeat) : null, lastHeartbeatTime: null,
accountName: accountData.name, accountName: accountData.name,
platform_type: accountData.platform_type, platform_type: accountData.platform_type,
is_enabled: accountData.is_enabled is_enabled: accountData.is_enabled
}; };
}); });
// 如果指定了在线状态筛选
if (isOnline !== undefined) {
list = list.filter(item => item.isOnline === isOnline);
}
return ctx.success({ return ctx.success({
total: list.length, total: result.count,
list: list.slice(0, limit) list: list
}); });
}, },
@@ -174,30 +169,28 @@ module.exports = {
'GET /device/overview': async (ctx) => { 'GET /device/overview': async (ctx) => {
const models = Framework.getModels(); const models = Framework.getModels();
const { pla_account } = models; const { pla_account } = models;
const deviceManager = require('../middleware/schedule/deviceManager');
// 从 pla_account 获取账号总数 // 从 pla_account 获取账号统计(直接查询数据库)
const totalDevices = await pla_account.count({ where: { is_delete: 0 } }); const totalDevices = await pla_account.count({ where: { is_delete: 0 } });
const onlineDevices = await pla_account.count({ where: { is_delete: 0, is_online: 1 } });
// 从 deviceManager 获取在线设备统计
const deviceStatus = deviceManager.getAllDevicesStatus();
const onlineDevices = Object.values(deviceStatus).filter(d => d.isOnline).length;
const runningDevices = 0; // 不再维护运行状态 const runningDevices = 0; // 不再维护运行状态
const healthyDevices = onlineDevices; // 简化处理,在线即健康 const healthyDevices = onlineDevices; // 简化处理,在线即健康
const warningDevices = 0; const warningDevices = 0;
const errorDevices = 0; const errorDevices = 0;
// 获取最近离线的设备(从内存状态中获取) // 获取最近离线的设备(从数据库查询)
const offlineDevicesList = Object.entries(deviceStatus) const offlineAccounts = await pla_account.findAll({
.filter(([sn_code, status]) => !status.isOnline) where: { is_delete: 0, is_online: 0 },
.map(([sn_code, status]) => ({ limit: 5,
sn_code, order: [['id', 'DESC']]
deviceName: sn_code, });
lastOfflineTime: status.lastHeartbeat ? new Date(status.lastHeartbeat) : null,
lastError: '' const offlineDevicesList = offlineAccounts.map(account => ({
})) sn_code: account.sn_code,
.sort((a, b) => (b.lastOfflineTime?.getTime() || 0) - (a.lastOfflineTime?.getTime() || 0)) deviceName: account.name || account.sn_code,
.slice(0, 5); lastOfflineTime: null,
lastError: ''
}));
return ctx.success({ return ctx.success({
totalDevices, totalDevices,

View File

@@ -278,8 +278,8 @@ class MqttDispatcher {
// 更新 pla_account 表中的在线和登录状态 // 更新 pla_account 表中的在线和登录状态
try { try {
const models = db.getModels(); const pla_account = db.getModel('pla_account');
await models.pla_account.update( await pla_account.update(
{ {
is_online: 1, is_online: 1,
is_logged_in: updateData.isLoggedIn ? 1 : 0 is_logged_in: updateData.isLoggedIn ? 1 : 0

View File

@@ -87,7 +87,7 @@ module.exports = {
enabled: true, enabled: true,
timezone: 'Asia/Shanghai' timezone: 'Asia/Shanghai'
}, },
qq_map_key: "7AXBZ-Z7M3V-BZQPK-53TUZ-2QLC6-RAFKU", qq_map_key: "VIABZ-3N6HT-4BLXK-VF3FD-TM6YF-YRFQM",
}; };