feat(remote): 支持按机器和实例下载日志

This commit is contained in:
yml
2026-11-22 15:09:54 +08:00
parent 5c659be732
commit 8862d94457
3 changed files with 115 additions and 13 deletions
+64 -11
View File
@@ -65,6 +65,14 @@
display: flex; gap: 10px; align-items: center; padding: 10px 12px;
background: #1c2229;
}
.machine-actions {
display: flex; gap: 6px; align-items: center; margin-left: auto;
}
.machine-actions button,
.download-actions button { padding: 4px 7px; font-size: 11px; }
.download-actions {
display: inline-flex; gap: 4px; align-items: center;
}
.instance {
display: grid; grid-template-columns: 1.25fr .65fr .8fr .8fr auto;
gap: 10px; align-items: center; padding: 9px 12px;
@@ -78,7 +86,7 @@
.state-dot.running { background: #f0ad4e; }
.bad { color: #f07a7a; }
.ok { color: #59d19b; }
.actions { display: flex; gap: 6px; justify-content: flex-end; }
.actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.actions button { padding: 5px 8px; font-size: 12px; }
.results-details {
display: none; padding: 7px 14px 10px 34px; background: #14191e;
@@ -149,8 +157,8 @@
<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="export-csv">导出当前 CSV</button>
<button id="export-json">导出当前 JSON</button>
<button id="clear">清空</button>
</div>
</div>
@@ -256,16 +264,16 @@
renderLogs();
}
async function exportLogs(format) {
async function exportLogs(format, scope = selectedScope, useTextFilter = true) {
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 (scope?.machineId) params.set('machineId', scope.machineId);
if (scope?.instanceId) params.set('instanceId', scope.instanceId);
if (scope?.runId) params.set('runId', scope.runId);
const query = useTextFilter ? filterInput.value.trim() : '';
if (query) params.set('q', query);
try {
const response = await fetch(`/api/export?${params}`, {
@@ -275,7 +283,7 @@
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.download = exportFileName(format, scope);
link.click();
setTimeout(() => URL.revokeObjectURL(link.href), 1000);
} catch (error) {
@@ -283,6 +291,15 @@
}
}
function exportFileName(format, scope) {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const label = String(scope?.label || scope?.instanceId || scope?.machineId || 'all')
.replace(/[\\/:*?"<>|]+/g, '_')
.replace(/\s+/g, '-')
.slice(0, 80);
return `douyu-logs-${label}-${timestamp}.${format}`;
}
function renderDashboard(data) {
const groups = new Map();
for (const item of data.instances || []) {
@@ -306,6 +323,13 @@
textNode('span', first.machineID || machineID, 'muted'),
textNode('span', `${instances.length} 个实例`, 'muted')
);
head.append(downloadButtons(
machineID,
'',
first.machineName || machineID,
'本机',
'machine-actions'
));
machine.append(head);
for (const item of instances) {
machine.append(renderInstance(item));
@@ -344,7 +368,14 @@
logButton(item),
commandButton(item, 'status', '刷新'),
commandButton(item, 'stop', '停止', 'danger'),
commandButton(item, 'cancel_schedule', '取消定时')
commandButton(item, 'cancel_schedule', '取消定时'),
downloadButtons(
item.machineId,
item.instanceId,
`${item.machineName || item.machineId}-${item.instanceName || item.instanceId}`,
'实例',
'download-actions'
)
);
row.append(name, status, resultCell, meta, actions);
return row;
@@ -389,11 +420,33 @@
function logButton(item) {
const button = element('button', '', '日志');
button.addEventListener('click', () => {
selectScope(item.machineId, item.instanceId, '', item.instanceName || item.instanceId);
selectScope(
item.machineId,
item.instanceId,
'',
`${item.machineName || item.machineId} · ${item.instanceName || item.instanceId}`
);
});
return button;
}
function downloadButtons(machineId, instanceId, label, scopeName, className) {
const actions = element('div', className);
if (scopeName === '本机') {
actions.append(textNode('span', '日志', 'muted'));
}
const scope = { machineId, instanceId, runId: '', label };
for (const format of ['CSV', 'JSON']) {
const button = element('button', '', format);
button.title = `下载${scopeName}完整日志 ${format}`;
button.addEventListener('click', () => {
void exportLogs(format.toLowerCase(), scope, false);
});
actions.append(button);
}
return actions;
}
function selectScope(machineId, instanceId, runId, label) {
selectedScope = { machineId, instanceId, runId, label };
lastEvent = 0;