/** Adaptive polling intervals for live-console views (reduce load when idle or hidden). */ export type MonitorActivity = "hot" | "warm" | "idle"; type RowLike = { process_status?: string; business_status?: string; is_pushing?: boolean; }; const HOT_BUSINESS = new Set([ "streaming", "relay_slow", "source_live", "source_stalled", "youtube_key_rejected", "error", ]); const WARM_BUSINESS = new Set(["waiting_source", "monitoring", "misconfigured", "stale"]); WARM_BUSINESS.add("relay_starting"); function pm2Online(status?: string): boolean { return /^(online|running)$/i.test((status || "").trim()); } export function classifyRowActivity(row?: RowLike): MonitorActivity { if (!row) return "idle"; const business = (row.business_status || "").trim().toLowerCase(); if (row.is_pushing || HOT_BUSINESS.has(business)) return "hot"; if (WARM_BUSINESS.has(business) || pm2Online(row.process_status)) return "warm"; return "idle"; } export function classifyProcessMonitorActivity(rows: RowLike[]): MonitorActivity { let activity: MonitorActivity = "idle"; for (const row of rows) { const rowActivity = classifyRowActivity(row); if (rowActivity === "hot") return "hot"; if (rowActivity === "warm") activity = "warm"; } return activity; } /** Background tab: slow heartbeat instead of stopping entirely. */ export const HIDDEN_TAB_POLL_MS = 30_000; export function processMonitorPollMs(options: { view: string; activity: MonitorActivity; pageVisible: boolean; }): number { if (!options.pageVisible) return HIDDEN_TAB_POLL_MS; if (options.view === "live_youtube" || options.view === "live_tiktok") { switch (options.activity) { case "hot": return 800; case "warm": return 2500; default: return 5000; } } return 4000; } export function statusPollMs(options: { activity: MonitorActivity; pageVisible: boolean; paused?: boolean; }): number { if (options.paused) return 0; if (!options.pageVisible) return HIDDEN_TAB_POLL_MS; switch (options.activity) { case "hot": return 800; case "warm": return 2000; default: return 4000; } } export function liveEventsPollMs(options: { activeEvents: number; pageVisible: boolean }): number { if (!options.pageVisible) return HIDDEN_TAB_POLL_MS; return options.activeEvents > 0 ? 4000 : 12_000; } export function qualityGuardPollMs(options: { pushingLanes: number; pageVisible: boolean }): number { if (!options.pageVisible) return HIDDEN_TAB_POLL_MS; return options.pushingLanes > 0 ? 6000 : 15_000; } export function urlConfigPollMs(options: { pageVisible: boolean }): number { if (!options.pageVisible) return 0; return 12_000; } export function formatPollBadge(ms: number, t: (zh: string, en: string) => string): string { if (ms <= 0) return t("已暂停", "Paused"); if (ms >= 30_000) return t("约 30 秒", "~30s"); if (ms >= 15_000) return t("约 15 秒", "~15s"); if (ms >= 12_000) return t("约 12 秒", "~12s"); if (ms >= 5000) return t("约 5 秒", "~5s"); if (ms >= 2500) return t("约 2.5 秒", "~2.5s"); if (ms >= 2000) return t("约 2 秒", "~2s"); if (ms >= 800) return t("约 1 秒", "~1s"); return `${ms}ms`; }