1
This commit is contained in:
@@ -20,21 +20,20 @@ module.exports = {
|
||||
'GET /dashboard/overview': async (ctx) => {
|
||||
const models = Framework.getModels();
|
||||
const {
|
||||
device_status,
|
||||
pla_account,
|
||||
task_status,
|
||||
job_postings,
|
||||
apply_records,
|
||||
chat_records,
|
||||
op
|
||||
} = models;
|
||||
const deviceManager = require('../../middleware/schedule/deviceManager');
|
||||
|
||||
|
||||
// 设备统计
|
||||
const [totalDevices, onlineDevices, runningDevices] = await Promise.all([
|
||||
device_status.count(),
|
||||
device_status.count({ where: { isOnline: true } }),
|
||||
device_status.count({ where: { isRunning: true } })
|
||||
]);
|
||||
// 设备统计(从 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 runningDevices = 0; // 不再维护运行状态
|
||||
|
||||
// 任务统计
|
||||
const [totalTasks, runningTasks, completedTasks, failedTasks] = await Promise.all([
|
||||
@@ -211,45 +210,58 @@ return ctx.success({
|
||||
*/
|
||||
'GET /dashboard/device-performance': async (ctx) => {
|
||||
const models = Framework.getModels();
|
||||
const { device_status } = models;
|
||||
const { pla_account, task_status, job_postings, apply_records, chat_records } = models;
|
||||
const deviceManager = require('../../middleware/schedule/deviceManager');
|
||||
|
||||
|
||||
const devices = await device_status.findAll({
|
||||
attributes: [
|
||||
'sn_code',
|
||||
'deviceName',
|
||||
'totalTasksCompleted',
|
||||
'totalTasksFailed',
|
||||
'totalJobsSearched',
|
||||
'totalApplies',
|
||||
'totalChats',
|
||||
'healthScore',
|
||||
'onlineDuration'
|
||||
],
|
||||
order: [['totalTasksCompleted', 'DESC']],
|
||||
limit: 20
|
||||
});
|
||||
// 从 pla_account 获取所有账号
|
||||
const accounts = await pla_account.findAll({
|
||||
where: { is_delete: 0 },
|
||||
attributes: ['id', 'sn_code', 'name'],
|
||||
limit: 20
|
||||
});
|
||||
|
||||
const performanceData = devices.map(device => {
|
||||
const total = device.totalTasksCompleted + device.totalTasksFailed;
|
||||
const successRate = total > 0 ? ((device.totalTasksCompleted / total) * 100).toFixed(2) : 0;
|
||||
|
||||
return {
|
||||
sn_code: device.sn_code,
|
||||
deviceName: device.deviceName,
|
||||
tasksCompleted: device.totalTasksCompleted,
|
||||
tasksFailed: device.totalTasksFailed,
|
||||
jobsSearched: device.totalJobsSearched,
|
||||
applies: device.totalApplies,
|
||||
chats: device.totalChats,
|
||||
successRate,
|
||||
healthScore: device.healthScore,
|
||||
onlineDuration: device.onlineDuration
|
||||
};
|
||||
});
|
||||
// 获取设备在线状态
|
||||
const deviceStatus = deviceManager.getAllDevicesStatus();
|
||||
|
||||
return ctx.success(performanceData);
|
||||
|
||||
// 为每个账号统计任务、岗位、投递、聊天数据
|
||||
const performanceData = await Promise.all(accounts.map(async (account) => {
|
||||
const snCode = account.sn_code;
|
||||
const status = deviceStatus[snCode] || { isOnline: false };
|
||||
|
||||
// 统计任务
|
||||
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),
|
||||
apply_records.count({ where: { sn_code: snCode } }).catch(() => 0),
|
||||
chat_records.count({ where: { sn_code: snCode } }).catch(() => 0)
|
||||
]);
|
||||
|
||||
const total = completedTasks + failedTasks;
|
||||
const successRate = total > 0 ? ((completedTasks / total) * 100).toFixed(2) : 0;
|
||||
|
||||
return {
|
||||
sn_code: snCode,
|
||||
deviceName: account.name || snCode,
|
||||
tasksCompleted: completedTasks,
|
||||
tasksFailed: failedTasks,
|
||||
jobsSearched,
|
||||
applies,
|
||||
chats,
|
||||
successRate,
|
||||
healthScore: status.isOnline ? 100 : 0,
|
||||
onlineDuration: 0 // 不再维护在线时长
|
||||
};
|
||||
}));
|
||||
|
||||
// 按完成任务数排序
|
||||
performanceData.sort((a, b) => b.tasksCompleted - a.tasksCompleted);
|
||||
|
||||
return ctx.success(performanceData);
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user