| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- {% extends 'layout.twig' %}
- {% block title %}{{ policy.name }} — Policy — IRDB{% endblock %}
- {% block content %}
- {# Twig's |merge calls PHP array_merge, which renumbers integer keys; key by
- slug (a string) so {scanners:40, indexer:30,…} round-trips faithfully. #}
- {% set thresholds_by_slug = {} %}
- {% for t in policy.thresholds|default([]) %}
- {% if t.category_slug %}
- {% set thresholds_by_slug = thresholds_by_slug|merge({(t.category_slug): t.threshold}) %}
- {% endif %}
- {% endfor %}
- <div class="mx-auto max-w-5xl">
- <a href="/app/policies" class="text-sm text-slate-500 hover:underline dark:text-slate-400">← Back to policies</a>
- <div class="mt-3 flex items-center justify-between">
- <h1 class="text-2xl font-semibold tracking-tight">
- <span class="font-mono">{{ policy.name }}</span>
- </h1>
- {% if can_write %}
- {% include 'partials/confirm_form.twig' with {
- action: '/app/policies/' ~ policy.id ~ '/delete',
- label: 'Delete policy',
- description: 'Refused if any consumer references this policy.',
- } only %}
- {% endif %}
- </div>
- <form method="post" action="/app/policies/{{ policy.id }}" class="mt-6">
- <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
- <section class="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm dark:border-slate-800 dark:bg-slate-900">
- <h2 class="text-sm font-semibold uppercase tracking-wider text-slate-500 dark:text-slate-400">Metadata</h2>
- <div class="mt-3 grid grid-cols-1 gap-3 md:grid-cols-3 text-sm">
- <div>
- <label for="p-name" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Name</label>
- <input type="text" id="p-name" name="name" value="{{ policy.name }}" {% if not can_write %}readonly{% endif %}
- class="mt-1 w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 font-mono dark:border-slate-700 dark:bg-slate-950">
- </div>
- <div class="md:col-span-2">
- <label for="p-desc" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Description</label>
- <input type="text" id="p-desc" name="description" value="{{ policy.description|default('') }}" {% if not can_write %}readonly{% endif %}
- class="mt-1 w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 dark:border-slate-700 dark:bg-slate-950">
- </div>
- <div class="md:col-span-3">
- <label class="flex items-center gap-2 text-xs text-slate-600 dark:text-slate-400">
- <input type="checkbox" name="include_manual_blocks" value="1"
- {% if policy.include_manual_blocks %}checked{% endif %}
- {% if not can_write %}disabled{% endif %}>
- include manual blocks
- </label>
- </div>
- </div>
- </section>
- <section class="mt-6 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm dark:border-slate-800 dark:bg-slate-900">
- <div class="flex items-center justify-between">
- <h2 class="text-sm font-semibold uppercase tracking-wider text-slate-500 dark:text-slate-400">Threshold matrix</h2>
- <span class="text-xs text-slate-400">Empty value ⇒ category not in policy</span>
- </div>
- <table class="mt-3 w-full text-sm">
- <thead class="text-left text-xs uppercase tracking-wider text-slate-400">
- <tr>
- <th class="pb-2 font-medium">Category</th>
- <th class="pb-2 font-medium">Decay</th>
- <th class="pb-2 text-right font-medium">Threshold</th>
- </tr>
- </thead>
- <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
- {% for c in categories %}
- <tr>
- <td class="py-2"><span class="font-mono">{{ c.slug }}</span> <span class="text-slate-400">— {{ c.name }}</span></td>
- <td class="py-2 text-xs text-slate-500 dark:text-slate-400">{{ c.decay_function }} ({{ c.decay_param }})</td>
- <td class="py-2 text-right">
- <input type="number" step="0.01" min="0"
- name="thresholds[{{ c.slug }}]"
- value="{{ thresholds_by_slug[c.slug]|default('') }}"
- {% if not can_write %}readonly{% endif %}
- class="w-32 rounded-md border border-slate-300 bg-white px-2 py-1 text-right font-mono dark:border-slate-700 dark:bg-slate-950">
- </td>
- </tr>
- {% endfor %}
- </tbody>
- </table>
- </section>
- {% if can_write %}
- <div class="mt-6 flex justify-end">
- <button type="submit" class="rounded-md bg-indigo-600 px-4 py-2 text-sm font-medium text-white hover:bg-indigo-500">Save policy</button>
- </div>
- {% endif %}
- </form>
- <section class="mt-6 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm dark:border-slate-800 dark:bg-slate-900"
- x-data="policyScoreDistribution({{ policy.id }})" x-init="load()">
- <div class="flex items-center justify-between">
- <h2 class="text-sm font-semibold uppercase tracking-wider text-slate-500 dark:text-slate-400">Score distribution</h2>
- <button type="button" x-on:click="load()" class="text-xs text-indigo-600 hover:underline dark:text-indigo-400">Refresh</button>
- </div>
- <p class="mt-2 text-xs text-slate-400">
- IPs grouped by max score across categories in steps of 5; vertical lines mark this policy's thresholds.
- </p>
- <div class="mt-3 h-64">
- <canvas x-ref="canvas"></canvas>
- </div>
- <p class="mt-2 text-xs text-slate-400" x-show="empty">No scored IPs in the database yet.</p>
- </section>
- <section class="mt-6 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm dark:border-slate-800 dark:bg-slate-900"
- x-data="policyPreview({{ policy.id }})" x-init="load()">
- <div class="flex items-center justify-between">
- <h2 class="text-sm font-semibold uppercase tracking-wider text-slate-500 dark:text-slate-400">Preview</h2>
- <button type="button" x-on:click="load()" class="text-xs text-indigo-600 hover:underline dark:text-indigo-400">Refresh</button>
- </div>
- <p class="mt-2 text-sm">
- <span x-show="loading">Loading…</span>
- <template x-if="!loading">
- <span><span class="font-mono" x-text="count"></span> entries</span>
- </template>
- </p>
- <ul class="mt-3 max-h-60 divide-y divide-slate-100 overflow-y-auto text-xs dark:divide-slate-800">
- <template x-for="entry in sample" :key="entry.key">
- <li class="flex items-baseline justify-between gap-3 py-1">
- <span class="font-mono text-slate-700 dark:text-slate-300" x-text="entry.label"></span>
- <span class="shrink-0 text-slate-500 dark:text-slate-400" :title="entry.tooltip" x-text="entry.expiry"></span>
- </li>
- </template>
- </ul>
- <p class="mt-2 text-xs text-slate-400">
- Sample = first 50 entries from the rendered blocklist. Expiry for scored
- entries is an estimate assuming no further reports; manual entries show
- the configured expiry.
- </p>
- </section>
- </div>
- <script>
- window.policyPreview = function (id) {
- function localeFallback() {
- const fallback = document.documentElement.getAttribute('data-irdb-locale-fallback');
- const locales = [];
- if (typeof navigator !== 'undefined' && navigator.language) {
- locales.push(navigator.language);
- }
- if (fallback && fallback.trim()) {
- locales.push(fallback.trim());
- }
- return locales.length > 0 ? locales : undefined;
- }
- let formatter;
- try {
- formatter = new Intl.DateTimeFormat(localeFallback(), {
- year: 'numeric', month: '2-digit', day: '2-digit',
- hour: '2-digit', minute: '2-digit',
- });
- } catch (e) {
- formatter = null;
- }
- function formatExpiry(iso) {
- if (!iso) return '';
- const d = new Date(iso);
- if (isNaN(d.getTime())) return iso;
- if (!formatter) return iso;
- try { return formatter.format(d); } catch (_) { return iso; }
- }
- function shapeEntry(raw, idx) {
- if (typeof raw === 'string') {
- return { key: 'r' + idx, label: raw, expiry: '', tooltip: '' };
- }
- const ip = raw.ip_or_cidr || '';
- let expiry = '';
- let tooltip = '';
- if (raw.expires_at) {
- const formatted = formatExpiry(raw.expires_at);
- expiry = (raw.expires_estimated ? '~ ' : '') + formatted;
- tooltip = (raw.expires_estimated
- ? 'Estimated falls-off date (assumes no further reports). ISO: '
- : 'Configured manual block expiry. ISO: ') + raw.expires_at;
- } else if (raw.reason === 'manual') {
- expiry = 'never';
- tooltip = 'Manual block has no configured expiry';
- } else {
- expiry = '—';
- tooltip = 'Score does not decay below threshold (threshold ≤ 0)';
- }
- return { key: 'r' + idx + ':' + ip, label: ip, expiry: expiry, tooltip: tooltip };
- }
- return {
- loading: true,
- count: 0,
- sample: [],
- async load() {
- this.loading = true;
- try {
- const res = await fetch('/app/policies/' + id + '/preview-proxy', { credentials: 'same-origin' });
- if (!res.ok) throw new Error('preview ' + res.status);
- const data = await res.json();
- this.count = data.count || 0;
- const items = Array.isArray(data.sample) ? data.sample : [];
- this.sample = items.map(shapeEntry);
- } catch (e) {
- this.count = 0;
- this.sample = [{ key: 'err', label: '(preview unavailable)', expiry: '', tooltip: '' }];
- } finally {
- this.loading = false;
- }
- },
- };
- };
- window.policyScoreDistribution = function (id) {
- const THRESHOLD_COLORS = [
- '#ef4444', '#f97316', '#eab308', '#22c55e',
- '#0ea5e9', '#a855f7', '#ec4899', '#14b8a6',
- ];
- function chartTheme() {
- const isDark = document.documentElement.classList.contains('dark');
- return {
- tickColor: isDark ? '#94a3b8' : '#475569',
- gridColor: isDark ? 'rgba(148,163,184,0.15)' : 'rgba(148,163,184,0.3)',
- legendColor: isDark ? '#cbd5e1' : '#334155',
- };
- }
- return {
- empty: false,
- chart: null,
- async load() {
- try {
- const res = await fetch('/app/policies/' + id + '/score-distribution-proxy', { credentials: 'same-origin' });
- if (!res.ok) throw new Error('distribution ' + res.status);
- const data = await res.json();
- this.render(data);
- } catch (e) {
- this.empty = true;
- if (this.chart) { this.chart.destroy(); this.chart = null; }
- }
- },
- render(data) {
- const buckets = Array.isArray(data.buckets) ? data.buckets : [];
- const bucketSize = Number(data.bucket_size) || 5;
- const thresholds = Array.isArray(data.thresholds) ? data.thresholds : [];
- const maxScore = Number(data.max_score) || 0;
- // Pad the histogram with zero-count buckets through the highest
- // observed bucket OR the highest threshold so the threshold
- // lines are always visible on the X axis.
- let upperX = maxScore;
- for (const t of thresholds) {
- if (typeof t.threshold === 'number' && t.threshold > upperX) {
- upperX = t.threshold;
- }
- }
- if (buckets.length > 0) {
- const lastStart = buckets[buckets.length - 1].start;
- if (lastStart + bucketSize > upperX) {
- upperX = lastStart + bucketSize;
- }
- }
- const points = buckets.map((b) => ({ x: Number(b.start) || 0, y: Number(b.count) || 0 }));
- this.empty = points.length === 0;
- const maxCount = points.reduce((m, p) => p.y > m ? p.y : m, 0);
- const yMax = maxCount > 0 ? maxCount : 1;
- const datasets = [{
- label: 'IPs',
- data: points,
- borderColor: '#6366f1',
- backgroundColor: 'rgba(99,102,241,0.18)',
- tension: 0.25,
- fill: true,
- pointRadius: 3,
- pointHoverRadius: 5,
- pointBackgroundColor: '#6366f1',
- stepped: false,
- order: 2,
- }];
- thresholds.forEach((t, i) => {
- if (typeof t.threshold !== 'number') return;
- const color = THRESHOLD_COLORS[i % THRESHOLD_COLORS.length];
- datasets.push({
- label: 'threshold ' + (t.category_slug || ('#' + t.category_id)),
- data: [
- { x: t.threshold, y: 0 },
- { x: t.threshold, y: yMax },
- ],
- borderColor: color,
- backgroundColor: color,
- borderWidth: 2,
- borderDash: [6, 4],
- pointRadius: 0,
- pointHoverRadius: 0,
- fill: false,
- tension: 0,
- order: 1,
- });
- });
- const t = chartTheme();
- const canvas = this.$refs.canvas;
- if (!canvas) return;
- if (this.chart) {
- this.chart.destroy();
- }
- // Chart is registered globally by app.js; if it isn't yet
- // (script ordering), bail silently.
- if (typeof window.Chart === 'undefined' && typeof Chart === 'undefined') {
- return;
- }
- const C = (typeof window.Chart !== 'undefined') ? window.Chart : Chart;
- this.chart = new C(canvas, {
- type: 'line',
- data: { datasets },
- options: {
- responsive: true,
- maintainAspectRatio: false,
- parsing: false,
- plugins: {
- legend: {
- position: 'bottom',
- labels: { color: t.legendColor, boxWidth: 12, font: { size: 11 } },
- },
- tooltip: {
- callbacks: {
- title: (items) => {
- if (!items.length) return '';
- const it = items[0];
- if (it.dataset.label === 'IPs') {
- const start = it.parsed.x;
- return 'score ' + start + '–' + (start + bucketSize);
- }
- return it.dataset.label + ' = ' + it.parsed.x;
- },
- label: (it) => it.dataset.label === 'IPs'
- ? (it.parsed.y + ' IP' + (it.parsed.y === 1 ? '' : 's'))
- : '',
- },
- },
- },
- scales: {
- x: {
- type: 'linear',
- min: 0,
- max: upperX > 0 ? upperX : bucketSize,
- title: { display: true, text: 'score', color: t.tickColor },
- ticks: { color: t.tickColor, stepSize: bucketSize },
- grid: { color: t.gridColor },
- },
- y: {
- beginAtZero: true,
- title: { display: true, text: 'IPs', color: t.tickColor },
- ticks: { color: t.tickColor, precision: 0 },
- grid: { color: t.gridColor },
- },
- },
- },
- });
- },
- };
- };
- </script>
- {% endblock %}
|