feat(remote): 支持按机器和实例下载日志
This commit is contained in:
+8
-2
@@ -266,6 +266,8 @@ https://control.example.com
|
|||||||
- 显示在线状态、运行状态、账号数和当前结果数量。
|
- 显示在线状态、运行状态、账号数和当前结果数量。
|
||||||
- 点击“结果”展开当前账号结果。
|
- 点击“结果”展开当前账号结果。
|
||||||
- 点击“日志”只看该实例日志。
|
- 点击“日志”只看该实例日志。
|
||||||
|
- 每台机器标题栏提供“本机 CSV / JSON”,直接下载该机器全部实例日志。
|
||||||
|
- 每个实例行提供“CSV / JSON”,直接下载该实例完整日志。
|
||||||
- 点击“刷新”立即请求实例状态。
|
- 点击“刷新”立即请求实例状态。
|
||||||
- 点击“停止”请求本地实例停止抢购。
|
- 点击“停止”请求本地实例停止抢购。
|
||||||
- 点击“取消定时”请求本地实例取消尚未触发的定时任务。
|
- 点击“取消定时”请求本地实例取消尚未触发的定时任务。
|
||||||
@@ -304,8 +306,10 @@ https://control.example.com
|
|||||||
|
|
||||||
Web 控制台提供:
|
Web 控制台提供:
|
||||||
|
|
||||||
- `导出 CSV`
|
- `导出当前 CSV`
|
||||||
- `导出 JSON`
|
- `导出当前 JSON`
|
||||||
|
- 机器级 `本机 CSV / JSON`
|
||||||
|
- 实例级 `CSV / JSON`
|
||||||
|
|
||||||
导出会应用当前选择条件:
|
导出会应用当前选择条件:
|
||||||
|
|
||||||
@@ -314,6 +318,8 @@ Web 控制台提供:
|
|||||||
- 当前文本过滤条件。
|
- 当前文本过滤条件。
|
||||||
- 没有选择实例时导出全部机器日志。
|
- 没有选择实例时导出全部机器日志。
|
||||||
|
|
||||||
|
机器级和实例级直下按钮不受当前文本过滤影响,始终下载目标机器或实例的完整日志。顶部“导出当前”应用当前日志范围和文本过滤。客户端下载文件名会包含机器名或实例名,便于区分多个日志文件。
|
||||||
|
|
||||||
CSV 使用 UTF-8 BOM,兼容 Excel 中文。
|
CSV 使用 UTF-8 BOM,兼容 Excel 中文。
|
||||||
|
|
||||||
CSV 列:
|
CSV 列:
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"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) {
|
func readSpoolEventually(spool *EventSpool, want int) ([]LogEvent, int64, error) {
|
||||||
deadline := time.Now().Add(3 * time.Second)
|
deadline := time.Now().Add(3 * time.Second)
|
||||||
for time.Now().Before(deadline) {
|
for time.Now().Before(deadline) {
|
||||||
|
|||||||
@@ -65,6 +65,14 @@
|
|||||||
display: flex; gap: 10px; align-items: center; padding: 10px 12px;
|
display: flex; gap: 10px; align-items: center; padding: 10px 12px;
|
||||||
background: #1c2229;
|
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 {
|
.instance {
|
||||||
display: grid; grid-template-columns: 1.25fr .65fr .8fr .8fr auto;
|
display: grid; grid-template-columns: 1.25fr .65fr .8fr .8fr auto;
|
||||||
gap: 10px; align-items: center; padding: 9px 12px;
|
gap: 10px; align-items: center; padding: 9px 12px;
|
||||||
@@ -78,7 +86,7 @@
|
|||||||
.state-dot.running { background: #f0ad4e; }
|
.state-dot.running { background: #f0ad4e; }
|
||||||
.bad { color: #f07a7a; }
|
.bad { color: #f07a7a; }
|
||||||
.ok { color: #59d19b; }
|
.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; }
|
.actions button { padding: 5px 8px; font-size: 12px; }
|
||||||
.results-details {
|
.results-details {
|
||||||
display: none; padding: 7px 14px 10px 34px; background: #14191e;
|
display: none; padding: 7px 14px 10px 34px; background: #14191e;
|
||||||
@@ -149,8 +157,8 @@
|
|||||||
<div class="log-filter">
|
<div class="log-filter">
|
||||||
<input id="filter" placeholder="按实例、账号或内容过滤">
|
<input id="filter" placeholder="按实例、账号或内容过滤">
|
||||||
<button id="scope-clear">全部日志</button>
|
<button id="scope-clear">全部日志</button>
|
||||||
<button id="export-csv">导出 CSV</button>
|
<button id="export-csv">导出当前 CSV</button>
|
||||||
<button id="export-json">导出 JSON</button>
|
<button id="export-json">导出当前 JSON</button>
|
||||||
<button id="clear">清空</button>
|
<button id="clear">清空</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -256,16 +264,16 @@
|
|||||||
renderLogs();
|
renderLogs();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function exportLogs(format) {
|
async function exportLogs(format, scope = selectedScope, useTextFilter = true) {
|
||||||
if (!token) {
|
if (!token) {
|
||||||
alert('请先连接控制台');
|
alert('请先连接控制台');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const params = new URLSearchParams({ format });
|
const params = new URLSearchParams({ format });
|
||||||
if (selectedScope.machineId) params.set('machineId', selectedScope.machineId);
|
if (scope?.machineId) params.set('machineId', scope.machineId);
|
||||||
if (selectedScope.instanceId) params.set('instanceId', selectedScope.instanceId);
|
if (scope?.instanceId) params.set('instanceId', scope.instanceId);
|
||||||
if (selectedScope.runId) params.set('runId', selectedScope.runId);
|
if (scope?.runId) params.set('runId', scope.runId);
|
||||||
const query = filterInput.value.trim();
|
const query = useTextFilter ? filterInput.value.trim() : '';
|
||||||
if (query) params.set('q', query);
|
if (query) params.set('q', query);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/export?${params}`, {
|
const response = await fetch(`/api/export?${params}`, {
|
||||||
@@ -275,7 +283,7 @@
|
|||||||
const blob = await response.blob();
|
const blob = await response.blob();
|
||||||
const link = document.createElement('a');
|
const link = document.createElement('a');
|
||||||
link.href = URL.createObjectURL(blob);
|
link.href = URL.createObjectURL(blob);
|
||||||
link.download = `douyu-logs-${new Date().toISOString().replace(/[:.]/g, '-')}.${format}`;
|
link.download = exportFileName(format, scope);
|
||||||
link.click();
|
link.click();
|
||||||
setTimeout(() => URL.revokeObjectURL(link.href), 1000);
|
setTimeout(() => URL.revokeObjectURL(link.href), 1000);
|
||||||
} catch (error) {
|
} 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) {
|
function renderDashboard(data) {
|
||||||
const groups = new Map();
|
const groups = new Map();
|
||||||
for (const item of data.instances || []) {
|
for (const item of data.instances || []) {
|
||||||
@@ -306,6 +323,13 @@
|
|||||||
textNode('span', first.machineID || machineID, 'muted'),
|
textNode('span', first.machineID || machineID, 'muted'),
|
||||||
textNode('span', `${instances.length} 个实例`, 'muted')
|
textNode('span', `${instances.length} 个实例`, 'muted')
|
||||||
);
|
);
|
||||||
|
head.append(downloadButtons(
|
||||||
|
machineID,
|
||||||
|
'',
|
||||||
|
first.machineName || machineID,
|
||||||
|
'本机',
|
||||||
|
'machine-actions'
|
||||||
|
));
|
||||||
machine.append(head);
|
machine.append(head);
|
||||||
for (const item of instances) {
|
for (const item of instances) {
|
||||||
machine.append(renderInstance(item));
|
machine.append(renderInstance(item));
|
||||||
@@ -344,7 +368,14 @@
|
|||||||
logButton(item),
|
logButton(item),
|
||||||
commandButton(item, 'status', '刷新'),
|
commandButton(item, 'status', '刷新'),
|
||||||
commandButton(item, 'stop', '停止', 'danger'),
|
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);
|
row.append(name, status, resultCell, meta, actions);
|
||||||
return row;
|
return row;
|
||||||
@@ -389,11 +420,33 @@
|
|||||||
function logButton(item) {
|
function logButton(item) {
|
||||||
const button = element('button', '', '日志');
|
const button = element('button', '', '日志');
|
||||||
button.addEventListener('click', () => {
|
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;
|
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) {
|
function selectScope(machineId, instanceId, runId, label) {
|
||||||
selectedScope = { machineId, instanceId, runId, label };
|
selectedScope = { machineId, instanceId, runId, label };
|
||||||
lastEvent = 0;
|
lastEvent = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user