/* Urutau - CONCENTRACAO page: do a few sources generate most of the attacks? Pareto curve with a dimension selector (IP, ASN, country, path, domain), Gini coefficient, top 10 share and a donut of the top 5 against the rest. Data: /api/v1/concentration. Log data is hostile: textContent only. */ (function () { 'use strict'; var U = window.Urutau; if (!U || !U.registerPage) return; var el = U.el, t = U.t; var DIMS = ['ip', 'asn', 'country', 'path', 'domain']; U.registerI18n({ en: { 'an.co.title': 'Concentration', 'an.co.lead': 'The top 10 {dim} generate {pct} of the attacks', 'an.co.lead.body': 'Gini {gini} across {distinct} distinct values, {n} malicious events {range}.', 'an.co.lead.empty': 'No malicious events to measure', 'an.co.lead.empty.body': 'Nothing was recorded {range}.', 'an.co.dim': 'Group by', 'an.co.dim.ip': 'IP', 'an.co.dim.asn': 'ASN', 'an.co.dim.country': 'country', 'an.co.dim.path': 'path', 'an.co.dim.domain': 'domain', 'an.co.curve': 'Pareto curve', 'an.co.curve.hint': 'Share of all events held by the top N, ranked from the heaviest.', 'an.co.x': 'Rank', 'an.co.y': 'Cumulative share', 'an.co.gini': 'Gini', 'an.co.top10': 'Top 10', 'an.co.donut': 'Top 5 and the rest', 'an.co.donut.hint': 'How the five heaviest compare with everything else.', 'an.co.rest': 'others', 'an.co.list': 'Ranking', 'an.co.list.hint': 'Up to the 200 heaviest, with the cumulative share.', 'an.co.total': 'events', 'an.co.off': 'The event registry is off', 'an.co.off.d': 'This analysis reads the event registry, which is not enabled on this instance.', 'an.co.err': 'Could not load this analysis.', 'an.co.retry': 'Try again' }, 'pt-BR': { 'an.co.title': 'Concentração', 'an.co.lead': 'Os 10 maiores por {dim} geram {pct} dos ataques', 'an.co.lead.body': 'Gini {gini} em {distinct} valores distintos, {n} eventos maliciosos {range}.', 'an.co.lead.empty': 'Sem eventos maliciosos para medir', 'an.co.lead.empty.body': 'Nada foi registrado {range}.', 'an.co.dim': 'Agrupar por', 'an.co.dim.ip': 'IP', 'an.co.dim.asn': 'ASN', 'an.co.dim.country': 'país', 'an.co.dim.path': 'caminho', 'an.co.dim.domain': 'domínio', 'an.co.curve': 'Curva de Pareto', 'an.co.curve.hint': 'Parcela de todos os eventos nas N primeiras posições, do mais pesado ao mais leve.', 'an.co.x': 'Posição', 'an.co.y': 'Parcela acumulada', 'an.co.gini': 'Gini', 'an.co.top10': 'Top 10', 'an.co.donut': 'Top 5 e o resto', 'an.co.donut.hint': 'Como os cinco mais pesados se comparam com todo o resto.', 'an.co.rest': 'outros', 'an.co.list': 'Ranking', 'an.co.list.hint': 'Até os 200 mais pesados, com a parcela acumulada.', 'an.co.total': 'eventos', 'an.co.off': 'O registro de eventos está desligado', 'an.co.off.d': 'Esta análise lê o registro de eventos, que não está ativo nesta instância.', 'an.co.err': 'Não foi possível carregar esta análise.', 'an.co.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 s(tag, attrs, kids) { var n = el(tag, attrs); (kids || []).forEach(function (k) { n.appendChild(k); }); return n; } function itemLabel(dim, it) { if (dim === 'country') return it.key ? U.fmt.flag(it.key) + ' ' + U.fmt.country(it.key) : it.key; if (dim === 'asn' && it.label) return it.key + ' ' + it.label; return it.key; } // Pareto line: x is the rank, y the cumulative share (0..100) function pareto(items) { var W = 640, H = 280, L = 44, R = 12, T = 12, B = 34, w = W - L - R, h = H - T - B, n = items.length; var svg = s('svg', { viewBox: '0 0 ' + W + ' ' + H, class: 'an-svg', role: 'img', 'aria-label': t('an.co.curve'), preserveAspectRatio: 'xMidYMid meet' }); function X(i) { return L + (n <= 1 ? 0 : i / (n - 1)) * w; } function Y(v) { return T + (1 - v / 100) * h; } [0, 25, 50, 75, 100].forEach(function (v) { svg.appendChild(el('line', { class: 'an-grid', x1: L, x2: W - R, y1: Y(v), y2: Y(v) })); svg.appendChild(el('text', { class: 'an-tick', x: L - 6, y: Y(v) + 4, 'text-anchor': 'end' }, v + '%')); }); [0, 0.5, 1].forEach(function (f) { var i = Math.round(f * (n - 1)); svg.appendChild(el('text', { class: 'an-tick', x: X(i), y: H - 16, 'text-anchor': f === 0 ? 'start' : f === 1 ? 'end' : 'middle' }, String(i + 1))); }); svg.appendChild(el('text', { class: 'an-tick', x: L + w / 2, y: H - 2, 'text-anchor': 'middle' }, t('an.co.x'))); svg.appendChild(el('line', { class: 'an-guide', x1: L, x2: W - R, y1: Y(80), y2: Y(80) })); var pts = items.map(function (it, i) { return X(i).toFixed(1) + ',' + Y(it.cum_pct).toFixed(1); }); if (n > 0) { svg.appendChild(el('polygon', { class: 'an-fill', points: X(0).toFixed(1) + ',' + Y(0) + ' ' + pts.join(' ') + ' ' + X(n - 1).toFixed(1) + ',' + Y(0) })); svg.appendChild(el('polyline', { class: 'an-line', points: pts.join(' ') })); items.forEach(function (it, i) { if (n > 40 && i % Math.ceil(n / 40) && i !== n - 1) return; svg.appendChild(el('circle', { class: 'an-dot', cx: X(i), cy: Y(it.cum_pct), r: 3 }, el('title', null, '#' + (i + 1) + ' ' + it.key + ': ' + it.cum_pct + '%'))); }); } return el('div', { class: 'an-chart' }, svg); } var inst = null, dimSaved = 'ip'; function create(root, ctx) { var seq = 0, alive = true, data = null, c = ctx, dim = dimSaved; var grid = el('div', { class: 'grid-12 an-page' }); var lead = el('div', { class: 'span-12' }); var chips = el('div', { class: 'chip-group', role: 'group', 'aria-label': t('an.co.dim') }); var btns = {}; DIMS.forEach(function (d) { var b = el('button', { type: 'button', class: 'chip-btn', 'aria-pressed': d === dim ? 'true' : 'false' }, t('an.co.dim.' + d)); b.addEventListener('click', function () { if (dim === d) return; dim = dimSaved = d; data = null; DIMS.forEach(function (x) { btns[x].setAttribute('aria-pressed', x === d ? 'true' : 'false'); }); load(); }); btns[d] = b; chips.appendChild(b); }); var curve = U.ui.panel({ title: t('an.co.curve'), hint: t('an.co.curve.hint'), variant: 'chart', loading: true, actions: chips }); tip(curve, 'term.pareto'); var donut = U.ui.panel({ title: t('an.co.donut'), hint: t('an.co.donut.hint'), loading: true }); var list = U.ui.panel({ title: t('an.co.list'), hint: t('an.co.list.hint'), loading: true }); var kpis = el('div', { class: 'an-kpis' }); grid.appendChild(lead); grid.appendChild(curve); curve.classList.add('span-8'); grid.appendChild(donut); donut.classList.add('span-4'); grid.appendChild(list); list.classList.add('span-12'); root.appendChild(grid); function paint() { clear(lead); [curve, donut, list].forEach(function (p) { clear(p.body); }); if (!data) return; if (!data.enabled) { lead.appendChild(U.ui.empty(t('an.co.off'), t('an.co.off.d'))); return; } var d = data, range = U.rangeLabel({ inline: true }), dname = t('an.co.dim.' + d.dim); if (!d.total) { lead.appendChild(U.ui.lead(t('an.co.lead.empty'), t('an.co.lead.empty.body', { range: range }), 'neutral')); curve.body.appendChild(U.ui.empty(t('charts.empty'))); donut.body.appendChild(U.ui.empty(t('charts.empty'))); list.body.appendChild(U.ui.empty(t('charts.empty'))); return; } lead.appendChild(U.ui.lead(t('an.co.lead', { dim: dname, pct: U.fmt.pct(d.top10_pct / 100) }), t('an.co.lead.body', { gini: d.gini.toFixed(2), distinct: U.fmt.num(d.distinct), n: U.fmt.num(d.total), range: range }), 'neutral')); kpis = el('div', { class: 'an-kpis' }, el('div', { class: 'an-kpi' }, el('span', { class: 'an-kpi-v' }, d.gini.toFixed(2)), el('span', { class: 'an-kpi-l' }, t('an.co.gini'), ' ', (function () { var w = el('span'); U.help && U.help(w, 'term.gini'); return w; })())), el('div', { class: 'an-kpi' }, el('span', { class: 'an-kpi-v' }, U.fmt.pct(d.top10_pct / 100)), el('span', { class: 'an-kpi-l' }, t('an.co.top10')))); curve.body.appendChild(kpis); curve.body.appendChild(pareto(d.items)); var top5 = d.items.slice(0, 5), used = 0; var rows = top5.map(function (it) { used += it.n; return { key: it.key, label: itemLabel(d.dim, it), value: it.n }; }); if (d.total > used) rows.push({ key: '_rest', label: t('an.co.rest'), value: d.total - used }); donut.body.appendChild(U.charts.donut(rows, { title: t('an.co.donut'), centerLabel: t('an.co.total'), colors: ['--viz-1', '--viz-2', '--viz-3', '--viz-4', '--viz-5', '--viz-8'] })); var tbl = el('table', { class: 'an-table' }); tbl.appendChild(el('thead', null, el('tr', null, el('th', null, '#'), el('th', null, t('an.co.dim.' + d.dim)), el('th', { class: 'num' }, t('an.co.total')), el('th', { class: 'num' }, t('an.co.y'))))); var tb = el('tbody'); d.items.forEach(function (it, i) { var cell = el('td', { class: 'an-key mono' }); if (d.dim === 'ip') { var a = el('a', { href: '#/ip/' + encodeURIComponent(it.key) }, it.key); cell.appendChild(a); } else cell.textContent = itemLabel(d.dim, it); tb.appendChild(el('tr', null, el('td', { class: 'num' }, String(i + 1)), cell, el('td', { class: 'num' }, U.fmt.num(it.n)), el('td', { class: 'num' }, it.cum_pct.toFixed(1) + '%'))); }); tbl.appendChild(tb); list.body.appendChild(el('div', { class: 'an-scroll' }, tbl)); } function load() { var my = ++seq; var p = Object.assign({ dim: dim }, U.rangeParams()); if (c.host && c.host !== 'all') p.host = c.host; U.api('/api/v1/concentration', p).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.co.retry')); b.addEventListener('click', load); lead.appendChild(el('div', { class: 'state-error' }, el('strong', null, t('an.co.err')), b)); }); } load(); return { root: root, lang: ctx.lang, update: function (cx) { c = cx; load(); }, destroy: function () { alive = false; seq++; clear(root); } }; } U.registerPage({ id: 'concentration', group: 'analyze', titleKey: 'an.co.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; } } }); })();