Files
urutau/internal/server/web/reports.js
T
Lucas RayanandClaude Opus 5.5 9bdfdb491b feat: report preview page, sharp report logo, time to ban by blocker
The Reports page now previews the HTML report itself (same document as the
download and the print), with only Download and Print. The report logo is
the repository logo at 800 px. Survival splits fail2ban and the Urutau
second firewall.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 00:06:29 -03:00

131 lines
5.3 KiB
JavaScript

/* Urutau - REPORTS page: a live preview of the HTML report (the same document
that is downloaded and printed), following the period and host chosen in the
header, plus the report language. Two actions only: download and print.
The report is served by GET /api/v1/report?format=html (self-contained, no
script); the preview is a same-origin iframe of it with inline=1. */
(function () {
'use strict';
var ICON = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M7 3h7l5 5v13H7z"/><path d="M14 3v5h5M10 13h6M10 17h6"/></svg>';
var LANG_KEY = 'urutau.report.lang';
function boot() {
var U = window.Urutau;
if (!U || !U.registerPage) return false;
U.registerI18n({
en: {
'rpt.title': 'Reports',
'rpt.dl': 'Download',
'rpt.print': 'Print',
'rpt.lang': 'Report language',
'rpt.scope': 'Period and server follow the selection at the top of the page.',
'rpt.preview': 'Report preview',
'rpt.loading': 'Building the report...',
'rpt.err': 'Could not build the report.',
'rpt.retry': 'Try again'
},
'pt-BR': {
'rpt.title': 'Relatórios',
'rpt.dl': 'Baixar',
'rpt.print': 'Imprimir',
'rpt.lang': 'Idioma do relatório',
'rpt.scope': 'O período e o servidor seguem a seleção no topo da página.',
'rpt.preview': 'Prévia do relatório',
'rpt.loading': 'Montando o relatório...',
'rpt.err': 'Não foi possível montar o relatório.',
'rpt.retry': 'Tentar de novo'
}
});
var t = U.t, el = U.el;
var ctx0 = null, root = null, frame = null, status = null, lang = '';
try { lang = localStorage.getItem(LANG_KEY) || ''; } catch (e) { lang = ''; }
function reportLang() {
if (lang === 'en' || lang === 'pt-BR') return lang;
return ctx0 && ctx0.lang === 'en' ? 'en' : 'pt-BR';
}
function url(inline) {
var p = U.rangeParams();
if (ctx0 && ctx0.host && ctx0.host !== 'all') p.host = ctx0.host;
p.format = 'html';
p.lang = reportLang();
if (inline) p.inline = '1';
return '/api/v1/report?' + new URLSearchParams(p).toString();
}
function showStatus(key, retry) {
while (status.firstChild) status.removeChild(status.firstChild);
status.hidden = !key;
if (!key) return;
status.appendChild(el('span', null, t(key)));
if (retry) {
var b = el('button', { type: 'button', class: 'btn' }, t('rpt.retry'));
b.addEventListener('click', refresh);
status.appendChild(b);
}
}
function refresh() {
if (!frame) return;
showStatus('rpt.loading');
frame.src = url(true);
}
function build(c) {
ctx0 = c;
root = el('div', { class: 'rpt' });
var bar = el('div', { class: 'rpt-bar' });
var sel = el('select', { class: 'rpt-sel', 'aria-label': t('rpt.lang') });
[['pt-BR', 'Português'], ['en', 'English']].forEach(function (o) {
var opt = el('option', { value: o[0] }, o[1]);
if (reportLang() === o[0]) opt.selected = true;
sel.appendChild(opt);
});
sel.addEventListener('change', function () {
lang = sel.value;
try { localStorage.setItem(LANG_KEY, lang); } catch (e) { /* private mode */ }
refresh();
});
bar.appendChild(el('label', { class: 'rpt-opt' }, el('span', null, t('rpt.lang')), sel));
bar.appendChild(el('p', { class: 'rpt-hint' }, t('rpt.scope')));
var actions = el('div', { class: 'rpt-actions' });
var dl = el('button', { type: 'button', class: 'btn' }, t('rpt.dl'));
dl.addEventListener('click', function () {
var a = document.createElement('a');
a.href = url(false); a.download = '';
document.body.appendChild(a); a.click(); document.body.removeChild(a);
});
var pr = el('button', { type: 'button', class: 'btn btn-primary' }, t('rpt.print'));
pr.addEventListener('click', function () {
try { frame.contentWindow.focus(); frame.contentWindow.print(); }
catch (e) { window.open(url(true), '_blank', 'noopener'); }
});
actions.appendChild(dl); actions.appendChild(pr);
bar.appendChild(actions);
status = el('div', { class: 'rpt-status', role: 'status' });
frame = el('iframe', { class: 'rpt-frame', title: t('rpt.preview') });
frame.addEventListener('load', function () {
var ok = false;
try { ok = !!(frame.contentDocument && frame.contentDocument.querySelector('h1')); } catch (e) { ok = false; }
showStatus(ok ? '' : 'rpt.err', !ok);
});
root.appendChild(bar);
root.appendChild(status);
root.appendChild(el('div', { class: 'rpt-sheetwrap' }, frame));
return root;
}
U.registerPage({
id: 'reports', group: 'reports', icon: ICON, titleKey: 'rpt.title',
mount: function (host, c) { host.appendChild(build(c)); refresh(); },
update: function (c) { ctx0 = c; refresh(); },
unmount: function () { if (root && root.parentNode) root.parentNode.removeChild(root); root = frame = status = null; }
});
return true;
}
if (!boot()) {
var tries = 0, iv = setInterval(function () { if (boot() || ++tries > 50) clearInterval(iv); }, 100);
}
})();