forked from laysense/laysite
Expand admin system information and license notices
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
'use strict';
|
||||
|
||||
jQuery(function ($) {
|
||||
const $metrics = $('[data-about-metrics]');
|
||||
if (!$metrics.length) return;
|
||||
|
||||
const $cpu = $metrics.find('[data-about-cpu]');
|
||||
const $memory = $metrics.find('[data-about-memory]');
|
||||
const percent = new Intl.NumberFormat('zh-CN', {style: 'percent', maximumFractionDigits: 1});
|
||||
const {cpuTotal, cpuIdle} = $metrics[0].dataset;
|
||||
let previous = cpuTotal === '' ? null : {total: Number(cpuTotal), idle: Number(cpuIdle)};
|
||||
let sessionExpired = false;
|
||||
if (previous) $cpu.text('采样中…');
|
||||
|
||||
function poll() {
|
||||
if (document.hidden) {
|
||||
previous = null;
|
||||
window.setTimeout(poll, 5000);
|
||||
return;
|
||||
}
|
||||
|
||||
$.ajax({url: '/admin/about/metrics', dataType: 'json', timeout: 4000})
|
||||
.done(({cpu, memory}) => {
|
||||
if (!cpu || !previous || cpu.total <= previous.total) {
|
||||
$cpu.text(cpu ? '采样中…' : '暂不可用');
|
||||
} else {
|
||||
const total = cpu.total - previous.total;
|
||||
const idle = cpu.idle - previous.idle;
|
||||
$cpu.text(percent.format(Math.max(0, Math.min(1, 1 - idle / total))));
|
||||
}
|
||||
previous = cpu;
|
||||
$memory.text(memory ?? '暂不可用');
|
||||
})
|
||||
.fail(({status}) => {
|
||||
previous = null;
|
||||
sessionExpired = status === 401;
|
||||
const message = sessionExpired ? '登录已过期' : '暂不可用';
|
||||
$cpu.text(message);
|
||||
$memory.text(message);
|
||||
})
|
||||
.always(() => {
|
||||
if (!sessionExpired) window.setTimeout(poll, 5000);
|
||||
});
|
||||
}
|
||||
|
||||
window.setTimeout(poll, 1000);
|
||||
});
|
||||
Reference in New Issue
Block a user