sozday-veb-prilozhenie-cyber/js/reports.js

84 lines
4.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
/* CyberReport: отчёт за период — печать/сохранение в PDF. */
CR.Reports = {};
CR.Reports.period = () => CR.state.report;
function esc(s) { return String(s == null ? "" : s); }
CR.Reports.render = function (view) {
const s = CR.state;
const all = s.online ? s.attacks : [];
const allTotal = all.length;
const inWorkAll = all.filter(a => a.status === "progress").length;
const closedAll = allTotal - inWorkAll;
const fromInput = s.report.from;
const toInput = s.report.to;
let list = all.slice();
if (fromInput) list = list.filter(a => a.date >= fromInput);
if (toInput) list = list.filter(a => a.date <= toInput);
list.sort((a, b) => a.date.localeCompare(b.date) || (a.title || "").localeCompare(b.title || ""));
const listTotal = list.length;
const inWork = list.filter(a => a.status === "progress").length;
const closed = list.filter(a => a.status === "closed").length;
const fresh = list.filter(a => a.status === "new").length;
const byType = CR.ATTACK_TYPES.map(t => ({ t, n: list.filter(a => a.type === t.id).length })).filter(x => x.n);
const periodText = CR.fmtDate(fromInput) + " — " + CR.fmtDate(toInput);
const rows = list.map(a =>
"<tr>" +
"<td>" + CR.fmtDate(a.date) + "</td>" +
"<td>" + esc(a.title) + "</td>" +
"<td>" + esc(CR.typeLabel(a.type)) + "</td>" +
"<td>" + esc(CR.statusLabel(a.status)) + "</td>" +
"<td>" + esc(a.description) + "</td>" +
"<td>" + esc(a.measures) + "</td>" +
"</tr>"
).join("") || '<tr><td colspan="6" style="color:var(--kt-ai-fg-muted)">Нет атак за выбранный период</td></tr>';
const byTypeRows = byType.map(x => "<tr><td>" + esc(x.t.label) + "</td><td>" + x.n + "</td></tr>").join("") ||
'<tr><td colspan="2" style="color:var(--kt-ai-fg-muted)">Нет данных</td></tr>';
view.innerHTML =
'<div class="cr-report-tools no-print" style="display:flex;align-items:center;gap:var(--kt-ai-space-3);flex-wrap:wrap">' +
'<div class="cr-periodfield"><span>Период</span><input class="kt-ai-input" type="date" id="r-from" value="' + fromInput + '"><span>—</span><input class="kt-ai-input" type="date" id="r-to" value="' + toInput + '"></div>' +
'<button type="button" class="kt-ai-btn" id="r-preset-month">Последние 30 дней</button>' +
'<button type="button" class="kt-ai-btn" id="r-preset-all">Весь период</button>' +
'<div class="spacer" style="margin-left:auto"></div>' +
'<button type="button" class="kt-ai-btn" data-variant="ghost" id="r-attack">' + CR.icon("layers") + " К атакам</button>" +
'<button type="button" class="kt-ai-btn" data-variant="primary" id="r-print">' + CR.icon("download") + " Скачать отчёт (PDF)</button>" +
"</div>" +
'<div class="cr-report" id="report-doc">' +
"<h1>Отчёт по кибератакам</h1>" +
'<p>Период: <strong>' + periodText + "</strong> · сформирован " + CR.fmtDateTime(CR.todayIso()) + "</p>" +
'<h2>Общее количество</h2>' +
"<table><thead><tr><th>Показатель</th><th>За период</th><th>Всего</th></tr></thead><tbody>" +
"<tr><td>Всего атак</td><td>" + listTotal + "</td><td>" + allTotal + "</td></tr>" +
"<tr><td>В работе</td><td>" + inWork + "</td><td>" + inWorkAll + "</td></tr>" +
"<tr><td>Закрыто</td><td>" + closed + "</td><td>" + closedAll + "</td></tr>" +
(fresh ? "<tr><td>Новых за период</td><td>" + fresh + "</td><td>—</td></tr>" : "") +
"</tbody></table>" +
"<h2>По типам</h2>" +
"<table><thead><tr><th>Тип атаки</th><th>За период</th></tr></thead><tbody>" + byTypeRows + "</tbody></table>" +
"<h2>Список атак</h2>" +
'<table><thead><tr><th>Дата</th><th>Название</th><th>Тип</th><th>Статус</th><th>Описание</th><th>Принятые меры</th></tr></thead><tbody>' + rows + "</tbody></table>" +
"</div>";
const setPeriod = (f, t) => {
CR.state.report.from = f; CR.state.report.to = t;
CR.renderView();
};
document.getElementById("r-from").addEventListener("change", (e) => setPeriod(e.target.value, CR.state.report.to));
document.getElementById("r-to").addEventListener("change", (e) => setPeriod(CR.state.report.from, e.target.value));
document.getElementById("r-preset-month").addEventListener("click", () => setPeriod(CR.daysAgoIso(30), CR.todayIso()));
document.getElementById("r-preset-all").addEventListener("click", () => setPeriod("", CR.todayIso()));
document.getElementById("r-attack").addEventListener("click", () => CR.navigate("attacks"));
document.getElementById("r-print").addEventListener("click", () => {
CR.toast("Откроется диалог печати — выберите «Сохранить как PDF»");
setTimeout(() => window.print(), 250);
});
};