feat: 优化日志搜索功能,增强用户体验和性能
This commit is contained in:
1 parent
0e2ff7be6e
commit
eb96d6869e
2 files changed
+68
-13
No files matched your search
+8
-4
@@ -2780,7 +2780,6 @@ def main():
|
||||
log_search_entry = ttk.Entry(
|
||||
log_search_frame, textvariable=log_search_var, width=20
|
||||
)
|
||||
log_search_entry.pack(side=LEFT, padx=(0, 5))
|
||||
|
||||
def refresh_run_log():
|
||||
"""从内存中读取最近若干运行日志并在界面中刷新(行为上与行为日志一致)"""
|
||||
@@ -2947,12 +2946,17 @@ def main():
|
||||
|
||||
log_search_entry.bind("<KeyPress>", on_log_search_entry_key)
|
||||
|
||||
ttk.Button(log_search_frame, text="搜索", command=search_next_in_log).pack(
|
||||
side=LEFT, padx=(0, 2)
|
||||
)
|
||||
# 将 刷新 放在筛选后面,搜索区域与行为日志一致(带分隔条)
|
||||
ttk.Button(log_search_frame, text="刷新", command=refresh_run_log).pack(
|
||||
side=LEFT, padx=(0, 8)
|
||||
)
|
||||
ttk.Separator(log_search_frame, orient="vertical").pack(side=LEFT, fill=Y, padx=5, pady=3)
|
||||
ttk.Label(log_search_frame, text="搜索:").pack(side=LEFT)
|
||||
# 把搜索输入框放在分隔符后
|
||||
log_search_entry.pack(side=LEFT, padx=(0, 5))
|
||||
ttk.Button(log_search_frame, text="搜索", command=search_next_in_log).pack(
|
||||
side=LEFT, padx=(0, 2)
|
||||
)
|
||||
ttk.Button(log_search_frame, text="上一个", command=search_prev_in_log).pack(
|
||||
side=LEFT, padx=(0, 2)
|
||||
)
|
||||
|
||||
+60
-9
@@ -10,6 +10,10 @@ let runLogBuffer = [];
|
||||
let runLogFilter = "";
|
||||
const runLogMaxLines = 50000;
|
||||
|
||||
let runLogMode = 'filter'; // 'filter' or 'search'
|
||||
let runSearchResultsBuffer = [];
|
||||
let runSearchActive = false;
|
||||
|
||||
let actionsLogBuffer = [];
|
||||
let actionsLogFilter = "";
|
||||
const actionsLogMaxLines = 5000;
|
||||
@@ -111,12 +115,18 @@ function appendLog(lines) {
|
||||
for (const ln of toAppend) runLogBuffer.push(ln);
|
||||
// 限制缓冲
|
||||
if (runLogBuffer.length > runLogMaxLines) runLogBuffer = runLogBuffer.slice(-runLogMaxLines);
|
||||
// 如果没有过滤器,直接将文本拼接(性能优先)
|
||||
if (!runLogFilter) {
|
||||
log.textContent += newText;
|
||||
|
||||
// 如果当前处于 run search 结果显示状态,则不改变显示(保持搜索结果不被实时追加打断)
|
||||
if (runSearchActive && runLogMode === 'search') {
|
||||
// 仍保持缓冲更新,但不修改 DOM
|
||||
} else {
|
||||
// 有过滤器时重渲染整个视图
|
||||
renderRunLog();
|
||||
// 如果没有过滤器,直接将文本拼接(性能优先)
|
||||
if (!runLogFilter) {
|
||||
log.textContent += newText;
|
||||
} else {
|
||||
// 有过滤器时重渲染整个视图
|
||||
renderRunLog();
|
||||
}
|
||||
}
|
||||
|
||||
// 性能优化:限制日志长度,避免内存溢出(保留最近较多字符以便桌面端查看)
|
||||
@@ -140,6 +150,15 @@ function appendLog(lines) {
|
||||
function renderRunLog() {
|
||||
const el = $("runLog");
|
||||
if (!el) return;
|
||||
// 如果处于搜索结果显示状态,优先显示 search 返回的结果
|
||||
if (runSearchActive && runLogMode === 'search') {
|
||||
const lines = runSearchResultsBuffer || [];
|
||||
const prevScrollBottom = el.scrollHeight - el.scrollTop - el.clientHeight;
|
||||
el.textContent = lines.join("\n") + (lines.length ? "\n" : "");
|
||||
if (prevScrollBottom < 5) el.scrollTop = el.scrollHeight; else el.scrollTop = Math.max(0, el.scrollHeight - el.clientHeight - prevScrollBottom);
|
||||
return;
|
||||
}
|
||||
|
||||
const filter = (runLogFilter || "").toLowerCase();
|
||||
let lines = runLogBuffer;
|
||||
if (filter) {
|
||||
@@ -1187,12 +1206,44 @@ window.addEventListener("DOMContentLoaded", async () => {
|
||||
});
|
||||
|
||||
// 日志搜索与动作日志相关交互绑定
|
||||
// 日志搜索与动作日志相关交互绑定
|
||||
const runModeEl = $("runLogMode");
|
||||
const runSearchEl = $("runLogSearch");
|
||||
if (runSearchEl) {
|
||||
runSearchEl.addEventListener('input', (e) => { runLogFilter = String(e.target.value || ''); renderRunLog(); });
|
||||
}
|
||||
const runSearchBtn = $("btnRunLogSearch");
|
||||
const runClear = $("btnRunLogClear");
|
||||
if (runClear) runClear.addEventListener('click', () => { runLogBuffer = []; renderRunLog(); });
|
||||
|
||||
async function doRunLogSearch() {
|
||||
const q = runSearchEl ? String(runSearchEl.value || '') : '';
|
||||
if (!q) {
|
||||
runSearchActive = false;
|
||||
runSearchResultsBuffer = [];
|
||||
renderRunLog();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const data = await apiGet(`/api/logs?q=${encodeURIComponent(q)}&lines=1000`);
|
||||
runSearchResultsBuffer = Array.isArray(data.lines) ? data.lines : [];
|
||||
runSearchActive = true;
|
||||
renderRunLog();
|
||||
} catch (e) {
|
||||
console.error('doRunLogSearch error', e);
|
||||
}
|
||||
}
|
||||
|
||||
if (runModeEl) {
|
||||
runModeEl.addEventListener('change', (e) => { runLogMode = String(e.target.value || 'filter'); runSearchActive = false; renderRunLog(); });
|
||||
}
|
||||
|
||||
if (runSearchEl) {
|
||||
runSearchEl.addEventListener('input', (e) => {
|
||||
const v = String(e.target.value || '');
|
||||
if (runLogMode === 'filter') { runLogFilter = v; renderRunLog(); }
|
||||
});
|
||||
runSearchEl.addEventListener('keydown', async (e) => { if (e.key === 'Enter' && runLogMode === 'search') { await doRunLogSearch(); } });
|
||||
}
|
||||
|
||||
if (runSearchBtn) runSearchBtn.addEventListener('click', async () => { await doRunLogSearch(); });
|
||||
if (runClear) runClear.addEventListener('click', () => { runLogBuffer = []; runSearchResultsBuffer = []; runSearchActive = false; runLogFilter = ''; if (runSearchEl) runSearchEl.value=''; renderRunLog(); });
|
||||
|
||||
const actionsSearchEl = $("actionsLogSearch");
|
||||
if (actionsSearchEl) {
|
||||
|
||||
Reference in new issue
Block a user