mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-09-24 12:13:28 +00:00
Refactor(pbsUtils): Extract task categorization and list creation to helpers
This commit is contained in:
+49
-34
@@ -11,13 +11,6 @@ function processPbsTasks(allTasks) {
|
||||
pruneTasks: { recentTasks: [], summary: { ok: 0, failed: 0, total: 0, lastOk: null, lastFailed: null } }
|
||||
};
|
||||
|
||||
const taskResults = {
|
||||
backup: { list: [], ok: 0, failed: 0, lastOk: 0, lastFailed: 0 },
|
||||
verify: { list: [], ok: 0, failed: 0, lastOk: 0, lastFailed: 0 },
|
||||
sync: { list: [], ok: 0, failed: 0, lastOk: 0, lastFailed: 0 },
|
||||
pruneGc: { list: [], ok: 0, failed: 0, lastOk: 0, lastFailed: 0 } // Combined prune/gc
|
||||
};
|
||||
|
||||
const taskTypeMap = {
|
||||
backup: 'backup',
|
||||
verify: 'verify',
|
||||
@@ -28,26 +21,7 @@ function processPbsTasks(allTasks) {
|
||||
prune: 'pruneGc'
|
||||
};
|
||||
|
||||
allTasks.forEach(task => {
|
||||
const taskType = task.worker_type || task.type;
|
||||
const categoryKey = taskTypeMap[taskType];
|
||||
|
||||
if (categoryKey) {
|
||||
const category = taskResults[categoryKey];
|
||||
category.list.push(task);
|
||||
|
||||
const isOk = task.status === 'OK';
|
||||
const isFailed = task.status && task.status !== 'OK' && task.status !== 'running';
|
||||
|
||||
if (isOk) {
|
||||
category.ok++;
|
||||
if (task.endtime > category.lastOk) category.lastOk = task.endtime;
|
||||
} else if (isFailed) {
|
||||
category.failed++;
|
||||
if (task.endtime > category.lastFailed) category.lastFailed = task.endtime;
|
||||
}
|
||||
}
|
||||
});
|
||||
const taskResults = categorizeAndCountTasks(allTasks, taskTypeMap);
|
||||
|
||||
const createDetailedTask = (task) => ({
|
||||
upid: task.upid,
|
||||
@@ -61,13 +35,18 @@ function processPbsTasks(allTasks) {
|
||||
});
|
||||
|
||||
const sortTasksDesc = (a, b) => (b.startTime || 0) - (a.startTime || 0);
|
||||
|
||||
const getRecentTasksList = (taskList, detailedTaskFn, sortFn, count = 20) => {
|
||||
if (!taskList) return []; // Guard against undefined taskList
|
||||
return taskList.map(detailedTaskFn).sort(sortFn).slice(0, count);
|
||||
};
|
||||
|
||||
const recentBackupTasks = taskResults.backup.list.map(createDetailedTask).sort(sortTasksDesc).slice(0, 20);
|
||||
const recentVerifyTasks = taskResults.verify.list.map(createDetailedTask).sort(sortTasksDesc).slice(0, 20);
|
||||
const recentSyncTasks = taskResults.sync.list.map(createDetailedTask).sort(sortTasksDesc).slice(0, 20);
|
||||
const recentPruneGcTasks = taskResults.pruneGc.list.map(createDetailedTask).sort(sortTasksDesc).slice(0, 20);
|
||||
|
||||
console.log(`INFO: [pbsUtils] Processed PBS Tasks - Backup: ${taskResults.backup.list.length} (...), Verify: ${taskResults.verify.list.length} (...), Sync: ${taskResults.sync.list.length} (...), Prune/GC: ${taskResults.pruneGc.list.length} (...)`); // Shortened log
|
||||
const recentBackupTasks = getRecentTasksList(taskResults.backup.list, createDetailedTask, sortTasksDesc);
|
||||
const recentVerifyTasks = getRecentTasksList(taskResults.verify.list, createDetailedTask, sortTasksDesc);
|
||||
const recentSyncTasks = getRecentTasksList(taskResults.sync.list, createDetailedTask, sortTasksDesc);
|
||||
const recentPruneGcTasks = getRecentTasksList(taskResults.pruneGc.list, createDetailedTask, sortTasksDesc);
|
||||
|
||||
console.log(`INFO: [pbsUtils] Processed PBS Tasks - Backup: ${taskResults.backup.list.length}, Verify: ${taskResults.verify.list.length}, Sync: ${taskResults.sync.list.length}, Prune/GC: ${taskResults.pruneGc.list.length}`);
|
||||
|
||||
// Helper function to create the summary object
|
||||
const createSummary = (category) => ({
|
||||
@@ -98,4 +77,40 @@ function processPbsTasks(allTasks) {
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { processPbsTasks };
|
||||
function categorizeAndCountTasks(allTasks, taskTypeMap) {
|
||||
const results = {
|
||||
backup: { list: [], ok: 0, failed: 0, lastOk: 0, lastFailed: 0 },
|
||||
verify: { list: [], ok: 0, failed: 0, lastOk: 0, lastFailed: 0 },
|
||||
sync: { list: [], ok: 0, failed: 0, lastOk: 0, lastFailed: 0 },
|
||||
pruneGc: { list: [], ok: 0, failed: 0, lastOk: 0, lastFailed: 0 }
|
||||
};
|
||||
|
||||
if (!allTasks || !Array.isArray(allTasks)) { // Added check for allTasks being an array
|
||||
return results; // Return empty results if no tasks or invalid format
|
||||
}
|
||||
|
||||
allTasks.forEach(task => {
|
||||
const taskType = task.worker_type || task.type;
|
||||
const categoryKey = taskTypeMap[taskType];
|
||||
|
||||
if (categoryKey) {
|
||||
const category = results[categoryKey];
|
||||
category.list.push(task);
|
||||
|
||||
const isOk = task.status === 'OK';
|
||||
// Consider 'WARNING' or other non-OK statuses as failed for summary purposes if needed
|
||||
const isFailed = task.status && task.status !== 'OK' && task.status !== 'running';
|
||||
|
||||
if (isOk) {
|
||||
category.ok++;
|
||||
if (task.endtime > category.lastOk) category.lastOk = task.endtime;
|
||||
} else if (isFailed) {
|
||||
category.failed++;
|
||||
if (task.endtime > category.lastFailed) category.lastFailed = task.endtime;
|
||||
}
|
||||
}
|
||||
});
|
||||
return results;
|
||||
}
|
||||
|
||||
module.exports = { processPbsTasks };
|
||||
|
||||
Reference in New Issue
Block a user