fix: refresh public dashboard and BI telemetry
This commit is contained in:
@@ -5,8 +5,12 @@
|
||||
const BUSINESS_TIME_ZONE = "Asia/Bangkok";
|
||||
const I18N = window.ARRI18n;
|
||||
const $ = (selector) => document.querySelector(selector);
|
||||
const BI_POLL_INTERVAL = 5000;
|
||||
let csrf = "";
|
||||
let lastData = null;
|
||||
let dataLoading = false;
|
||||
let biPollTimer = null;
|
||||
let biPollLoading = false;
|
||||
|
||||
function escapeHtml(value) {
|
||||
return String(value ?? "").replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """).replaceAll("'", "'");
|
||||
@@ -131,13 +135,60 @@
|
||||
renderMatrix(data);
|
||||
}
|
||||
|
||||
async function load() {
|
||||
function populateMonths(months) {
|
||||
const select = $("#h5-month");
|
||||
const current = select.value;
|
||||
select.innerHTML = months.length
|
||||
? months.map((item) => `<option value="${escapeHtml(item.month_key)}">${escapeHtml(item.month_key)}</option>`).join("")
|
||||
: `<option value="${monthNow()}">${monthNow()}</option>`;
|
||||
if (months.some((item) => item.month_key === current)) select.value = current;
|
||||
}
|
||||
|
||||
async function load(showErrors = true, preserveOnError = false) {
|
||||
if (dataLoading) return;
|
||||
dataLoading = true;
|
||||
const month = $("#h5-month").value || monthNow();
|
||||
try {
|
||||
render(await api(`/api/public/h5/analytics?month=${encodeURIComponent(month)}`));
|
||||
} catch (error) {
|
||||
reset(I18N?.t("bi.empty") || "该月份暂无可用看板数据");
|
||||
notify(error.message);
|
||||
if (!preserveOnError) reset(I18N?.t("bi.empty") || "该月份暂无可用看板数据");
|
||||
if (showErrors) notify(error.message);
|
||||
} finally {
|
||||
dataLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
function clearBiPoll() {
|
||||
window.clearTimeout(biPollTimer);
|
||||
biPollTimer = null;
|
||||
}
|
||||
|
||||
function scheduleBiPoll(delay = BI_POLL_INTERVAL) {
|
||||
clearBiPoll();
|
||||
if (document.hidden) return;
|
||||
biPollTimer = window.setTimeout(checkBiFreshness, delay);
|
||||
}
|
||||
|
||||
async function checkBiFreshness() {
|
||||
if (biPollLoading || dataLoading || document.hidden) {
|
||||
scheduleBiPoll();
|
||||
return;
|
||||
}
|
||||
biPollLoading = true;
|
||||
try {
|
||||
const months = await api("/api/public/h5/months");
|
||||
populateMonths(months);
|
||||
const month = $("#h5-month").value || monthNow();
|
||||
const metadata = months.find((item) => item.month_key === month);
|
||||
const changed = !lastData
|
||||
|| lastData.month_key !== month
|
||||
|| Boolean(metadata && metadata.updated_at !== lastData.updated_at);
|
||||
if (changed) await load(false, Boolean(lastData));
|
||||
} catch (_) {
|
||||
// Keep the last good H5 snapshot during a transient background failure.
|
||||
} finally {
|
||||
biPollLoading = false;
|
||||
scheduleBiPoll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,13 +249,17 @@
|
||||
}
|
||||
let months = [];
|
||||
try { months = await api("/api/public/h5/months"); } catch (_) { /* handled by load */ }
|
||||
populateMonths(months);
|
||||
const select = $("#h5-month");
|
||||
select.innerHTML = months.length
|
||||
? months.map((item) => `<option value="${escapeHtml(item.month_key)}">${escapeHtml(item.month_key)}</option>`).join("")
|
||||
: `<option value="${monthNow()}">${monthNow()}</option>`;
|
||||
select.addEventListener("change", load);
|
||||
select.addEventListener("change", () => load(true));
|
||||
await load();
|
||||
scheduleBiPoll(250);
|
||||
}
|
||||
|
||||
document.addEventListener("visibilitychange", () => {
|
||||
if (document.hidden) clearBiPoll();
|
||||
else checkBiFreshness();
|
||||
});
|
||||
|
||||
boot();
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user