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()
await Promise.all([
(async () => {
// System info
const getSystemInfo = async () => {
const data = statsData.system
if (!data.cache && data.generating) {
@ -680,7 +679,7 @@ self.stats = async (req, res, next) => {
Distro: `${os.distro} ${os.release}`,
Kernel: os.kernel,
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(', '),
'System Memory': {
value: {
@ -708,9 +707,9 @@ self.stats = async (req, res, next) => {
data.cache = stats[data.title]
data.generating = false
}
})(),
(async () => {
// File systems
}
const getFileSystems = async () => {
const data = statsData.fileSystems
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}`] = {
value: {
total: fs.size,
used: fs.used
used: fs.used,
available: fs.available
},
type: 'byteUsage'
}
@ -739,9 +739,9 @@ self.stats = async (req, res, next) => {
data.cache = stats[data.title]
data.generating = false
}
})(),
(async () => {
// Uploads
}
const getUploadsStats = async () => {
const data = statsData.uploads
if (!data.cache && data.generating) {
@ -766,14 +766,14 @@ self.stats = async (req, res, next) => {
}
}
await Promise.all([
(async () => {
const getTotalCountAndSize = async () => {
const uploads = await db.table('files')
.select('size')
stats[data.title].Total = uploads.length
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')
.where(function () {
for (const ext of self.imageExts) {
@ -782,8 +782,9 @@ self.stats = async (req, res, next) => {
})
.count('id as count')
.then(rows => rows[0].count)
})(),
(async () => {
}
const getVideosCount = async () => {
stats[data.title].Videos = await db.table('files')
.where(function () {
for (const ext of self.videoExts) {
@ -792,8 +793,9 @@ self.stats = async (req, res, next) => {
})
.count('id as count')
.then(rows => rows[0].count)
})(),
(async () => {
}
const getAudiosCount = async () => {
stats[data.title].Audios = await db.table('files')
.where(function () {
for (const ext of self.audioExts) {
@ -802,13 +804,21 @@ self.stats = async (req, res, next) => {
})
.count('id as count')
.then(rows => rows[0].count)
})(),
(async () => {
}
const getOthersCount = async () => {
stats[data.title].Temporary = await db.table('files')
.whereNotNull('expirydate')
.count('id as count')
.then(rows => rows[0].count)
})()
}
await Promise.all([
getTotalCountAndSize(),
getImagesCount(),
getVideosCount(),
getAudiosCount(),
getOthersCount()
])
stats[data.title].Others = stats[data.title].Total -
@ -820,9 +830,9 @@ self.stats = async (req, res, next) => {
data.cache = stats[data.title]
data.generating = false
}
})(),
(async () => {
// Users
}
const getUsersStats = async () => {
const data = statsData.users
if (!data.cache && data.generating) {
@ -864,9 +874,9 @@ self.stats = async (req, res, next) => {
data.cache = stats[data.title]
data.generating = false
}
})(),
(async () => {
// Albums
}
const getAlbumsStats = async () => {
const data = statsData.albums
if (!data.cache && data.generating) {
@ -910,7 +920,14 @@ self.stats = async (req, res, next) => {
data.cache = stats[data.title]
data.generating = false
}
})()
}
await Promise.all([
getSystemInfo(),
getFileSystems(),
getUploadsStats(),
getUsersStats(),
getAlbumsStats()
])
return res.json({ success: true, stats, hrtime: process.hrtime(hrstart) })

View File

@ -2934,9 +2934,14 @@ page.getStatistics = (params = {}) => {
case 'byte':
parsed = page.getPrettyBytes(value)
break
case 'byteUsage':
parsed = `${page.getPrettyBytes(value.used)} / ${page.getPrettyBytes(value.total)} (${(value.used / value.total * 100).toFixed(2)}%)`
case 'byteUsage': {
// 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
}
case 'uptime':
parsed = page.getPrettyUptime(value)
break