feat(remote): 增加远端控制台与日志汇总导出
This commit is contained in:
@@ -0,0 +1,482 @@
|
||||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>斗鱼抢购远端控制台</title>
|
||||
<style>
|
||||
:root {
|
||||
color-scheme: dark;
|
||||
font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
|
||||
background: #111418;
|
||||
color: #e9eef4;
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body { margin: 0; background: #111418; }
|
||||
button, input { font: inherit; }
|
||||
.shell { min-height: 100vh; display: grid; grid-template-rows: auto 1fr; }
|
||||
header {
|
||||
display: flex; align-items: center; gap: 16px; padding: 14px 20px;
|
||||
border-bottom: 1px solid #2a3038; background: #171b20;
|
||||
}
|
||||
header h1 { font-size: 16px; margin: 0; white-space: nowrap; }
|
||||
.muted { color: #8f9baa; }
|
||||
.grow { flex: 1; }
|
||||
.token-box { display: flex; gap: 8px; align-items: center; }
|
||||
input {
|
||||
width: min(360px, 44vw); border: 1px solid #333b45; background: #0d1013;
|
||||
color: #e9eef4; border-radius: 6px; padding: 8px 10px;
|
||||
}
|
||||
button {
|
||||
border: 1px solid #3a4654; background: #202832; color: #e9eef4;
|
||||
border-radius: 6px; padding: 7px 11px; cursor: pointer;
|
||||
}
|
||||
button:hover { background: #293442; }
|
||||
button:disabled { opacity: .45; cursor: not-allowed; }
|
||||
.danger { border-color: #74434a; background: #3a2428; }
|
||||
main {
|
||||
display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(440px, .8fr);
|
||||
gap: 14px; padding: 14px; min-height: 0;
|
||||
}
|
||||
.panel {
|
||||
min-height: 0; border: 1px solid #2a3038; background: #171b20;
|
||||
border-radius: 8px; overflow: hidden; display: flex; flex-direction: column;
|
||||
}
|
||||
.panel-head {
|
||||
display: flex; align-items: center; gap: 12px; padding: 11px 14px;
|
||||
border-bottom: 1px solid #2a3038;
|
||||
}
|
||||
.panel-head strong { font-size: 14px; }
|
||||
.machines { overflow: auto; padding: 10px; }
|
||||
.subhead {
|
||||
padding: 9px 14px; border-top: 1px solid #2a3038; border-bottom: 1px solid #2a3038;
|
||||
background: #1c2229; font-size: 13px; font-weight: 600;
|
||||
}
|
||||
.runs { overflow: auto; max-height: 230px; padding: 4px 12px 10px; }
|
||||
.run {
|
||||
display: grid; grid-template-columns: 72px 1fr 160px 72px;
|
||||
gap: 10px; padding: 7px 0; border-bottom: 1px solid #232a32; font-size: 12px;
|
||||
}
|
||||
.machine {
|
||||
border: 1px solid #2a3038; border-radius: 7px; margin-bottom: 10px;
|
||||
overflow: hidden;
|
||||
}
|
||||
.machine-head {
|
||||
display: flex; gap: 10px; align-items: center; padding: 10px 12px;
|
||||
background: #1c2229;
|
||||
}
|
||||
.instance {
|
||||
display: grid; grid-template-columns: 1.25fr .65fr .8fr .8fr auto;
|
||||
gap: 10px; align-items: center; padding: 9px 12px;
|
||||
border-top: 1px solid #252c34; font-size: 13px;
|
||||
}
|
||||
.state-dot {
|
||||
width: 8px; height: 8px; border-radius: 50%; display: inline-block;
|
||||
margin-right: 7px; background: #6b7280;
|
||||
}
|
||||
.state-dot.online { background: #34c38f; }
|
||||
.state-dot.running { background: #f0ad4e; }
|
||||
.bad { color: #f07a7a; }
|
||||
.ok { color: #59d19b; }
|
||||
.actions { display: flex; gap: 6px; justify-content: flex-end; }
|
||||
.actions button { padding: 5px 8px; font-size: 12px; }
|
||||
.results-details {
|
||||
display: none; padding: 7px 14px 10px 34px; background: #14191e;
|
||||
border-top: 1px solid #252c34; font-size: 12px;
|
||||
}
|
||||
.results-details.open { display: block; }
|
||||
.result-line {
|
||||
display: grid; grid-template-columns: minmax(130px, .45fr) minmax(0, 1fr);
|
||||
gap: 12px; padding: 3px 0;
|
||||
}
|
||||
.logs-panel { min-width: 0; }
|
||||
.log-filter { display: flex; gap: 8px; align-items: center; }
|
||||
.log-filter input { width: 180px; }
|
||||
.log-scope {
|
||||
padding: 4px 8px; border: 1px solid #35404c; border-radius: 999px;
|
||||
color: #9dabba; font-size: 11px; white-space: nowrap;
|
||||
}
|
||||
#logs {
|
||||
overflow: auto; flex: 1; padding: 8px 0;
|
||||
font-family: "Cascadia Mono", "Consolas", monospace; font-size: 12px;
|
||||
}
|
||||
.log {
|
||||
display: grid; grid-template-columns: 92px 58px 150px minmax(0, 1fr);
|
||||
gap: 8px; padding: 4px 12px; border-bottom: 1px solid #20262d;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.log:hover { background: #1b222a; }
|
||||
.log .message { white-space: pre-wrap; word-break: break-word; }
|
||||
.level-warn { color: #f1c27d; }
|
||||
.level-error { color: #f07a7a; }
|
||||
.level-success { color: #59d19b; }
|
||||
.empty { padding: 24px; text-align: center; color: #778493; }
|
||||
@media (max-width: 980px) {
|
||||
main { grid-template-columns: 1fr; }
|
||||
.logs-panel { min-height: 52vh; }
|
||||
.instance { grid-template-columns: 1fr 1fr; }
|
||||
.actions { justify-content: flex-start; }
|
||||
.token-box input { width: 180px; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="shell">
|
||||
<header>
|
||||
<h1>斗鱼抢购远端控制台</h1>
|
||||
<span id="connection" class="muted">未连接</span>
|
||||
<div class="grow"></div>
|
||||
<div class="token-box">
|
||||
<input id="token" type="password" autocomplete="off" placeholder="管理令牌">
|
||||
<button id="connect">连接</button>
|
||||
</div>
|
||||
</header>
|
||||
<main>
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<strong>机器与实例</strong>
|
||||
<span id="summary" class="muted"></span>
|
||||
</div>
|
||||
<div id="machines" class="machines"><div class="empty">等待连接</div></div>
|
||||
<div class="subhead">最近运行</div>
|
||||
<div id="runs" class="runs"><div class="empty">暂无运行记录</div></div>
|
||||
</section>
|
||||
<section class="panel logs-panel">
|
||||
<div class="panel-head">
|
||||
<strong>实时日志</strong>
|
||||
<span id="log-scope" class="log-scope">全部实例</span>
|
||||
<div class="grow"></div>
|
||||
<div class="log-filter">
|
||||
<input id="filter" placeholder="按实例、账号或内容过滤">
|
||||
<button id="scope-clear">全部日志</button>
|
||||
<button id="export-csv">导出 CSV</button>
|
||||
<button id="export-json">导出 JSON</button>
|
||||
<button id="clear">清空</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="logs"></div>
|
||||
</section>
|
||||
</main>
|
||||
</div>
|
||||
<script>
|
||||
const tokenInput = document.getElementById('token');
|
||||
const connectButton = document.getElementById('connect');
|
||||
const connection = document.getElementById('connection');
|
||||
const machinesRoot = document.getElementById('machines');
|
||||
const runsRoot = document.getElementById('runs');
|
||||
const summary = document.getElementById('summary');
|
||||
const logsRoot = document.getElementById('logs');
|
||||
const filterInput = document.getElementById('filter');
|
||||
const clearButton = document.getElementById('clear');
|
||||
const scopeLabel = document.getElementById('log-scope');
|
||||
const scopeClearButton = document.getElementById('scope-clear');
|
||||
const exportCsvButton = document.getElementById('export-csv');
|
||||
const exportJsonButton = document.getElementById('export-json');
|
||||
|
||||
const bootstrap = new URLSearchParams(location.hash.replace(/^#/, ''));
|
||||
const bootstrapToken = bootstrap.get('admin') || '';
|
||||
let token = bootstrapToken || localStorage.getItem('douyu-control-token') || '';
|
||||
let lastEvent = Number(localStorage.getItem('douyu-control-last-event') || '0');
|
||||
let selectedScope = { machineId: '', instanceId: '', runId: '', label: '全部实例' };
|
||||
let polling = false;
|
||||
const logs = [];
|
||||
|
||||
tokenInput.value = token;
|
||||
if (bootstrapToken) {
|
||||
localStorage.setItem('douyu-control-token', bootstrapToken);
|
||||
history.replaceState(null, '', location.pathname + location.search);
|
||||
}
|
||||
connectButton.addEventListener('click', () => {
|
||||
token = tokenInput.value.trim();
|
||||
localStorage.setItem('douyu-control-token', token);
|
||||
lastEvent = 0;
|
||||
logs.length = 0;
|
||||
renderLogs();
|
||||
startPolling();
|
||||
});
|
||||
clearButton.addEventListener('click', () => {
|
||||
logs.length = 0;
|
||||
renderLogs();
|
||||
});
|
||||
scopeClearButton.addEventListener('click', () => selectScope('', '', '', '全部实例'));
|
||||
exportCsvButton.addEventListener('click', () => exportLogs('csv'));
|
||||
exportJsonButton.addEventListener('click', () => exportLogs('json'));
|
||||
filterInput.addEventListener('input', renderLogs);
|
||||
|
||||
async function api(path, options = {}) {
|
||||
const headers = new Headers(options.headers || {});
|
||||
headers.set('X-Control-Token', token);
|
||||
if (options.body) headers.set('Content-Type', 'application/json');
|
||||
const response = await fetch(path, { ...options, headers });
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(text || `HTTP ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
function startPolling() {
|
||||
if (!token) {
|
||||
connection.textContent = '请填写管理令牌';
|
||||
return;
|
||||
}
|
||||
if (polling) return;
|
||||
polling = true;
|
||||
poll();
|
||||
}
|
||||
|
||||
async function poll() {
|
||||
try {
|
||||
const data = await api('/api/dashboard');
|
||||
connection.textContent = '已连接';
|
||||
connection.className = 'ok';
|
||||
renderDashboard(data);
|
||||
await loadEvents();
|
||||
} catch (error) {
|
||||
connection.textContent = '连接失败';
|
||||
connection.className = 'bad';
|
||||
} finally {
|
||||
if (polling) setTimeout(poll, 2000);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadEvents() {
|
||||
const params = new URLSearchParams({ since: String(lastEvent), limit: '300' });
|
||||
if (selectedScope.machineId) params.set('machineId', selectedScope.machineId);
|
||||
if (selectedScope.instanceId) params.set('instanceId', selectedScope.instanceId);
|
||||
if (selectedScope.runId) params.set('runId', selectedScope.runId);
|
||||
const data = await api(`/api/events?${params}`);
|
||||
if (!data.events || data.events.length === 0) return;
|
||||
for (const event of data.events) {
|
||||
lastEvent = Math.max(lastEvent, event.seq || 0);
|
||||
logs.push(event);
|
||||
}
|
||||
localStorage.setItem('douyu-control-last-event', String(lastEvent));
|
||||
while (logs.length > 2000) logs.shift();
|
||||
renderLogs();
|
||||
}
|
||||
|
||||
async function exportLogs(format) {
|
||||
if (!token) {
|
||||
alert('请先连接控制台');
|
||||
return;
|
||||
}
|
||||
const params = new URLSearchParams({ format });
|
||||
if (selectedScope.machineId) params.set('machineId', selectedScope.machineId);
|
||||
if (selectedScope.instanceId) params.set('instanceId', selectedScope.instanceId);
|
||||
if (selectedScope.runId) params.set('runId', selectedScope.runId);
|
||||
const query = filterInput.value.trim();
|
||||
if (query) params.set('q', query);
|
||||
try {
|
||||
const response = await fetch(`/api/export?${params}`, {
|
||||
headers: { 'X-Control-Token': token }
|
||||
});
|
||||
if (!response.ok) throw new Error(await response.text() || `HTTP ${response.status}`);
|
||||
const blob = await response.blob();
|
||||
const link = document.createElement('a');
|
||||
link.href = URL.createObjectURL(blob);
|
||||
link.download = `douyu-logs-${new Date().toISOString().replace(/[:.]/g, '-')}.${format}`;
|
||||
link.click();
|
||||
setTimeout(() => URL.revokeObjectURL(link.href), 1000);
|
||||
} catch (error) {
|
||||
alert(`导出失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
function renderDashboard(data) {
|
||||
const groups = new Map();
|
||||
for (const item of data.instances || []) {
|
||||
const key = item.machineId || item.machineName;
|
||||
if (!groups.has(key)) groups.set(key, []);
|
||||
groups.get(key).push(item);
|
||||
}
|
||||
const online = (data.instances || []).filter(item => item.online).length;
|
||||
summary.textContent = `${groups.size} 台机器,${data.instances?.length || 0} 个实例,${online} 在线`;
|
||||
machinesRoot.replaceChildren();
|
||||
if (groups.size === 0) {
|
||||
machinesRoot.innerHTML = '<div class="empty">暂无实例连接</div>';
|
||||
return;
|
||||
}
|
||||
for (const [machineID, instances] of groups) {
|
||||
const first = instances[0];
|
||||
const machine = element('div', 'machine');
|
||||
const head = element('div', 'machine-head');
|
||||
head.append(
|
||||
textNode('strong', first.machineName || machineID),
|
||||
textNode('span', first.machineID || machineID, 'muted'),
|
||||
textNode('span', `${instances.length} 个实例`, 'muted')
|
||||
);
|
||||
machine.append(head);
|
||||
for (const item of instances) {
|
||||
machine.append(renderInstance(item));
|
||||
machine.append(renderResults(item));
|
||||
}
|
||||
machinesRoot.append(machine);
|
||||
}
|
||||
renderRuns(data.runs || []);
|
||||
}
|
||||
|
||||
function renderInstance(item) {
|
||||
const row = element('div', 'instance');
|
||||
const name = element('div');
|
||||
const dot = element('span', `state-dot ${item.online ? 'online' : ''} ${item.running ? 'running' : ''}`);
|
||||
name.append(dot, document.createTextNode(item.instanceName || item.instanceId));
|
||||
|
||||
const status = element('div');
|
||||
status.append(textNode('strong', item.running ? '抢购中' : (item.online ? '在线' : '离线'), item.online ? '' : 'bad'));
|
||||
|
||||
const results = Object.values(item.results || {});
|
||||
const success = results.filter(value => String(value).startsWith('已抢到') || String(value).startsWith('成功') || String(value).includes('|积分-')).length;
|
||||
const resultCell = textNode('div', `结果 ${success}/${results.length || item.accountCount || 0}`);
|
||||
|
||||
const meta = textNode('div', `${item.accountCount || 0} 账号 · ${item.version || ''}`, 'muted');
|
||||
|
||||
const actions = element('div', 'actions');
|
||||
actions.append(
|
||||
resultButton(item),
|
||||
logButton(item),
|
||||
commandButton(item, 'status', '刷新'),
|
||||
commandButton(item, 'stop', '停止', 'danger'),
|
||||
commandButton(item, 'cancel_schedule', '取消定时')
|
||||
);
|
||||
row.append(name, status, resultCell, meta, actions);
|
||||
return row;
|
||||
}
|
||||
|
||||
function logButton(item) {
|
||||
const button = element('button', '', '日志');
|
||||
button.addEventListener('click', () => {
|
||||
selectScope(item.machineId, item.instanceId, '', item.instanceName || item.instanceId);
|
||||
});
|
||||
return button;
|
||||
}
|
||||
|
||||
function selectScope(machineId, instanceId, runId, label) {
|
||||
selectedScope = { machineId, instanceId, runId, label };
|
||||
lastEvent = 0;
|
||||
logs.length = 0;
|
||||
localStorage.removeItem('douyu-control-last-event');
|
||||
scopeLabel.textContent = label || '全部实例';
|
||||
renderLogs();
|
||||
}
|
||||
|
||||
function renderResults(item) {
|
||||
const panel = element('div', 'results-details');
|
||||
panel.dataset.instance = encodeURIComponent(`${item.machineId}:${item.instanceId}`);
|
||||
const entries = Object.entries(item.results || {});
|
||||
if (entries.length === 0) {
|
||||
panel.append(textNode('div', '当前没有结果记录', 'muted'));
|
||||
return panel;
|
||||
}
|
||||
for (const [rawKey, value] of entries) {
|
||||
const line = element('div', 'result-line');
|
||||
line.append(
|
||||
textNode('span', rawKey.replace(/_第\d+包$/, ''), 'muted'),
|
||||
textNode('span', String(value), String(value).startsWith('失败') ? 'bad' : 'ok')
|
||||
);
|
||||
panel.append(line);
|
||||
}
|
||||
return panel;
|
||||
}
|
||||
|
||||
function resultButton(item) {
|
||||
const button = element('button', '', '结果');
|
||||
button.addEventListener('click', () => {
|
||||
const key = encodeURIComponent(`${item.machineId}:${item.instanceId}`);
|
||||
const panel = machinesRoot.querySelector(`.results-details[data-instance="${key}"]`);
|
||||
if (panel) panel.classList.toggle('open');
|
||||
});
|
||||
return button;
|
||||
}
|
||||
|
||||
function renderRuns(runs) {
|
||||
runsRoot.replaceChildren();
|
||||
if (runs.length === 0) {
|
||||
runsRoot.innerHTML = '<div class="empty">暂无运行记录</div>';
|
||||
return;
|
||||
}
|
||||
for (const run of runs.slice(0, 40)) {
|
||||
const values = Object.values(run.results || {});
|
||||
const success = values.filter(value => String(value).startsWith('成功') || String(value).startsWith('已抢到') || String(value).includes('|积分-')).length;
|
||||
const row = element('div', 'run');
|
||||
row.style.cursor = 'pointer';
|
||||
row.title = '查看这次运行的日志';
|
||||
row.addEventListener('click', () => {
|
||||
const label = `${run.machineName || run.machineId} · ${run.instanceName || run.instanceId} · ${formatTime(run.startedAt)}`;
|
||||
selectScope(run.machineId, run.instanceId, run.runId, label);
|
||||
});
|
||||
row.append(
|
||||
textNode('span', formatTime(run.startedAt), 'muted'),
|
||||
textNode('span', `${run.machineName || run.machineId} · ${run.instanceName || run.instanceId}`),
|
||||
textNode('span', run.running ? '进行中' : (run.finishedAt ? '已结束' : '状态未知'), run.running ? 'level-warn' : 'muted'),
|
||||
textNode('span', `${success}/${values.length}`, success > 0 ? 'ok' : 'muted')
|
||||
);
|
||||
runsRoot.append(row);
|
||||
}
|
||||
}
|
||||
|
||||
function commandButton(item, type, label, className = '') {
|
||||
const button = element('button', className, label);
|
||||
button.disabled = !item.online;
|
||||
button.addEventListener('click', async () => {
|
||||
button.disabled = true;
|
||||
try {
|
||||
await api('/api/command', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ machineId: item.machineId, instanceId: item.instanceId, type })
|
||||
});
|
||||
} catch (error) {
|
||||
alert(error.message);
|
||||
} finally {
|
||||
setTimeout(() => { button.disabled = false; }, 1200);
|
||||
}
|
||||
});
|
||||
return button;
|
||||
}
|
||||
|
||||
function renderLogs() {
|
||||
const filter = filterInput.value.trim().toLowerCase();
|
||||
logsRoot.replaceChildren();
|
||||
const visible = logs.filter(event => {
|
||||
if (!filter) return true;
|
||||
return `${event.machineName || ''} ${event.instanceName || ''} ${event.account || ''} ${event.message || ''}`.toLowerCase().includes(filter);
|
||||
}).slice(-500);
|
||||
if (visible.length === 0) {
|
||||
logsRoot.innerHTML = '<div class="empty">暂无日志</div>';
|
||||
return;
|
||||
}
|
||||
for (const event of visible) {
|
||||
const row = element('div', 'log');
|
||||
row.append(
|
||||
textNode('span', formatTime(event.time), 'muted'),
|
||||
textNode('span', event.level || '', `level-${event.level || 'info'}`),
|
||||
textNode('span', `${event.instanceName || event.instanceId}${event.account ? ' · ' + event.account : ''}`, 'muted'),
|
||||
textNode('span', event.message || '', 'message')
|
||||
);
|
||||
logsRoot.append(row);
|
||||
}
|
||||
logsRoot.scrollTop = logsRoot.scrollHeight;
|
||||
}
|
||||
|
||||
function element(tag, className = '', text = '') {
|
||||
const node = document.createElement(tag);
|
||||
if (className) node.className = className;
|
||||
if (text) node.textContent = text;
|
||||
return node;
|
||||
}
|
||||
|
||||
function textNode(tag, text, className = '') {
|
||||
return element(tag, className, text);
|
||||
}
|
||||
|
||||
function formatTime(value) {
|
||||
if (!value) return '';
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.getTime())) return String(value).slice(11, 23);
|
||||
return date.toLocaleTimeString('zh-CN', { hour12: false });
|
||||
}
|
||||
|
||||
if (token) startPolling();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user