/* Urutau - INTEGRITY page ("Is the data complete?"): volume ingested per host and hour (holes show as empty cells), state of every source and peer, share of malformed lines, event log and geolocation base. Log data is hostile: every dynamic string goes through textContent (the shell's el/h helpers). */ (function () { 'use strict'; var STATES = { following: 1, waiting: 1, backfill: 1, error: 1, degraded: 1, disabled: 1 }; var ICON = ''; function boot() { var U = window.Urutau; if (!U || !U.registerPage) return false; U.registerI18n({ en: { 'ig.title': 'Integrity', 'ig.lead.ok': 'The data is complete', 'ig.lead.okBody': 'No gaps of {n} hours or more, every source answers and few lines were rejected.', 'ig.lead.bad.one': '{n} hole in the data', 'ig.lead.bad.other': '{n} holes in the data', 'ig.lead.badBody': 'Silences of two hours or more in sources that normally report, offline peers or sources in error. Check the panels below.', 'ig.lead.loading': 'Checking the data...', 'ig.stat.hosts': 'Hosts reporting', 'ig.stat.gaps': 'Gaps found', 'ig.stat.parse': 'Invalid lines', 'ig.stat.log': 'Event log', 'ig.stat.logSub': '{days} days, {size}', 'ig.stat.logOff': 'off', 'ig.heat.title': 'Events ingested per hour', 'ig.heat.hint': 'One row per host, one column per hour. Empty cells are hours with no event at all.', 'ig.heat.empty': 'No events in this period.', 'ig.gaps.title': 'Gaps', 'ig.gaps.none': 'No silence of two hours or more.', 'ig.gaps.line': '{host}: {from} to {to} ({h} h)', 'ig.src.title': 'Sources', 'ig.src.host': 'Host', 'ig.src.name': 'Source', 'ig.src.state': 'State', 'ig.src.lines': 'Lines', 'ig.src.bad': 'Invalid', 'ig.src.last': 'Last event', 'ig.src.none': 'No sources reported.', 'ig.state.following': 'following', 'ig.state.waiting': 'waiting', 'ig.state.backfill': 'catching up', 'ig.state.error': 'error', 'ig.state.degraded': 'degraded', 'ig.state.disabled': 'disabled', 'ig.peers.title': 'Peers', 'ig.peers.none': 'No peers configured.', 'ig.peers.online': 'online', 'ig.peers.offline': 'offline', 'ig.peers.live': 'live', 'ig.peers.sync': 'synced {t}', 'ig.peers.never': 'never synced', 'ig.log.title': 'Event log and geolocation', 'ig.log.off': 'The event log is off: periods, sessions and replay have no data.', 'ig.log.days': 'Days kept', 'ig.log.size': 'Size on disk', 'ig.log.dropped': 'Events dropped', 'ig.geo.state': 'Geolocation base', 'ig.geo.city': 'City base build', 'ig.geo.asn': 'ASN base build', 'ig.geo.unknown': 'unknown', 'ig.lag': 'Delay', 'ig.host.lagLine': '{host}: last event {t}' }, 'pt-BR': { 'ig.title': 'Integridade', 'ig.lead.ok': 'Os dados estão completos', 'ig.lead.okBody': 'Nenhum buraco de {n} horas ou mais, todas as fontes respondem e poucas linhas foram rejeitadas.', 'ig.lead.bad.one': '{n} buraco nos dados', 'ig.lead.bad.other': '{n} buracos nos dados', 'ig.lead.badBody': 'Silêncios de duas horas ou mais em fontes que costumam registrar, peers fora do ar ou fontes com erro. Veja os painéis abaixo.', 'ig.lead.loading': 'Conferindo os dados...', 'ig.stat.hosts': 'Hosts reportando', 'ig.stat.gaps': 'Buracos encontrados', 'ig.stat.parse': 'Linhas inválidas', 'ig.stat.log': 'Registro de eventos', 'ig.stat.logSub': '{days} dias, {size}', 'ig.stat.logOff': 'desligado', 'ig.heat.title': 'Eventos ingeridos por hora', 'ig.heat.hint': 'Uma linha por host, uma coluna por hora. Células vazias são horas sem nenhum evento.', 'ig.heat.empty': 'Nenhum evento neste período.', 'ig.gaps.title': 'Buracos', 'ig.gaps.none': 'Nenhum silêncio de duas horas ou mais.', 'ig.gaps.line': '{host}: de {from} a {to} ({h} h)', 'ig.src.title': 'Fontes', 'ig.src.host': 'Host', 'ig.src.name': 'Fonte', 'ig.src.state': 'Estado', 'ig.src.lines': 'Linhas', 'ig.src.bad': 'Inválidas', 'ig.src.last': 'Último evento', 'ig.src.none': 'Nenhuma fonte informada.', 'ig.state.following': 'acompanhando', 'ig.state.waiting': 'aguardando', 'ig.state.backfill': 'recuperando', 'ig.state.error': 'erro', 'ig.state.degraded': 'degradada', 'ig.state.disabled': 'desativada', 'ig.peers.title': 'Peers', 'ig.peers.none': 'Nenhum peer configurado.', 'ig.peers.online': 'no ar', 'ig.peers.offline': 'fora do ar', 'ig.peers.live': 'ao vivo', 'ig.peers.sync': 'sincronizado {t}', 'ig.peers.never': 'nunca sincronizou', 'ig.log.title': 'Registro de eventos e geolocalização', 'ig.log.off': 'O registro de eventos está desligado: períodos, sessões e replay ficam sem dados.', 'ig.log.days': 'Dias guardados', 'ig.log.size': 'Tamanho em disco', 'ig.log.dropped': 'Eventos descartados', 'ig.geo.state': 'Base de geolocalização', 'ig.geo.city': 'Build da base de cidades', 'ig.geo.asn': 'Build da base de ASN', 'ig.geo.unknown': 'desconhecida', 'ig.lag': 'Atraso', 'ig.host.lagLine': '{host}: último evento {t}' } }); var t = U.t, nf = U.fmt.num; var h = function (tag, cls) { return U.el.apply(null, [tag, cls ? { class: cls } : null].concat([].slice.call(arguments, 2))); }; function when(ts) { return ts ? U.fmt.date(ts) : '-'; } var root = null, box = null, ctx0 = null, seq = 0, alive = false, data = null, timer = null; function params(c) { var p = U.rangeParams(); if (c && c.host && c.host !== 'all') p.host = c.host; return p; } function load(c) { var my = ++seq; U.api('/api/v1/integrity', params(c || ctx0)).then(function (d) { if (!alive || my !== seq) return; data = d; paint(); }).catch(function () { if (!alive || my !== seq || data) return; while (box.firstChild) box.removeChild(box.firstChild); box.appendChild(U.ui.empty(t('error.load'))); }); } function clear(n) { while (n.firstChild) n.removeChild(n.firstChild); } function hourLabel(hn) { var d = new Date(hn * 3600000); return ('0' + d.getDate()).slice(-2) + '/' + ('0' + d.getHours()).slice(-2); } function table(headers, rows) { var tb = h('table', 'ig-table'); var tr = h('tr'); headers.forEach(function (x) { tr.appendChild(h('th', null, x)); }); tb.appendChild(h('thead', null, tr)); var body = h('tbody'); rows.forEach(function (r) { var row = h('tr'); r.forEach(function (c) { row.appendChild(h('td', null, c)); }); body.appendChild(row); }); tb.appendChild(body); return h('div', 'ig-scroll', tb); } function paint() { if (!data) return; clear(box); var hosts = data.hosts || [], peers = data.peers || []; var gaps = 0, lines = 0, bad = 0, errors = 0, onlineN = 0; hosts.forEach(function (x) { gaps += (x.gaps || []).length; if (x.online) onlineN++; (x.sources || []).forEach(function (s) { lines += s.lines; bad += s.malformed; if (s.state === 'error' || s.state === 'degraded') errors++; }); }); var offline = peers.filter(function (p) { return !p.online; }).length; var rate = lines + bad > 0 ? bad / (lines + bad) : 0; var holes = gaps + offline + errors; var ok = holes === 0 && rate < 0.01; var title = ok ? t('ig.lead.ok') : U.pl('ig.lead.bad', holes || 1); box.appendChild(U.ui.lead(title, ok ? t('ig.lead.okBody', { n: 2 }) : t('ig.lead.badBody'), ok ? 'ok' : 'danger')); var stats = h('div', 'grid-12 ig-stats'); function sp(node) { node.classList.add('span-3'); stats.appendChild(node); } sp(U.ui.stat(t('ig.stat.hosts'), nf(onlineN) + ' / ' + nf(hosts.length), null, onlineN === hosts.length ? 'ok' : 'danger')); sp(U.ui.stat(t('ig.stat.gaps'), nf(gaps), null, gaps ? 'danger' : 'ok', null, 'term.integrity_gap')); sp(U.ui.stat(t('ig.stat.parse'), (rate * 100).toFixed(rate < 0.1 ? 2 : 1) + '%', nf(bad) + ' / ' + nf(lines + bad), rate >= 0.01 ? 'danger' : 'ok')); var lg = data.eventlog || {}; sp(U.ui.stat(t('ig.stat.log'), lg.enabled ? nf(lg.days) : t('ig.stat.logOff'), lg.enabled ? t('ig.stat.logSub', { days: nf(lg.days), size: U.fmt.bytes(lg.bytes) }) : null, lg.enabled ? 'ok' : 'neutral')); box.appendChild(stats); // heatmap var hp = U.ui.panel({ title: t('ig.heat.title'), hint: t('ig.heat.hint'), variant: 'chart' }); var n = hosts.length ? (hosts[0].hourly || []).length : 0; if (!n || !hosts.some(function (x) { return (x.hourly || []).some(function (v) { return v > 0; }); })) { hp.body.appendChild(U.ui.empty(t('ig.heat.empty'))); } else { var xl = []; for (var i = 0; i < n; i++) xl.push(hourLabel(data.from_hour + i)); hp.body.appendChild(h('div', 'ig-heatbox', U.charts.heatmap(hosts.map(function (x) { return x.hourly; }), xl, hosts.map(function (x) { return x.name; }), { title: t('ig.heat.title') }))); } box.appendChild(hp); var grid = h('div', 'grid-12'); // gaps var gp = U.ui.panel({ title: t('ig.gaps.title') }); gp.classList.add('span-6'); var gl = h('ul', 'ig-list'); hosts.forEach(function (x) { (x.gaps || []).forEach(function (g) { gl.appendChild(h('li', null, t('ig.gaps.line', { host: x.name, from: when(g.from), to: when(g.to), h: nf(Math.round((g.to - g.from) / 3600)) }))); }); }); gp.body.appendChild(gl.children.length ? gl : U.ui.empty(t('ig.gaps.none'))); grid.appendChild(gp); // peers var pp = U.ui.panel({ title: t('ig.peers.title') }); pp.classList.add('span-6'); if (!peers.length) pp.body.appendChild(U.ui.empty(t('ig.peers.none'))); else { var pl = h('ul', 'ig-list'); peers.forEach(function (p) { pl.appendChild(h('li', null, h('strong', null, p.name), ' ', U.ui.badge(t(p.online ? 'ig.peers.online' : 'ig.peers.offline'), p.online ? 'ok' : 'danger'), p.live ? ' ' : null, p.live ? U.ui.badge(t('ig.peers.live'), 'neutral') : null, ' ', h('span', 'ig-muted', p.last_sync ? t('ig.peers.sync', { t: U.fmt.ago(p.last_sync) }) : t('ig.peers.never')))); }); pp.body.appendChild(pl); } grid.appendChild(pp); // sources var sp2 = U.ui.panel({ title: t('ig.src.title') }); sp2.classList.add('span-8'); var rows = []; hosts.forEach(function (x) { (x.sources || []).forEach(function (s) { var tone = (s.state === 'error' || s.state === 'degraded') ? 'danger' : s.state === 'following' ? 'ok' : 'neutral'; rows.push([x.name, s.name, U.ui.badge(STATES[s.state] ? t('ig.state.' + s.state) + (s.state === 'degraded' && s.error ? ' (' + s.error + ')' : '') : String(s.state || ''), tone), nf(s.lines), nf(s.malformed), s.last ? U.fmt.ago(s.last) : '-']); }); }); sp2.body.appendChild(rows.length ? table([t('ig.src.host'), t('ig.src.name'), t('ig.src.state'), t('ig.src.lines'), t('ig.src.bad'), t('ig.src.last')], rows) : U.ui.empty(t('ig.src.none'))); grid.appendChild(sp2); // log + geo var lp = U.ui.panel({ title: t('ig.log.title') }); lp.classList.add('span-4'); if (!lg.enabled) lp.body.appendChild(h('p', 'ig-muted', t('ig.log.off'))); var kv = h('dl', 'ig-kv'); function row(k, v) { kv.appendChild(h('dt', null, k)); kv.appendChild(h('dd', null, v)); } if (lg.enabled) { row(t('ig.log.days'), nf(lg.days)); row(t('ig.log.size'), U.fmt.bytes(lg.bytes)); row(t('ig.log.dropped'), nf(lg.dropped)); } var g = data.geo || {}; row(t('ig.geo.state'), g.state ? (STATES[g.state] ? t('ig.state.' + g.state) : String(g.state)) : t('ig.geo.unknown')); row(t('ig.geo.city'), g.city_build ? U.fmt.date(g.city_build) : t('ig.geo.unknown')); row(t('ig.geo.asn'), g.asn_build ? U.fmt.date(g.asn_build) : t('ig.geo.unknown')); lp.body.appendChild(kv); grid.appendChild(lp); box.appendChild(grid); } U.registerPage({ id: 'integrity', group: 'operate', icon: ICON, titleKey: 'ig.title', mount: function (el, c) { ctx0 = c; alive = true; data = null; root = h('div', 'ig'); box = h('div', 'ig-box'); root.appendChild(box); box.appendChild(U.ui.lead(t('ig.lead.loading'), null, 'neutral')); el.appendChild(root); load(c); }, update: function (c) { ctx0 = c; load(c); if (data) paint(); }, refresh: function () { load(ctx0); }, unmount: function () { alive = false; seq++; if (root && root.parentNode) root.parentNode.removeChild(root); root = box = data = null; } }); return true; } if (!boot()) { var tries = 0, iv = setInterval(function () { if (boot() || ++tries > 50) clearInterval(iv); }, 100); } })();