feat: add history report switcher dropdown in report detail view

This commit is contained in:
2026-06-17 10:35:56 +08:00
parent 3019390592
commit 074800b741
3 changed files with 84 additions and 0 deletions

View File

@@ -4,11 +4,16 @@ const Report = {
distChart: null,
currentDetail: null,
activeGrouping: null,
_switcherLoaded: false,
// 加载并渲染指定运行的完整报告。
async render(runId) {
const empty = document.getElementById("report-empty");
const content = document.getElementById("report-content");
// 加载历史报告下拉(仅首次)
Report._loadSwitcher(runId);
if (!runId) {
empty.hidden = false;
content.hidden = true;
@@ -28,6 +33,10 @@ const Report = {
Report.renderLowest(detail.report);
Report.renderAdvice(detail.summary, detail.report);
content.style.opacity = "1";
// 同步下拉选中项
const sel = document.getElementById("report-switcher-select");
if (sel) sel.value = runId;
} catch (err) {
empty.hidden = false;
content.hidden = true;
@@ -35,6 +44,55 @@ const Report = {
}
},
// 加载并填充历史报告下拉选择框
async _loadSwitcher(currentRunId) {
const sel = document.getElementById("report-switcher-select");
if (!sel) return;
// 已加载过就只更新选中值,不重复请求
if (Report._switcherLoaded) {
if (currentRunId) sel.value = currentRunId;
return;
}
try {
const data = await API.runs();
const runs = data.runs || [];
sel.innerHTML = "";
if (runs.length === 0) {
sel.innerHTML = '<option value="">(无历史运行)</option>';
return;
}
runs.forEach((run) => {
const opt = document.createElement("option");
opt.value = run.run_id;
const timeStr = App.shortTime(run.finished_at);
const meanText = run.metric_means
? Object.entries(run.metric_means)
.filter(([, v]) => v !== null && v !== undefined)
.slice(0, 2)
.map(([k, v]) => `${App.shortMetric(k)}=${v.toFixed(2)}`)
.join(" ")
: "";
opt.textContent = `${run.scenario_name || run.run_id} ${timeStr}${meanText ? " [" + meanText + "]" : ""}`;
sel.appendChild(opt);
});
Report._switcherLoaded = true;
if (currentRunId) sel.value = currentRunId;
} catch (_e) {
sel.innerHTML = '<option value="">(加载失败)</option>';
}
// 绑定切换事件(只绑一次)
sel.addEventListener("change", () => {
const rid = sel.value;
if (!rid) return;
App.currentRunId = rid;
App.enableReportNav();
Report.render(rid);
});
},
// 顶部元信息条。
renderMeta(summary) {
const el = document.getElementById("report-meta");