refactor: cleanup info, better cpu usage %

isekai-main
Barış Soner Uşaklı 3 years ago
parent 890bf03fcc
commit 4b738c8cd3

@ -8,7 +8,11 @@
"nodejs": "nodejs",
"online": "online",
"git": "git",
"memory": "memory",
"process-memory": "process memory",
"system-memory": "system memory",
"used-memory-process": "Used memory by process",
"used-memory-os": "Used system memory",
"total-memory-os": "Total system memory",
"load": "system load",
"cpu-usage": "cpu usage",
"uptime": "uptime",

@ -11,6 +11,8 @@ const rooms = require('../../socket.io/admin/rooms');
const infoController = module.exports;
let info = {};
let previousUsage = process.cpuUsage();
let usageStartDate = Date.now();
infoController.get = function (req, res) {
info = {};
@ -69,7 +71,7 @@ async function getNodeInfo() {
version: process.version,
memoryUsage: process.memoryUsage(),
uptime: process.uptime(),
cpuUsage: process.cpuUsage(),
cpuUsage: getCpuUsage(),
},
os: {
hostname: os.hostname(),
@ -88,14 +90,12 @@ async function getNodeInfo() {
jobsDisabled: nconf.get('jobsDisabled'),
},
};
data.process.cpuUsage.user /= 1000000;
data.process.cpuUsage.user = data.process.cpuUsage.user.toFixed(2);
data.process.cpuUsage.system /= 1000000;
data.process.cpuUsage.system = data.process.cpuUsage.system.toFixed(2);
data.process.memoryUsage.humanReadable = (data.process.memoryUsage.rss / (1024 * 1024)).toFixed(2);
data.process.memoryUsage.humanReadable = (data.process.memoryUsage.rss / (1024 * 1024 * 1024)).toFixed(3);
data.process.uptimeHumanReadable = humanReadableUptime(data.process.uptime);
data.os.freemem = (data.os.freemem / 1000000).toFixed(2);
data.os.totalmem = (data.os.totalmem / 1000000).toFixed(2);
data.os.freemem = (data.os.freemem / (1024 * 1024 * 1024)).toFixed(2);
data.os.totalmem = (data.os.totalmem / (1024 * 1024 * 1024)).toFixed(2);
data.os.usedmem = (data.os.totalmem - data.os.freemem).toFixed(2);
const [stats, gitInfo] = await Promise.all([
rooms.getLocalStats(),
getGitInfo(),
@ -105,6 +105,16 @@ async function getNodeInfo() {
return data;
}
function getCpuUsage() {
const newUsage = process.cpuUsage();
const diff = (newUsage.user + newUsage.system) - (previousUsage.user + previousUsage.system);
const now = Date.now();
const result = diff / ((now - usageStartDate) * 1000) * 100;
previousUsage = newUsage;
usageStartDate = now;
return result.toFixed(2);
}
function humanReadableUptime(seconds) {
if (seconds < 60) {
return `${Math.floor(seconds)}s`;

@ -17,7 +17,8 @@
<td>[[admin/development/info:online]]</td>
<td>[[admin/development/info:git]]</td>
<td>[[admin/development/info:cpu-usage]]</td>
<td>[[admin/development/info:memory]]</td>
<td>[[admin/development/info:process-memory]]</td>
<td>[[admin/development/info:system-memory]]</td>
<td>[[admin/development/info:load]]</td>
<td>[[admin/development/info:uptime]]</td>
</tr>
@ -38,8 +39,14 @@
<span title="[[admin/development/info:sockets]]">{info.stats.socketCount}</span>
</td>
<td>{info.git.branch}@<a href="https://github.com/NodeBB/NodeBB/commit/{info.git.hash}" target="_blank">{info.git.hashShort}</a></td>
<td>{info.process.cpuUsage.user} / {info.process.cpuUsage.system}</td>
<td>{info.process.memoryUsage.humanReadable} mb / {info.os.freemem} mb</td>
<td>{info.process.cpuUsage}%</td>
<td>
<span title="[[admin/development/info:used-memory-process]]">{info.process.memoryUsage.humanReadable} gb</span>
</td>
<td>
<span title="[[admin/development/info:used-memory-os]]">{info.os.usedmem} gb</span> /
<span title="[[admin/development/info:total-memory-os]]">{info.os.totalmem} gb</span>
</td>
<td>{info.os.load}</td>
<td>{info.process.uptimeHumanReadable}</td>
</tr>

Loading…
Cancel
Save