// Domains: the most requested and most blocked, and one domain's detail. import { el } from '../ui.js'; import { get } from '../api.js'; import { seriesLegend, stackedBars, hideTip } from '../charts.js'; import { ago, hm, ms, num, pct, when } from '../fmt.js'; import { adguardProblem, clientLink, domainLink, resultChip, screen, section, table, fill } from '../common.js'; export function mount(root, ctx) { return ctx.rest ? detail(root, ctx, decodeURIComponent(ctx.rest)) : overview(root, ctx); } function openBox(ctx) { const input = el('input.input', { type: 'search', placeholder: 'example.com', 'aria-label': 'Domain to open' }); const form = el('form', { style: 'display:contents', onsubmit: (e) => { e.preventDefault(); const d = input.value.trim().toLowerCase().replace(/^https?:\/\//, '').split('/')[0]; if (d) ctx.navigate(`/domains/${encodeURIComponent(d)}`); } }, input, el('button.btn', { type: 'submit' }, 'Open domain')); return form; } function overview(root, ctx) { const ui = screen(root, [openBox(ctx), el('span.spacer'), el('button.btn', { type: 'button', onclick: () => load() }, 'Refresh')]); async function load() { try { const d = await get('/api/domains'); const list = (rows) => table(['Domain', { text: 'Requests', props: { class: 'num' } }], rows.map((r) => el('tr', {}, el('td', {}, domainLink(r.domain)), el('td.num', {}, num(r.count)))), { empty: 'Nothing yet.' }); fill(ui.body, el('div.cols', {}, el('div', {}, section('Most requested'), list(d.top_queried)), el('div', {}, section('Most blocked'), list(d.top_blocked)))); ui.setStatus(`${num(d.num_queries)} queries · ${num(d.num_blocked)} blocked in AdGuard’s stats period`); } catch (err) { fill(ui.body, adguardProblem(err)); ui.setStatus(err.message, 'error'); } } load(); } function detail(root, ctx, name) { ctx.setTitle(name); const ui = screen(root, [ openBox(ctx), el('a.btn', { href: `/queries?domain=${encodeURIComponent(name)}` }, 'Open in query log'), el('span.spacer'), el('button.btn', { type: 'button', onclick: () => load() }, 'Refresh'), ]); async function load() { ui.body.classList.add('stale'); ui.setStatus('Reading the query log…'); try { const d = await get(`/api/domains/${encodeURIComponent(name)}`); render(d); ui.setStatus(`Based on ${num(d.sample_rows)} queries for *.${d.base}${d.since ? ` since ${when(d.since)}` : ''}${d.complete ? ' (the whole AdGuard log)' : ''}`); } catch (err) { fill(ui.body, adguardProblem(err)); ui.setStatus(err.message, 'error'); } finally { ui.body.classList.remove('stale'); } } function render(d) { const now = Date.now() / 1000; const hourLabel = (i, full) => { const t = now - (23 - i) * 3600; return full ? `${hm(t - 3600)} – ${hm(t)}` : i === 23 ? 'now' : `${23 - i} h ago`; }; fill(ui.body, el('div.title-row', {}, el('h2', {}, d.domain), d.blocked ? el('span.chip.danger', {}, d.blocked === d.count ? 'Always blocked' : 'Sometimes blocked') : d.count ? el('span.chip.ok', {}, 'Allowed') : null, d.domain !== d.base ? el('a.chip', { href: `/domains/${encodeURIComponent(d.base)}` }, `part of ${d.base}`) : null), el('div.facts', {}, fact('Requests', num(d.count), 'in the sample below'), fact('Blocked', d.count ? `${num(d.blocked)} (${pct(d.blocked / d.count)})` : '—'), fact('Clients', num(d.clients.length)), fact('Avg response', ms(d.avg_ms)), fact('Last request', d.last ? ago(d.last) : '—'), d.stats_count != null ? fact('AdGuard stats', num(d.stats_count), 'top-domain count for its stats period') : null), d.count ? el('div', {}, section('Last 24 hours', 'Requests per hour'), seriesLegend([{ name: 'Allowed', color: 'var(--viz-1)' }, { name: 'Blocked', color: 'var(--viz-2)' }], 'box'), stackedBars({ n: 24, xLabel: hourLabel, height: 150, label: `Requests per hour for ${d.domain}`, series: [{ name: 'Allowed', color: 'var(--viz-1)', values: d.hours.map((h, i) => h - d.blocked_hours[i]) }, { name: 'Blocked', color: 'var(--viz-2)', values: d.blocked_hours }] })) : el('div.callout', { style: 'margin-top:12px' }, `No requests for ${d.domain} in the sample. Related domains under ${d.base} are below.`), el('div.cols', {}, el('div', {}, section('Clients requesting it'), table(['Client', { text: 'Requests', props: { class: 'num' } }, { text: 'Blocked', props: { class: 'num' } }, 'Last'], d.clients.map((c) => el(`tr${c.device?.suspicious ? '.flagged' : ''}`, {}, el('td', {}, clientLink(c.ip, c.device, c.name)), el('td.num', {}, num(c.count)), el('td.num', {}, c.blocked ? num(c.blocked) : ''), el('td.nowrap', {}, ago(c.last)))), { empty: 'No clients in the sample.' })), el('div', {}, section(`Other names under ${d.base}`, 'Same domain, different subdomains'), table(['Domain', { text: 'Requests', props: { class: 'num' } }, { text: 'Blocked', props: { class: 'num' } }], d.related.map((r) => el('tr', {}, el('td', {}, domainLink(r.domain)), el('td.num', {}, num(r.count)), el('td.num', {}, r.blocked ? num(r.blocked) : ''))), { empty: 'No other names under this domain in the sample.' }))), d.rules.length ? el('div', {}, section('Matching rules'), el('ul.mono', {}, ...d.rules.map(([r, n]) => el('li', {}, `${r} ×${n}`)))) : null, section('Recent requests'), table(['Time', 'Client', 'Type', 'Result', { text: 'Response', props: { class: 'num' } }], d.recent.map((q) => el('tr', {}, el('td.nowrap', {}, when(q.ts)), el('td', {}, clientLink(q.client, q.device, q.client_name)), el('td.mono', {}, q.type), el('td', {}, resultChip(q)), el('td.num', {}, ms(q.elapsed_ms)))), { empty: 'None in the sample.' }), ); } const fact = (label, value, title) => el('div.fact', { title: title || '' }, el('span.label', {}, label), el('strong', {}, value)); load(); return hideTip; }