fix: systeminformation v5 breaking changes

Made the codes for stats generation a bit more readable.

Usage percentage for file systems will now properly reflect "non-root"
usage percentage in ext2/3/4 file systems.
This commit is contained in:
Bobby Wibowo 2021-01-27 23:50:45 +07:00
parent 51c9cd2ff4
commit 968494bb37
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
2 changed files with 258 additions and 236 deletions

View File

@ -647,9 +647,8 @@ self.stats = async (req, res, next) => {
}) })
const os = await si.osInfo() const os = await si.osInfo()
await Promise.all([
(async () => { const getSystemInfo = async () => {
// System info
const data = statsData.system const data = statsData.system
if (!data.cache && data.generating) { if (!data.cache && data.generating) {
@ -680,7 +679,7 @@ self.stats = async (req, res, next) => {
Distro: `${os.distro} ${os.release}`, Distro: `${os.distro} ${os.release}`,
Kernel: os.kernel, Kernel: os.kernel,
Scanner: self.clamscan.version || 'N/A', Scanner: self.clamscan.version || 'N/A',
'CPU Load': `${currentLoad.currentload.toFixed(1)}%`, 'CPU Load': `${currentLoad.currentLoad.toFixed(1)}%`,
'CPUs Load': currentLoad.cpus.map(cpu => `${cpu.load.toFixed(1)}%`).join(', '), 'CPUs Load': currentLoad.cpus.map(cpu => `${cpu.load.toFixed(1)}%`).join(', '),
'System Memory': { 'System Memory': {
value: { value: {
@ -708,9 +707,9 @@ self.stats = async (req, res, next) => {
data.cache = stats[data.title] data.cache = stats[data.title]
data.generating = false data.generating = false
} }
})(), }
(async () => {
// File systems const getFileSystems = async () => {
const data = statsData.fileSystems const data = statsData.fileSystems
if (!data.cache && data.generating) { if (!data.cache && data.generating) {
@ -729,7 +728,8 @@ self.stats = async (req, res, next) => {
stats[data.title][`${fs.fs} (${fs.type}) on ${fs.mount}`] = { stats[data.title][`${fs.fs} (${fs.type}) on ${fs.mount}`] = {
value: { value: {
total: fs.size, total: fs.size,
used: fs.used used: fs.used,
available: fs.available
}, },
type: 'byteUsage' type: 'byteUsage'
} }
@ -739,9 +739,9 @@ self.stats = async (req, res, next) => {
data.cache = stats[data.title] data.cache = stats[data.title]
data.generating = false data.generating = false
} }
})(), }
(async () => {
// Uploads const getUploadsStats = async () => {
const data = statsData.uploads const data = statsData.uploads
if (!data.cache && data.generating) { if (!data.cache && data.generating) {
@ -766,14 +766,14 @@ self.stats = async (req, res, next) => {
} }
} }
await Promise.all([ const getTotalCountAndSize = async () => {
(async () => {
const uploads = await db.table('files') const uploads = await db.table('files')
.select('size') .select('size')
stats[data.title].Total = uploads.length stats[data.title].Total = uploads.length
stats[data.title]['Size in DB'].value = uploads.reduce((acc, upload) => acc + parseInt(upload.size), 0) stats[data.title]['Size in DB'].value = uploads.reduce((acc, upload) => acc + parseInt(upload.size), 0)
})(), }
(async () => {
const getImagesCount = async () => {
stats[data.title].Images = await db.table('files') stats[data.title].Images = await db.table('files')
.where(function () { .where(function () {
for (const ext of self.imageExts) { for (const ext of self.imageExts) {
@ -782,8 +782,9 @@ self.stats = async (req, res, next) => {
}) })
.count('id as count') .count('id as count')
.then(rows => rows[0].count) .then(rows => rows[0].count)
})(), }
(async () => {
const getVideosCount = async () => {
stats[data.title].Videos = await db.table('files') stats[data.title].Videos = await db.table('files')
.where(function () { .where(function () {
for (const ext of self.videoExts) { for (const ext of self.videoExts) {
@ -792,8 +793,9 @@ self.stats = async (req, res, next) => {
}) })
.count('id as count') .count('id as count')
.then(rows => rows[0].count) .then(rows => rows[0].count)
})(), }
(async () => {
const getAudiosCount = async () => {
stats[data.title].Audios = await db.table('files') stats[data.title].Audios = await db.table('files')
.where(function () { .where(function () {
for (const ext of self.audioExts) { for (const ext of self.audioExts) {
@ -802,13 +804,21 @@ self.stats = async (req, res, next) => {
}) })
.count('id as count') .count('id as count')
.then(rows => rows[0].count) .then(rows => rows[0].count)
})(), }
(async () => {
const getOthersCount = async () => {
stats[data.title].Temporary = await db.table('files') stats[data.title].Temporary = await db.table('files')
.whereNotNull('expirydate') .whereNotNull('expirydate')
.count('id as count') .count('id as count')
.then(rows => rows[0].count) .then(rows => rows[0].count)
})() }
await Promise.all([
getTotalCountAndSize(),
getImagesCount(),
getVideosCount(),
getAudiosCount(),
getOthersCount()
]) ])
stats[data.title].Others = stats[data.title].Total - stats[data.title].Others = stats[data.title].Total -
@ -820,9 +830,9 @@ self.stats = async (req, res, next) => {
data.cache = stats[data.title] data.cache = stats[data.title]
data.generating = false data.generating = false
} }
})(), }
(async () => {
// Users const getUsersStats = async () => {
const data = statsData.users const data = statsData.users
if (!data.cache && data.generating) { if (!data.cache && data.generating) {
@ -864,9 +874,9 @@ self.stats = async (req, res, next) => {
data.cache = stats[data.title] data.cache = stats[data.title]
data.generating = false data.generating = false
} }
})(), }
(async () => {
// Albums const getAlbumsStats = async () => {
const data = statsData.albums const data = statsData.albums
if (!data.cache && data.generating) { if (!data.cache && data.generating) {
@ -910,7 +920,14 @@ self.stats = async (req, res, next) => {
data.cache = stats[data.title] data.cache = stats[data.title]
data.generating = false data.generating = false
} }
})() }
await Promise.all([
getSystemInfo(),
getFileSystems(),
getUploadsStats(),
getUsersStats(),
getAlbumsStats()
]) ])
return res.json({ success: true, stats, hrtime: process.hrtime(hrstart) }) return res.json({ success: true, stats, hrtime: process.hrtime(hrstart) })

View File

@ -2934,9 +2934,14 @@ page.getStatistics = (params = {}) => {
case 'byte': case 'byte':
parsed = page.getPrettyBytes(value) parsed = page.getPrettyBytes(value)
break break
case 'byteUsage': case 'byteUsage': {
parsed = `${page.getPrettyBytes(value.used)} / ${page.getPrettyBytes(value.total)} (${(value.used / value.total * 100).toFixed(2)}%)` // Reasoning: https://github.com/sebhildebrandt/systeminformation/issues/464#issuecomment-756406053
const totalForPercentage = typeof value.available !== 'undefined'
? (value.used + value.available)
: value.total
parsed = `${page.getPrettyBytes(value.used)} / ${page.getPrettyBytes(value.total)} (${(value.used / totalForPercentage * 100).toFixed(2)}%)`
break break
}
case 'uptime': case 'uptime':
parsed = page.getPrettyUptime(value) parsed = page.getPrettyUptime(value)
break break