1
This commit is contained in:
@@ -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 // 不再维护在线时长
|
||||
};
|
||||
}));
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user