Skip to content

Commit 3ea1675

Browse files
committed
intuitive Disk IO sorting
1 parent 64c1426 commit 3ea1675

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/lib/definitions/columns.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ export let column_definitions: Column[] = [
3030
},
3131
{
3232
id: "disk_usage",
33-
label: "Disk R/W",
33+
label: "Disk I/O (R/W)",
3434
visible: true,
35-
format: (v) =>
36-
`${(v[0] / (1024 * 1024)).toFixed(1)} / ${(v[1] / (1024 * 1024)).toFixed(1)} MB`,
35+
format: (v) => {
36+
const readMB = (v[0] / (1024 * 1024)).toFixed(1);
37+
const writeMB = (v[1] / (1024 * 1024)).toFixed(1);
38+
return `${readMB}/${writeMB} MB`;
39+
},
3740
},
3841
{ id: "ppid", label: "Parent PID", visible: false },
3942
{ id: "root", label: "Root", visible: false },

src/lib/utils/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,43 @@ export function sortProcesses(
142142
const aValue = a[sortConfig.field];
143143
const bValue = b[sortConfig.field];
144144

145+
// Special handling for disk_usage which is an array [read_bytes, written_bytes]
146+
if (sortConfig.field === "disk_usage") {
147+
const aRead = (aValue as [number, number])[0];
148+
const aWrite = (aValue as [number, number])[1];
149+
const bRead = (bValue as [number, number])[0];
150+
const bWrite = (bValue as [number, number])[1];
151+
152+
// Smart sorting: analyze if this is a read-heavy or write-heavy comparison
153+
const totalReads = aRead + bRead;
154+
const totalWrites = aWrite + bWrite;
155+
156+
if (totalWrites > totalReads * 1.5) {
157+
// Write-heavy scenario: prioritize writes, use reads as tiebreaker
158+
if (aWrite !== bWrite) {
159+
return direction * (aWrite - bWrite);
160+
}
161+
return direction * (aRead - bRead);
162+
} else if (totalReads > totalWrites * 1.5) {
163+
// Read-heavy scenario: prioritize reads, use writes as tiebreaker
164+
if (aRead !== bRead) {
165+
return direction * (aRead - bRead);
166+
}
167+
return direction * (aWrite - bWrite);
168+
} else {
169+
// Balanced I/O: sort by total, use max as tiebreaker
170+
const aTotalDisk = aRead + aWrite;
171+
const bTotalDisk = bRead + bWrite;
172+
if (aTotalDisk !== bTotalDisk) {
173+
return direction * (aTotalDisk - bTotalDisk);
174+
}
175+
// Tiebreaker: use the dominant operation
176+
const aMaxDisk = Math.max(aRead, aWrite);
177+
const bMaxDisk = Math.max(bRead, bWrite);
178+
return direction * (aMaxDisk - bMaxDisk);
179+
}
180+
}
181+
145182
// Type-specific comparisons
146183
if (typeof aValue === "string") {
147184
return direction * aValue.localeCompare(bValue as string);

0 commit comments

Comments
 (0)