douyinWEB/frontend/src/lib/ytRelayStatus.ts
eric dea036c50d Prefer YouTube receiving/Studio on-air for live-map and sync assign lives to hub.
Tighten http/youtubei live probes, classify Studio encoder_lost vs waiting, and keep dy/dylive public live counts aligned via edge assign_lives.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-05 22:57:52 -05:00

158 lines
5.9 KiB
TypeScript

export type RelayMonitorRow = {
group_id?: string;
label?: string;
process_status?: string;
business_status?: string;
business_note?: string;
is_pushing?: boolean;
ffmpeg_active?: boolean;
ffmpeg_pid?: number;
ffmpeg_speed?: number | null;
ffmpeg_frame?: number | null;
ffmpeg_fps?: number | null;
ffmpeg_bitrate_kbps?: number | null;
ffmpeg_time_seconds?: number | null;
ffmpeg_speed_low?: boolean;
ffmpeg_progress_stalled?: boolean;
log_age_seconds?: number | null;
recent_log?: string;
recent_error?: string;
error?: string;
push_seconds?: number;
source_url?: string;
source_title?: string;
source_display?: string;
source_room_id?: string;
url_count?: number;
douyin_count?: number;
has_stream_key?: boolean;
stream_key_suffix?: string;
neko_instance_id?: number;
neko_enabled?: boolean;
neko?: {
id?: number;
running?: boolean;
ready?: boolean;
status?: string;
studio_url?: string;
detail?: string;
studio?: {
live_page_open?: boolean;
on_air?: boolean;
live_status?: string;
health?: string;
viewer_count?: number | null;
like_count?: number | null;
total_views?: number | null;
live_duration?: string;
live_duration_sec?: number | null;
stream_title?: string;
url?: string;
received_at?: number;
age_sec?: number | null;
detect_source?: string;
minute_reports?: Array<{
ts?: number;
on_air?: boolean;
live_page_open?: boolean;
live_status?: string;
health?: string;
viewer_count?: number | null;
total_views?: number | null;
live_duration?: string;
live_duration_sec?: number | null;
stream_title?: string;
}>;
neko_ref?: string;
neko_kind?: "local" | "remote" | string;
ui_source?: string;
} | null;
open_prefer?: "local" | "remote" | string;
remote_bound?: boolean;
ops_neko_slot?: number;
} | null;
};
export function businessStatusLabel(row?: RelayMonitorRow): string {
const business = (row?.business_status || "").trim().toLowerCase();
if (business === "ready") return "已配置";
if (business === "streaming") return "推流中";
if (business === "source_stalled") return "源卡住";
if (business === "relay_slow") return "低速推流";
if (business === "relay_starting") return "转推启动中";
if (business === "waiting_source") return "等待直播";
if (business === "source_live") return "源已开播";
if (business === "monitoring") return "监听中";
if (business === "outside_slot") return "时段外";
if (business === "misconfigured") return "密钥异常";
if (business === "youtube_key_rejected") return "密钥被拒";
if (business === "stale") return "心跳过期";
if (business === "error") return "异常";
if (business === "inactive") return "未运行";
// Do NOT fall back to vague「运行中」while leaking live metrics — idle/unknown only.
if (row?.is_pushing) return "推流中";
return "空闲";
}
export function businessStatusBadgeClass(row?: RelayMonitorRow): string {
const business = (row?.business_status || "").trim().toLowerCase();
if (business === "ready") return "bg-sky-500/15 text-sky-100 ring-sky-500/25";
if (business === "streaming") return "bg-emerald-500/15 text-emerald-200 ring-emerald-500/30";
if (business === "relay_slow") return "bg-amber-500/15 text-amber-100 ring-amber-500/25";
if (business === "source_stalled") return "bg-rose-500/15 text-rose-100 ring-rose-500/25";
if (business === "waiting_source" || business === "monitoring" || business === "relay_starting" || business === "outside_slot") {
return "bg-amber-500/15 text-amber-100 ring-amber-500/25";
}
if (business === "source_live") return "bg-sky-500/15 text-sky-100 ring-sky-500/25";
if (business === "misconfigured" || business === "youtube_key_rejected" || business === "error" || business === "stale") {
return "bg-rose-500/15 text-rose-100 ring-rose-500/25";
}
if (row?.is_pushing) return "bg-emerald-500/15 text-emerald-200 ring-emerald-500/30";
return "bg-zinc-600/20 text-zinc-400 ring-zinc-500/25";
}
export function laneStatusEmoji(row?: RelayMonitorRow): string {
const business = (row?.business_status || "").trim().toLowerCase();
if (business === "streaming" || row?.is_pushing) return "🟢";
if (business === "relay_slow") return "🟡";
if (business === "source_stalled") return "🟠";
if (business === "relay_starting") return "🔵";
if (business === "youtube_key_rejected" || business === "error") return "🔴";
if (business === "source_live") return "🔵";
if (business === "outside_slot") return "⏳";
if (business === "waiting_source") return "⚪";
return "⚫";
}
export function isLivePushingRow(row?: RelayMonitorRow): boolean {
if (!row) return false;
if (row.is_pushing === true) return true;
const business = (row.business_status || "").trim().toLowerCase();
return business === "streaming" || business === "relay_slow";
}
export function procStartBlocked(row?: RelayMonitorRow): boolean {
if (isLivePushingRow(row)) return true;
if (row?.business_status === "relay_starting") return true;
return false;
}
/** Real failure text for「无法开始」— not success states like「正在推流」. */
export function procStartBlockReason(row?: RelayMonitorRow): string {
if (!row) return "";
if (isLivePushingRow(row) || row.business_status === "relay_starting") return "";
const err = String(row.recent_error || row.error || "").trim();
if (err && !/时段外|非当前时段|outside_slot|轮播窗口/i.test(err)) return err;
const business = String(row.business_status || "").trim().toLowerCase();
if (
business === "misconfigured" ||
business === "youtube_key_rejected" ||
business === "error" ||
business === "stale" ||
business === "source_stalled"
) {
return String(row.business_note || "").trim();
}
return "";
}