From c113184385f5dee1e328b01e6ec550fcd2f6e8ca Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Tue, 8 Sep 2020 19:04:12 +0700 Subject: [PATCH] Improved disk usage entry in statistics Now will calculate usage as (total - avail). In Linux, ext filesystems by default reserves 5% of the space to be usable by root, making them essentially already "used" space. Originally we didn't take that into account. Disk usage percentage will now round down: Math.round() -> Math.floor(). The general behavior in other tools such as "df". --- controllers/utilsController.js | 8 +++++--- src/js/dashboard.js | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/controllers/utilsController.js b/controllers/utilsController.js index b0e1411..84d1033 100644 --- a/controllers/utilsController.js +++ b/controllers/utilsController.js @@ -746,7 +746,7 @@ self.stats = async (req, res, next) => { await new Promise((resolve, reject) => { const proc = spawn('df', [ '--block-size=1', - '--output=used,size', + '--output=size,avail', paths.uploads ]) @@ -764,9 +764,11 @@ self.stats = async (req, res, next) => { // Skip lines that have non-number chars if (columns.some(w => !/^\d+$/.test(w))) continue + const total = parseInt(columns[0]) + const avail = parseInt(columns[1]) stats.disk.drive = { - used: parseInt(columns[0]), - total: parseInt(columns[1]) + total, + used: total - avail } } }) diff --git a/src/js/dashboard.js b/src/js/dashboard.js index 35cb271..743640b 100644 --- a/src/js/dashboard.js +++ b/src/js/dashboard.js @@ -2860,7 +2860,7 @@ page.getStatistics = (params = {}) => { if ((types.byte || []).includes(valKeys[j])) parsed = page.getPrettyBytes(value) if ((types.byteUsage || []).includes(valKeys[j])) - parsed = `${page.getPrettyBytes(value.used)} / ${page.getPrettyBytes(value.total)} (${Math.round(value.used / value.total * 100)}%)` + parsed = `${page.getPrettyBytes(value.used)} / ${page.getPrettyBytes(value.total)} (${Math.floor(value.used / value.total * 100)}%)` if ((types.uptime || []).includes(valKeys[j])) parsed = page.getPrettyUptime(value)