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", "nodejs": "nodejs",
"online": "online", "online": "online",
"git": "git", "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", "load": "system load",
"cpu-usage": "cpu usage", "cpu-usage": "cpu usage",
"uptime": "uptime", "uptime": "uptime",

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

@ -17,7 +17,8 @@
<td>[[admin/development/info:online]]</td> <td>[[admin/development/info:online]]</td>
<td>[[admin/development/info:git]]</td> <td>[[admin/development/info:git]]</td>
<td>[[admin/development/info:cpu-usage]]</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:load]]</td>
<td>[[admin/development/info:uptime]]</td> <td>[[admin/development/info:uptime]]</td>
</tr> </tr>
@ -38,8 +39,14 @@
<span title="[[admin/development/info:sockets]]">{info.stats.socketCount}</span> <span title="[[admin/development/info:sockets]]">{info.stats.socketCount}</span>
</td> </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.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.cpuUsage}%</td>
<td>{info.process.memoryUsage.humanReadable} mb / {info.os.freemem} mb</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.os.load}</td>
<td>{info.process.uptimeHumanReadable}</td> <td>{info.process.uptimeHumanReadable}</td>
</tr> </tr>

Loading…
Cancel
Save