/** * 中控振幅统计:OKX ETH/BTC 时段点数振幅. */ (function () { const page = document.getElementById("page-amp-stats"); if (!page) return; let meta = null; let lastResult = null; let pageNo = 1; let bound = false; const el = (id) => document.getElementById(id); async function apiFetch(url, opts) { const r = await fetch(url, { credentials: "same-origin", ...(opts || {}) }); const ct = (r.headers.get("content-type") || "").toLowerCase(); if (ct.includes("application/json")) { const data = await r.json(); if (!r.ok) throw new Error((data && (data.detail || data.msg)) || r.statusText || "请求失败"); return data; } if (!r.ok) throw new Error(r.statusText || "请求失败"); return r; } function esc(s) { return String(s ?? "") .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); } function setStatus(msg) { const s = el("amp-status"); if (s) s.textContent = msg || ""; } function setView(view) { const isHist = view === "history"; el("amp-panel-stats")?.classList.toggle("hidden", isHist); el("amp-panel-history")?.classList.toggle("hidden", !isHist); page.querySelectorAll(".amp-view-tab").forEach((btn) => { const on = btn.getAttribute("data-view") === view; btn.classList.toggle("is-active", on); btn.setAttribute("aria-selected", on ? "true" : "false"); }); if (isHist) void loadHistory(); } function syncCustomDays() { const period = el("amp-period")?.value || "2m"; const wrap = el("amp-custom-wrap"); if (wrap) wrap.classList.toggle("hidden", period !== "custom"); } function fillMetaControls() { const hourSel = el("amp-start-hour"); if (hourSel && !hourSel.options.length) { for (let h = 0; h < 24; h++) { const opt = document.createElement("option"); opt.value = String(h); opt.textContent = String(h).padStart(2, "0") + ":00"; if (h === 16) opt.selected = true; hourSel.appendChild(opt); } } } function renderSummary(summary, result) { const box = el("amp-summary"); if (!box) return; const s = summary || {}; if (!s.sample_count) { box.innerHTML = '
暂无汇总
'; return; } box.innerHTML = `加载中…
'; try { const data = await apiFetch("/api/amp-stats/history?limit=50"); const items = data.items || []; if (!items.length) { box.innerHTML = '暂无历史
'; return; } box.innerHTML = items .map( (it) => `${esc(String(e && e.message ? e.message : e))}
`; } } async function openHistory(id) { try { const data = await apiFetch("/api/amp-stats/history/" + encodeURIComponent(id)); lastResult = data.item || null; setView("stats"); if (lastResult) { if (el("amp-symbol")) el("amp-symbol").value = lastResult.symbol || "eth"; if (el("amp-start-hour")) el("amp-start-hour").value = String(lastResult.start_hour ?? 16); renderSummary(lastResult.summary, lastResult); const pagePayload = { page: 1, page_size: 20, total: (lastResult.rows || []).length, total_pages: Math.max(1, Math.ceil((lastResult.rows || []).length / 20)), rows: (lastResult.rows || []).slice(0, 20), }; pageNo = 1; renderTable(pagePayload); setStatus("已载入历史 " + id); } } catch (e) { setStatus(String(e && e.message ? e.message : e)); } } function bind() { if (bound) return; bound = true; fillMetaControls(); page.querySelectorAll(".amp-view-tab").forEach((btn) => { btn.addEventListener("click", () => setView(btn.getAttribute("data-view"))); }); el("amp-period")?.addEventListener("change", syncCustomDays); el("amp-btn-compute")?.addEventListener("click", () => void compute(true)); el("amp-btn-save")?.addEventListener("click", () => void saveHistory()); el("amp-btn-download")?.addEventListener("click", downloadCurrent); syncCustomDays(); } window.hubAmpStatsPage = { init() { bind(); setView("stats"); setStatus(""); }, }; })();