diff --git a/OpenBench/static/event_viewer.js b/OpenBench/static/event_viewer.js new file mode 100644 index 0000000..f2080d1 --- /dev/null +++ b/OpenBench/static/event_viewer.js @@ -0,0 +1,237 @@ +/* Event viewer: move navigation for PGNs, filtering for plain logs, and the + copy / download buttons. + + Hook for the board analyser: whenever the selected move changes, a + CustomEvent named "ob:ply" is dispatched on document, with + + event.detail = { ply: , move: , moves: } + + and window.OpenBenchViewer exposes { moves, select(ply), current() }. */ + +(function () { + 'use strict'; + + function $(id) { return document.getElementById(id); } + + function readJSON(id, fallback) { + var element = $(id); + if (!element) return fallback; + try { return JSON.parse(element.textContent); } catch (error) { return fallback; } + } + + var raw = readJSON('ev-raw', ''); + var moves = readJSON('ev-moves', []); + var toastTimer = null; + + function toast(message) { + + var element = $('toast'); + if (!element) return; + + element.textContent = message; + element.classList.add('show'); + + window.clearTimeout(toastTimer); + toastTimer = window.setTimeout(function () { element.classList.remove('show'); }, 1800); + } + + function copyText(text) { + + if (navigator.clipboard && window.isSecureContext) return navigator.clipboard.writeText(text); + + return new Promise(function (resolve, reject) { + + var area = document.createElement('textarea'); + area.value = text; + area.style.position = 'fixed'; + area.style.opacity = '0'; + document.body.appendChild(area); + area.select(); + + try { document.execCommand('copy') ? resolve() : reject(); } + catch (error) { reject(error); } + finally { document.body.removeChild(area); } + }); + } + + function download(name, text) { + + var url = URL.createObjectURL(new Blob([text], { type: 'text/plain;charset=utf-8' })); + var link = document.createElement('a'); + + link.href = url; + link.download = name; + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + + window.setTimeout(function () { URL.revokeObjectURL(url); }, 1000); + } + + function bindActions() { + + var copy = $('ev-copy'), save = $('ev-download'); + + if (copy) copy.addEventListener('click', function () { + copyText(raw).then(function () { toast('Copied to clipboard'); }, function () { toast('Copy failed'); }); + }); + + if (save) save.addEventListener('click', function () { + download(save.getAttribute('data-filename') || 'event.log', raw); + }); + } + + function setText(id, text) { + var element = $(id); + if (element) element.textContent = text; + } + + function initMoves() { + + var list = $('ev-movelist'); + if (!list || !moves.length) return; + + var buttons = {}; + var current = -1; + + Array.prototype.forEach.call(list.querySelectorAll('.ev-move[data-ply]'), function (button) { + buttons[button.getAttribute('data-ply')] = button; + button.addEventListener('click', function () { select(parseInt(button.getAttribute('data-ply'), 10), false); }); + }); + + function select(ply, scroll) { + + ply = Math.max(0, Math.min(moves.length - 1, ply)); + + var move = moves[ply]; + + if (buttons[current]) { + buttons[current].classList.remove('is-active'); + buttons[current].removeAttribute('aria-selected'); + } + + current = ply; + + var button = buttons[ply]; + if (button) { + button.classList.add('is-active'); + button.setAttribute('aria-selected', 'true'); + if (scroll !== false) { + list.scrollTop = Math.max(0, button.offsetTop - list.clientHeight / 2 + button.offsetHeight / 2); + } + } + + setText('sel-counter', 'Ply ' + (ply + 1) + ' / ' + moves.length); + setText('sel-label', move.num + (move.color === 'w' ? '.' : '\u2026')); + setText('sel-san', move.san); + setText('sel-eval', move.eval || '\u2013'); + setText('sel-depth', move.depth || '\u2013'); + setText('sel-time', move.time || '\u2013'); + + var evalElement = $('sel-eval'); + if (evalElement) evalElement.className = 'mono ' + (move.tone || ''); + + var note = $('sel-note'); + if (note) { + note.textContent = move.note || ''; + note.hidden = !move.note; + } + + ['ev-first', 'ev-prev'].forEach(function (id) { if ($(id)) $(id).disabled = ply === 0; }); + ['ev-next', 'ev-last'].forEach(function (id) { if ($(id)) $(id).disabled = ply === moves.length - 1; }); + + document.dispatchEvent(new CustomEvent('ob:ply', { detail: { ply: ply, move: move, moves: moves } })); + } + + function bind(id, handler) { + if ($(id)) $(id).addEventListener('click', handler); + } + + bind('ev-first', function () { select(0); }); + bind('ev-prev', function () { select(current - 1); }); + bind('ev-next', function () { select(current + 1); }); + bind('ev-last', function () { select(moves.length - 1); }); + + document.addEventListener('keydown', function (event) { + + if (event.altKey || event.ctrlKey || event.metaKey) return; + if (/^(INPUT|TEXTAREA|SELECT)$/.test((event.target && event.target.tagName) || '')) return; + + var handled = true; + + switch (event.key) { + case 'ArrowLeft': select(current - 1); break; + case 'ArrowRight': select(current + 1); break; + case 'Home': select(0); break; + case 'End': select(moves.length - 1); break; + default: handled = false; + } + + if (handled) event.preventDefault(); + }); + + window.OpenBenchViewer = { + moves : moves, + select : select, + current : function () { return current; } + }; + + // Start on the last move, which is usually the one that ended the game + select(moves.length - 1); + } + + function initLog() { + + var body = $('log-body'); + if (!body) return; + + var filter = $('log-filter'), wrap = $('log-wrap'), issues = $('log-issues'); + var empty = $('log-empty'); + + var lines = Array.prototype.map.call(body.querySelectorAll('.ev-line'), function (line) { + return { + element : line, + level : line.getAttribute('data-level'), + text : line.querySelector('.ev-lt').textContent.toLowerCase() + }; + }); + + function apply() { + + var query = filter ? filter.value.trim().toLowerCase() : ''; + var only = issues ? issues.checked : false; + var shown = 0; + + lines.forEach(function (line) { + var visible = (!query || line.text.indexOf(query) !== -1) && (!only || line.level !== 'info'); + line.element.hidden = !visible; + if (visible) shown++; + }); + + if (empty) empty.hidden = shown !== 0; + } + + if (filter) filter.addEventListener('input', apply); + if (issues) issues.addEventListener('change', apply); + + if (wrap) wrap.addEventListener('change', function () { + body.classList.toggle('ev-wrap', wrap.checked); + }); + + // Jump to the first problem, since that is what people came here for + var first = body.querySelector('.ev-line-bad'); + if (first) body.scrollTop = Math.max(0, first.offsetTop - body.clientHeight / 3); + } + + function ready(fn) { + if (document.readyState !== 'loading') fn(); + else document.addEventListener('DOMContentLoaded', fn); + } + + ready(function () { + bindActions(); + initMoves(); + initLog(); + }); + +})(); diff --git a/OpenBench/static/ux.css b/OpenBench/static/ux.css new file mode 100644 index 0000000..083f6f2 --- /dev/null +++ b/OpenBench/static/ux.css @@ -0,0 +1,903 @@ +/* =========================================================================== + OpenBench - UX layer + + Loaded after style.css and builds on its design tokens ( --color*, --accent, + --line, ... ). Everything new lives here, so the base theme stays untouched + and this file is easy to review, tweak, or drop. + =========================================================================== */ + +:root { + + /* Slightly softer corners than the base theme ( 2px ). + Set this back to 2px to revert the rounding everywhere. */ + --radius: 4px; + + --purple: #A78BFA; + --purple-soft: rgba(167, 139, 250, 0.14); + --shadow-card: inset 0 1px 0 rgba(255, 255, 255, 0.025), 0 1px 3px rgba(0, 0, 0, 0.3); +} + +/* ---------- Depth + interaction polish ---------- */ + +.card { + box-shadow: var(--shadow-card); +} + +#content table.hoverable tr:not(.table-header):not(.table-spacer):not(.table-spacer-small):hover > td:first-child { + box-shadow: inset 2px 0 0 var(--accent); +} + +#content tr[data-href] { + cursor: pointer; +} + +#content th.numeric { + text-align: right; +} + +#content th.sorted { + color: var(--color-font1); +} + +.muted-small { + color: var(--muted); + font-size: 12.5px; +} + +/* ---------- Sidebar: collapsible sections ---------- */ + +#sidebar .nav-title.collapsible { + display: flex; + align-items: center; + justify-content: space-between; + cursor: pointer; + user-select: none; + border-radius: var(--radius); +} + +#sidebar .nav-title.collapsible:hover { + color: var(--color-font2); +} + +#sidebar .nav-title.collapsible::after { + content: ''; + width: 6px; + height: 6px; + margin-right: 4px; + border-right: 1.5px solid currentColor; + border-bottom: 1.5px solid currentColor; + transform: rotate(45deg) translate(-1px, -1px); + transition: transform 0.15s ease; +} + +#sidebar .nav-section.collapsed .nav-title.collapsible::after { + transform: rotate(-45deg); +} + +#sidebar .nav-section.collapsed ul { + display: none; +} + +/* ---------- Stat cards ---------- */ + +.stat-grid { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); + gap: 12px; + margin-bottom: 20px; +} + +.stat-card { + display: flex; + align-items: center; + gap: 14px; + min-width: 0; + padding: 14px 16px; + background-color: var(--color2); + border: 1px solid var(--line); + border-radius: var(--radius); + box-shadow: var(--shadow-card); +} + +.stat-icon { + display: grid; + place-items: center; + flex: none; + width: 40px; + height: 40px; + border-radius: var(--radius); + background-color: var(--accent-soft); + color: var(--accent-hover); + font-size: 16px; +} + +.stat-green .stat-icon { background-color: rgba(52, 211, 153, 0.13); color: var(--ok); } +.stat-yellow .stat-icon { background-color: rgba(245, 185, 65, 0.13); color: var(--warn); } +.stat-purple .stat-icon { background-color: var(--purple-soft); color: var(--purple); } + +.stat-value { + color: var(--color-font1); + font-family: var(--font-mono); + font-size: 22px; + font-weight: 650; + line-height: 1.1; + font-variant-numeric: tabular-nums; +} + +.stat-label { + margin-top: 2px; + color: var(--muted); + font-size: 11.5px; + font-weight: 600; + letter-spacing: 0.1em; + text-transform: uppercase; +} + +.stat-sub { + margin-top: 2px; + color: var(--muted); + font-size: 12px; +} + +/* ---------- Test status dot + progress ---------- */ + +.status-dot { + display: inline-block; + flex: none; + width: 8px; + height: 8px; + margin-right: 9px; + border-radius: 50%; + background-color: var(--muted); + vertical-align: middle; + position: relative; + top: -1px; +} + +.status-running { background-color: var(--accent); animation: ob-pulse 2s ease-out infinite; } +.status-passed { background-color: var(--ok); } +.status-failed { background-color: var(--bad); } +.status-pending { background-color: var(--warn); } +.status-awaiting { background-color: var(--purple);} +.status-stopped, +.status-deleted { background-color: #566074; } + +@keyframes ob-pulse { + 0% { box-shadow: 0 0 0 0 rgba(76, 141, 255, 0.55); } + 70% { box-shadow: 0 0 0 7px rgba(76, 141, 255, 0); } + 100% { box-shadow: 0 0 0 0 rgba(76, 141, 255, 0); } +} + +.ob-progress { + min-width: 96px; + height: 3px; + margin-top: 7px; + border-radius: 2px; + background-color: var(--line); + overflow: hidden; +} + +.ob-progress > span { + display: block; + height: 100%; + border-radius: inherit; + background-color: var(--accent); + transition: width 0.4s ease; +} + +.ob-progress-pos > span { background-color: var(--ok); } +.ob-progress-neg > span { background-color: var(--bad); } + +/* ---------- Empty states ---------- */ + +#content .empty-row td { + padding: 38px 14px; +} + +#content .empty-row i { + display: block; + margin-bottom: 10px; + font-size: 24px; + opacity: 0.55; +} + +.empty-state { + padding: 52px 20px; + color: var(--muted); + text-align: center; +} + +.empty-state i { + display: block; + margin-bottom: 12px; + font-size: 28px; + opacity: 0.5; +} + +.empty-state p { + margin: 0; +} + +/* ---------- Segmented control ---------- */ + +.seg { + display: inline-flex; + gap: 2px; + padding: 3px; + background-color: var(--color2); + border: 1px solid var(--line); + border-radius: calc(var(--radius) + 2px); +} + +#content a.seg-btn:link, +#content a.seg-btn:visited { + padding: 5px 13px; + border-radius: var(--radius); + color: var(--color-font2); + font-size: 13px; + font-weight: 500; + transition: background-color 0.15s ease, color 0.15s ease; +} + +#content a.seg-btn:hover { + background-color: var(--surface-hover); + color: var(--color-font1); +} + +#content a.seg-btn.active { + background-color: var(--accent-soft); + box-shadow: inset 0 0 0 1px rgba(76, 141, 255, 0.4); + color: var(--color-font1); +} + +/* ---------- Users: STC / LTC split ---------- */ + +.splitbar { + display: flex; + min-width: 150px; + height: 8px; + border-radius: 4px; + background-color: var(--line); + overflow: hidden; +} + +.splitbar-lg { + height: 12px; + border-radius: 6px; +} + +.splitbar > span { + display: block; + height: 100%; +} + +.splitbar-stc { background-color: var(--accent); } +.splitbar-ltc { background-color: var(--purple); } +.splitbar-other { background-color: #566074; } + +.legend { + display: flex; + flex-wrap: wrap; + gap: 4px 16px; + color: var(--color-font2); + font-size: 12.5px; + font-weight: 500; +} + +.legend .dot { + display: inline-block; + width: 9px; + height: 9px; + margin-right: 7px; + border-radius: 2px; + vertical-align: middle; +} + +.dot-stc { background-color: var(--accent); } +.dot-ltc { background-color: var(--purple); } +.dot-other { background-color: #566074; } + +.split-overview { + padding: 16px; +} + +.split-overview p { + margin: 12px 0 0; +} + +#content td.num-stc { color: var(--accent-hover); } +#content td.num-ltc { color: var(--purple); } + +#content td.rank { + width: 44px; + color: var(--muted); + font-family: var(--font-mono); +} + +/* ---------- Errors table ---------- */ + +#content a.ev-open:link, +#content a.ev-open:visited { + display: inline-flex; + align-items: center; + gap: 7px; + font-weight: 600; +} + +#content a.ev-open i { + font-size: 11px; + transition: transform 0.15s ease; +} + +#content tr:hover a.ev-open i { + transform: translateX(3px); +} + +.tc-chip { + margin-left: 8px; + color: var(--muted); + font-family: var(--font-mono); + font-size: 12px; +} + +/* ---------- Event viewer ---------- */ + +#content a.crumb:link, +#content a.crumb:visited { + display: inline-flex; + align-items: center; + gap: 8px; + margin-bottom: 8px; + color: var(--muted); + font-size: 13px; + font-weight: 500; +} + +#content a.crumb:hover { + color: var(--color-font1); +} + +.ev-meta { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); + gap: 1px; + margin: 0 0 20px; + background-color: var(--line); + border: 1px solid var(--line); + border-radius: var(--radius); + overflow: hidden; +} + +.ev-meta > div { + min-width: 0; + padding: 11px 14px; + background-color: var(--color2); +} + +.ev-meta dt { + margin-bottom: 4px; + color: var(--muted); + font-size: 11px; + font-weight: 600; + letter-spacing: 0.1em; + text-transform: uppercase; +} + +.ev-meta dd { + margin: 0; + overflow: hidden; + color: var(--color-font1); + font-size: 14px; + text-overflow: ellipsis; + white-space: nowrap; +} + +.ev-versus { + display: grid; + grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); + align-items: center; + gap: 16px; + padding: 24px 20px; +} + +.ev-player { + display: flex; + flex-direction: column; + gap: 4px; + min-width: 0; +} + +.ev-player strong { + overflow: hidden; + color: var(--color-font1); + font-size: 18px; + text-overflow: ellipsis; + white-space: nowrap; +} + +.ev-player small { + display: flex; + align-items: center; + gap: 7px; + color: var(--muted); + font-size: 11.5px; + font-weight: 600; + letter-spacing: 0.1em; + text-transform: uppercase; +} + +.ev-black { + align-items: flex-end; + text-align: right; +} + +.ev-black small { + flex-direction: row-reverse; +} + +.ev-piece { + display: inline-block; + width: 10px; + height: 10px; + border: 1px solid var(--line-strong); + border-radius: 50%; +} + +.ev-white .ev-piece { background-color: #F1F4FA; } +.ev-black .ev-piece { background-color: #0B0E14; } + +.ev-result { + padding: 6px 16px; + background-color: var(--color1); + border: 1px solid var(--line-strong); + border-radius: var(--radius); + color: var(--color-font1); + font-size: 22px; + font-weight: 650; +} + +.ev-callout { + display: flex; + align-items: flex-start; + gap: 12px; + margin: 0 20px 18px; + padding: 12px 14px; + background-color: var(--color1); + border: 1px solid var(--line); + border-left: 3px solid currentColor; + border-radius: var(--radius); +} + +.ev-callout i { + margin-top: 3px; +} + +.ev-callout-bad { color: var(--bad); } +.ev-callout-warn { color: var(--warn); } +.ev-callout-info { color: var(--accent); } + +.ev-callout-body { + min-width: 0; + color: var(--color-font2); + word-break: break-word; +} + +.ev-callout-body strong { + display: block; + color: var(--color-font1); +} + +.ev-facts { + display: flex; + flex-wrap: wrap; + gap: 10px 32px; + margin: 0; + padding: 14px 20px 16px; + border-top: 1px solid var(--line); +} + +.ev-facts > div { + min-width: 0; +} + +.ev-facts dt { + margin-bottom: 2px; + color: var(--muted); + font-size: 11px; + font-weight: 600; + letter-spacing: 0.1em; + text-transform: uppercase; +} + +.ev-facts dd { + margin: 0; + color: var(--color-font1); + font-size: 13px; + word-break: break-all; +} + +.ev-headers { + border-top: 1px solid var(--line); +} + +.ev-headers summary { + padding: 11px 20px; + color: var(--muted); + font-size: 12.5px; + font-weight: 600; + cursor: pointer; + user-select: none; +} + +.ev-headers summary:hover { + color: var(--color-font1); +} + +.ev-headers table { + width: 100%; + margin: 0 0 8px; +} + +.ev-headers td:first-child { + width: 180px; + color: var(--muted); +} + +.ev-grid { + display: grid; + grid-template-columns: minmax(0, 1fr) 360px; + gap: 20px; + align-items: start; +} + +.ev-grid .card { + margin-bottom: 0; +} + +.ev-side { + position: sticky; + top: calc(var(--topbar-h) + 16px); + display: flex; + flex-direction: column; + gap: 20px; +} + +.ev-movelist { + position: relative; + max-height: 580px; + padding: 8px; + overflow-y: auto; + font-family: var(--font-mono); + font-size: 13px; + outline: none; +} + +.ev-row { + display: grid; + grid-template-columns: 46px minmax(0, 1fr) minmax(0, 1fr); + gap: 2px; +} + +.ev-num { + padding: 7px 8px; + color: var(--muted); + text-align: right; +} + +.ev-move { + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; + padding: 7px 10px; + background: transparent; + border: 1px solid transparent; + border-radius: var(--radius); + color: var(--color-font1); + font: inherit; + text-align: left; + cursor: pointer; + transition: background-color 0.12s ease; +} + +button.ev-move:hover { + background-color: var(--surface-hover); +} + +.ev-move.is-active { + background-color: var(--accent-soft); + border-color: rgba(76, 141, 255, 0.45); +} + +.ev-move.is-final { + box-shadow: inset 2px 0 0 var(--bad); +} + +.ev-move.ev-empty { + color: var(--muted); + cursor: default; +} + +.ev-eval { + color: var(--muted); + font-size: 11.5px; + font-style: normal; +} + +.ev-eval.pos, #sel-eval.pos { color: var(--ok); } +.ev-eval.neg, #sel-eval.neg { color: var(--bad); } + +.ev-sel { + padding: 16px; +} + +.ev-sel-main { + display: flex; + align-items: baseline; + gap: 8px; +} + +.ev-sel-label { + color: var(--muted); + font-size: 15px; +} + +.ev-sel-san { + color: var(--color-font1); + font-size: 28px; + font-weight: 650; +} + +.ev-sel-stats { + display: grid; + grid-template-columns: repeat(3, 1fr); + gap: 8px; + margin-top: 14px; +} + +.ev-sel-stats > div { + padding: 9px 11px; + background-color: var(--color1); + border: 1px solid var(--line); + border-radius: var(--radius); +} + +.ev-sel-stats span { + display: block; + margin-bottom: 2px; + color: var(--muted); + font-size: 10.5px; + font-weight: 600; + letter-spacing: 0.1em; + text-transform: uppercase; +} + +.ev-sel-stats strong { + color: var(--color-font1); + font-size: 14px; +} + +.ev-sel-note { + margin: 12px 0 0; + padding: 9px 11px; + border-left: 3px solid var(--warn); + border-radius: var(--radius); + background-color: rgba(245, 185, 65, 0.08); + color: var(--color-font2); + font-size: 13px; +} + +.ev-sel-note[hidden] { + display: none; +} + +.ev-nav { + display: grid; + grid-template-columns: repeat(4, 1fr); + gap: 6px; + margin-top: 14px; +} + +.ev-nav button:disabled { + opacity: 0.4; + cursor: not-allowed; +} + +.ev-hint { + margin: 12px 0 0; + color: var(--muted); + font-size: 12px; +} + +.ev-hint kbd { + display: inline-block; + min-width: 20px; + padding: 1px 6px; + background-color: var(--color3); + border: 1px solid var(--line-strong); + border-bottom-width: 2px; + border-radius: var(--radius); + color: var(--color-font2); + font: 11px var(--font-mono); + text-align: center; +} + +/* Reserved for the board analyser. It listens for the "ob:ply" event that + event_viewer.js dispatches whenever the selected move changes. */ + +.ev-board { + position: relative; + aspect-ratio: 1; + margin: 16px; + border: 1px solid var(--line-strong); + border-radius: var(--radius); + background: repeating-conic-gradient(#1B2230 0 25%, #131925 0 50%) 0 0 / 25% 25%; + overflow: hidden; +} + +.ev-board-msg { + position: absolute; + inset: 0; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + gap: 6px; + padding: 24px; + background-color: rgba(11, 14, 20, 0.72); + color: var(--muted); + font-size: 13px; + text-align: center; +} + +.ev-board-msg i { + margin-bottom: 4px; + font-size: 30px; + opacity: 0.6; +} + +.ev-board-msg strong { + color: var(--color-font1); + font-size: 15px; +} + +/* Plain text logs */ + +.ev-tools { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px 14px; +} + +.ev-input { + width: 220px; + max-width: 100%; + padding: 6px 10px; + background-color: var(--color1); + border: 1px solid var(--line-strong); + border-radius: var(--radius); + color: var(--color-font1); + font-family: var(--font-sans); + font-size: 13px; +} + +.ev-input::placeholder { + color: var(--muted); +} + +.ev-toggle { + display: inline-flex; + align-items: center; + gap: 6px; + color: var(--color-font2); + font-size: 12.5px; + cursor: pointer; + user-select: none; +} + +.ev-logbody { + max-height: 70vh; + padding: 8px 0; + overflow: auto; + background-color: var(--color1); + font-family: var(--font-mono); + font-size: 12.5px; + line-height: 1.55; +} + +.ev-line { + display: flex; + gap: 14px; + padding: 0 14px; + border-left: 2px solid transparent; +} + +.ev-line[hidden] { + display: none; +} + +.ev-line:hover { + background-color: var(--surface-hover); +} + +.ev-ln { + flex: 0 0 48px; + color: #4A5469; + text-align: right; + user-select: none; +} + +.ev-lt { + min-height: 1.55em; + color: var(--color-font2); + white-space: pre; +} + +.ev-wrap .ev-lt { + min-width: 0; + white-space: pre-wrap; + word-break: break-word; +} + +.ev-line-bad { background-color: rgba(248, 113, 113, 0.08); border-left-color: var(--bad); } +.ev-line-warn { background-color: rgba(245, 185, 65, 0.07); border-left-color: var(--warn); } +.ev-line-bad .ev-lt { color: #FFB4B4; } +.ev-line-warn .ev-lt { color: #FFD479; } + +.ev-log-note { + padding: 8px 14px; + border-bottom: 1px solid var(--line); + color: var(--muted); + font-size: 12.5px; +} + +/* ---------- Responsive ---------- */ + +@media (max-width: 1100px) { + + .ev-grid { + grid-template-columns: minmax(0, 1fr); + } + + .ev-side { + position: static; + order: -1; + } + + .ev-board-card { + display: none; + } +} + +@media (max-width: 767px) { + + .ev-versus { + grid-template-columns: 1fr; + gap: 10px; + } + + .ev-black { + align-items: flex-start; + text-align: left; + } + + .ev-black small { + flex-direction: row; + } + + .ev-result { + justify-self: start; + } + + .ev-meta dd { + white-space: normal; + } +} + +@media (prefers-reduced-motion: reduce) { + + .status-running { + animation: none; + } +} diff --git a/OpenBench/static/ux.js b/OpenBench/static/ux.js new file mode 100644 index 0000000..f11e188 --- /dev/null +++ b/OpenBench/static/ux.js @@ -0,0 +1,149 @@ +/* OpenBench UX layer: small progressive enhancements, no dependencies. + Every feature degrades gracefully when this file does not load. */ + +(function () { + 'use strict'; + + function ready(fn) { + if (document.readyState !== 'loading') fn(); + else document.addEventListener('DOMContentLoaded', fn); + } + + function normalise(path) { + path = (path || '').replace(/\/+$/, ''); + return path === '' ? '/' : path.toLowerCase(); + } + + /* Highlight the sidebar entry for the current page. Pages that live under + another section ( an event belongs to Errors or Events ) can say so with + a data-nav="/errors" attribute anywhere in their content. */ + + function highlightNav() { + + var nav = document.getElementById('sidebar'); + if (!nav) return; + + var hint = document.querySelector('[data-nav]'); + var current = normalise(hint ? hint.getAttribute('data-nav') : window.location.pathname); + + if (current === '/' || /^\/index(\/|$)/.test(current)) current = '/index'; + + var best = null, bestLength = -1; + + Array.prototype.forEach.call(nav.querySelectorAll('li a'), function (link) { + + if (link.origin !== window.location.origin) return; + + var path = normalise(link.pathname); + var hit = current === path || current.indexOf(path + '/') === 0; + + if (hit && path.length > bestLength) { best = link; bestLength = path.length; } + }); + + if (best) { + best.classList.add('active'); + best.setAttribute('aria-current', 'page'); + } + } + + /* Sidebar sections can be folded, and the choice is remembered. The list of + Engines starts folded, since it grows with every engine added. */ + + function collapsibleNav() { + + var KEY = 'ob:nav:collapsed'; + var collapsed = ['Engines']; + + try { + var stored = JSON.parse(window.localStorage.getItem(KEY)); + if (Array.isArray(stored)) collapsed = stored; + } catch (error) { /* storage unavailable, use the default */ } + + Array.prototype.forEach.call(document.querySelectorAll('#sidebar .nav-section'), function (section) { + + var title = section.querySelector('.nav-title'); + if (!title) return; + + var name = title.textContent.trim(); + + title.classList.add('collapsible'); + title.setAttribute('role', 'button'); + title.setAttribute('tabindex', '0'); + + function apply(state) { + section.classList.toggle('collapsed', state); + title.setAttribute('aria-expanded', String(!state)); + } + + // Never hide the section that holds the current page + apply(collapsed.indexOf(name) !== -1 && !section.querySelector('a.active')); + + function toggle() { + + var state = !section.classList.contains('collapsed'); + var index = collapsed.indexOf(name); + + apply(state); + + if (state && index === -1) collapsed.push(name); + if (!state && index !== -1) collapsed.splice(index, 1); + + try { window.localStorage.setItem(KEY, JSON.stringify(collapsed)); } catch (error) { /* ignore */ } + } + + title.addEventListener('click', toggle); + title.addEventListener('keydown', function (event) { + if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); toggle(); } + }); + }); + } + + /* Browser tabs say what the page is, instead of "RektBench" everywhere */ + + function setPageTitle() { + + var heading = document.querySelector('.page-title'); + var brand = document.querySelector('.brand-name'); + if (!heading) return; + + var name = brand ? brand.textContent.trim() : 'OpenBench'; + document.title = heading.textContent.trim() + ' \u00B7 ' + name; + } + + /* Whole rows of the test lists are clickable. Real links, buttons, and text + selection keep working as usual. */ + + function clickableRows() { + + var rows = document.querySelectorAll('#content table.test-list tbody tr'); + + Array.prototype.forEach.call(rows, function (row) { + var link = row.querySelector('.col-test a'); + if (link && !row.getAttribute('data-href')) row.setAttribute('data-href', link.getAttribute('href')); + }); + + document.addEventListener('click', function (event) { + + if (event.defaultPrevented || !event.target.closest) return; + + var row = event.target.closest('tr[data-href]'); + if (!row) return; + + if (event.target.closest('a, button, input, select, textarea, label, summary')) return; + if (window.getSelection && String(window.getSelection())) return; + + var href = row.getAttribute('data-href'); + + if (event.metaKey || event.ctrlKey) window.open(href, '_blank'); + else window.location.href = href; + }); + } + + ready(function () { + highlightNav(); + collapsibleNav(); + setPageTitle(); + clickableRows(); + }); + +})(); diff --git a/OpenBench/templatetags/uitags.py b/OpenBench/templatetags/uitags.py new file mode 100644 index 0000000..90e739c --- /dev/null +++ b/OpenBench/templatetags/uitags.py @@ -0,0 +1,86 @@ +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# Template filters for the UX layer ( status dots, progress bars, ... ). # +# Kept apart from mytags.py. Use with {% load uitags %} # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +import re + +from django import template + +from OpenBench.uiutils import event_tone + +register = template.Library() + +@register.filter +def test_status(test): + + # One of: running, pending, awaiting, passed, failed, stopped, deleted + + if test.deleted: + return 'deleted' + + if test.finished: + if test.passed: return 'passed' + if test.failed: return 'failed' + return 'stopped' + + if not test.approved: + return 'pending' + + if test.awaiting: + return 'awaiting' + + return 'running' + +@register.filter +def progress_info(test): + + # Only running tests get a progress bar. Returns None, or a dict holding the + # percentage and a tone: pos ( leaning to a pass ), neg ( leaning to a fail ), + # or neutral ( for tests without a meaningful direction ) + + if test_status(test) != 'running': + return None + + try: + + if test.test_mode == 'SPRT': + + current, lower, upper = float(test.currentllr), float(test.lowerllr), float(test.upperllr) + + if current >= 0 and upper > 0: + fraction, tone = current / upper, 'pos' if current > 0 else 'neutral' + elif current < 0 and lower < 0: + fraction, tone = current / lower, 'neg' + else: + fraction, tone = 0.0, 'neutral' + + return { 'pct' : int(max(0.0, min(1.0, fraction)) * 100), 'tone' : tone } + + if test.max_games: + fraction = test.games / float(test.max_games) + return { 'pct' : int(max(0.0, min(1.0, fraction)) * 100), 'tone' : 'neutral' } + + except (TypeError, ValueError, ZeroDivisionError): + pass + + return None + +@register.filter +def fleet_stats(status): + + # Parses the string from utils.getMachineStatus(), which looks like + # ": 3 Machines / 48 Threads / 12.5 MNPS " + + match = re.search(r'(\d+)\s+Machines\s*/\s*(\d+)\s+Threads\s*/\s*([\d.]+)\s+MNPS', status or '') + + if not match: + return None + + return { 'machines' : int(match.group(1)), 'threads' : int(match.group(2)), 'mnps' : match.group(3) } + +@register.filter(name='event_tone') +def event_tone_filter(summary): + return event_tone(summary) diff --git a/OpenBench/uiutils.py b/OpenBench/uiutils.py new file mode 100644 index 0000000..9ef09c1 --- /dev/null +++ b/OpenBench/uiutils.py @@ -0,0 +1,367 @@ +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# UI helpers for the OpenBench website. # +# # +# Kept out of views.py / utils.py so the presentation logic ( time control # +# buckets, PGN and log parsing for the event viewer ) is easy to find, # +# test, and extend. Nothing in here touches the database schema. # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +import re + +from functools import lru_cache + +from django.db.models import Sum + +from OpenBench.models import Profile, Result + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# TIME CONTROLS # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +# Fischer time controls with a base time below this many seconds are STC, and +# anything at or above it is LTC. ( 10+0.1 is STC, 60+0.6 is LTC ) +STC_MAX_BASE_SECONDS = 30 + +BUCKETS = ('STC', 'LTC', 'OTHER') + +@lru_cache(maxsize=512) +def classify_time_control(time_control): + + # Imported here, since utils.py imports views.py which imports this module + from OpenBench.utils import TimeControl + + try: + tc = str(time_control or '') + + # Fixed nodes, depth, movetime, and cyclic controls are not STC or LTC + if TimeControl.control_type(tc) != TimeControl.FISCHER: + return 'OTHER' + + base = TimeControl.control_base(tc) + return 'STC' if base < STC_MAX_BASE_SECONDS else 'LTC' + + except Exception: + return 'OTHER' + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# USERS PAGE # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +SORT_KEYS = { + 'total' : lambda row: (-row['total'], -row['tests']), + 'stc' : lambda row: (-row['stc' ], -row['total']), + 'ltc' : lambda row: (-row['ltc' ], -row['total']), + 'tests' : lambda row: (-row['tests'], -row['total']), +} + +def _share(part, whole): + # Returned as a string, so templates never localise it into "63,20" + return '%.2f' % (100.0 * part / whole) if whole else '0.00' + +def users_context(request): + + sort = request.GET.get('sort', 'total') + if sort not in SORT_KEYS: + sort = 'total' + + # Games are credited to whoever's machines played them, which is not + # necessarily the author of the test. Results hold one row per (test, machine) + # so grouping by user and time control in the database stays cheap. + + per_user = {} + grouped = Result.objects.values('machine__user__username', 'test__dev_time_control') + grouped = grouped.annotate(total=Sum('games')) + + for entry in grouped: + buckets = per_user.setdefault(entry['machine__user__username'], dict.fromkeys(BUCKETS, 0)) + buckets[classify_time_control(entry['test__dev_time_control'])] += entry['total'] or 0 + + rows = [] + for profile in Profile.objects.select_related('user'): + + if not (profile.games or profile.tests or profile.approver): + continue + + buckets = per_user.get(profile.user.username, dict.fromkeys(BUCKETS, 0)) + split = sum(buckets.values()) + + rows.append({ + 'username' : profile.user.username, + 'approver' : profile.approver, + 'engine' : profile.engine, + 'joined' : profile.user.date_joined, + 'total' : profile.games, + 'tests' : profile.tests, + 'stc' : buckets['STC' ], + 'ltc' : buckets['LTC' ], + 'other' : buckets['OTHER'], + 'stc_share' : _share(buckets['STC' ], split), + 'ltc_share' : _share(buckets['LTC' ], split), + 'oth_share' : _share(buckets['OTHER'], split), + }) + + rows.sort(key=lambda row: SORT_KEYS[sort](row) + (row['username'].lower(),)) + + stc = sum(row['stc' ] for row in rows) + ltc = sum(row['ltc' ] for row in rows) + other = sum(row['other'] for row in rows) + whole = stc + ltc + other + + summary = { + 'users' : len(rows), + 'total' : sum(row['total'] for row in rows), + 'stc' : stc, + 'ltc' : ltc, + 'other' : other, + 'stc_share' : _share(stc, whole), + 'ltc_share' : _share(ltc, whole), + 'oth_share' : _share(other, whole), + } + + return { + 'rows' : rows, + 'summary' : summary, + 'sort' : sort, + 'show_other' : other > 0, + 'stc_limit' : STC_MAX_BASE_SECONDS, + } + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# EVENT VIEWER # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +def event_tone(summary): + + # Colour used for badges: bad ( red ), warn ( amber ), info ( blue ) + text = (summary or '').lower() + + bad = ('illegal', 'disconnect', 'crash', 'fail', 'bad bench', 'missing', 'error') + if any(word in text for word in bad): + return 'bad' + + if any(word in text for word in ('stall', 'timeout', 'timeloss', 'warn')): + return 'warn' + + return 'info' + +# When a worker reports an Illegal Move, Disconnect, or Stalled game, the client +# uploads the PGN of that game ( see PGNHelper.pretty_format in Client/worker.py ). +# Other events ( build failures, bad benches, ... ) carry plain text logs. + +PGN_PROBE_RE = re.compile(r'^\s*\[Event\s+"', re.MULTILINE) +HEADER_RE = re.compile(r'^\[(\w+)\s+"(.*)"\]\s*$') +TOKEN_RE = re.compile(r'\{[^}]*\}|\([^)]*\)|\$\d+|\d+\.(?:\.\.)?|1-0|0-1|1/2-1/2|\*|[^\s{}()]+') +NUMBER_RE = re.compile(r'^\d+\.(?:\.\.)?$') +EVAL_RE = re.compile(r'^(?P[+-]?(?:M\d+|\d+(?:\.\d+)?))/(?P\d+)(?:\s+(?P