diff --git a/docs/远程控制.md b/docs/远程控制.md index cfb070f..aee1842 100644 --- a/docs/远程控制.md +++ b/docs/远程控制.md @@ -266,6 +266,8 @@ https://control.example.com - 显示在线状态、运行状态、账号数和当前结果数量。 - 点击“结果”展开当前账号结果。 - 点击“日志”只看该实例日志。 +- 每台机器标题栏提供“本机 CSV / JSON”,直接下载该机器全部实例日志。 +- 每个实例行提供“CSV / JSON”,直接下载该实例完整日志。 - 点击“刷新”立即请求实例状态。 - 点击“停止”请求本地实例停止抢购。 - 点击“取消定时”请求本地实例取消尚未触发的定时任务。 @@ -304,8 +306,10 @@ https://control.example.com Web 控制台提供: -- `导出 CSV` -- `导出 JSON` +- `导出当前 CSV` +- `导出当前 JSON` +- 机器级 `本机 CSV / JSON` +- 实例级 `CSV / JSON` 导出会应用当前选择条件: @@ -314,6 +318,8 @@ Web 控制台提供: - 当前文本过滤条件。 - 没有选择实例时导出全部机器日志。 +机器级和实例级直下按钮不受当前文本过滤影响,始终下载目标机器或实例的完整日志。顶部“导出当前”应用当前日志范围和文本过滤。客户端下载文件名会包含机器名或实例名,便于区分多个日志文件。 + CSV 使用 UTF-8 BOM,兼容 Excel 中文。 CSV 列: diff --git a/internal/remote/remote_test.go b/internal/remote/remote_test.go index bd5a8ec..31f6f58 100644 --- a/internal/remote/remote_test.go +++ b/internal/remote/remote_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "io" "net/http" "net/http/httptest" "os" @@ -358,6 +359,48 @@ func TestExportEventsCSVAndJSON(t *testing.T) { } } +func TestExportEndpointSupportsMachineAndInstanceScopes(t *testing.T) { + server, err := NewServer(ServerOptions{ + DataDir: t.TempDir(), + OperatorToken: "operator-token", + }) + if err != nil { + t.Fatal(err) + } + defer server.Close() + server.hub.AppendEvents([]LogEvent{ + {MachineID: "m1", MachineName: "PC-1", InstanceID: "i1", Message: "实例一日志"}, + {MachineID: "m1", MachineName: "PC-1", InstanceID: "i2", Message: "实例二日志"}, + }) + httpServer := httptest.NewServer(server.Handler()) + defer httpServer.Close() + + request, err := http.NewRequest( + http.MethodGet, + httpServer.URL+"/api/export?format=csv&machineId=m1&instanceId=i1", + nil, + ) + if err != nil { + t.Fatal(err) + } + request.Header.Set("X-Control-Token", "operator-token") + response, err := http.DefaultClient.Do(request) + if err != nil { + t.Fatal(err) + } + defer response.Body.Close() + body, err := io.ReadAll(response.Body) + if err != nil { + t.Fatal(err) + } + if response.StatusCode != http.StatusOK { + t.Fatalf("export status=%d body=%s", response.StatusCode, body) + } + if !bytes.Contains(body, []byte("实例一日志")) || bytes.Contains(body, []byte("实例二日志")) { + t.Fatalf("instance export contains wrong events: %s", body) + } +} + func readSpoolEventually(spool *EventSpool, want int) ([]LogEvent, int64, error) { deadline := time.Now().Add(3 * time.Second) for time.Now().Before(deadline) { diff --git a/internal/remote/web/index.html b/internal/remote/web/index.html index c6e9e16..eff63a1 100644 --- a/internal/remote/web/index.html +++ b/internal/remote/web/index.html @@ -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 @@
- - + +
@@ -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;