/* Urutau - EVOLUCAO page: are we getting better or worse? Daily stacked area by attack family, weekday x hour heatmap, families that appeared or vanished and the change against the previous period. Data: /api/v1/evolution. Log data is hostile: every string goes through textContent (Urutau.el). */ (function () { 'use strict'; var U = window.Urutau; if (!U || !U.registerPage) return; var el = U.el, t = U.t; U.registerI18n({ en: { 'an.ev.title': 'Evolution', 'an.ev.lead.better': 'Getting better: {pct} fewer attacks', 'an.ev.lead.worse': 'Getting worse: {pct} more attacks', 'an.ev.lead.same': 'Holding steady against the previous period', 'an.ev.lead.none': 'No previous period to compare', 'an.ev.lead.body': '{n} malicious events {range}, against {prev} in the period before.', 'an.ev.area': 'Attacks per day, by family', 'an.ev.area.hint': 'The seven busiest families; the rest are grouped as "other".', 'an.ev.heat': 'When do they come? (UTC)', 'an.ev.heat.hint': 'Malicious events by weekday and hour of the day.', 'an.ev.new': 'Appeared', 'an.ev.new.hint': 'Families seen now that did not exist in the previous period.', 'an.ev.gone': 'Vanished', 'an.ev.gone.hint': 'Families of the previous period that did not come back.', 'an.ev.none': 'None', 'an.ev.nocmp': 'There is no previous period with events to compare against.', 'an.ev.other': 'other', 'an.ev.off': 'The event registry is off', 'an.ev.off.d': 'This analysis reads the event registry, which is not enabled on this instance.', 'an.ev.err': 'Could not load this analysis.', 'an.ev.retry': 'Try again' }, 'pt-BR': { 'an.ev.title': 'Evolução', 'an.ev.lead.better': 'Melhorando: {pct} menos ataques', 'an.ev.lead.worse': 'Piorando: {pct} mais ataques', 'an.ev.lead.same': 'Estável em relação ao período anterior', 'an.ev.lead.none': 'Sem período anterior para comparar', 'an.ev.lead.body': '{n} eventos maliciosos {range}, contra {prev} no período anterior.', 'an.ev.area': 'Ataques por dia, por família', 'an.ev.area.hint': 'As sete famílias mais ativas; as demais ficam em "outras".', 'an.ev.heat': 'Quando eles vêm? (UTC)', 'an.ev.heat.hint': 'Eventos maliciosos por dia da semana e hora do dia.', 'an.ev.new': 'Surgiram', 'an.ev.new.hint': 'Famílias vistas agora que não existiam no período anterior.', 'an.ev.gone': 'Sumiram', 'an.ev.gone.hint': 'Famílias do período anterior que não voltaram.', 'an.ev.none': 'Nenhuma', 'an.ev.nocmp': 'Não há período anterior com eventos para comparar.', 'an.ev.other': 'outras', 'an.ev.off': 'O registro de eventos está desligado', 'an.ev.off.d': 'Esta análise lê o registro de eventos, que não está ativo nesta instância.', 'an.ev.err': 'Não foi possível carregar esta análise.', 'an.ev.retry': 'Tentar de novo' } }); function tip(p, key) { var h2 = p.querySelector('.panel-h h2'); if (h2 && U.help) U.help(h2, key); return p; } function clear(n) { while (n.firstChild) n.removeChild(n.firstChild); } function params(ctx) { var p = Object.assign({}, U.rangeParams()); if (ctx && ctx.host && ctx.host !== 'all') p.host = ctx.host; return p; } function famList(rows, tone) { if (!rows || !rows.length) return el('p', { class: 'an-muted' }, t('an.ev.none')); var ul = el('ul', { class: 'an-list' }); rows.slice(0, 10).forEach(function (r) { ul.appendChild(el('li', null, el('span', { class: 'an-k mono' }, r.fam), el('span', { class: 'an-n an-' + tone }, U.fmt.num(r.n)))); }); return ul; } var inst = null; function create(root, ctx) { var seq = 0, alive = true, data = null, lang = ctx.lang, c = ctx; var grid = el('div', { class: 'grid-12 an-page' }); var lead = el('div', { class: 'span-12' }); var area = U.ui.panel({ title: t('an.ev.area'), hint: t('an.ev.area.hint'), variant: 'chart', loading: true }); var heat = U.ui.panel({ title: t('an.ev.heat'), hint: t('an.ev.heat.hint'), loading: true }); var nw = U.ui.panel({ title: t('an.ev.new'), hint: t('an.ev.new.hint'), loading: true }); var gone = U.ui.panel({ title: t('an.ev.gone'), hint: t('an.ev.gone.hint'), loading: true }); tip(area, 'metric.attacks_per_hour_24h'); [lead, area, heat, nw, gone].forEach(function (n, i) { grid.appendChild(n); if (i) n.classList.add(i === 1 ? 'span-12' : i === 2 ? 'span-12' : 'span-6'); }); root.appendChild(grid); function paint() { clear(lead); [area, heat, nw, gone].forEach(function (p) { clear(p.body); }); if (!data.enabled) { lead.appendChild(U.ui.empty(t('an.ev.off'), t('an.ev.off.d'))); return; } var d = data, pct = d.delta_pct, title, tone = 'neutral'; if (pct === null || pct === undefined) title = t('an.ev.lead.none'); else if (Math.abs(pct) < 5) title = t('an.ev.lead.same'); else if (pct < 0) { title = t('an.ev.lead.better', { pct: Math.abs(Math.round(pct)) + '%' }); tone = 'ok'; } else { title = t('an.ev.lead.worse', { pct: Math.round(pct) + '%' }); tone = 'danger'; } lead.appendChild(U.ui.lead(title, t('an.ev.lead.body', { n: U.fmt.num(d.total), range: U.rangeLabel({ inline: true }), prev: U.fmt.num(d.prev) }), tone)); // area: top 7 families + other var tot = {}; d.days.forEach(function (x) { Object.keys(x.by_fam || {}).forEach(function (f) { tot[f] = (tot[f] || 0) + x.by_fam[f]; }); }); var top = Object.keys(tot).filter(function (f) { return f !== 'other'; }).sort(function (a, b) { return tot[b] - tot[a]; }).slice(0, 7); var series = top.map(function (f) { return { key: f, label: f, values: d.days.map(function (x) { return (x.by_fam || {})[f] || 0; }) }; }); var rest = d.days.map(function (x) { var s = 0; Object.keys(x.by_fam || {}).forEach(function (f) { if (top.indexOf(f) < 0) s += x.by_fam[f]; }); return s; }); if (rest.some(function (v) { return v > 0; })) series.push({ key: 'other', label: t('an.ev.other'), values: rest }); var start = d.days.length ? Date.parse(d.days[0].d + 'T00:00:00Z') / 1000 : 0; if (series.length && d.days.length >= 3) area.body.appendChild(U.charts.stackedArea(series, 86400, { start: start, title: t('an.ev.area') })); else area.body.appendChild(U.ui.empty(t('charts.empty'))); // heatmap, Monday first var order = [1, 2, 3, 4, 5, 6, 0], fmt = new Intl.DateTimeFormat(c.lang || undefined, { weekday: 'short', timeZone: 'UTC' }); var labels = order.map(function (w) { return fmt.format(new Date(Date.UTC(2023, 0, 1 + w))); }); var hours = []; for (var h = 0; h < 24; h++) hours.push(h % 3 === 0 ? String(h) : ''); heat.body.appendChild(U.charts.heatmap(order.map(function (w) { return d.hour_dow[w] || []; }), hours, labels, { title: t('an.ev.heat') })); if (pct === null || pct === undefined) { nw.body.appendChild(el('p', { class: 'an-muted' }, t('an.ev.nocmp'))); gone.body.appendChild(el('p', { class: 'an-muted' }, t('an.ev.nocmp'))); } else { nw.body.appendChild(famList(d.new_families, 'bad')); gone.body.appendChild(famList(d.gone_families, 'good')); } } function load() { var my = ++seq; U.api('/api/v1/evolution', params(c)).then(function (r) { if (!alive || my !== seq) return; data = r; paint(); }, function () { if (!alive || my !== seq || data) return; clear(lead); var b = el('button', { type: 'button', class: 'btn' }, t('an.ev.retry')); b.addEventListener('click', load); lead.appendChild(el('div', { class: 'state-error' }, el('strong', null, t('an.ev.err')), b)); }); } load(); return { root: root, lang: lang, update: function (cx) { c = cx; load(); }, destroy: function () { alive = false; seq++; clear(root); } }; } U.registerPage({ id: 'evolution', group: 'analyze', titleKey: 'an.ev.title', icon: '', mount: function (el, ctx) { if (inst) inst.destroy(); inst = create(el, ctx || {}); }, update: function (ctx) { ctx = ctx || {}; if (inst && ctx.lang !== inst.lang) { var r = inst.root; inst.destroy(); inst = create(r, ctx); } else if (inst) inst.update(ctx); }, unmount: function () { if (inst) { inst.destroy(); inst = null; } } }); })();