forked from laysense/laysite
Address system information and license review suggestions
This commit is contained in:
@@ -7,22 +7,26 @@ jQuery(function ($) {
|
||||
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 previous = null;
|
||||
let timer = null;
|
||||
let pending = null;
|
||||
let sessionExpired = false;
|
||||
if (previous) $cpu.text('采样中…');
|
||||
|
||||
function active() {
|
||||
return $metrics[0].open && !document.hidden && !sessionExpired;
|
||||
}
|
||||
|
||||
function poll() {
|
||||
if (document.hidden) {
|
||||
previous = null;
|
||||
window.setTimeout(poll, 5000);
|
||||
return;
|
||||
}
|
||||
timer = null;
|
||||
if (!active()) return;
|
||||
|
||||
$.ajax({url: '/admin/about/metrics', dataType: 'json', timeout: 4000})
|
||||
let delay = 5000;
|
||||
pending = $.ajax({url: '/admin/about/metrics', dataType: 'json', timeout: 4000});
|
||||
pending
|
||||
.done(({cpu, memory}) => {
|
||||
if (!cpu || !previous || cpu.total <= previous.total) {
|
||||
$cpu.text(cpu ? '采样中…' : '暂不可用');
|
||||
if (cpu) delay = 1000;
|
||||
} else {
|
||||
const total = cpu.total - previous.total;
|
||||
const idle = cpu.idle - previous.idle;
|
||||
@@ -31,7 +35,8 @@ jQuery(function ($) {
|
||||
previous = cpu;
|
||||
$memory.text(memory ?? '暂不可用');
|
||||
})
|
||||
.fail(({status}) => {
|
||||
.fail(({status}, reason) => {
|
||||
if (reason === 'abort') return;
|
||||
previous = null;
|
||||
sessionExpired = status === 401;
|
||||
const message = sessionExpired ? '登录已过期' : '暂不可用';
|
||||
@@ -39,9 +44,26 @@ jQuery(function ($) {
|
||||
$memory.text(message);
|
||||
})
|
||||
.always(() => {
|
||||
if (!sessionExpired) window.setTimeout(poll, 5000);
|
||||
pending = null;
|
||||
if (active()) timer = window.setTimeout(poll, delay);
|
||||
});
|
||||
}
|
||||
|
||||
window.setTimeout(poll, 1000);
|
||||
function sync() {
|
||||
if (active()) {
|
||||
if (timer === null && pending === null) {
|
||||
$cpu.text('采样中…');
|
||||
poll();
|
||||
}
|
||||
return;
|
||||
}
|
||||
window.clearTimeout(timer);
|
||||
timer = null;
|
||||
previous = null;
|
||||
pending?.abort();
|
||||
}
|
||||
|
||||
$metrics.on('toggle', sync);
|
||||
$(document).on('visibilitychange', sync);
|
||||
sync();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user