/**
* Live progress for the "Cache warming" row of the VITEC SSR module.
* Polls the vitec_ssr_health ajax route (GET /__health of the node):
* every 2 s while a warm run is active, every 15 s otherwise so runs
* started from the CLI or the context menu show up without a reload.
* Also refreshes the "Cache: N pages / M MB" line.
*/
import AjaxRequest from '@typo3/core/ajax/ajax-request.js';
const cell = document.getElementById('ssr-warm-status');
if (cell) {
const wrap = document.getElementById('ssr-warm-progress');
const bar = document.getElementById('ssr-warm-bar');
const text = document.getElementById('ssr-warm-text');
const cacheLine = document.getElementById('ssr-cache-line');
let timer = null;
const num = (v) => (typeof v === 'number' ? v : 0);
const render = (h) => {
const w = h.warming || {};
const queued = num(w.queued);
const handled = num(w.done) + num(w.skipped) + num(w.failed);
const pct = queued > 0 ? Math.min(100, Math.round((handled / queued) * 100)) : 0;
const failed = num(w.failed)
? '' + num(w.failed) + ' failed'
: '0 failed';
if (w.running) {
wrap.style.display = '';
bar.style.width = pct + '%';
bar.textContent = pct + ' %';
text.innerHTML = 'running '
+ handled + ' of ' + queued + ' pages ('
+ num(w.done) + ' rendered, ' + num(w.skipped) + ' still fresh, ' + failed + ')'
+ '
Live - updates every 2 seconds.';
} else if (w.startedAt) {
wrap.style.display = 'none';
const finished = w.finishedAt
? ' finished ' + new Date(w.finishedAt).toLocaleTimeString() + ''
: '';
text.innerHTML = 'Last run: ' + num(w.done) + ' of ' + queued + ' pages rendered ('
+ num(w.skipped) + ' still fresh, ' + failed + ')'
+ (w.aborted ? ' aborted by restart' : '')
+ finished;
} else {
wrap.style.display = 'none';
text.innerHTML = 'no run since the server started';
}
if (cacheLine && typeof h.cachedPages === 'number') {
cacheLine.textContent = 'Cache: ' + h.cachedPages + ' pages / ' + num(h.cacheMB) + ' MB';
}
return !!w.running;
};
const schedule = (ms) => {
clearTimeout(timer);
timer = setTimeout(tick, ms);
};
const tick = () => {
new AjaxRequest(TYPO3.settings.ajaxUrls.vitec_ssr_health).get()
.then((response) => response.resolve())
.then((data) => {
const running = data && data.health ? render(data.health) : false;
schedule(running ? 2000 : 15000);
})
.catch(() => schedule(15000));
};
schedule(cell.dataset.running === '1' ? 1000 : 15000);
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
clearTimeout(timer);
} else {
tick();
}
});
}